Sprite Sheet Packer
Monkey Forums/Monkey Code/Sprite Sheet Packer
| ||
| I thought this might come in handy. It is the code I use to open the 'png' and related 'txt' mapping file exported by the excellent [url=http://spritesheetpacker.codeplex.com/]Sprite Sheet Packer[/url] Example Usage: Using the tool, add all your images and then export (e.g. 'packed') producing 2 files (e.g. 'packed.png' and 'packed.txt')
Global s:SpriteSheet = New SpriteSheet("packed")
Global si:SpriteSheetItem = item = s.GetSpriteSheetItemByName("player1")
.
.
.
(On Render)
s.DrawSpriteSheetImage(item,0,0)
'or alternatively
s.DrawSpriteSheetImage("player1",0,0)
Room for lots more to be added, but I thought it might help someone out there
Import mojo
'comment:Class to handle sprites sheets created with 'Sprite Sheet Packer' (spritesheetpacker.codeplex.com)
Class SpriteSheet
'#Region Class Fields
Private Field _packfileimageurl:String
Private Field _packfiletxturl:String
Private Field _packfile:String
Private Field _packfiletxt:String
Field _packfileimage:Image
Field _mappings:StringMap<SpriteSheetItem> = new StringMap<SpriteSheetItem>
'#End Region
'#Region Constructor
'comment:Parameters - packfile 'The name of the pack image without the extension, the same name is used for the map data, e.g. player.png, player.txt'
Method New(packfile:String)
_packfile = packfile
_packfileimageurl = _packfile + ".png"
_packfiletxturl = _packfile + ".txt"
_packfileimage = LoadImage(_packfileimageurl)
_packfiletxt = LoadString(_packfiletxturl)
For Local line:String=Eachin _packfiletxt.Split( "~n" )
Local items:String[] = line.Split(" ")
if items.Length() = 6 then
Local sName:String = items[0]
_mappings.Set(sName, New SpriteSheetItem(sName,int(items[2]),int(items[3]),int(items[4]),int(items[5])))
End if
Next
End Method
'#End Region
'#Region Methods
'comment:Draws the image from the Sprite Sheet Image, using the name to look up the Sprite Sheet Item
Method DrawSpriteSheetImage:Void(name:String,x:Int,y:Int)
Local item:SpriteSheetItem = _mappings.ValueForKey(name)
DrawImageRect(_packfileimage,x,y,item.X,item.Y,item.Width,item.Height)
End
'comment:Draws the image from the Sprite Sheet Image, using the Sprite Sheet Item passed in
Method DrawSpriteSheetImage:Void(item:SpriteSheetItem, x:Int, y:int)
DrawImageRect(_packfileimage,x,y,item.X,item.Y,item.Width,item.Height)
End
'comment:Returns SpriteSheetItem based on name
Method GetSpriteSheetItemByName:SpriteSheetItem(name:String)
Print name
Local o:SpriteSheetItem = _mappings.ValueForKey(name)
Local oNew:SpriteSheetItem = New SpriteSheetItem(o.Name,o.X,o.Y,o.Width,o.Height)
Return oNew
End
'#End Region
End Class
'comment:Class to contain a line from the 'Sprite Sheet Packer' exported text file
Class SpriteSheetItem
Public
'#Region Fields
Field Name:String = ""
Field X:Int = 0
Field Y:Int = 0
Field Width:Int = 0
Field Height:Int = 0
'#End Region
'#Region Constructor
'comment:
Method New(name:String, x:Int, y:Int, width:Int, height:Int)
self.Name = name
self.X = x
self.Y = y
self.Width = width
self.Height = height
End
'#End Region
End Class
|
| ||
| Cheers Rob. Dead useful bit of code that. |