Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

optimized TexturePacker code?
Started by producerism Nov 08 2011 08:25 PM

2 replies to this topic
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

producerism

[GLOBAL: userInfoPane.html]
producerism
  • Contributor

  • 249 posts
  • Corona SDK

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:

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			},		}	}endreturn SpriteSheet


But I've modified that to be this: (notice that I've created internal functions for "getSpriteSheetFromData" and "getSpriteSet"

module(..., package.seeall)local sprite = require("sprite")local frameCountfunction 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 sheetendfunction getSpriteSheetFromData()  return sprite.newSpriteSheetFromData( "assets/images/environment/clubhouse/chair.png", getSpriteSheetData() )endfunction 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 spriteSetend


That way, in my game logic I simply call:

local obstacleChair = require("entities.obstacles.chair")obj = sprite.newSprite(obstacleChair.getSpriteSet())


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?
uid: 49447 topic_id: 17502 reply_id: 317502


[TOPIC: post.html]
#2

andreas.loew

[GLOBAL: userInfoPane.html]
andreas.loew
  • Enthusiast

  • 79 posts
  • Corona SDK

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

uid: 9611 topic_id: 17502 reply_id: 66450


[TOPIC: post.html]
#3

producerism

[GLOBAL: userInfoPane.html]
producerism
  • Contributor

  • 249 posts
  • Corona SDK

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:

local sprite = require("sprite")local SpriteSheet = {}local frameCountlocal 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 sheetendlocal getSpriteSheetFromData = function()  return sprite.newSpriteSheetFromData( "assets/images/environment/clubhouse/statue.png", getSpriteSheetData() )endSpriteSheet.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 spriteSetendreturn SpriteSheet
uid: 49447 topic_id: 17502 reply_id: 67166



[topic_controls]
[/topic_controls]