optimized TexturePacker code?

I’m trying to consolidate and optimize code right now, and I’ve been modifying the Lua that Texture Packer publishes. Just wondering if there is any reason what I’m doing is poor practice.

The default output is this:

[lua]local SpriteSheet = {}
SpriteSheet.getSpriteSheetData = function ()
return {
frames = {
{
name = “chair1.png”,
spriteColorRect = { x = 0, y = 0, width = 43, height = 58 },
textureRect = { x = 0, y = 0, width = 43, height = 58 },
spriteSourceSize = { width = 43, height = 60 },
spriteTrimmed = true,
textureRotated = false
},
{
name = “chair2.png”,
spriteColorRect = { x = 0, y = 0, width = 43, height = 58 },
textureRect = { x = 43, y = 0, width = 43, height = 58 },
spriteSourceSize = { width = 43, height = 60 },
spriteTrimmed = true,
textureRotated = false
},
{
name = “chair3.png”,
spriteColorRect = { x = 4, y = 8, width = 39, height = 52 },
textureRect = { x = 86, y = 0, width = 39, height = 52 },
spriteSourceSize = { width = 43, height = 60 },
spriteTrimmed = true,
textureRotated = false
},
}
}
end
return SpriteSheet[/lua]

But I’ve modified that to be this: (notice that I’ve created internal functions for “getSpriteSheetFromData” and “getSpriteSet”

[lua]module(…, package.seeall)

local sprite = require(“sprite”)

local frameCount

function getSpriteSheetData()
local sheet = {
frames = {
{
name = “chair1.png”,
spriteColorRect = { x = 0, y = 0, width = 43, height = 58 },
textureRect = { x = 0, y = 0, width = 43, height = 58 },
spriteSourceSize = { width = 43, height = 60 },
spriteTrimmed = true,
textureRotated = false
},
{
name = “chair2.png”,
spriteColorRect = { x = 0, y = 0, width = 43, height = 58 },
textureRect = { x = 43, y = 0, width = 43, height = 58 },
spriteSourceSize = { width = 43, height = 60 },
spriteTrimmed = true,
textureRotated = false
},
{
name = “chair3.png”,
spriteColorRect = { x = 4, y = 8, width = 39, height = 52 },
textureRect = { x = 86, y = 0, width = 39, height = 52 },
spriteSourceSize = { width = 43, height = 60 },
spriteTrimmed = true,
textureRotated = false
},
}
}
frameCount = #sheet.frames
return sheet
end

function getSpriteSheetFromData()
return sprite.newSpriteSheetFromData( “assets/images/environment/clubhouse/chair.png”, getSpriteSheetData() )
end

function getSpriteSet()
local spriteSet = sprite.newSpriteSet(getSpriteSheetFromData(), 1, frameCount)

sprite.add( spriteSet, “chair1”, 1, 1, 350, 0)–
sprite.add( spriteSet, “chair2”, 2, 1, 350, 0)–
sprite.add( spriteSet, “chair3”, 3, 1, 350, 0)–

return spriteSet
end[/lua]

That way, in my game logic I simply call:

[lua]local obstacleChair = require(“entities.obstacles.chair”)
obj = sprite.newSprite(obstacleChair.getSpriteSet())[/lua]

I’m guessing that I’ve either made a really great discovery, or I’m terribly misguided. Although my method certainly helps me keep the code tidy… I’m not sure if there is an impact on performance (either good or bad). Care to share any insight? [import]uid: 49447 topic_id: 17502 reply_id: 317502[/import]

Hi,

I exported the module() thing in earlier version of TexturePacker but changed it for 2.4.0. This was because I read this blog post here:

http://blog.anscamobile.com/2011/09/a-better-approach-to-external-modules/

Adding the getSpriteSet might be a good idea for future improvements.

Cheers
Andreas

[import]uid: 9611 topic_id: 17502 reply_id: 66450[/import]

an update to the code I posted above, so it’s not using module anymore. I believe this may be a better way of exporting spritesheets and and spritesets…

Let me know if there is anything else I could do to improve this, or if I’m overlooking anything:

[lua]local sprite = require(“sprite”)

local SpriteSheet = {}
local frameCount

local getSpriteSheetData = function()
local sheet = {
frames = {
{
name = “statue1.png”,
spriteColorRect = { x = 0, y = 0, width = 36, height = 59 },
textureRect = { x = 0, y = 0, width = 36, height = 59 },
spriteSourceSize = { width = 36, height = 63 },
spriteTrimmed = true,
textureRotated = false
},
{
name = “statue2.png”,
spriteColorRect = { x = 0, y = 1, width = 36, height = 61 },
textureRect = { x = 36, y = 0, width = 36, height = 61 },
spriteSourceSize = { width = 36, height = 63 },
spriteTrimmed = true,
textureRotated = false
},
{
name = “statue3.png”,
spriteColorRect = { x = 0, y = 14, width = 36, height = 49 },
textureRect = { x = 72, y = 0, width = 36, height = 49 },
spriteSourceSize = { width = 36, height = 63 },
spriteTrimmed = true,
textureRotated = false
},
}
}
frameCount = #sheet.frames
return sheet
end

local getSpriteSheetFromData = function()
return sprite.newSpriteSheetFromData( “assets/images/environment/clubhouse/statue.png”, getSpriteSheetData() )
end

SpriteSheet.getSpriteSet = function()
local spriteSet = sprite.newSpriteSet(getSpriteSheetFromData(), 1, frameCount)

– unarmed
sprite.add( spriteSet, “statue1”, 1, 1, 350, 0)–
sprite.add( spriteSet, “statue2”, 2, 1, 350, 0)–
sprite.add( spriteSet, “statue3”, 3, 1, 350, 0)–

return spriteSet
end

return SpriteSheet[/lua] [import]uid: 49447 topic_id: 17502 reply_id: 67166[/import]