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

Author Topic: Can you "send to back" with graphics?  (Read 4276 times)

0 Members and 1 Guest are viewing this topic.

Sivak

  • Jr. Member
  • **
  • Posts: 51
    • View Profile
Can you "send to back" with graphics?
« on: March 27, 2010, 03:52:31 am »
Basically the topic says all.  I wondered if there was any easy way to send any graphic to the back or if I just have to resort to drawing them in the correct order?

Thanks.

Mindiell

  • Hero Member
  • *****
  • Posts: 1261
    • ICQ Messenger - 41484135
    • View Profile
Can you "send to back" with graphics?
« Reply #1 on: March 27, 2010, 08:52:01 am »
You have to order them, they are drawn in the code order.
Mindiell
----

Cyrano

  • Newbie
  • *
  • Posts: 19
    • View Profile
Can you "send to back" with graphics?
« Reply #2 on: April 22, 2010, 07:15:48 pm »
Quote from: "Mindiell"
You have to order them, they are drawn in the code order.


You could make a ZOrder manager for your sprites, but it's a fiasco try to do if you're just starting out. It would probably have to manually assign the order sprites are to be drawn in and...it gets messy, but it is doable.
– A cape? Forsooth! 'Tis a peninsula!

gsaurus

  • Sr. Member
  • ****
  • Posts: 262
    • View Profile
    • Evolution Engine
Can you "send to back" with graphics?
« Reply #3 on: April 22, 2010, 07:42:26 pm »
I suggest a std::multiset in which order is given by your zOrder. When you update a sprite, if it's Z coordinate changes,  remove it from the multiset and add it again (so it becomes ordered). This should be enough.
To draw, just iterate over the multiset and draw them.

If they don't move too much in the z axis, instead of a multiset, a sorted vector should be enough. On that case a change on the zOrder of a sprite implies to look at the zorder of the neighbourhood objects and move them as needed to keep the order.



Edit: I said "sorted map", I wanted to say a std::multiset (replaced on the msg now)
Pluma - Plug-in Management Framework

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Can you "send to back" with graphics?
« Reply #4 on: April 22, 2010, 08:42:57 pm »
If you recreate sprites every frame, std::priority_queue would be an alternative.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

gsaurus

  • Sr. Member
  • ****
  • Posts: 262
    • View Profile
    • Evolution Engine
Can you "send to back" with graphics?
« Reply #5 on: April 22, 2010, 10:10:03 pm »
Ya sorry, I wanted to say std::multiset, objects can have the same z coordinate. I don't program on C++ for a few months and I mistake with other languages :lol: :roll:.

I used a std::multiset months ago on the prototype of my engine.

Here's some code:
Note, here a View is a kind of sprite with an update method and a < operator based on the Z coordinate.
somewhere on my ViewsManager.hpp:
Code: [Select]
       /** Functor for views ordering */
        struct ViewsFunctor {
           bool operator( )(const View* v1, const View* v2) {
              return(*v1 < *v2);
           }
        };

        typedef std::multiset<View*, ViewsFunctor> ViewsMultiset;

         /**
         *  All views here
         * Characters, goodies, boxes, and all other entities
         */
        ViewsMultiset views;

somewhere on the cpp:
Code: [Select]
bool ViewsManager::update(){
    // update all views, and store the list of changed ones
    std::list<ViewsMultiset::iterator> changedOrder;
    ViewsMultiset::iterator it;
    for( it = views.begin(); it!=views.end(); ++it){
        if ((*it)->update()){
            changedOrder.push_back(it);
        }
    }
    // remove and insert the changed views to keep the set ordered
    std::list<ViewsMultiset::iterator>::iterator changeIt;
    for( changeIt = changedOrder.begin(); changeIt!=changedOrder.end(); ++changeIt){
        views.erase(*changeIt);
        views.insert(**changeIt);
    }
    return !changedOrder.empty();
}
Pluma - Plug-in Management Framework

Vit

  • Newbie
  • *
  • Posts: 14
    • View Profile
Can you "send to back" with graphics?
« Reply #6 on: April 30, 2010, 07:08:17 pm »
I made a new Sprite class which inherits from sf::Sprite, but also stores a z-value (along with a few other extra things).
To order them, I used a std::list of sprite pointers and its sort function, calling a comparison function to check the sprites' z values.

I also have a std::map of the sprite pointers, indexed by std::strings, as I like being able to dynamically create sprites and refer to them with text rather than numbers. Each time the sprites are sorted, I clear the list and then re-add each sprite from the map (to ensure the list contains every sprite). I don't think this is very efficient, but I only update the order when a sprite's z is modified or a new sprite is added, and only once each frame, so it's not currently a problem. But if anyone cares to give me a better solution, go ahead.

gsaurus

  • Sr. Member
  • ****
  • Posts: 262
    • View Profile
    • Evolution Engine
Can you "send to back" with graphics?
« Reply #7 on: April 30, 2010, 07:16:14 pm »
If you've read my previous posts you see that I suggest a std::multiset. A multiset is a structure that always keep elements sorted, whenever you add or remove elements.

The basic idea is that when a sprite changes it's Z, remove it from the multiset, and add it again. This is much faster than sorting the whole set.
Pluma - Plug-in Management Framework