SFML community forums
Help => Graphics => Topic started by: bullno1 on December 10, 2008, 01:49:33 pm
-
Currently, I'm storing my sprites in a std::list then I sort them every frame according to their "z" values through std::list::sort so that I can draw them in the correct order.
To speed it up, I have different lists of sprite called layers. Their their rendering flags decides whether they are sorted every frame(moving object) or sorted only once(background objects)
However, I still want to know if there is a better way of doing this? Does OpenGL have any function to exploit GPU power to do sorting?
-
Well, I would do that :
if sprite added or their z has changed, set 'sort' to true (a bool). Before drawing, test 'sort' to know if you need to sort it or not.
-
Does OpenGL have any function to exploit GPU power to do sorting?
Yes, it certainly does. All of your 2d sprites are actually drawn as 3d squares in 3d space. If SFML only passes a Z value (and sets some other states), then the graphics card will happily do Z ordering.
I do wish that SFML would add Z values as an option. Something like if two Z values are 0 or not set then it goes by drawing order, otherwise it passes the Z values to OpenGL for the graphics card to take care of.
Remerber Manually setting Z-order (http://www.sfml-dev.org/forum/viewtopic.php?t=760&start=0&postdays=0&postorder=asc&highlight=) in feature requests from a while back?
-
To answer you actual question better, here's an idea to consider.
Make a class that inherits from sf::Drawable and adds Z value functions (e.g. GetZ() and SetZ()). Every frame create an empty std::set. Each frame, when you want to render an object, just insert it's pointer into this set (you'll need to define a less than operator for the pointers based on the Z value). The set should fairly efficiently sort each object (log n time, I believe). At the end of the frame iterate through the set and render each object for real.
-
SFML can't rely on Z-buffer being available because it might be unsupported by the graphics card, or even disabled explicitely by the user when creating the SFML window.