Sprite Atlas in FlashPunk


(Linck) #1

I’ve given starling a try a time ago, and a nice thing about it was the class SpriteAtlas. You can have different sprites in one big image. Then you need to pass a xml as a parameter that has all the positions of the sprites. Then you can get what image you want from that SpriteAtlas.

How can I integrate something similar with flash punk. Is there any nice library that does that?


(azrafe7) #2

Probably @zachwlewis can point you in the right direction, as he was working on something pretty pretty close to what you’re requesting. :wink:


(Ultima2876) #3

There isn’t a huge advantage to doing so in vanilla FlashPunk however. The way it renders means that having a single big Texture Atlas doesn’t really have much of an advantage (other than if you’re using the same textures for other ports of the same game) - FlashPunk uses bitmap blitting so it doesn’t really matter what the source bitmap is; a texture atlas, individual images, the screen buffer or a dynamically generated BitmapData. They all perform the same (except if you add transformations like rotations and scaling, that’s slightly slower but a bit off-point).


(rostok) #4

@Ultima2876 you are right. There’s no real advantage in matters of speed but you can save some memory. In my case I reduced ram usage by 75%. I must admit however, that all the gain is not really worth the effort.

Anyway, here’re the links for my implementation of SpritemapAtlas:

and some php script to convert the sprites: http://pastebin.com/HTn3tBar


(Ultima2876) #5

True! However even low level consumer computers these days (netbooks included) tend to have 2gb+ of RAM. It’s not the issue it once was… unless you’re targeting mobile :smile:


(Linck) #6

@Ultima2876 So there is no need to use Atlas unless I use transformations? So if I do “myImage.scaleX = 0.5” it will still be an issue? Because I’m using transformations all the time. Rotations and scale for now.

I see. I’m not developing for mobile right now, but mobile is in my plans. So at least I want to structure things in a way I won’t have so much rework later.

@rostok Thank you, soon I’ll take a look at your code. If you say it saves 75% of ram that is quite a big deal for me.


(rostok) #8

My 75% estimate is based on quite a big spritemap with character animation. For example: the original dimensions were 2048x1920 and packed to atlas 1053x994. I suggest doing some math before diving into this atlas mayhem. Instead of megabytes it may save you some time :slight_smile:


(Ultima2876) #9

I worded that poorly - a sprite atlas offers no performance advantage at all with FlashPunk. The only thing that makes a difference is whether you are using transformations or not!

However, if you were to hack FlashPunk to use GPU mode properly, a sprite atlas might offer a small performance boost on mobile… But it’s probably more work than it’s worth!

As @rostok pointed out, depending on your game you might save a bit of memory using it… but I don’t know if that’s worth the effort unless you’re using huge sheets of sprites. Oftentimes your development time will be way more important than any memory usage gain - you could use that time to improve your gameplay mechanics or game art for example, rather than spending it doing optimisations that might not actually make any difference at all for 99.8% of the people playing your game.

Premature optimisation is bad juju.

When the time comes that you might want to put the game out on mobile I have a solution if you don’t mind publishing with us on certain platforms. More details later as I’m planning a release this week, but long story short my Stage3DPunk engine handles rendering with Stage3D as well as automatically assembling these sprite atlases at runtime (albeit not very efficiently, but enough to give a decent performance boost in Stage3D mode which very much relies on sprites sharing textures).


(Zachary Lewis) #10

I started down this rabbit hole in an out-of-date FlashPunk branch. My reasoning for doing this was two-fold:

  1. Since GPUs pad any image to the next power of two, having a three enemy sprites with a resolution of 66x66 pixels each would take the same amount of texture memory as a single 256x256 pixel texture with the same three sprites next to each other, and the larger texture could easily hold nine of those enemy sprites.
  2. The GPU is very efficient at batched draws. Drawing from the same texture 100 times can be done just as quickly as drawing from the texture once. This means it would take three times longer to draw three individual 66x66 sprites than it would to draw 30 of each of the sprites (90 sprite draws total) from a texture atlas.

So, not only would an atlased texture take less texture memory (smaller download size), but it would draw faster (higher framerate). This only applies to GPU rendering with Stage3D.

To top it all off, texture atlas support would make FlashPunk even easier to work with, since atlased textures are extremely common (for the above reason). This makes porting to and from FlashPunk easier without having to add steps to your art pipeline.


(Ultima2876) #11

I worded that poorly - a sprite atlas offers no performance advantage at all with FlashPunk. The only thing that makes a difference is whether you are using transformations or not!

However, if you were to hack FlashPunk to use GPU mode properly, a sprite atlas might offer a small performance boost on mobile… But it’s probably more work than it’s worth!

Premature optimisation is bad juju.

Thanks! Dave