views:

113

answers:

3
+2  A: 

The fact that you are storing the different objects as members of the class shouldn't prevent you from storing pointers to them in the IRenderable* vector.

struct GameState {
  Ball ball;
  Bonus bonus;
  vector<IRenderable*> m_items;

  GameState() // constructor
  {
    m_items.push_back(&ball);
    m_items.push_back(&bonus);
  }

};

This way you also don't have to worry about freeing the pointers in m_items. They will be automatically destroyed when the GameState is destroyed.

Also, your syntax of foreach is funky.

shoosh
Looks good, thanks. Foreach in the way similiar to this can be done for example using BOOST_FOREACH.
Kotti
+2  A: 

You should really have separate subsystems in your game engine. One for rendering and another one for game logic. Your graphics subsystem should have a list of pointers to IRenderable objects. Your game logic subsystem should have its own list of objects with another interface.

Now you can easily have entities that have state but cannot be rendered and so on. You might even have a separate subsystem for audio. This should also help keep your code clean and modular.

bitc
Maybe, but I think this can generally bring a lot of headache, especially IRenderable* pointer synchronization after adding / deleting / ... of game objects and so on. I could be wrong about it, probably should give it a try.
Kotti
Use RAII (http://en.wikipedia.org/wiki/RAII). Register object in the constructor and remove it the destructor. This will ensure that the object will register and deregister correctly.
bitc
btw, visible IRenderable objects will update each time the screen refreshes. Game logic should be independent of frames per second and have its own refresh rate. This is another reason to separate these responsibilities.
bitc
+1  A: 

I'm not a c++ programmer but hopefully faux-code will help...

Class Renderable has the function Render

Class Movable inherits from Renderable and also has properties such as x, y, z and functions like Move

Class Ball inherits from Movable

Now your game state can have a list of Renderable objects and, when the time comes to Render, it can just loop through them.

Other actions, like a tick of the world clock, might loop through looking for objects which are instances of Moveable and tell them to update.

If your Bonus is renderable (I couldn't tell for sure from your question) then it could subclass Collectable which subclasses Moveable.

Zarigani
Does somebody know some effective C++ way to implement the second loop through these Renderable items which results in finding only Movable objects' subset? Either I'm plain stupid or this can be done only by invoking dynamic casting / typeid / your own RTTI implementation... Thanks for answer :)
Kotti
@Kotti just create another list of them. E.g. you have a list of Renderables, a list of Movables, a list of Collectibles. Then any time you want to operate on any subset, just traverse the appropriate list. (Again- add the Renderables to the renderable list in the Renderable constructor.)
dash-tom-bang