• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Nanook

Getting to the data of the derrived class

10 posts in this topic

I have this issue that I never find a good solution for that I'm happy with.

Eg. for global states in my scenegraph I have a base GlobalState class that I derrive different states from. Then I store an array with GlobalState pointers in the nodes. Now since the derrived classes stores data I need a way to get to this data.

The different ways I know of is:
- Use cast. Either dynamic_cast (I never use this) or have a virtual function in base class that returns the type of the GlobalState and use static_cast based on this. I like working with this method as I get the concrete class, but since casts should be avoided I want to find another way of doing it.
- Have functions in the interface to get all the different data. This gets messy with lots of derrived classes and the base class needs to be altered for each added derrived
- Use the visitor pattern to get to the data. I like this as it is efficient and it uses clean features of c++, but I find it kind of messy too have lots of visitors.
- have some sort of structure with unions and a set of different types of data. This is ok, but If a derrived class wants to store a type that is not in the union this wont work.

Another case where I come into the same issue is with events. I have A base Event class and when I'm handeling the events I need to get to the data stored in the derrived event.. atm I'm solving this with the static cast method where I got some template magic and casts the event to the concrete type before I call the handle function.. void OnDerrivedEvent(DerrivedEvent& event);

So how do you solve these issues? Sugestions for other ways than the one I've listed? Should I design things differently? How?
0

Share this post


Link to post
Share on other sites
A little vague on exactly what you mean, but could you add virtual functions that act on the internal data as necessary? Or you could make these 'data' types abstract and give [i]them [/i]virtual functions.

Hard to give any more advice without seeing something that resembles your actual code.
0

Share this post


Link to post
Share on other sites
Okay, here's a stripped down version of the GlobalState example.

[CODE]
class GlobalState
{
enum
{
STATE_MATERIAL,
STATE_COUNT
};
}
class MaterialState : public GlobalState
{
public:
ColorRGBA& GetSomeColor()
{
return m_someColor;
}
private:
ColorRGBA m_someColor;
};
class SceneGraphNode
{
GlobalState* GetGlobalState(int i)
{
return states[i];
}
private:
GlobalState* states[STATE_COUNT];
};
void Draw(SceneGraphNode& node)
{
GlobalState* materialStateRef = node.GetGlobalState(STATE_MATERIAL);
//Need to use one of the methods described to get to the color data of the material.
}
[/CODE]
0

Share this post


Link to post
Okay, heres a stripped down version of the GlobalState Example..

[CODE]
class GlobalState
{
enum
{
STATE_MATERIAL,
STATE_COUNT
};
}
class MaterialState : public GlobalState
{
public:
ColorRGBA& GetSomeColor()
{
return m_someColor;
}
private:
ColorRGBA m_someColor;
};
class SceneGraphNode
{
GlobalState* GetGlobalState(int i)
{
return states[i];
}
private:
GlobalState* states[STATE_COUNT];
};
void Draw(SceneGraphNode& node)
{
GlobalState* materialStateRef = node.GetGlobalState(STATE_MATERIAL);
//Need to use one of the methods described to get to the color data of the material.
}
[/CODE]
0

Share this post


Link to post
Share on other sites
Why doesn't SceneGraphNode just have a MaterialState member variable instead of the weird state array?
0

Share this post


Link to post
Share on other sites
Yeah I thought about that too.. but when adding new states it has to be added to the scenegraphnode.. also I have a function for propagating a state change down the scenegraph and with the array its easy to update all the child nodes.. with storing members for each of the states in the nodes I would have to add code to the propagation function for each new state I add to the system.. The idea is from David Eberlys 3d game engine architecture book..
0

Share this post


Link to post
Share on other sites
What does your propagation function look like? It's entirely possible that either templating or use a pointer to member argument can allow you to not have to special case every member variable. Alternately, give your SceneGraphNode a get MaterialState member function rather than needing to use the index to access it. The the getter does the cast and you can pretend it is a member variable for everything but the propagation function.
0

Share this post


Link to post
Share on other sites
Spatial is the base. Node and Renderable is derrived from Spatial.
UpdateRenderStateLocal is pure virtual.. UpdateRenderState is the entry point..
It starts by propagating the state from the root of the tree (if propagation was started from one of the child nodes), then using the passed stacks to push and pop states..

[CODE]
void TE::SceneGraph::Spatial::UpdateRenderState( std::stack< boost::shared_ptr< GlobalState > >* stateStack, std::vector< boost::shared_ptr< Light > >* lightStack )
{
bool initiator = stateStack == NULL;

if(initiator)
{
stateStack = new std::stack< boost::shared_ptr<GlobalState > >[GlobalState::STATE_COUNT];
for (unsigned i = 0; i < GlobalState::STATE_COUNT ; i++)
{
stateStack[i].push(GlobalState::s_default[i]);
}
lightStack = new std::vector< boost::shared_ptr<Light > >;

PropagateStateFromRoot(stateStack, lightStack);
}
else
{
PushState(stateStack, lightStack);
}

UpdateRenderStateLocal(stateStack, lightStack);

if (initiator)
{
delete []stateStack;
delete lightStack;
}
else
{
PopState(stateStack, lightStack);
}
}


void TE::SceneGraph::Spatial::PropagateStateFromRoot( std::stack< boost::shared_ptr< GlobalState > >* stateStack, std::vector< boost::shared_ptr< Light > >* lightStack )
{
if (m_parent) PropagateStateFromRoot(stateStack, lightStack);

PushState(stateStack, lightStack);
}

void TE::SceneGraph::Spatial::PushState( std::stack< boost::shared_ptr< GlobalState > >* stateStack, std::vector< boost::shared_ptr< Light > >* lightStack )
{
std::list<boost::shared_ptr<GlobalState> >::iterator stateItr = m_globalStates.begin();
while (stateItr != m_globalStates.end())
{
int type = (*stateItr)->GetGlobalStateType();
stateStack[type].push(*stateItr);
}

std::list<boost::shared_ptr<Light> >::iterator lightItr = m_lights.begin();
while (lightItr != m_lights.end())
{
lightStack->push_back(*lightItr);
}
}

void TE::SceneGraph::Spatial::PopState( std::stack< boost::shared_ptr< GlobalState > >* stateStack, std::vector< boost::shared_ptr< Light > >* lightStack )
{
std::list<boost::shared_ptr<GlobalState> >::iterator stateItr = m_globalStates.begin();
while (stateItr != m_globalStates.end())
{
int type = (*stateItr)->GetGlobalStateType();
stateStack[type].pop();
}

std::list<boost::shared_ptr<Light> >::iterator lightItr = m_lights.begin();
while (lightItr != m_lights.end())
{
lightStack->pop_back();
}
}

[/CODE]


[CODE]
void TE::SceneGraph::Node::UpdateRenderStateLocal( std::stack< boost::shared_ptr< GlobalState > >* stateStack, std::vector< boost::shared_ptr< Light > >* lightStack )
{
std::vector<Spatial*>::iterator current = m_children.begin();
std::vector<Spatial*>::const_iterator last = m_children.end();
while(current != last)
{
(*current)->UpdateRenderState(stateStack, lightStack);
++current;
}
}
[/CODE]

[CODE]
void TE::SceneGraph::Renderable::UpdateRenderStateLocal( std::stack< boost::shared_ptr< GlobalState > >* stateStack, std::vector< boost::shared_ptr< Light > >* lightStack )
{
for (unsigned i = 0; i < GlobalState::STATE_COUNT ; i++)
{
m_localStates[i] = stateStack[i].top();
assert(m_localStates[i]);
}
m_lights.clear();
m_lights.insert(m_lights.begin(),lightStack->begin(), lightStack->end());
}
[/CODE]
0

Share this post


Link to post
Share on other sites
Is this your actual code? I don't understand why you don't get a stack overflow calling PropagateStateFromRoot() from a node with a parent. I also don't see how Spatial::PushState() ever terminates because none of the iterators are ever incremented. Same deal with Spatial::PopState(), it seems like it should crash because no pointer iteration is going on. In short, I just don't understand what your code is trying to do.
0

Share this post


Link to post
Share on other sites
I'm still working on this so I haven't tested it yet.. Its based on eberly's game engine architecture design book..

I think I'm going to redesign this now anyways..
0

Share this post


Link to post
Share on other sites
Ok, but one comment on it anyways: looking at it some more I think you could simplify it a lot by instead of having an array of all your states, have a single struct that holds all of them. Then you could have a stack of that struct instead an array of stacks, you could just use struct assignment rather than looping, and you could probably keep the type information you're losing by putting them all in an array.
0

Share this post


Link to post
Share on other sites
Ah yes.. good idea.. I've looked at eberly's wildmagic 5 (the book is based on version 3) and he's gone away from this design.. he stores it in a VisualEffect class togheter with the shaders instead now..
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0