Jump to content
  • Advertisement
Sign in to follow this  
captacha

SDL and Inheritance

This topic is 2570 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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;
}

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!