Image order.

BlitzMax Forums/BlitzMax Programming/Image order.

Trader3564(Posted 2008) [#1]
Ok, this mebe more complicatd then i thoughd.
I used to use a software that allowed to me change the Z-Index of a 2D image, im talking about layering.
Now, in blitzmax that is the order of my coding really, if i draw one first, and nother, the last is on top of it. Basicly, i start my code, thats all in the background, when i end my code, that is in the foreground. Now, how do i change the order? Imagine i players that i want to sort on Y axis, then draw, etc...


Brucey(Posted 2008) [#2]
how do i change the order?

You'll need to draw them in the order you want them to appear in... first at the back, last at the front.

Perhaps you'll want to keep a list/array of the z-order level of each object, and as they move about, rearrange the list.

Then draw the objects by processing the list in order, where the first drawn object is farthest away.


Trader3564(Posted 2008) [#3]
Could i use a TList? does that take different objects? is it sortable? like, how do i sort it?


Perturbatio(Posted 2008) [#4]
A TList is sortable (with TList.sort), but you will need to implement your own compare function if you want to sort your custom objects properly


degac(Posted 2008) [#5]
A simple example
Graphics 640 , 480


Type tsprite
	Global sprite_list:TList
	Field image:timage
	Field x:Float
	Field y:Float
	Field _color:Int[]=[255,255,255]
	Field link:TLink
	
	
	
	Function Add:tsprite(_x:Float , _y:Float,r:Int=255,g:Int=255,b:Int=255) 
		Local ll:tsprite = New tsprite
		ll.x = _x
		ll.y = _y
		ll._color = [r , g , b]
		
		If sprite_list = Null sprite_list = New TList
		ll.link = Sprite_list.addlast(ll)	
		Return ll

	End Function
	
	Function Update()

		For Local as:tsprite = EachIn sprite_list
			as.draw() 
		Next
	End Function
	
	Method Draw()
		SetColor _color[0],_color[1],_color[2]
		DrawRect x , y , 32 , 32
	End Method
	
	Method Compare( other:Object )
		Local s:tsprite = tsprite(other)
		If s
			Return y - s.y
		Else
			Return - 1
		End If
	End Method

End Type


	tsprite.add(100 , 100) 
	tsprite.add(110 , 110) 
	tsprite.add(120 , 120,200,220,20) 
Local mm:tsprite = tsprite.add(130 , 130,255,0,0) 
Local ox:Int , oy:Int
Local x:Int , y:Int

While Not AppTerminate() 
	Cls
	tsprite.update()
	
	x = MouseX() 
	y = MouseY() 
	
	If x <> ox And y <> oy
		tsprite.sprite_list.sort() 
	End If
	
	mm.x = x
	mm.y = y
	
	Flip
	PollSystem
Wend



Trader3564(Posted 2008) [#6]
I see how this works. Thanks.


Czar Flavius(Posted 2008) [#7]
What do ll and as stand for?


ImaginaryHuman(Posted 2008) [#8]
All you basically need to do is decouple/de-attach your layer order from your program's sourcecode order. Make like a list of `layers` which you can sort into whatever order you want and then each layer has an image associated with it. Then you go through the list and draw the images in the order of the list.