I wrote this texture atlas creating library for a level editor I'm making and I think it's generally useful so Here's a link to it:
http://www.nooskewl.com/stuff/downloads/atlas-1.0.zip
There's an example that comes with it that shows how to use it (it's pretty simple). Basically you just create an atlas, feed it whatever images you have, then finish the atlas and it'll put everything into sheets (the size of which you tell it). It's not nearly an optimal algorithm spacing saving wise, but it does ok. I gave it a few hundred images and it made about 5 sheets without much wasted space.
Can you paste a code example? (Too lazy to look at the the zip.)
I was going to build something like this for my GUI.
Here's ex_atlas:
Lines 20 to 29 are the business stuff. 512, 512 is the size of the sheets you want and 2 is the border.
Posting the API:
I would kind of like:
bitmap = atlas_add_and_return_sub_bitmap(atlas, bitmap, id);
That is, I add the bitmap and it takes ownership of it (including destroying the original bitmap), and returns a pointer to the bitmap. I no longer have any control over it, although I could remove it later by id. At least that was how I was thinking about doing it.
It might not fit with your design... (Do you destroy the bitmap?) And perhaps it is a bad idea; I didn't think about it much.
Edit: I suppose since you have atlas_finish it wouldn't really be possible at that time.
Ya, it's not possible to return sub bitmaps at that point.
About destroying bitmaps, the final parameter of atlas_create tells the lib whether or not to destroy bitmaps when calling atlas_destroy.
Edit:
The reason for finish is the algorithm performs much better space wise if the bitmaps are sorted by size.
The reason for finish is the algorithm performs much better space wise if the bitmaps are sorted by size.
Yeah, in my use case, it would be dynamic. There would be no use for a finish because I'd be adding and removing.
Perhaps a better (or simply different) set up would be to have an optimize method (instead of finish) that you could call at any time. Obviously it would invalidate the previously returned pointers, but at least it would allow for both usage types.
Yeah, it really depends on how you're going to use it. For me it's simply to display tiles for the level editor (they can be any size) in a big grid instead of in rows or something like that. I'm not going to make it dynamic but patches for that would certainly be considered (though it's pretty simple, you could probably make your own without much trouble).
Now we would need a function in Allegro to move a sub-bitmap. Then you actually could return it, just later change its parent and position.
Regardless of the ATLAS stuff, being able to move a sub bitmap is probably useful.
Good stuff:
Here is a link to recursive bin-packing C++ source.
This might be more space-efficient.
The author released it as public domain.
It ports very easily to C.
http://clb.demon.fi/projects/rectangle-bin-packing
Cheers.