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 -->
    <name>CPlusPlus</name>
    <!-- display name of the exporter for the combo box -->
    <displayname>C Plus Plus</displayname>
    <!-- description of the exporter -->
    <description>C++ Exporter</description>
    <!-- exporter version -->
    <version>1.0</version>
 
    <!-- currently only one file allowed - more to come with update -->
    <files>
        <file>
            <!-- name of this file variable -->
            <name>cplusplus</name>
            <!-- human readable name (for GUI) -->
            <displayname>cplusplus Texture</displayname>
            <!-- file extension for the file -->
            <fileextension>cpp</fileextension>
            <!-- name of the template file -->
            <template>cplusplus.txt</template>
        </file>
    </files>

    <!-- target framework supports trimming -->
    <supportstrimming>true</supportstrimming>
    <!-- target framework supports rotated sprites -->
    <supportsrotation>true</supportsrotation>
    <!-- rotated sprites direction (cw/ccw) -->
    <rotationdirection>cw</rotationdirection>
    <!-- supports npot sizes -->
    <supportsnpot>false</supportsnpot>
    <!-- supports file name stripping (remove .png etc) -->
    <supportstrimspritenames>yes</supportstrimspritenames>
    <!-- supports texure subpath -->
    <supportstexturesubpath>yes</supportstexturesubpath>
</exporter>

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
enum
{ {% 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.

G'night
Da Voodoochief
aa

2 comments:

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

    ReplyDelete
  2. Glad anything I write was helpful to you.

    Good Luck with your projects,
    Da Voodoochief

    ReplyDelete