Abstract class implementation

Started by
7 comments, last by Telastyn 18 years, 4 months ago
I am making a game engine with abstract base classes so that it will be implemented in OpenGl and DirectX. The basic class structure is as follows:
 		 abstract Base
			|
			|
	_________________________________
	|		|		|
derived baseGL		|	Derived baseDX
			|
		*Abstract classA*
			|
			|
		Abstract classB
			|
			|
	_________________________________
	|				|
derived GLclass	B		derived DXclass B

The abstract base class will obviously not be implemented, but the methods will be implemented in the derived openGL and DirectX classes. The same will be done for abstract class B which inherits from class A, which inherits from the base class. How can I implement the functions of abstract class A, in both openGl and DirectX. Do I implement the methods as if they are a part of classB or do i have to derive some more classes. I'm getting really confused, please help.
Advertisement
Each class you derive from your abstract base class is a separate class. The way it would look is probably more like this:

AbstractBaseClass  - DirectXClass  - OpenGLClass



I don't see any particular reason to have a second "DirectXBase" or "OpenGLBase" as long as your classes are designed well.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

I forgot to mention there are other classes that inherit from class A other than B. So the abstract class A is needed.
It is more like: (to use a car as example)

Vehicle
-VehicleGL
-VehicleDX
-Car
--Gearbox
---GearboxGl
---GearboxDX
--Wheel
---WheelGL
---WheelDX


etc

how would i implement the methods of the car class? (if i were implementing this example)
I'm using this model too in my game engine.

For example:
- I have an abstract class IDriver (The interface for the drivers)
- I have an OpenGL driver and a DirectX driver.

class IDriver{public:	virtual void beginScene() = 0;	virtual void endScene() = 0;		// etc.		virtual void drawBox(float size) = 0;	virtual void drawCircle(float radius) = 0;		// etc.};class OpenGLDriver{public:	virtual void beginScene()	{		// OpenGL implementation	}	virtual void endScene()	{		// OpenGL implementation	}		virtual void drawBox(float size)	{		// OpenGL implementation	}	virtual void drawCircle(float radius)	{		// OpenGL implementation	}};class DirectX9Driver{public:	virtual void beginScene()	{		// DirectX 9 implementation	}	virtual void endScene()	{		// DirectX 9 implementation	}		virtual void drawBox(float size)	{		// DirectX 9 implementation	}	virtual void drawCircle(float radius)	{		// DirectX 9 implementation	}};


I hope this example will help you, good luck.
Quote:Original post by tanuki-san
I forgot to mention there are other classes that inherit from class A other than B. So the abstract class A is needed.
It is more like: (to use a car as example)

Vehicle
-VehicleGL
-VehicleDX
-Car
--Gearbox
---GearboxGl
---GearboxDX
--Wheel
---WheelGL
---WheelDX


etc

how would i implement the methods of the car class? (if i were implementing this example)



That seems to be highly wasteful. Your classes should be designed so that you only need a Vehicle, not a separate vehicle for OpenGL and DirectX. The point of using an abstract class as your access point to the OGL/DX logic is so that the rest of your code doesn't have to know which one is being used; hardcoding all of your other classes (and your entire design) to be specific to one API is counterproductive. You'll end up writing twice as much code for really not much benefit.

I'd suggest you take another look at your design and look for ways to keep your engine totally separate from the OGL/DX layer.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

I have implemented most of it in the same way as WalterTamboer, which works fine up until the structure gets more complex and abstract classes start inheriting from other abstract base classes etc.

The abstract class' methods will be used as interface functions. So i guess there would be really only one vehicle, but it can switch between opengl and DX9 implementations via one line of code.

I can't think how i could majorly alter the design and still achieve this :S
                 ,-- DXModelabstract Model <                 `-- GLModel                   ,-- DXRendererabstract Renderer <                   `-- GLRenderer


Model impliments an independant interface for the models. Draw, Move, Resize... things like that. **Model impliment each interface point, and store actual mesh/model data [or a pointer/reference/handle to it].

**Model.Draw would push that data to the API specific renderer to take care of it.

The vehicle would then contain a Model that represents its model/mesh. To change, you would replace the Model with a different concrete version [with the same data]. Vehicles have-a representation, not is-a representation.
Telastyn, you are doing a lot of work in implementing unnecessary base/implementation on your model. What it supposed to look like is this:

CVehicle
- >CCar
- >CTruck

Inside of each of them you should somehow access the renderer interface and use abstracted functions that you otherwise use in your CGLModel or CDXModel in a manner like this:

void CVehicle::Draw(){ pRenderer->BeginScene(); pRenderer->LoadIdentity(); pRenderer->SetLighting(0); // etc // etc pRenderer->SetVertexBufferSource(m_pVertexBuffer, IVertexBuffer::XYZ | IVertexBuffer::UV); pRenderer->Render();  pRenderer->EndScene();}


I beg to differ.

The added abstraction will save me far more time later on when dealing with game objects independant of their representation, for something like networking the game.

This topic is closed to new replies.

Advertisement