I want to know if there is any benefiting caching a sprite sheet and accessing the sprite by frame without using the CCSpriteBatchNode?
In some parts of my game the sprite batch node is useful because there is a lot on the screen, however on another part its not, because there are just a few things, and there are requirements for layers so CCSpriteBatchNode wouldn't be useful. However, for the sake of consistency I would like to use Sprite Sheets for all my sprites, and so was beginning to wonder if I would still receive any benefit from it? (Or worse that it could some how be slower...)
There is defiantly a benefit to putting all your sprites into a texture atlas or sprite sheet as you called it. Textures are stored in memory in power of 2 dimensions. So if you have a sprite that is 129px by 132px it is stored in memory as 256px by 256px which is the nearest power of 2 size. If you have many sprites that is quite a lot of extra memory used up.
By using a texture atlas you only have one texture in memory and then it pulls the pieces out of it that it needs for your sprites. These sprites can be whatever size you want without having to worry about power of 2 sizes.
You can read more details about it on this tutorial
http://www.raywenderlich.com/2361/how-to-create-and-optimize-sprite-sheets-in-cocos2d-with-texture-packer-and-pixel-formats
Related
I have an iPad application containing a grid of 400 CCSprites, some of which contain several sprites already layered together.
I have not used CCSpriteBatchNode as I believe the ZOrder needs to be the same for all items in the node, and this isn't the case. Performance is acceptable.
However, I need to optionally overlay a small dot in the centre of each of the 400 sprites (if the user wants a grid displayed to help with layout). The problem is, by adding another CCSprite (using addChild) I now have over 800 sprites on the screen and it's hitting the performance hard.
Is there a way to draw the new sprite onto the one that's already there that doesn't affect performance? (I guess like dynamically generating a new sprite made up of the parts, but that isn't a set of stacked CCSprites)
I'm using Cocos2d V2.3 - I did attempt to convert the project to Cocos2d V3, but found performance slightly worse, and it caused many scaling issues, which, given the project is close to completion, mean it's better to finish this in V2.3.
Many thanks in advance for any suggestions. PS: I found some similar questions but they dealt with how to stack sprites - this isn't a problem, I can do that, it's the drawing a new sprite that I'm struggling with.
Change textures of existing sprites to the textures with the dot.
If this solution doesn't match your case, please supply a screen shot (or part of screen shot), or more detailed description of the sprites on your scene.
The answer comes in the form of CCRenderTexture - draw all the sprites into a a single texture, and then use that for a single CCSprite.
I developed a game with SDL2.00 and c++. I seemed to be having memory and CPU issues. CPU usage goes up to 40% and memory usage goes up by 5mg a second.
So I think the reason is the way Im handling Textures\Sprites.
My question is should I create a different/new sprite/texture for every instance ?
For example, I have a class called Enemy which contains all the variable and methods related to enemy monsters such as HP, damage, location , image(Texture) etc. This class contains its own texture to be rendered onto the renderer.
Is this the right way? Or should I create a Sprite/Texture for all the images before hand and render them as needed?
and I'm wondering if this will render two different images onto the renderer:
RenderCopy(renderer, image);
image->SetPosition(X,Y);
RenderCopy(renderer,image);
or is it going to move the sprite to the new position?
I think my issues are caused my overloading the renderer and/or having too many textures being loaded.
Let me know what you think .
OpenGL is not a scene graph. It's a drawing API. So everytime you're allocating a new texture you're creating a new data object that consumes memory. You should reuse and share resources where possible.
My question is should I create a different/new sprite/texture for
every instance ?
No, not unless every instance of the sprite uses different textures, otherwise you should reuse them, ie only load them once and store a pointer to the texture the sprite uses.
and I'm wondering if this will render two different images onto the
renderer:
RenderCopy(renderer, image);
image->SetPosition(X,Y);
RenderCopy(renderer,image);
or is it going to move the sprite to the new position?
This will copy the image twice. First at the old position and then at the new. What you will get is a trailing image. You should probably either clear the previous position or render the background at that position (or whatever should be there).
What is the best approach to display a same image multiple times e.g. 1000-2000 times? The image has to be rendered in discrete sizes across the screen. The most straight forward idea seems to be declaring different sprites for each image, but there should be a better approach?
That would be a lot ! I would create a batch node with the texture, and add to the batch node 999-1999 additional CCSprites that would be scaled and positioned where you want them. Then add the batch node to the scene and position it.
Then i would test that on the slowest-most-memory-limited device you intend to support with your app. I've gone up to 500 or so replicas like this (remember, only one draw call with a batch node). I dont have a good 'feel' for your use case. In my case, the texture is small.
I'm building a cocos2d game where I use two background sprites, actually one is a sprite, the other one is a CCMask that is used to make holes into the other background, but the performance problem is the same even when using 2 regular background sprites on top of each other.
When I use one background sprite, my FPS is around 60 all the time, when I use two background sprites the FPS drops to 30 every time. I've googled around, tried different solutions including reading sprites from a sprite frame cash instead of from a file, unfortunately the result is the same.
I just can't figure out why this is happening. Does any one here have any idea why this is happening and how to get around it?
On older devices (1st & 2nd generation, ie iPhone 3G) this can easily happen since they have terrible fillrates.
If possible try to SpriteBatch the two background images. You need to add both to a texture atlas, for example with TexturePacker. Sprite batching is particularly effective if the sprites are large.
Also, just in case: don't test performance in the Simulator. Simulator performance has no relation to actual device performance whatsoever.
My idea would be to draw several Graphics objects on memory and combine them when drawing the image.
But I haven't got a precise idea of how to do that. Shall I use GraphicsContainer's? Or save the objects as Metafile's? (these are temporary objects, I would like to keep them on memory)
Simplest method: create multiple bitmaps. Draw what you want to them. Composite them by drawing them back to front.
If you have a lot of text, then using a metafile for those layer(s) may improve the rendering quality somewhat.