Suggestions for image layering?

BlitzMax Forums/BlitzMax Beginners Area/Suggestions for image layering?

blackwater(Posted 2010) [#1]
Hi everyone, I did some forum searches but didn't find anything on the topic. When drawing an image and using the flip command (without clearing) the new image gets drawn on top of the old image, if there is one present.

I'm building a game editor and made a fair amount of progress the last few days. I was wondering if there was a method for controlling on what layer the image is displayed? For example, image C is on top of image B and image B is on top of image A. what if I now wanted image a to be on top of B and C?

One thought I had was to simply redraw the entire screen and code a method that controls in what order the images get drawn according to whatever "layer" I assigned to them. Is that the way to go or is there a better method out there?


Jesse(Posted 2010) [#2]
Blitzmax uses only two layers; The back layer which is where everything is draw into and the front layer which is used to display. the layers are swapped when flipped and that is why you have to use cls to clear the images on the back buffer before drawing to the layer again. if you want to stack images in alternate sequence. then you are going to have to create some type of functionality to take care of that. some programmers use a technic called z sorting. it's a technic that sorts the images by distance for display before drawing to the buffer. I assume you are using procedural programming for your editor. that would make it a bit more hard to modify than if you were using Object programming since for this to work you would have to do all of your updates and calculations for your images, save each position and order into memory and finally display everything in the order desired.

if you have many layers in your editor I suggest drawing each layer to the screen starting from the bottom layer sequentially all the way to the top layer. if you need to change the order than you are going to have to program some logic to take care of that.

I assume quite a bit as I don't know how much you really know about programming but based on your question's I concluded that you don't know much. If it's not the case forgive me, I tend to do that a lot. Either way, the logic works the same way.

Last edited 2010


Yan(Posted 2010) [#3]
Here's some *very* old and crusty code. It'll give you an idea of one way to go...

http://blitzbasic.com/Community/posts.php?topic=56615#630162


ImaginaryHuman(Posted 2010) [#4]
You can't rely on the backbuffer containing the frame you previously flipped. It depends entirely on the graphics driver as to whether it clears the frame for you, whether you have to clear it yourself, whether it might contain garbled scrambled data, and whether it might implement multiple framebuffers and cycle through them. On my iMac for example it does not retain the contents of the previous frame after a flip. If your system does this it's a side-effect and a coincidence you can't depend on.


blackwater(Posted 2010) [#5]
Thanks guys, I'm going to need "layering" at some point, I just wanted to know if there was something already in the books to handle that or if I would have to invent my own.

I suspect the route I'm going to take is write some code to handle in what order the images are drawn to the screen, with layer 1 images being drawn last (to be on top of everything else).


Kryzon(Posted 2010) [#6]
Why don't you make every layer one big image itself? say if you want something to be on layer 2, you output whatever you want onto the layer 2 image.

At render time, each layer is rendered in the correct order:
DrawImage Layer1,x,y
DrawImage Layer2,x,y
DrawImage Layer3,x,y
DrawImage Layer4,x,y

[...]



xlsior(Posted 2010) [#7]
Keep in mind: you really, really should redraw everything each frame.

After a flip, the contents of the backbuffer are undetermined. The OpenGL spec tells you not to count on any contents surviving.
In DirectX, different cards use a different number of backbuffers, which means that after a flip the contents of the buffer may not be identical to what it was before the flip, but all of a sudden 'rewind' one, two, or even three frames since it may cycle through up to 4 different backbuffers instead of using just one.
This can lead to really annoying flickering on some computers.

The only way to ensure that everything looks the way you want it to, is to redraw the entire screen after each flip.

Since you're redrawing everything anyway, it should be fairly straightforward to assign your own 'layer' to images, and organize your image drawing function to draw these layers in order of back to front.


TomToad(Posted 2010) [#8]
One method would be to use separate TLists for each layer. All layer 1 objects would go into the layer 1 TList, layer 2 objects in the layer 2 TList, etc... Then all you need to do is draw the contents of each list in order. If an object changes layers, then all you need to do is remove it from one list and add it to the other.