Tuesday, April 23, 2013

Texture Packer C++ Exporter pt1

I have written about how excellent the Texture Packer is, and you can find out more by visiting the site at http://www.codeandweb.com/texturepacker.

I am using this tool for my Marmalade code and to that end I am using the language C++. There was no C++ output for the texture packer so I created my own. I have realised recently that although I have mentioned this in past posts. I have not divulged my exporter scripts.

It creates a '.cpp' file for me, and although it isn't a perfect solution, it is very easy to deal wiht its 'issues'. The issue I face when running Texture Packer is that within my '.cpp' file I also have the enumeration of the sprites, this enumeration needs to be copied into a '.h' file and removed from the '.ccp' file. That is so I can use the enumeration to choose which sprite to render from anywhere in my drawing routines.

Here is a sample of the output.
char tp_version[] = "cpp = 1.0";
char textureName[] = "sprite_atlas.png";
int  textureWidth  = 2048;
int  textureHeight = 2048;

struct  sSPRITE_INFO
{
 char name[32];
 bool rotation;
 float texturePoints[4];
 int size[2];
 int offset[2];
 int originalSize[2];
};


sSPRITE_INFO  BlueDream_Final = {
  "BlueDream_Final", // name
 false, // rotation
 { 1704, 1004, 1858, 1130 }, // TexturePoints
 { 154, 126 }, // size
    { 0, 0 }, // corner offset, relative to original sprite
    { 154, 126 }, // original sprite width
};

static const sSPRITE_INFO *sprite_atlas[] =
{
  &BlueDream_Final,
};

// Enumeration for defining the image indices for the lookup table above
enum
{
  SA_BlueDream_Final,
};


After I have generated this file I simply cut the Enum table and place into my header file.

You can see from the code that I declare a 'struct' that will allow me to easily acccess the elements that are important to rendering each sprite. You may also notice that I use the Enumeration to index into the Sprite Info array that has pointers to all the individual sprite structures. Using these sprite structures allows me to easily render the sprite.

I will continue with this development posts. Next post will be showing you how I created this '.cpp' file.

G'night
Da Voodoochief

No comments:

Post a Comment