Wednesday, April 24, 2013

Texture Packer C++ Exporter pt2

Here is the second part of my C++ exporter Blog posting. Below I will detail all you need to export your own '.cpp' file, like the one I listed in yesterdays post.

Here is the exporter.xml file that allows me to choose the C++ exporter script I have created.
<exporter version="1.0">
    <!-- identifier of the exporter -->
    <!-- display name of the exporter for the combo box -->
    <displayname>C Plus Plus</displayname>
    <!-- description of the exporter -->
    <description>C++ Exporter</description>
    <!-- exporter version -->
    <!-- currently only one file allowed - more to come with update -->
            <!-- name of this file variable -->
            <!-- human readable name (for GUI) -->
            <displayname>cplusplus Texture</displayname>
            <!-- file extension for the file -->
            <!-- name of the template file -->

    <!-- target framework supports trimming -->
    <!-- target framework supports rotated sprites -->
    <!-- rotated sprites direction (cw/ccw) -->
    <!-- supports npot sizes -->
    <!-- supports file name stripping (remove .png etc) -->
    <!-- supports texure subpath -->

And here is the 'cplusplus.txt' file that contains the script itself for the exporter to create that wonderful output.
char tp_version[] = "cpp = 1.0";
char textureName[] = "{{texture.fullName}}";
int  textureWidth  = {{texture.size.width}};
int  textureHeight = {{texture.size.height}};

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

{% for sprite in allSprites %}
sSPRITE_INFO  {{sprite.trimmedName}} = {
  "{{sprite.trimmedName}}", // name
 {{sprite.rotated}}, // rotation
 { {{sprite.frameRect.x}}, {{sprite.frameRect.y}}, {{sprite.frameRect.x|add:sprite.frameRect.width}}, {{sprite.frameRect.y|add:sprite.frameRect.height}} }, // TexturePoints
 { {{sprite.frameRectWithoutRotation.width}}, {{sprite.frameRectWithoutRotation.height}} }, // size
    { {{sprite.cornerOffset.x}}, {{sprite.cornerOffset.y}} }, // corner offset, relative to original sprite
    { {{sprite.untrimmedSize.width}}, {{sprite.untrimmedSize.height}} }, // original sprite width
{% endfor %}

static const sSPRITE_INFO *{{texture.trimmedName}}[] =
{ {% for sprite in allSprites %}
  &{{sprite.trimmedName}},{% endfor %}

// Enumeration for defining the image indices for the lookup table above
{ {% for sprite in allSprites %}
  SA_{{sprite.trimmedName}},{% endfor %}

Obviously you can change this to export whatever you want to customize the output to your needs. This will hopefully give you a good starting point.

Da Voodoochief


  1. thank you very much!!
    That solves my problem pretty well
    Best regards

  2. Glad anything I write was helpful to you.

    Good Luck with your projects,
    Da Voodoochief