Game Structure

Started by
2 comments, last by induster 16 years, 1 month ago
Hello, this may be a big post so please bear with me. I am in progress of creating a small maze type game making use of Direct3D and PhysX using C++. I'm a little concerned about with the way I have structured my game though. Right now I have a class call World. This has functions such as CreateCube, CreateSphere etc. My Graphics class (using Direct3D), has functions such as CreateCube, CreateSphere, LoadShader, LoadTexture etc. Each of which create an id. The id is created via:
 m_entityVector.push_back(newCube);
id = (unsigned short)m_entityVector.size()-1; 
where m_entityVector is defined as a member:
 std::vector<IGfxEntity*> m_entityVector; 
and IGfxEntity is a base class to all my objects (Cube, Sphere, Teapot etc.) My Physics class (using PhysX), works is a similar way with functions like CreateCube, CreateSphere etc. and these also create an id for each object. My World class uses both Graphics and Physics class (as members), and when World's CreateCube member function is called, it calls both the functions in Graphics+Physics. And stores their id's in world's std::vector<IWorldEntity*> m_entityVector (class IWorldEntity is a base class), whilst creating an id of it's own. If I want to change an objects position (or something else) I would used function:
 SetTranslate(int id, float x, float y, float z); 
I feel that this structure for a game is not that good, and IWorldEntity also needs pointers to Graphics and Physics class which is provided by World. I was hoping for some feedback on it, or if anyone knows of good insite or a good website to give advice on game structure? Thank you.
Advertisement
My first suggestion would be to change this to something else:
 id = (unsigned short)m_entityVector.size()-1; 

When you remove from m_entityVector, you're going to end up with duplicate id's.

If I understand correctly it sounds like when you create an entity through the world class, you're storing it's graphics data, physics data, and world data in three different vectors with three id's. You might want to consider having the IWorldEntity class contain both IGfxEntity and IPhysEntity. So instead of having both your graphics class and physics class keep tracking of their own lists, there's a single list of entities that the World class contains, and each entity has its own graphics and physics data. Then there would be only be a single id contained in IWorldEntity, and only one list to keep track of.

SetTranslate(int id, float x, float y, float z);

Uh, well ok, that is one way to handle it. Why would you not just create some base class for all the objects, say: class Object and have a function for it called translate? Object->Translate(x,y,z); Pass around the pointer to the base class instead of the int id, since you'll have to do a lookup on the id every time you call SetTranslate(). If you need a way to find objects, use an std::map<DWORD,Object*> to index them, remove them, and so on.

In answer to fd9_, one way you could solve that issue in the code you have is to approach it this way: have a vector of object pointers, and a vector of ints that mark free indexes. When you free an object, make its pointer null and add that index to the free index vector list. Then when you create an object, see if there are any free indexes before you add a new one at the end. This probably reads more complex in english than it does in code.
This is an awesome topic to discuss. One of the hardest things for me in learning isn't code syntax and logic but how to structure a game in the best way. If anyone knows of reference books on the subject or web articles I'd love to hear about them.

This topic is closed to new replies.

Advertisement