A simple (not necessarily flexible or powerful) approach is to define a base game_object
class that defines your interface with game objects, and store those. Your objects inherit from it.:
class game_object
{
public:
virtual ~game_object() {}
virtual void update() = 0;
virtual void draw() = 0;
// get and set position
virtual vector3 position() = 0;
virtual void position(const vector3&) = 0;
// etc...
};
class ball : public game_object
{
public:
void update() { /* update the ball */ }
void draw() { /* draw the ball */ }
// etc
};
// etc
Now you have a common way of using a game object. Next, you can store these in a map (preferably an unordered_map
if you have Boost, TR1, or C++0x), and make that map globally available:
// game_object_manager.h
typedef std::map<std::string, game_object*> game_object_manager;
extern game_object_manager manager;
You define this in a translation unit (either main.cpp
or a game_object_manager.cpp
) somewhere. Lastly, you use it by inserting things by name:
// somewhere, add the ball
manager.insert(std::make_pair("ball", new ball()));
And use it:
game_object* b = manager["ball"];
if (!b) /* there is no ball object */
// use b as ball */
Again, this is a simple solution and isn't necessarily robust, a best practice, or flexible. But for a first game, it'll work fine. (To improve it, you want to look into exception safety, smart pointers, and other things (game books). I recommend you do this before you try to make games.)