官术网_书友最值得收藏!

Using an atlas

As I have mentioned already, texture memory is one of your core resources. In fact, it is common to run out of memory because of all the textures required to animate a typical 2D game. It is also time-consuming to load inpidual textures rather than loading on a larger texture. So, we have to come up with methods to use texture memory more efficiently.

One common technique designed to pack more textures into less space is known as atlasing. A texture atlas works much like a sprite sheet described earlier in this chapter. Instead of storing each texture as its own image, we pack all of the textures for the entire game into one or more textures known as atlases.

As the word suggests, an atlas works much like a map. We simply need to know the location of any particular image, and we can find and extract it out of the atlas. Every atlas consists of two parts:

  • The texture that contains all of the images
  • A text file that contains the positions of each image in the atlas

As you can imagine, efficiently packing thousands of images into an atlas and then keeping track of each image's position within the atlas would be almost impossible to manage manually. This is why there are programs to do this for us.

I use a free texture atlas tool called Texture Atlas Generator. You can download this at http://www.gogo-robot.com/2010/03/20/texture-atlas-sprite-sheet-generator/.

A detailed example of atlasing is beyond the scope of this chapter. If you want to explore this on your own, here are the steps that you require:

  1. Use a program, such as the one just mentioned, to create your atlas.
  2. Save your data as an XML file.
  3. Write a class to parse the XML saved in the previous step (I suggest TinyXML at http://www.grinninglizard.com/tinyxml/ as a starter).
  4. Using the code to work with sprite sheets, modify the sprite class to be able to handle sub-textures from any arbitrary position in a larger texture.
主站蜘蛛池模板: 武宣县| 屏南县| 九龙坡区| 马龙县| 甘泉县| 麟游县| 永平县| 桂平市| 长丰县| 南投市| 莱州市| 喀喇沁旗| 山阳县| 随州市| 永和县| 桐梓县| 沂南县| 宜君县| 友谊县| 类乌齐县| 柳河县| 会理县| 伊金霍洛旗| 乐都县| 苏州市| 玉树县| 亚东县| 新巴尔虎右旗| 苍溪县| 通榆县| 探索| 山阴县| 准格尔旗| 浦东新区| 宣化县| 河北省| 徐水县| 兰坪| 库车县| 景泰县| 乐安县|