Preload Images in cocos2d v3 - ios

I'm trying to preload the images that I'm using before the game starts.
But, I looking around, I just find this line which is wrote in cocos2d v2.
[[CCTextureCache sharedTextureCache ] addImage:#"objects.png" ];
How can I preload the images in cocos2d v3?

In cocos2d-v3, instead of using CCTextureCache, you can use simply CCTexture’s (previously CCTexture2D) textureWithFile: initializer.
Your code would look like:
CCTexture* texture = [CCTexture textureWithFile:#"objects.png"];
If I understand the docs correctly, it will cache the texture file (see CCTexture extureWithFile:).
So, if you already have loaded the texture before, it will be in cache, and the previously created texture will be returned by the code above.
There is another "work-around" solution also here: https://gamedev.stackexchange.com/questions/72713/preload-in-cocos2d-v3

Actually CCTextureCache is still there in Cocos2D version 3.x. But it is not directly accessible if you just import "cocos2d.h". To access it you have to import "CCTextureCache.h".
Try this code:
#import "CCTextureCache.h"
- (void) preloadImages
{
[[CCTextureCache sharedTextureCache] addImage:#"image1.png"];
[[CCTextureCache sharedTextureCache] addImage:#"image2.png"];
[[CCTextureCache sharedTextureCache] addImage:#"image3.png"];
[[CCTextureCache sharedTextureCache] addImage:#"image4.png"];
[[CCTextureCache sharedTextureCache] addImage:#"image5.png"];
}

Try this:
cocos2d::Director::getInstance()->getTextureCache()->addImage("objects.png");

Related

Easiest way to create layer filled with image pattern in cocos2d v3

I am using cocos2d v3 which has easy to access functionality to create CCSprites (images), CCDrawNode (primitives) and other types of layer like objects.
But I haven't found examples how to create simple layer (i.e. 100x100px) which filled by smaller image (i.e. 4x4px).
What is the easiest way to solve this problem?
Thanks in advice.
I don't know if you would consider this the easiest way, but I think it is the fastest from a performance point of view:
Subclass CCNode and implement -(void)draw. Inside draw you can make custom openGL calls, bind a texture with GL_REPEAT as parameter and draw it.
You can find a good Intro and examples to openGL-es here: http://iphonedevelopment.blogspot.de/2009/05/opengl-es-from-ground-up-part-6_25.html
The easiest way I can think of btw would be to have some for loops creating millions of CCSprites filling the screen. But I wouldn't recommend that ;)
CCTexture* texture = [[CCTextureCache sharedTextureCache] addImage:#"bg.png"];
CCSprite *bgNode = [CCSprite spriteWithTexture:texture
rect:CGRectMake(0, 0, _rootNode.contentSize.width, _appCtrl.winSize.height)];
bgNode.position = ccp(bgNode.contentSize.width / 2.0f, bgNode.contentSize.height / 2.0f);
ccTexParams params = {GL_LINEAR,GL_LINEAR,GL_REPEAT,GL_REPEAT};
[bgNode.texture setTexParameters:&params];
In Cocos2d v3 Unit Tests sources I have found another way to solve this problem.
Note: you must do
#import "CCTexture_Private.h"

If your pre-setting up SKAction do you need to preload SKTextureAtlas?

By pre-setting up strong SKActions for all my in game animations is the actual image data cached by Sprite Kit for future use. What I am trying to understand is if I setup all my animation as retained SKActions is there any point in also preloading the SKTextureAtlas that they originally came from, my thinking is no as the images for the SKActions are already loaded in memory?
for(NSUInteger index=1; index<=15; index++) {
NSString *image = [NSString stringWithFormat:#"Drone_FLYING_%04ld", (long)index];
SKTexture *texture = [SKTexture textureWithImageNamed:image];
[[self framesDroneFlying] addObject:texture];
}
[self setDroneFlyAction:[SKAction animateWithTextures:[self framesDroneFlying] timePerFrame:DRONE_FLY_SPEED]];
I am guessing at this from the Apple Adventure Example, which although the authors use lots of SKTextureAtlas(es) they don't do a single preloadTextureAtlases in any of the code.
To keep textures (of an atlas) cached, use SKTextureAtlas preload method and keep a strong reference to the atlas object. That is all. That'll keep all textures in the atlas in memory.
You can leave the above code unchanged and instead of loading the image it will look in the atlas - provided that you set up the atlas correctly (ie .atlas folder with all resources as reference instead of group, and not adding image files with the same name elsewhere to the project).

Cocos2d not releasing memory

I have a Cocos2d game that contains a loading between 3 'worlds'. To do this the app uses:
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:spriteSheetName];
To unload the previous 'world' in the loading I do:
[[CCSpriteFrameCache sharedSpriteFrameCache]removeSpriteFramesFromFile:plist];
NSString * textureFileName = [plist stringByDeletingPathExtension];
CCTexture2D *texture = [[CCTextureCache sharedTextureCache] textureForKey:[textureFileName stringByAppendingPathExtension:#"pvr.ccz"]];
[[CCTextureCache sharedTextureCache] removeTexture:texture];
When using dumpCachedTextureInfo I receive:
CCTextureCache dumpDebugInfo: 36 textures, for 190476 KB (186.01 MB)
However Xcode memory tab shows around 600MB. I'm using an iPad 3 for testing. Finally after some memory warnings Xcode shows: 'Terminated due to Memory Pressure'.
Do anyone know why I have different memory values?
Make sure you are also releasing your touch delegate when you change scenes.

Replace scene goes on increasing memory usage and memory leak too how to handle that? or How should I enable ARC in my project

I am working on a cocos2d project which is a game with many scenes.
When I call replace scene ,I use
[scene2 removeAllChildrenWithCleanup:YES];
In the dealloc method.
Also, I have removed all the unused spritesheets by using
NSString *PngName = [atlas.AtlasName stringByAppendingString:#".png"];
NSString *PlistName = [atlas.AtlasName stringByAppendingString:#".plist"];
[[CCSpriteFrameCache sharedSpriteFrameCache] removeSpriteFramesFromFile:PlistName];
[[CCSpriteFrameCache sharedSpriteFrameCache] removeSpriteFrameByName:PngName];
[[CCSpriteFrameCache sharedSpriteFrameCache] removeSpriteFrames];
When I check the description of my
[CCSpriteFrameCache sharedSpriteFrameCache]
, It shows me that it removes the unused spritesheets but it does not free the memory when I check on device with Instrumets. This results in app crash.
After many trials I thought of using ARC in my project but when I convert it to ARC project even after following steps shown in
http://www.youtube.com/watch?v=Klj9xRafog4
I get many errors in my CCArray, CCDirectorIOS etc..
Can anybody help me to solve this issue??
Check that you are removing your touch delegate when you are changing scenes.
Try using purgeSharedSpriteFrameCache of CCSpriteFrameCache and removeUnusedSpriteFrames and removeUnusedTextures. Have a look at these threads for more details:
Release all texture memory
Correct way to remove sprite cache
Hope it helps!

Clearing elements from a CCBI file from CCTextureCache and CCSpriteFrameCache

Currently i'm making a game using cocos2d and i've come up on a situation that i'm having trouble dealing with. The game has a loading screen that uses a CCB file and is read in using CCBReader and this causes it to be added to the caches. After the user exits the loading screen and enters the game i would like to clear the textures that the loading screen uses from the cache. I know you can use something like
[[CCSpriteFrameCache sharedSpriteFrameCache] removeSpriteFrameByName:item];
[[CCTextureCache sharedTextureCache] removeTextureForKey:item];
but this doesn't clear it from the texture when loaded from a CCB file. Any body have this situation before or knows how to deal with it?
Instantiate and use a CCSpriteFrameCache and CCTextureCache for your own assets. Then clear the shared caches completely:
[[CCSpriteFrameCache sharedSpriteFrameCache] removeSpriteFrames];
[[CCTextureCache sharedTextureCache] removeAllTextures];

Resources