Question

I am doing a iPhone game using cocos2d that involves a huge set of character animations (atleast 25 different of them).. for which I am having a lot of spritesheets each weighing around 100KB to 200KB.. And I used to create the AtlasSpriteManagers for these on-demand. But when I tested this in the device, there was lotsa performance hiccups around these animations.. there were frames missing.. game hanging while the images where loading and all..

So I decided to create a pre-loading and caching technique in which I constantly look ahead and create the possible AtlasSpriteManagers I may need and maintain them in a NSMutableDictionary and take it from there during the animation.. and based on a usageCount variable, I will remove them off this dictionary later and free memory.. And I use the performSelectorInBackground method to call this function 'loadASM' which does this creation of AtlasSpriteManager and storing it in the dictionary.

[self performSelectorInBackground:@selector(loadASM:) withObject:fileName];

-(ASMHolder *)loadASM:(NSString *)fileName
{
AtlasSpriteManager *sprMgr = [AtlasSpriteManager spriteManagerWithFile:fileName capacity:40];
ASMHolder *asmHolder = [[ASMHolder alloc] init];
asmHolder.sprMgr = sprMgr; asmHolder.retainCount++;
[asmDict setObject:asmHolder forKey:fileName];

return asmHolder;
}

But the images dont seem to load if I call this method in the background using performSelectorInBackground.. A white colored box appears where the images are supposed to appear.. However, the AtlasSpriteManager object is created and it is there in the dictionary.. but it is just turning out to blank sprites.. And instead of the background call, if i try calling the method 'loadASM' directly (i.e. [self loadASM:fileName]), the images are loading.. but of course, there is the delay as this is synchronous calling..

I even tried caching Texture2D objects also instead of the AtlasSpriteManagers, but the result is the same..

( I have also tried addImageAsync of TextureManager.. but that doesnt help my purpose too.. as it does not give the file name it is loading in the callback method's arguments, without which my dictionary cache's key cannot be created..)

Please help..

Thanks Sankar

Was it helpful?

Solution

you need to do special things to use OpenGL from multiple threads. See this question.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top