Page 1 of 1

Game engine structure question

Posted: Mon May 07, 2012 2:47 pm
by Blackflower-1996
ok so i have a question about game engine development for example i have 2 classes:

Code: Select all

"graphics.h"
class graphics
{
    public:
             graphics();
             ~graphics();
            void DrawImage();
}

"engine.h"
class engine: public graphics; //that inherit from graphics class
{
   public:
            engine();
            ~engine();
}

and the question is, is okay to use the graphics constructor in engine.cpp that includes engine.h that inherit from graphics class instead than in graphics.cpp

Code: Select all

"engine.cpp"

#include "engine.h"

engine::engine(){}
graphics::graphics(){}
and in the graphics.cpp just define what the function draw image is going to do

Code: Select all

"graphics.cpp"

#include "graphics.h"

void graphics::DrawImage()
{
   SDL_BlitSurface(...);
}
so then in the main.cpp whan i call the function DrawImage(); i call it this way

Code: Select all

int main(int argc, char *argv[])
{
    engine engine;
   // son now i call tha function DrawImage() like this
   engine.DrawImage();
  //instead of
   graphics.DrawImage();
is ok to do it that way so every function in my graphics class i call it from my engine class that inherit them???
so if i do the same thing also with a sound class instead off calling each function from its respective class i call them from my engine class that inherit them.
and is there any way to have in the engine class a funtion called for example "StartEngine()" that initialize al the constructors from my other classes like sound, graphics input etc ?? thanx for your time

Re: Game engine structure question

Posted: Mon May 07, 2012 3:31 pm
by JarrodParkes
In this case, I think you would want to steer away from inheritance and use pointers to your respective "sub-engines". You can still have functions defined in engine.cpp that can in turn be wrappers that call sub-engine functionality. That is just one simple approach, but I am willing to say some other people on this forum have delved way deeper into this issue.

Re: Game engine structure question

Posted: Mon May 07, 2012 5:16 pm
by bbguimaraes
This is an example of inheritance overuse. Inheritance is meant to be used for polymorphism. You could pass the engine a pointer to a graphic class:

Code: Select all

class engine {
    engine(graphic * g) : m_g(g) {}
    graphic * graphic() { return m_g; }
    // ...
};

graphic g(parameter1, paramter2);
engine e(&g);
e.graphic()->DrawImage();
and is there any way to have in the engine class a funtion called for example "StartEngine()" that initialize al the constructors from my other classes like sound, graphics input etc ?? thanx for your time
That is not necessary for the method above (you construct your objects outside of the engine class), but just for completion: the constructors for the base classes (the ones you derive from) are called before the constructor for the class. If you need to pass parameters to it, you do it like this:

Code: Select all

//                   \/ base class constructor
engine::engine() : graphic(parameter1, parameter2) {
    // constructor body
}
And it's not good to define functions of a class in the same file you define those of another. graphic functinos should be in graphic.cpp, not anywhere else (except for graphic.h, in some cases). It doesn't make a difference to the program, but it keeps things organized.