I'm not sure what's best practice here as I have little experience with this. Essentially what I am asking is...

if it's better to get your single PNG with all your different sprites on it for use in-game, cut out every sprite on startup and store them in memory, then access the already-cut-out sprite from memory quickly


Only have the single PNG with all the different sprites residing in memory, and when you need, for example, a tree. You cut out the tree from the PNG and then continue to use it as normal.

I imagine the former is more CPU friendly than the latter but less memory friendly, vice versa for the latter. I want to know what the norm is for game dev. This is a pixel based game using 2D art. Each PNG is actually an avatar's sprite sheet with each body part separated and then later joined to form the full body of the avatar.

  • \$\begingroup\$ Are you using GPU acceleration (OpenGL/Direct3D)? \$\endgroup\$ – API-Beast Jun 3 '12 at 22:35
  • \$\begingroup\$ Yes it will be using hardware acceleration but I was hoping that wouldn't really influence the decision? \$\endgroup\$ – xLite Jun 3 '12 at 22:57
  • \$\begingroup\$ It has a significant effect as it makes all the difference as to where the images are stored, how they would be 'cut out', how they would be rendered, etc. \$\endgroup\$ – Kylotan Jun 3 '12 at 23:26
  • \$\begingroup\$ So what would be the difference compared to using OpenGL as opposed to Flash's display list with the Bitmap class? \$\endgroup\$ – xLite Jun 3 '12 at 23:38
  • \$\begingroup\$ That's not something a single comment can answer, but the APIs are different and the performance implications are different. \$\endgroup\$ – Kylotan Jun 4 '12 at 13:28

Usually there is nothing like "cut out every sprite", because you aren't working with the data itself, but just with references. The hero sprite is not known to the application as "[...insert huge data array here...]" but as "Position 0,64 with size of 48x64 on Texture 5". And that data is trivial to generate nothing you could or should sweat over.

For GPU acceleration (something you should do) it's way better to have one big texture than many very small ones. Because with one texture you could just draw all sprites in one batch, the more you can draw at once the better. That's even more true for 2D because the amount of data is so little, puny 4 vertices per sprite. It's no problem for the GPU to draw four million vertices, but if you have one million sprites and you are going drawing them one by one instead of all at once you will have really bad performance due to the enormous overhead.

| improve this answer | |

Typically if you have the memory to spare you want to load/cache as much info as possible up front to gain speed later. This is the cause of most loading screens when starting a level in a game.

This can however be very dependent on your graphics library. For something like OpenGL you would typically use the image file to texture a poly. In this case you only need to store texture coordinates for each sprite so there isn't much of a memory hit.

In short, cut out the sprites ahead of time if at all possible.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.