3

I am giving my first steps with cocos2d-iphone.

I am using:

CCSpriteFrameCache *frameCache = [CCSpriteFrameCache sharedSpriteFrameCache];
[frameCache addSpriteFramesWithFile:@"textures.plist"];

to use my zwoptex file. With that set, I am creating my CCSprites using the frameCache like this:

[CCSprite spriteWithSpriteFrameName:@"filename.png"];

So far, so good. Now I am creating my own particle system and I need to set the texture. I guess it would make sense to read from the zwoptex but I couldn't find a way to do it. I also checked the examples and they do something like:

emitter.texture = [[CCTextureCache sharedTextureCache] addImage: @"fire.pvr"];

So my questions are:

  • If I am going to have more than a particle system with different textures. Should I create a zwoptext with them? If so, how?
  • Why do the examples do [[CCTextureCache sharedTextureCache] addImage: @"file"];? it's because it's not added two times?

EDIT: I just posted this in the cocos2d's forum.

Macarse
  • 91,829
  • 44
  • 175
  • 230
  • From my understanding, pvr textures are not optimal for 2D situations, you should rethink that bit of code – arclight Apr 14 '11 at 21:22

1 Answers1

6

If I am going to have more than a particle system with different textures. Should I create a zwoptext with them? If so, how?

A texture sheet might improve the performance for rendering, because it would not change OpenGL ES state (binding textures). Please take a look at "How to Create and Optimize Sprite Sheets in Cocos2D with Texture Packer and Pixel Formats". I strongly recommend you to use TexturePacker to create texture sheets.

Why do the examples do [[CCTextureCache sharedTextureCache] addImage: @"file"];? it's because it's not added two times?

It is easy to use for loading OpenGL texture. You must not use OpenGL ES API such as glTexImage2D or so forth. Also, of course, CCTextureCache always caches textures. It returns immediately CCTexture2D instance if the specified file is already cached.

EDIT:

You can get CCTexture2D instance from CCSpriteFrameCache with zwoptext plist.

CCSpriteFrameCache *frameCache = [CCSpriteFrameCache sharedSpriteFrameCache];
[frameCache addSpriteFramesWithFile:@"textures.plist"];

CCSpriteFrame *frameFire = [frameCache spriteFrameByName:@"particle_fire"];
CCTexture2D *texFire = frameFire.texture;
...
glBindTexture(GL_TEXTURE_2D, texFire.name);
/* You may need to use frameFire.rectInPixels and frameFire.rotated for the texture coordinates. */
...

CCSpriteFrame *frameWater = [frameCache spriteFrameByName:@"particle_water"];
CCTexture2D *texWater = frameWater.texture;
...
glBindTexture(GL_TEXTURE_2D, texWater.name);
...
Kazuki Sakamoto
  • 13,929
  • 2
  • 34
  • 96
  • Thanks for pointing out TexturePacker, but I am using zwoptex. The link with the tutorial doesn't mention how to get a texture from a texture sheet. – Macarse Apr 15 '11 at 11:32
  • Cool, thanks. Almost there but `frameFire.texture` refers to the big texture instead of the "cropped" one. – Macarse Apr 15 '11 at 16:54
  • Yes, thus it is fast. Use fireFrame properties such as rectInpixels for texture coordinates. The cost to change texture binding is expensive. – Kazuki Sakamoto Apr 16 '11 at 01:32