Loading a strip of sprites

BlitzMax Forums/BlitzMax OpenGL Programming/Loading a strip of sprites

Could somebody please tell me the best way to load an entire strip animation into OpenGL using Blitzmax?

I would just prefer to use a strip image of a 2d sprite rather than say 6 individual files loaded as pixmaps upon initialization. As far as I know pixmaps can't contain frames or be rectangular in demension.

I have a 16 frame sprite it's saved as a png file. I understand how to apply textures to polygons but how can one allow for a frame strip as a png file? I know that the transparency mask is easly defined in Blitzmax I'm sure I'll need that done in OpenGL aswell.

Thanks,
Ryan

LoadAnimImage

Function LoadAnimImage:TImage( url:Object,cell_width,cell_height,first_cell,cell_count,flags=-1 )


LoadAnimImage extracts multiple image frames from a single, larger image. url can be either a string or an existing pixmap.

See LoadImage for valid flags values.

Thanks Rik, I was under the impression that you could only use Pixmaps for images. Am I mistaken?

I've found learning OpenGL within Blitzmax frusterating. It seems that there is even less documentation for OpenGL in Blitzmax. Some commands you must know and there is no master list. Other times commands have to be done differently.

I still need some help with this.

Don't many people use OpenGL with blitzmax?

Pixmaps can have any dimensions whatsoever. Now on to the main event!


Animated Images 101:

So you want to load an image strip? Well, BlitzMax has you covered for that, but if you want to load a strip of images, and then get the Pixmaps, you'll need to know how the TImage Class works. First use this to load the strip:

Local myanimation:TImage=LoadAnimImage(myurl:Object,width,height,first,count,flags=-1)


Now you must extract the pixmaps. You'll need to access the pixmap field of the TImage Class on the instance 'myanimation' you created previously. This is the BRL code used to define a TImage (DO NOT INCORPORATE IN TO YOUR OWN CODE, IT'S AUTOMATICALLY DONE FOR YOU):

Strict

Import "driver.bmx"

Import BRL.Data

Type TImage Extends TData

	Field width,height,flags
	Field mask_r,mask_g,mask_b
	Field handle_x#,handle_y#
	Field pixmaps:TPixmap[]

	Field frames:TImageFrame[]
	Field seqs[]
	
	Method DataType:TImageDataType()
		Return ImageDataType
	End Method
	
	Method Frame:TImageFrame( index )
		If seqs[index]=GraphicsSeq Return frames[index]
		frames[index]=_max2dDriver.CreateFrameFromPixmap( Lock(index,True,False),flags )
		If frames[index] seqs[index]=GraphicsSeq Else seqs[index]=0
		Return frames[index]
	End Method
	
	Method Lock:TPixmap( index,read,write )
		If write
			seqs[index]=0
			frames[index]=Null
		EndIf
		If Not pixmaps[index]
			pixmaps[index]=CreatePixmap( width,height,PF_RGBA8888 )
		EndIf
		Return pixmaps[index]
	End Method
	
	Method SetPixmap( index,pixmap:TPixmap )
		If (flags & MASKEDIMAGE) And AlphaBitsPerPixel[pixmap.format]=0
			pixmap=MaskPixmap( pixmap,mask_r,mask_g,mask_b )
		EndIf
		pixmaps[index]=pixmap
		seqs[index]=0
		frames[index]=Null
	End Method
	
	Function Create:TImage( width,height,frames,flags,mr,mg,mb )
		Local t:TImage=New TImage
		t.width=width
		t.height=height
		t.flags=flags
		t.mask_r=mr
		t.mask_g=mg
		t.mask_b=mb
		t.pixmaps=New TPixmap[frames]
		t.frames=New TImageFrame[frames]
		t.seqs=New Int[frames]
		Return t
	End Function
	
	Function Load:TImage( url:Object,flags,mr,mg,mb )
		Local pixmap:TPixmap=TPixmap(url)
		If Not pixmap pixmap=LoadPixmap(url)
		If Not pixmap Return
		Local t:TImage=Create( pixmap.width,pixmap.height,1,flags,mr,mg,mb )
		t.SetPixmap 0,pixmap
		Return t
	End Function

	Function LoadAnim:TImage( url:Object,cell_width,cell_height,first,count,flags,mr,mg,mb )
		Local pixmap:TPixmap=TPixmap(url)
		If Not pixmap pixmap=LoadPixmap(url)
		If Not pixmap Return

		Local x_cells=pixmap.width/cell_width
		Local y_cells=pixmap.height/cell_height
		If first+count>x_cells*y_cells Return
		
		Local t:TImage=Create( cell_width,cell_height,count,flags,mr,mg,mb )

		For Local cell=first To first+count-1
			Local x=cell Mod x_cells * cell_width
			Local y=cell / x_cells * cell_height
			Local window:TPixmap=pixmap.Window( x,y,cell_width,cell_height )
			t.SetPixmap cell-first,window.Copy()
		Next
		Return t
	End Function
	
End Type

Type TImageDataType Extends TDataType

	Method Tag$()
		Return "brl.max2d.TImage"
	End Method
	
	Method ReadObject:Object( stream:TStream )
		Local image:TImage=New TImage
		image.width=stream.ReadInt()
		image.height=stream.ReadInt()
		image.flags=stream.ReadInt()
		image.mask_r=stream.ReadInt()
		image.mask_g=stream.ReadInt()
		image.mask_b=stream.ReadInt()
		image.handle_x=stream.ReadFloat()
		image.handle_y=stream.ReadFloat()
		Local count=stream.ReadInt()
		image.pixmaps=New TPixmap[count]
		For Local i=0 Until count
			image.pixmaps[i]=TPixmap( stream.ReadObject() )
		Next
		image.frames=New TImageFrame[count]
		image.seqs=New Int[count]
	End Method

	Method WriteObject( o:Object,stream:TStream )
		Local image:TImage=TImage(o)
		stream.WriteInt image.width
		stream.WriteInt image.height
		stream.WriteInt image.flags
		stream.WriteInt image.mask_r
		stream.WriteInt image.mask_g
		stream.WriteInt image.mask_b
		stream.WriteFloat image.handle_x
		stream.WriteFloat image.handle_y

		stream.WriteInt image.pixmaps.length
		For Local pixmap:TPixmap=EachIn image.pixmaps
			stream.WriteObject pixmap
		Next

	End Method
End Type

Global ImageDataType:TImageDataType=New TImageDataType


As you can see, one of the field vars is 'pixmaps', a TPixmap Array. You'll need to get these manually. Ahem, Mark, ahem, Simon, there needs to be a BRL function for returning that. Or maybe not. Your program will look something like this now:

Local myanimation:TImage=LoadAnimImage(myurl:Object,width,height,first,count,flags=-1)
Local pixmaps:TPixmap[]=myanimation.pixmaps


There! Pixmaps loaded! All you have to do now, is do whatever you want with the resulting pixmaps. You can additionally turn each one in to a TImage for all the special rendering features that are provided with it. Like this:

Local myanimation:TImage=LoadAnimImage(myurl:Object,width,height,first,count,flags=-1)
Local pixmaps:TPixmap[]=myanimation.pixmaps
Local images:TImage[pixmaps.length]

For j=0 to pixmaps.length-1
	images[j]=LoadImage(pixmaps[j])
Next


Hope this helps! Now, as for loading them specifically with OpenGL, I dunno, but I know that OpenGL is much easier to understand than DirectX, so keep trying. You'll get it. ;)

Thank yo very much. I will try to impliment this code when i get a chance...
Thanks again. :)