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 #define
s
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)