Skip to main content
deleted 487 characters in body; edited tags; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

How to optimize loading Loading of images? (especially cocos2d)

So iI recently started game development (which is freaking awesome), but one of the things that iI never put much thought to was the loading of images. i'mI'm curious as to advice for improving my current code on loading images. (i'mI'm using cocos2d in iOS code, but if you know of optimization techniques in other languages i'dI'd like to hear that as well) Here's how i used to load images:.

//above the @implementation
#define kZLayerBase -1
#define kOffsetImg1 (-50)
#define kOffsetImg2 (50)
#define kPositionXImg1 (screenSize.width/2)
#define kPositionYImg1 (screenSize.height/2)
#define kPositionXImg2 kPositionXImg1
#define kPositionYImg2 kPositionYImg1
#define kWidthImg1 100
#define kHeightImg1 100
#define kWidhtImg2 100
#define kHeightImg2 100

//...

//individually
-(void)loadImages1{
    CGSize screenSize = [[CCDirector sharedDirector] winSize];

    CCSprite *img1 = [CCSprite spriteWithFile:@"img1.png"];
    [img1 setPosition:ccp(kPositionXImg1, kPositionImg1)];
    [self addChild:img1 z:kTagLayerBase];

    CCSprite *img2 = [CCSprite spriteWithFile:@"img2.png"];
    [img2 setPosition:ccp(kPositionXImg2, kPositionImg2)];
    [self addChild:img2 z:kTagLayerBase];

}

//using a sprite sheet
-(void)loadImages2{
    CCSprite *img1 = [CCSprite spriteWithFile:@"img.png" rect:CGRectMake(kWidthBean*0, kHeightBean*0, kWidthBean, kHeightBean)];
    [img1 setPosition:ccp(kPositionXImg1, kPositionImg1)];
    [self addChild:img1 z:kTagLayerBase];

    CCSprite *img2 = [CCSprite spriteWithFile:@"img.png" rect:CGRectMake(kWidthBean*1, kHeightBean*0, kWidthBean, kHeightBean)];
    [img2 setPosition:ccp(kPositionXImg2, kPositionImg2)];
    [self addChild:img2 z:kTagLayerBase];       
}

asAs you can see in loadImages1, i'veI've basically just made 2 images, and loaded them separately. i'm

I'm curious on your input on this -:

  • isIs this slow or a bad way to load images?
  • isIs there some sort of sprite cache in cocos2d?
  • ifIf this wasn't using ARC, does this ever pose a memory problem?

asAs you can see in loadImages2, iI tried having a sprite sheet and loading from there. i'm

I'm curious:

  • isIs this better way to load images than the first?
  • isIs there a way to load the picture at once and then just pick out segments of it? theThe code seems to keep reloading the same image.

Lastly, if you have any other advice for the code, it would be greatly appreciated!!! (for example, maybe the way i used my #define is too excessive, or you know of an alternative/better way than using so many #defines

P.S. i will give an upvote for all good attempts, whether you answer all parts of the question or just the main topic - how to optimize image loading for game development (esp with cocos2d)

How to optimize loading of images? (especially cocos2d)

So i recently started game development (which is freaking awesome), but one of the things that i never put much thought to was the loading of images. i'm curious as to advice for improving my current code on loading images. (i'm using cocos2d in iOS code, but if you know of optimization techniques in other languages i'd like to hear that as well) Here's how i used to load images:

//above the @implementation
#define kZLayerBase -1
#define kOffsetImg1 (-50)
#define kOffsetImg2 (50)
#define kPositionXImg1 (screenSize.width/2)
#define kPositionYImg1 (screenSize.height/2)
#define kPositionXImg2 kPositionXImg1
#define kPositionYImg2 kPositionYImg1
#define kWidthImg1 100
#define kHeightImg1 100
#define kWidhtImg2 100
#define kHeightImg2 100

//...

//individually
-(void)loadImages1{
    CGSize screenSize = [[CCDirector sharedDirector] winSize];

    CCSprite *img1 = [CCSprite spriteWithFile:@"img1.png"];
    [img1 setPosition:ccp(kPositionXImg1, kPositionImg1)];
    [self addChild:img1 z:kTagLayerBase];

    CCSprite *img2 = [CCSprite spriteWithFile:@"img2.png"];
    [img2 setPosition:ccp(kPositionXImg2, kPositionImg2)];
    [self addChild:img2 z:kTagLayerBase];

}

//using a sprite sheet
-(void)loadImages2{
    CCSprite *img1 = [CCSprite spriteWithFile:@"img.png" rect:CGRectMake(kWidthBean*0, kHeightBean*0, kWidthBean, kHeightBean)];
    [img1 setPosition:ccp(kPositionXImg1, kPositionImg1)];
    [self addChild:img1 z:kTagLayerBase];

    CCSprite *img2 = [CCSprite spriteWithFile:@"img.png" rect:CGRectMake(kWidthBean*1, kHeightBean*0, kWidthBean, kHeightBean)];
    [img2 setPosition:ccp(kPositionXImg2, kPositionImg2)];
    [self addChild:img2 z:kTagLayerBase];       
}

as you can see in loadImages1, i've basically just made 2 images, and loaded them separately. i'm curious on your input on this -

  • is this slow or a bad way to load images?
  • is there some sort of sprite cache in cocos2d?
  • if this wasn't using ARC, does this ever pose a memory problem?

as you can see in loadImages2, i tried having a sprite sheet and loading from there. i'm curious:

  • is this better way to load images than the first?
  • is there a way to load the picture at once and then just pick out segments of it? the code seems to keep reloading the same image

Lastly, if you have any other advice for the code, it would be greatly appreciated!!! (for example, maybe the way i used my #define is too excessive, or you know of an alternative/better way than using so many #defines

P.S. i will give an upvote for all good attempts, whether you answer all parts of the question or just the main topic - how to optimize image loading for game development (esp with cocos2d)

Loading of images

I recently started game development, but one of the things that I never put much thought to was the loading of images. I'm curious as to advice for improving my current code on loading images. I'm using cocos2d in iOS code, but if you know of optimization techniques in other languages I'd like to hear that as well.

//above the @implementation
#define kZLayerBase -1
#define kOffsetImg1 (-50)
#define kOffsetImg2 (50)
#define kPositionXImg1 (screenSize.width/2)
#define kPositionYImg1 (screenSize.height/2)
#define kPositionXImg2 kPositionXImg1
#define kPositionYImg2 kPositionYImg1
#define kWidthImg1 100
#define kHeightImg1 100
#define kWidhtImg2 100
#define kHeightImg2 100

//...

//individually
-(void)loadImages1{
    CGSize screenSize = [[CCDirector sharedDirector] winSize];

    CCSprite *img1 = [CCSprite spriteWithFile:@"img1.png"];
    [img1 setPosition:ccp(kPositionXImg1, kPositionImg1)];
    [self addChild:img1 z:kTagLayerBase];

    CCSprite *img2 = [CCSprite spriteWithFile:@"img2.png"];
    [img2 setPosition:ccp(kPositionXImg2, kPositionImg2)];
    [self addChild:img2 z:kTagLayerBase];

}

//using a sprite sheet
-(void)loadImages2{
    CCSprite *img1 = [CCSprite spriteWithFile:@"img.png" rect:CGRectMake(kWidthBean*0, kHeightBean*0, kWidthBean, kHeightBean)];
    [img1 setPosition:ccp(kPositionXImg1, kPositionImg1)];
    [self addChild:img1 z:kTagLayerBase];

    CCSprite *img2 = [CCSprite spriteWithFile:@"img.png" rect:CGRectMake(kWidthBean*1, kHeightBean*0, kWidthBean, kHeightBean)];
    [img2 setPosition:ccp(kPositionXImg2, kPositionImg2)];
    [self addChild:img2 z:kTagLayerBase];       
}

As you can see in loadImages1, I've basically just made 2 images, and loaded them separately.

I'm curious on your input on this:

  • Is this slow or a bad way to load images?
  • Is there some sort of sprite cache in cocos2d?
  • If this wasn't using ARC, does this ever pose a memory problem?

As you can see in loadImages2, I tried having a sprite sheet and loading from there.

I'm curious:

  • Is this better way to load images than the first?
  • Is there a way to load the picture at once and then just pick out segments of it? The code seems to keep reloading the same image.
Source Link
David T.
  • 151
  • 1
  • 6

How to optimize loading of images? (especially cocos2d)

So i recently started game development (which is freaking awesome), but one of the things that i never put much thought to was the loading of images. i'm curious as to advice for improving my current code on loading images. (i'm using cocos2d in iOS code, but if you know of optimization techniques in other languages i'd like to hear that as well) Here's how i used to load images:

//above the @implementation
#define kZLayerBase -1
#define kOffsetImg1 (-50)
#define kOffsetImg2 (50)
#define kPositionXImg1 (screenSize.width/2)
#define kPositionYImg1 (screenSize.height/2)
#define kPositionXImg2 kPositionXImg1
#define kPositionYImg2 kPositionYImg1
#define kWidthImg1 100
#define kHeightImg1 100
#define kWidhtImg2 100
#define kHeightImg2 100

//...

//individually
-(void)loadImages1{
    CGSize screenSize = [[CCDirector sharedDirector] winSize];

    CCSprite *img1 = [CCSprite spriteWithFile:@"img1.png"];
    [img1 setPosition:ccp(kPositionXImg1, kPositionImg1)];
    [self addChild:img1 z:kTagLayerBase];

    CCSprite *img2 = [CCSprite spriteWithFile:@"img2.png"];
    [img2 setPosition:ccp(kPositionXImg2, kPositionImg2)];
    [self addChild:img2 z:kTagLayerBase];

}

//using a sprite sheet
-(void)loadImages2{
    CCSprite *img1 = [CCSprite spriteWithFile:@"img.png" rect:CGRectMake(kWidthBean*0, kHeightBean*0, kWidthBean, kHeightBean)];
    [img1 setPosition:ccp(kPositionXImg1, kPositionImg1)];
    [self addChild:img1 z:kTagLayerBase];

    CCSprite *img2 = [CCSprite spriteWithFile:@"img.png" rect:CGRectMake(kWidthBean*1, kHeightBean*0, kWidthBean, kHeightBean)];
    [img2 setPosition:ccp(kPositionXImg2, kPositionImg2)];
    [self addChild:img2 z:kTagLayerBase];       
}

as you can see in loadImages1, i've basically just made 2 images, and loaded them separately. i'm curious on your input on this -

  • is this slow or a bad way to load images?
  • is there some sort of sprite cache in cocos2d?
  • if this wasn't using ARC, does this ever pose a memory problem?

as you can see in loadImages2, i tried having a sprite sheet and loading from there. i'm curious:

  • is this better way to load images than the first?
  • is there a way to load the picture at once and then just pick out segments of it? the code seems to keep reloading the same image

Lastly, if you have any other advice for the code, it would be greatly appreciated!!! (for example, maybe the way i used my #define is too excessive, or you know of an alternative/better way than using so many #defines

P.S. i will give an upvote for all good attempts, whether you answer all parts of the question or just the main topic - how to optimize image loading for game development (esp with cocos2d)