Jump to content
  • Advertisement
Sign in to follow this  
cptrnet

Scene Manager Question

This topic is 4830 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I am using c++. I want to make a SceneManager class to make it easier for me to manage all my scene nodes. The first thing I need to have is a storage container of all the nodes. I was thinking vector or a list. I just need to iterate through the nodes and render them. Vector doesn't have a sort method, but how would I sort from a list? I don't know how to say what to draw first so I was thinking I could have a z-index for a node and it would sort based on that. Would I have to implement my own sort method? Could I also inherit from either of these and add my own methods for rendering? If so how would I iterate the storage container. this.begin() to this.end()?

Share this post


Link to post
Share on other sites
Advertisement
You could use a priority queue which would do the sorting for you like so:


#include <queue>
#include <deque>
#include <iostream>

class Node
{
public:
Node(double v):zValue(v){}
double getZValue() const { return zValue; }
private:
double zValue;
};

bool sortNode( Node*& l, Node*& r )
{
return l->getZValue() < r->getZValue();
}

typedef bool (*function)(Node*&,Node*&);

int main()
{
// declaring the queue to use pointers and a deque to hold the stuff and to use a functionpointer for sorting
// p is the queue and sortNode it sort function
std::priority_queue<Node*, std::deque<Node*>, function> p(sortNode);

// adding Node to test stuff
p.push(new Node(1));
p.push(new Node(3));
p.push(new Node(2));
p.push(new Node(5));
p.push(new Node(4));

// go through the complete queue
while(p.size() > 0)
{
// get the top node from the queue (in this case the one with the highest value)
Node* pNode = p.top();
// remove the top
p.pop();
// print out what we have
std::cout << "ZValue: " << pNode->getZValue() << std::endl;
// clean it up since it's not used anymore
delete pNode;
}

return 0;
}





output:
ZValue: 5
ZValue: 4
ZValue: 3
ZValue: 2
ZValue: 1

EDIT2: fixed some typos and added comments

[Edited by - dragongame on August 23, 2005 1:40:22 PM]

Share this post


Link to post
Share on other sites
Don't worry about sorting until you can figure out why you might need to sort and what criteria you would use. Use std::sort to sort a vector, use list::sort to sort a list. Deriving classes from STL containers is generally not recommended.

Share this post


Link to post
Share on other sites
What I need to sort is the z-index so I can render things that are supposed to be in the background etc. I wanted a class that I didn't have to rewrite, like vector or list, I don't want it to be just one type either, like a scene node. Maybe I could write just a base Manager class that SceneManager inherits from. Why isn't it not recommended to derive a stl class, don't people do that with std::string?

That queue thing is to crazy for me. I had to read it like 5 times before I barely understood what was happening.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Deriving from standard container isn't a good idea because they don't have a virtual destructor or other virtual methods for that matter. That means no runtime polymorphism behaviour is possible. People do it just because too lazy to write the composite properly, which is a wrong reason to derive in the first place.


// ville



Share this post


Link to post
Share on other sites
to begin with, if you are doing lots of inserts/removals then use a std::list. If your scene data is (mostly) static then use a std::vector. Seeing as I have loads of objects being added/deleted frequently I use a std::list.

When sorting, be sure to sort the pointers and not the objects or you will waste cpu cycles copying data back and forth.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!