I have a Game class with my main game functions and instances of game objects like screen, etc. My game is made using C++ and SDL.

I was wondering, though, should main be a function of my class? Or should I do "Game game;" from "int main();" and have the loop inside main?

My doubt is - if I do it from an outside-main, then most of my stuff will have to be public, and that is kinda bad, isn't it?

Any ideas? Thank you!

  • \$\begingroup\$ This is not something that really matters. Write whatever you think is easier. It could be done either way or many other ways, and the choice affects your actual game in no meaningful way at all. \$\endgroup\$ – Sean Middleditch Aug 6 '13 at 16:41

You could have a single public method in your Game class to serve as an entry point (e.g. a method called Run()). Then the only thing you'd need to do inside main() to get everything up and running would be to call that method - nothing more.

The Run() method could start by initializing all subsystems and then proceed to enter the main game loop. It should also take care of shutting everything down properly once the loop ends.

Everything else in the class can remain private (or protected if you'd prefer to create a concrete game by inheriting from it) because it will only be needed by your game implementation.


Note: Completely bare bones, no timing calculations, extremely simplistic game loop, just an example to get you started. For an actual game it should probably have a lot more complexity than this:

class Game
    void Run()

    void Initialize() { /* Initialize subsystems */ }
    void Shutdown() { /* Shutdown subsystems */ }
    void Frame() { /* Update and draw game */ }
    bool _running = true;

And on your main() method:

int main()
    Game game;
    return 0;
  • 1
    \$\begingroup\$ This is typically how it's done. It makes it easier to provide a good place for a last-chance exception catch, plus you can now destroy your game and recreate it easily. \$\endgroup\$ – Patrick Hughes Mar 9 '12 at 23:40
  • 1
    \$\begingroup\$ I agree for exception (catched in main()) function). But why would you want to destroy and recreate whole game ? (thus also main SDL window ?). Not sure to understand. \$\endgroup\$ – tigrou Mar 9 '12 at 23:48
  • 5
    \$\begingroup\$ @user1083855 not all games are huge monstrosities, many are small and simply deleting the entire game object and rebuilding it is far easier than writing a Reset() method and hoping that you remembered to reset and clear all the states. So it's both faster to write and no debugging required. Even some large games do this with engine systems like maps every time you transition or quit to the main menu so it's really just a matter of how much is torn down and rebuilt and not whether it's a bad idea or not. \$\endgroup\$ – Patrick Hughes Mar 10 '12 at 0:14
  • 2
    \$\begingroup\$ If you do build a larger game, it is nice to have a soft reset function, it makes things a lot better user experience wise, and for some games you just can't just deconstruct and reconstruct the game class. Say that you do all your set-up and initialization in the game class, then its a time problem, what if your game class controls your menu system, do you really want the game to restart just because you want to exit the current game type? This completely depends on the design of your game, but adding core functions is better now than later. \$\endgroup\$ – Matt Jensen Mar 10 '12 at 6:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.