Sprite Z ordering...

BlitzMax Forums/BlitzMax Programming/Sprite Z ordering...

orgos(Posted 2005) [#1]
Hi people, i want to perform some Z ordering to sprites, but i no have a cleared idea.

I create a TList of graphic, first I create a Base objet an when the object has created are added to the sprite LIST, the object have the Z field.

I want some way to organize by Z field the TLIST objects in the DrawProcess function to Draw the sprites in right way.

thanks.


Robert(Posted 2005) [#2]
You could create a custom type which has two fields, one for the Z depth and one for the sprite object. Create a Compare() method so that the type can be sorted in a TList.

Type ZSortable
	Field z:Float
	Field o:Object 'Replace this with your sprite

	Method Compare(other:Object)
		If ZSortable(other).z < z Return -1
		Return 1
	End Method
End Type


Alternatively, you could just add a Compare method to your Sprite type which sorts according to z-depth.


orgos(Posted 2005) [#3]
I don't understand you very well.

I have the object module, x, y, x fileds and the image of sprite, then I want to create some sprites based on the objet, some sprite have diferent Z coor, and some sprite can be created on the fly with diferents Z. All the sprite object defined are in one list SpriteList, then how i can order the elements of the list based on one field, in other word how i can move one elemt of the list to top of the list or to number 10 or 12 on the list?? to draw the sprites in the order that apear on the list.

Sorry about mi english.


Robert(Posted 2005) [#4]
Here is a more complete example, I hope this explains better:

Type Sprite
  Field x:Float
  Field y:Float
  Field z:Float

  Field green
  Field red
  Field blue

  Method Compare(other:Object)
    If Sprite(other).z < z Return -1
    Return 1
  End Method
End Type

Graphics 800,600,0

Local sprites:Sprite[3]

sprites[0]=New Sprite
sprites[0].green=255
sprites[0].red=0
sprites[0].blue=0
sprites[0].x=50
sprites[0].y=50
sprites[0].z=5

sprites[1]=New Sprite
sprites[1].green=0
sprites[1].red=255
sprites[1].blue=0
sprites[1].x=150
sprites[1].y=150
sprites[1].z=20

sprites[2]=New Sprite
sprites[2].green=0
sprites[2].red=0
sprites[2].blue=255
sprites[2].x=250
sprites[2].y=250
sprites[2].z=10


Local spriteList:TList=New TList

ListAddLast spriteList,sprites[0]
ListAddLast spriteList,sprites[1]
ListAddLast spriteList,sprites[2]



While Not KeyHit(KEY_ESCAPE)

	SortList spriteList	
	For Local s:Sprite=EachIn spriteList
		SetColor s.red,s.green,s.blue
		DrawRect s.x,s.y,200,200
	Next
	
	Flip
	Cls
Wend


- Change the z field for the sprite objects to alter the drawing order.
- You must call SortList on the list whenever you modify the list (add or remove items) or if you modify the z-coordinates of any of the sprites
- I have used a coloured rectangle above, you just need to replace the red,green,blue fields with a field for your TImage object.


REDi(Posted 2005) [#5]
Add a compare method to your type like Robert did above, then when you call SpriteList.Sort() (or SortList()) max will sort the list for you using the compare method you supply.

*EDIT* to slow again :)


orgos(Posted 2005) [#6]
Ok i understand some thing.

And i can see that the Compare method apear called by the SortList function?? or it is wrong? it is documented??

It is an internal behavior or what?


Robert(Posted 2005) [#7]
And i can see that the Compare method apear called by the SortList function?? or it is wrong? it is documented??



Yes, the Compare() method is documented on the page about Objects in BlitzMAX (in the Language section)


orgos(Posted 2005) [#8]
Thanks for the help.