Rendering Sprites by order of screen position.

I'm currently having a great time creating a 2D overhead Zelda esq game using C++ and Allegro 5.

However my game is more of a Beyond Oasis perspective then a Zelda Perspective and objects need to appear behind other objects when they have a higher y position and above when they have a lower y position. I have gotten it to work with one object on the other. I created and if/else depending on the y coordinate of the one object.

How would I go about creating this function using every moving object in the game. How can I define the two y variables within the class to create a correct draw order?

Right now I have all characters except the user character inherit all x,y coordinates from a base class. What do I need to do so that I can have two characters who inherit the same x,y to interact?

I got the suggestion to using an SLT container and algorithm std::sort function. I understand how to sort the values using these tools but how do I then sort the functions themselves.

For example:

I have these functions to draw:

1
2
3
4
5
6
7
8
9
bush[1].DrawBush(bushImage);
				bush[2].DrawBush(bushImage);
				rock[1].DrawRock(rockImage);
				aBear[1].DrawsBear(bearImage2);
				pigs[1].DrawPig(pigImage);
				pigs[2].DrawPig(pigImage);
				gremlin[1].DrawGremlins(gremlinImage);
				toad[1].DrawToad(toadImage);
				reginald->DrawVanJohnson(reginaldImage);


I created a variable to define where I would want them to switch order. int ysort. However, how can I connect the entire function whatever.drawwhater(); with this variable and sort them at the same time.
Hmm perhaps you should sort objects ( as in class ) instead of functions and use inheritance
so that you can have this universal array of objects and
sort them by y
Just a bit more detail on @rmxhaha's response:

You said you have a base class, which is used to inherit all X and Y coordinates. What you could do, is assuming you have them all inherit from it, you could have a container (maybe a std::vector) storing pointers to the objects, and call their draw function from that, sorting on Y value. Here is an example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <vector>
#include <memory>

class Object {
    public:
        Object(int y, Image img) // assuming 'Image' is an image type
            : _y(y), _img(img) {}

        virtual ~Object();
        
        void draw() {
            // generic drawing code here, make this virtual if overriding is required
        }

        int getYPos() const { return this->_y; }

    private: // only Object needs these: The objects themselves don't care :)
        int _y;
        Image _img;
};

class Bush : public Object { 
    public:
        Bush (int y, Image img) : Object(y, img) {}
        /* ... */ 
};
class Rock : public Object { /* ... */ };
class Pigs : pubic Object { /* ... */ };

// ...

std::vector<std::unique_ptr<Object>> objects;

bool compareObjs(const std::unique_ptr<Object>& lhs, 
                 const std::unique_ptr<Object>& rhs) {
    return lhs->getYPos() < rhs->getYPos();
}

// ...
// adding objects

objects.emplace_back(new Bush(0, bushImage));
objects.emplace_back(new Rock(1, rockImage));
objects.emplace_back(new Pigs(5, pigImage));
std::sort(objects.begin(), objects.end(), compareObjs);

// drawing objects

for (const auto& obj : objects)
    obj->draw();


Hope this gives you an idea. Of course, the player class should subclass the Object class too. However, this method suffers from type erasure, which can cause some problems. I don't have time to show you an alternative at the moment, but there is a very good article on it here: http://www.cplusplus.com/articles/oz18T05o/
Topic archived. No new replies allowed.