Converting Zwoptex output coordinates into a useable table

Hi guys,

As most of you might know Zwoptex can be used to convert multiple images into one sprite sheet. While this is extremely useful I’ve noticed that for most animations the size of the cells within the sprite sheet can vary; this makes it difficult to use the below easy code for animations impossible to use.

local options =
{
–required parameters
width = 70,
height = 41,
numFrames = 2,

–optional parameters; used for dynamic resolution support
sheetContentWidth = 70, – width of original 1x size of entire sheet
sheetContentHeight = 82 – height of original 1x size of entire sheet
}

local imageSheet = graphics.newImageSheet( “fishies.png”, options )

Instead the “complex” declaration of each frame needs to be used, which looks like this.

local options =

{
–array of tables representing each frame (required)
frames =
{
– FRAME 1:
{
–all parameters below are required for each frame
x = 2,
y = 70,
width = 50,
height = 50
},

– FRAME 2:
{
x = 2,
y = 242,
width = 50,
height = 52
},
},

–optional parameters; used for dynamic resolution support
sheetContentWidth = 1024,
sheetContentHeight = 1024
}

local imageSheet = graphics.newImageSheet( “imageframes.png”, options )

While using Zwoptex, however, you might have come across the output coordinates of your sprite sheet show up in the below format.

local sheet = {

frames = {

{

name = “pigAnimation0001.png”,

spriteColorRect = { x = 0, y = 0, width = 58, height = 44 }, 

textureRect = { x = 0, y = 0, width = 58, height = 44 }, 

spriteSourceSize = { width = 58, height = 44 }, 

spriteTrimmed = true,

textureRotated = false

},

{

name = “pigAnimation0002.png”,

spriteColorRect = { x = 0, y = 0, width = 58, height = 44 }, 

textureRect = { x = 60, y = 0, width = 58, height = 44 }, 

spriteSourceSize = { width = 58, height = 44 }, 

spriteTrimmed = true,

textureRotated = false

}

This is interesting because if you just try to copy and paste this table into your .lua file and use it as an animation options table it will not work correctly. I’ve spent a few hours turning the above jargon by hand into useable data like the following code.

{

      name = “pigAnimation0001.png”,

      x = 0,

       y = 0,

       width = 58,

       height = 44

}

After countless copy and paste sessions between the output coordinates and my actual game file I finally came up with a solution to this.  I’ve created a conversion function that will take the outputted table from Zwoptex and convert it into a useable table that the graphics.newImageSheet( ) function can use.  The code is shown below.

local function zwoptexConvert(zTable)

           

            local _name, _x, _y, _width, _height – temporary variables to store values

            local convTable = {frames = {}} – This is the table that all the necessary values are stored in and returned as

           --For each index in the “frames” table of zTable find and store the needed info in the new table

            for i = 1, #zTable.frames do 

                       

                        --Create the index keys to store the values in the new table

                        convTable.frames[i] =

                        {

                                    name = 0,

                                    x = 0,

                                    y = 0,

                                    width = 0,

                                    height = 0

                        }

                       

                       --Search for the index terms needed in zTable and store them in the temporary variables

                        for key, value in next, zTable.frames[i], nil do 

                                    if key == “name” then

                                                 _name = value

                           

                        --Store the needed textureRect index values in the corresponding temporary variables.

                        elseif key == “textureRect” then 

                                                for k, v in next, zTable.frames[i].textureRect, nil do

                                                            if k == “x” then

                                                                         _x = v

                                                                         

                                                elseif k == “y” then

                                                                         _y = v

                                   

                                                elseif k == “width” then

                                                                        _width = v

                                   

                                                elseif k == “height” then

                                                                        _height = v

                                   

                                                            end

                                                end

                                    end

                        end

                        --Store all the values of the temporary variables in the new table’s (convTable) corresponding index variables

                        convTable.frames[i].name = _name

                        convTable.frames[i].x = _x

                        convTable.frames[i].y = _y

                        convTable.frames[i].width = _width

                        convTable.frames[i].height = _height

                       

                        --Uncomment the below chunk of code to test the validity of the stored data

                       

                        --[[print("name = "…convTable.frames[i].name)

                        print("x = "…convTable.frames[i].x)

                        print("y = "…convTable.frames[i].y)

                        print("width = "…convTable.frames[i].width)

                        print("height = "…convTable.frames[i].height)–]]

            end

           

            return convTable

           

end

So all you have to do after you set up the function is pass it into a variable with the Zwoptex table as the only parameter.

local newAnimationOptions = zwoptexConvert(zwoptexOutputTable)

Sorry for the horrible formatting.

Rob