Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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!

share|improve this question
    
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. –  Sean Middleditch Aug 6 '13 at 16:41
add comment

1 Answer

up vote 8 down vote accepted

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.

Example

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
{
public:
    void Run()
    {
        Initialize();
        while(_running)
            Frame();
        Shutdown();
    }

private:
    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;
    game.Run();
    return 0;
}
share|improve this answer
1  
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. –  Patrick Hughes Mar 9 '12 at 23:40
1  
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. –  tigrou Mar 9 '12 at 23:48
4  
@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. –  Patrick Hughes Mar 10 '12 at 0:14
1  
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. –  Matt Jensen Mar 10 '12 at 6:37
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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