views:

111

answers:

1

Hi,

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

A: 

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

David Maymudes
Thanks for the help, David. I am new to OpenGL.. so any idea how I can do this in cocos2d.. I think AtlasSpriteManager uses OpenGL internally.. but can I write OpenGL code that will affect AtlasSpriteManager?
Sankar
you probably need to do the magic that they talk about in the linked article to tell your new thread to use a matching OpenGL context. I'd suggest searching for cocos2d and EAGLShareGroup and seeing if you can find somebody who's already doing this... Also, you'll probably need to manage your own threads rather than just using performSelectorInBackground.
David Maymudes
Thanks David.. I did search on this and found that addImageAsync function already does these things internally.. So I changed my code to somehow use that function itself.. Thanks anyway.. :)
Sankar