There are some details missing but the spirit of the solution is the same.
Code: Select all
// the base class for all graphics in my game
class Graphic
{
public:
// use this for sorting, and testing if graphic list is dirty
// during update pass
virtual float zOrder( void ) const =0;
// accessors for position, rotation, etc.
private:
friend class Graphic_manager;
// called every frame. if returns false remove
// from graphic list, delete it.
virtual bool update( float dt ) =0;
// called each frame
virtual void render( /*graphic context if needed*/ ) =0;
};
class Sprite : public Graphic
{
// stores reference to texture, as well as quad coords.
};
class GLPrimative : public Graphic
{
public:
const unsigned* world_generation;
const unsigned my_world_generation;
GLenum gl_begin_param;
std::vector<b2Vec2> vertices;
std::vector<b2Vec2> texture_coords;
Color c;
glTexture_id texture;
private:
void render( )
{
// do open gl stuff
// optionally use textures if texture id is not 0.
}
bool update( float dt )
{
// besure you don't have an off by one error here.
return my_world_generation == *world_generation;
}
};
I have a graphic manager that essentially manages a list of graphic pointers.
It loops over the graphic list calling update on each, testing if a resort
is needed, and removing expired graphic objects. If the resort is needed
it performs a sort. And finally it again loops over the graphic list calling
render on each
I increment an integer in my b2World wrapper everytime I call step. The b2Debugdraw
callback passes the address of this integer into the GlPrimative when it
is allocated. I call this integer world generation. The Glprimative remembers
what generation it was created, so it can stop rendering when a new set of
debug primatives are rendered.
I implement this with shared pointers to help with graphic lifetime management.
You can see all the gory details at
http://ammo.googlecode.com-nick