Using one texture with two different pixel formats - cocos2d-iphone

I'm not sure when you'd want to do this (maybe when moving from a low quality preview -> high quality image), but anyway the Cocos2D texture cache doesn't let you use one image with two different texture formats.
[CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_A8];
CCSprite* a = [CCSprite spriteWithFile#"image.png"];
[CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_Default];
CCSprite* b = [CCSprite spriteWithFile#"image.png"]; // uses A8 not default
Is there a way around this?

No, the texture cache regards textures using the same file name as identical.
There's only two ways to make this work:
modify CCTextureCache
save the same image using two different file names
If you're worried about app size for download, you could also copy image files from the main bundle to the documents directory and change the file names when the app starts for the first time.

Related

Convert CocosBuilder CCB to PNG

I have a bunch of CCB files that consist of CCSprite and 4-5 layers - no animations.
I need a way to convert these CCBs to flat PNG images.
Creating Smart Sprite Sheets is not good, as it saves each layer as different sprite on the sheet and I need those merged. How would I do this?
I'm not sure why you would want to do this....but if you really want a png file of a ccb file, why don't you take a screenshot of your file's layout in spritebuilder/cocosbuilder and save it as a png?
I have to agree with everyone that this sounds like a strange request. But, if you really need to save the result of loading CCB into PNG image you can try to use CCRenderTexture.

How to change the image in a sprite made from textureAtlas image using spriteWithSpriteFrameName

I have a cocos2d iOS app with Box2D (and Kobold2D); i have an array of 18 CCSprites in a layer. They are now created using spriteWithSpriteFrameName and a textureAtlas (thank you texturePacker). When i want to update the 18 sprites, i think i can either a) change the image (but i am not how to do that -- i saw a reference to setDisplayFrame, but i need to get the image from the batch node / texture atlas using spriteWithSPriteFrameName) or b) destroy the sprite i previously created and added to the layer with addChild and create a new one it it's place (18 sprites, 16 times in one "game"). In terms of resource usage and performance, which method is preferred? Seems like a), but again, not sure how to do that.
thanks
You could add the following code as extension to CCSprite:
-(void) setDisplayFrameNamed:(NSString*)name
{
[self setDisplayFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:name]];
}
If you are using box2d you could also use GBox2D
which is described in detail in the MonkeyJump Tutorial
The ideal solution is not to switch textures of a sprite at all if you can avoid it. Second best is changing texture via Spriteframe (note that this rules out the use of a CCSpriteBatchNode).
Creating new sprites is typically the operation which has the highest negative performance impact.

Cocos2D - Efficient Sprite Sheets with Transposed Sprites

I have just started using sprite sheets in Cocos2D in an attempted to better utilize the texture memory and the artist generating my assets has a script that he used for some previous games in Unity3D. The tool takes a number of images, removes the transparent and white space and stuffs them into atlases. It returns the "position" and "uvs" for each sprite in a text file. One thing the tool does that we can't seem to disable is that it transposes some of the sprites to fit them better.
I want to load the animations from a plist file in Cocos2D. Is there any way to transpose them back to normal while loading the frames into the Texture cache? If not how would I transpose the individual frames after I've loaded them into a CCAnimation?
If none of this works I'll just cut and paste all of the transposed sprites into more atlases and deal with using a little extra texture memory.
I can only recommend to use one of the texture tools available for cocos2d. There's Zwoptex, and I personally would recommend TexturePacker. You'll get a lot more options out of it and don't have to worry about any of these issues.
You can use Sprite Master. You can export your spritesheet in png,tiff format and also it supports Cocos2D spritesheet .plist format. You can export to Corona, LibGDX, Sparrow game engines and additionally it exports CSS for web developers.
With this solution, It doesn't matter what game engine you are using.

Strange scaling behavior

I have my own class that holds a sprite.
The sprite is an animation made with Zwoptex. I got both retina and standard images alright.
I put my class in middle of the scene. And, for some reason, the sprite displays with a really small size.
I thought it might be because of scaling (although I never scale the sprite). So I decided to put two NSLogs:
NSLog(#"%f",enemy2.scale);
NSLog(#"%f",enemy2.sprite.scale);
One tells me the scale of my custom class itself and the other the scale of the sprite itself.
However, when I put those two lines of code, the sprite appears with the expected size (bigger).
And the NSLog result is 1.0.
Why? Any ideas?
Unless the scale getter method has been implemented and changes the scale_ instance variable this should not happen.
Initially I would say that the sprites being small size seems to indicate that you are loading the SD images on a Retina device, instead of the HD images. Be sure to name your SD and HD assets like this if you're using a texture atlas:
// SD images
textureatlas.png
textureatlas.plist
--> player.png
--> enemy.png
// HD images
textureatlas-hd.png
textureatlas-hd.plist
--> player.png // no -HD suffix!
--> enemy.png // no -HD suffix!
It is a common mistake to also suffix the sprite frames inside the texture atlas. So if your sprite frames in the HD texture atlas are named player-hd.png and enemy-hd.png then Cocos2D will not find them and reverts to loading the SD images.
It should be noted that TexturePacker will detect and can automatically correct this for you. Last time I used Zwoptex, it would allow you to create such incorrect texture atlases.

DirectX9 Texture of arbitrary size (non 2^n)

I'm relatively new to DirectX and have to work on an existing C++ DX9 application. The app does tracking on a camera images and displays some DirectDraw (ie. 2d) content. The camera has an aspect ratio of 4:3 (always) and the screen is undefined.
I want to load a texture and use this texture as a mask, so tracking and displaying of the content only are done within the masked area of the texture. Therefore I'd like to load a texture that has exactly the same size as the camera images.
I've done all steps to load the texture, but when I call GetDesc() the fields Width and Height of the D3DSURFACE_DESC struct are of the next bigger power-of-2 size. I do not care that the actual memory used for the texture is optimized for the graphics card but I did not find any way to get the dimensions of the original image file on the harddisk.
I do (and did, but with no success) search a possibility to load the image into the computers RAM only (graphicscard is not required) without adding a new dependency to the code. Otherwise I'd have to use OpenCV (which might anyway be a good idea when it comes to tracking), but at the moment I still try to avoid including OpenCV.
thanks for your hints,
Norbert
D3DXCreateTextureFromFileEx with parameters 3 and 4 being
D3DX_DEFAULT_NONPOW2.
After that, you can use
D3DSURFACE_DESC Desc;
m_Sprite->GetLevelDesc(0, &Desc);
to fetch the height & width.
D3DXGetImageInfoFromFile may be what you are looking for.
I'm assuming you are using D3DX because I don't think Direct3D automatically resizes any textures.