Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Custom draw order (depth)  (Read 3122 times)

0 Members and 1 Guest are viewing this topic.

Gleade

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
Custom draw order (depth)
« on: November 01, 2014, 05:54:23 pm »
Hello, I am quite new to C++ and SFML. I have made a few small games from SFML so far and am really enjoying it. Right now I am trying to create an RPG game, then I realised that unlike my other simpler games I've made previously, this game requires a draw order. I was a little bit stumped on how to do this at first, I tried searching on the forums but couldn't quite get a good understanding.

I have been trying to figure out the best & easiest way to draw sprites in a specific order related to their depth. At the moment the class I have made seems to work so far (see code below) , even though I still need to add a few more features. Am I on the right track for this? Sorry if this is in the wrong section of the forums. Thanks in advance.

Here is what I have come up with so far:


void SceneGraph::addToList(Entity entity, int depth)
{
    //drawList.emplace(depth, entity);
    drawList.insert ( std::pair<int,Entity>(depth,entity) );
}

void SceneGraph::updateList(sf::Time &deltaTime)
{
    // Put our current draw list into a new container
    tempDrawList.insert(drawList.begin(), drawList.end());
    //tempDrawList = drawList;
    // Clear our current draw list
    drawList.clear();
    //
    for(std::multimap<int, Entity>::iterator it=tempDrawList.begin(); it!=tempDrawList.end(); it++)
    {
        // Add our new updated depth to the list
        addToList((*it).second, (*it).second.getDepth());
    }
    // Clear the temp list now we're done with it
    tempDrawList.clear();

}


void SceneGraph::draw(sf::RenderWindow& target)
{
    for(std::multimap<int, Entity>::iterator it=drawList.begin(); it!=drawList.end(); it++)
    {
        (*it).second.draw(target);
    }

}

 

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6278
  • Thor Developer
    • View Profile
    • Bromeon
Re: Custom draw order (depth)
« Reply #1 on: November 01, 2014, 06:09:34 pm »
Simply sort the entities according to their Z value. std::multimap is one possibility, but if you're not using the Z order as an index, I'd probably work with std::vector and std::sort() instead. I don't see the point of having both drawList and tempDrawList.

By the way, check out the following C++ features, they will simplify your code a lot:
  • std::make_pair
  • auto
  • Range-based for loop
And are you sure you want to copy the Entity objects all the time?
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Gleade

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
Re: Custom draw order (depth)
« Reply #2 on: November 01, 2014, 06:13:21 pm »
Simply sort the entities according to their Z value. std::multimap is one possibility, but if you're not using the Z order as an index, I'd probably work with std::vector and std::sort() instead.

By the way, check out the following C++ features, they will simplify your code a lot:
  • std::make_pair
  • auto
  • Range-based for loop
And are you sure you want to copy the Entity objects all the time?

Thank you for your answer, I will have a look at std::vector & std::sort(). I will also have a look at those features.
Currently one of the problems I'm having with this class is the copying of the Entity object all the time, it's giving me trouble when I'm trying to update it's velocity.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6278
  • Thor Developer
    • View Profile
    • Bromeon
Re: Custom draw order (depth)
« Reply #3 on: November 01, 2014, 06:15:05 pm »
Why don't you have only one container to store the entities and keep it sorted all the time?

By the way, please avoid full quotes if the original post is just above, it tends to make threads unreadable ;)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Gleade

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
Re: Custom draw order (depth)
« Reply #4 on: November 01, 2014, 06:41:05 pm »
I just moved it over to a vector with std::sort and it is working like a charm! it even fixed a lot of issues I was having. Thanks a lot for your input! :)