SDL and Inheritance

Started by
1 comment, last by rip-off 12 years, 4 months ago
For the past few days, I've been taking my SDL program into a more OOP structure. Right now I have two classes. The Base Class is called Functions and is used for Routine/Misc functions. The Sub Class is called Physics where I implement physics for my game. I've tried to implement a Fall function in Physics. It doesn't work in the Physics class, but when I put it in the Functions class it does work.
My Functions Class:

class Functions
{
public:
bool Running;
Functions();
Draw();
Input();
Movement();
FreeMem();

private:
SDL_Surface *Screen;
SDL_Surface *BG;
SDL_Surface *Crate;
SDL_Surface *Ground;
char PDir;

protected:
SDL_Rect RCrate;
SDL_Rect RGround;
SDL_Event Event;
};

My Sub Class looks the same except instead of
class Functions
The declaration looks like
class Physics: public Functions

My Main.cpp looks like this:

int main(int argc, char **argv)
{

Functions FInst;
Gravity GInst;

if(FInst.Running)
{
FInst.Draw();
FInst.Input();
FInst.Movement();
GInst.Fall();
}
FInst.FreeMem();
SDL_Quit();
return 0;
}
Advertisement
What do you mean by "doesn't work"? Does it not compile? Does it not link? Runtime error?
It is unclear what your object model is supposed to represent, as neither "Functions" nor "Physics" are objects in the domain of games. Using classes and inheritance are not what makes a project object oriented.

If you tell us more about the game, we can help you design a better model to fit it.

As for your error (assuming it compiles and links): I guess you are doing SDL initialisation in the constructor of the Functions class. However, the constructor is called twice, once when you instantiate "FInst", and again when you instantiate "GInst". This will cause you some trouble.

Your Functions class probably does too much. Keep the SDL initialisation inside main() for the moment.

This topic is closed to new replies.

Advertisement