Packed Textures

Monkey Forums/Monkey Code/Packed Textures

Wagenheimer(Posted 2011) [#1]
I have added support to packed textures! I'm using this texture packer from Spoonman :

http://dev.ixchels.net/forum/showthread.php?t=650&highlight=texture+packer

It's export a .png file and a .txt file with the patterns info.

Here is the Monkey class to load and use this packed textures!

I hope that is quite useful for you as it is for me! Any questions or suggestions for improvement please reply here! =)

Cezar

It's very easy to use, example

' Create and Load
Field PackedTexture : TPackedTexture
PackedTexture = New TPackedTexture("sprites.png","sprites.txt") ' Image name and patterns info filename

' Draw a custom pattern from Image
PackedTexture.Draw(patternname,X,Y,Angle,ScalrX,ScaleY)




Strict

Import mojo

Class TPattern
   Field Name:String
   Field X:Int
   Field Y:Int
   Field Width:Int
   Field Height:Int

   Method New(AName:String,AX:Int,AY:Int,AWidth:Int,AHeight:Int)
      Name=AName
      X=AX
      Y=AY
      Width=AWidth
      Height=AHeight
   End
End

Class TPackedTexture
    Field img:Image
	Field Patterns:List<TPattern>
	Field Pattern:TPattern

	Method New(AName:String,APatterns:String)
		img=LoadImage(AName)
	    Patterns = New List<TPattern>
			
		'Load Patterns from File
		Local str$=LoadString(APatterns)
		Local pos:Int
		Local patternscount:Int
		
		Local Pattern : TPattern
		
		pos=0

		Pattern = New TPattern()		
		For Local line$=Eachin str.Split( "~n" )
			If pos=0 patternscount=Int(line.Trim())
			If pos=1 Pattern.Name=line.Trim()   
			If pos=2 Pattern.X=Int(line.Trim())
			If pos=3 Pattern.Y=Int(line.Trim())
			If pos=4 Pattern.Width=Int(line.Trim())  
			If pos=5 
				Pattern.Height=Int(line.Trim())						
				Patterns.AddLast(Pattern)
				Print ("Pattern " + Pattern.Name + " " + Pattern.X + " " + Pattern.Y + " " + Pattern.Width + " " + Pattern.Height)

				Pattern = New TPattern()
				pos=0
			Endif
	
			pos+=1
		Next
	End
	
	Method FindPattern:TPattern(APatternName:String)
	    For Local tmpPattern:=Eachin Patterns
	        If tmpPattern.Name=APatternName Return tmpPattern
	    Next
	    Return Patterns.First()
	End
	
	
	Method Draw:Void(APatternName:String, AX:Int,AY:Int,ARotation:Float,AScaleX:Float,AScaleY:Float)
	    
	    'Only change pattern if the current pattern is different than the cached one
		If (Not Pattern) Or (Pattern.Name<>APatternName) Pattern=FindPattern(APatternName)
	    img.SetHandle( Pattern.Width/2, Pattern.Height/2)
		DrawImageRect( img, AX, AY, Pattern.X, Pattern.Y, Pattern.Width, Pattern.Height, ARotation, AScaleX, AScaleY, 0 )
	End
End




Virtech(Posted 2011) [#2]
Hi!

Thanks for this, very useful.
Will it work ok with animation too? Could you post an example please.


Wagenheimer(Posted 2011) [#3]
I have created a simple sprite class with animations support which use this packed textures! I will make an example and post it here!


degac(Posted 2011) [#4]
@Wagenheimer

Why dont' use Map instead of List? It should a little faster in finding the correct 'pattern' I think. You have already the KEY_NAME

I'm doing a similar thing, when it's ready I'll post it.


pinete(Posted 2011) [#5]
Thanks! completely useful! some example could be great!