Texture Atlas

Originally posted by Thomas.

The texture atlas was one of the first thing that I implemented in the engine. It was actually something I started working on the day work started for Energetic, which also means the code is a bit messy 😀

 So I decided an update was needed.

So what is a texture atlas? Basically it is something that has been made to solve two major problems when rendering 2D GUI stuff (fonts, HUD, menus,etc):
1) One usually want this kind of graphics in non-power-of-two sizes like 23×43, 3×19, etc. But these kinds of sizes are not supported by most graphics hardware.
2) One wants to draw alot of different images on screen at once. However, 3d cards work best when a lot of things can be drawn at once and having 100 images using different texture is going to be slow.
The solution is to many images sharing the same texture. That way you can have any size you like and many images can be drawn at the same time!

One of the biggest problem with the current code was that a deleted image continued to use memory until all of the images sharing the same texture was deleted. To fix this I decided to add all images on the same texture again once an image was deleted and skipping the deleted image. While working on this code, it hit me that I could sort the images before adding them again, making them fit better, which would save quite a bit of memory. Since I could not do this every time an image was added, I decided on only sorting the images on a texture once an new image did not fit and then see if it fitted after the reorganisation. This way turned out to work great.

So now the engine has a system that can delete images without wasting memory and also saves alot of memory when saving the bitmaps 😀

Attached are pictures of one texture with unsorted images and one where the images are sorted.