need avice on class structure problem

Started by
1 comment, last by bobbinus 18 years, 8 months ago
Hello I am trying to tidy up my game classes for the actual game objects that players interact with. So in my game there is balls, boxes, cylinders etc. All these are child classes of class Object. So i think i want all the functions that relate to device objects(mesh and texture) for these in the parent class(class Object) - ie. InitiateDevObj(), DeleteDevObj(), Render() because the procedure for each should be identical regardless of the sub class. However i dont want to have a mesh and texture per object, rather per type of object(ball, box). Which is what i currently have, by including a static mesh and static texture member for each sub class then defining InitiateDevObj(), DeleteDevObj(), Render() per subclass. This seems to create a high amount of duplicate code although it does give me the maximum flexibilty. Can anyone suggest a way to clean this up. I came up with the following but its still seems inadequate... class Object { Mesh* pMesh; Texture* pTex; InitiateDevObj( string pMeshFileName, string pTexFileName); DeleteDevObj(); Render(); } class Ball : public Object { Ball specific members and functions; } There is a mesh&tex pointer per object but there also be a PhysicsEngine class which has Ball array, Box Array etc members. So i thought i would just do this: //create the dev obj for the first ball object pBallArray->InitiateDevObj(); //link all other Ball objects to this mesh for( iBall=1;iBall<m_iNumBalls, iBall++ ) pBallArray[iBall]->pMesh = pBallArray->pMesh ; Just seems a bit messy because the PhysicsEngine class is mucking around with the Balls internal data. [Edited by - bobbinus on July 30, 2005 11:11:38 PM]
Advertisement
How about a Mesh-Factory in combination with Singletons?

BallMeshSingleton
{
static BallMesh* instance()
{
static BallMesh* m = NULL;
if( !m ) m = new BallMesh;
return m;
}
}

class MeshFactory
{
public:
static Mesh* createMesh( MeshType mt )
{
if( BallMeshType == mt )
return BallMesh::instance()
}
}


class Ball
{
Ball()
{
pMesh = MeshFactory::createMesh( BallMeshType );
}
}


With this each class can get it's onw Mesh as needed, and you have with the Singleton only one Mesh for Balls....If you really want put this code direct into Object, you could vary the factory, for example, by generating a Map < FileName, Mesh*> and loading each Mesh on it's own, and so integrating the singleton into the Factory:

if( meshmap.find( filename ) == meshmap.end() )
meshmap[filename] = new Mesh( filename );
retrun meshmap[filename]
thanks for the reply ;p

Looks like it has a lot of potential but i dont really understand the benefits of the Factory-Singleton combination. Bacause if i want to make each subclass has its own shared mesh I can just have a static Mesh pointer member.

class Ball: public class Object
{
static Mesh* pMesh

//device object functions
InitiateDevObj( string pMeshFileName, string pTexFileName);
DeleteDevObj();
Render();
}

The point is that with this i have to define the three device object functions for class Ball, class Box, class Cylinder etc.. but the code is identical. So it seems i should have all this in the parent class(Object) because it is shared.

This topic is closed to new replies.

Advertisement