2D Drawing Problem...

hey all, I am currently working on a 2 RTS and have come across a small problem, when i run the game it is drawing the objects in the order they were created, and this is all fine till they start overlapping and the objects that are higher (drawn higher up on the screeen ) are being drawn over the objects below them ( which isnt very pretty ), now is there any way in OpenGL to fix this or am i going to have to sort the objects according to their y positions and draw it like that?

OpenGL's depth testing is very fast on any card newer than the Rage 128. You should sort front-to-back and enable depth testing, because it will be orders of magnitude faster than sorting back-to-front and drawing without depth test.

Well, sorting front to back takes just as long as sorting back to front. However, I believe what his real problem is that he's using blending to mask 2D objects. In this case, he MUST sort from back to front to draw things correctly. Unless you have like a million little 2D objects on your screen or huge 512x512 sprites moving around, don't worry about drawing speed with a 2D engine using OpenGL until you actually notice a speed issue.

Sorry Justin, but I agree with JeroMiya. Thats a *good* way of doing things. Get it working first. Profile second. Optimize third.

Maybe you could optimize a section of code - but it turns out its fast enough unoptimized. Or after profiling you realize the cpu spends even *more* time in a different section of code - and its best to spend your energies optimizing that first. And finally - I feel often programmers get bogged down in optimizing some algorithm - but run out of gas before they finish their project (I know Ive done that many times).

Im working on a qbert clone, and encountered almost exactly the same problem. What I did was add a z-order data member to my sprite class. When the sprite is created, or I need to, I modified the sprite z-order value. Then, in order to organize drawing of sprites, I add them to a std::vector in my sprite engine (which renders them in order). After my sprites are added to the engine, I use std::stable_sort() on my vector of sprites, using a custom function that returns the lesser z-value of two sprites. In this way, sprites are drawn as intended; lower z-order first.