I am building the engine for a terraria-like 2D sandbox game using SDL2. Currently, I am working on a scene management system. I was wondering if just having a single local variable in main called scene_num and some if statements in main to control the scene the player is currently on would suffice, especially as the game gets more complex?

This is what I meant in terms of basic structure:

int scene_num = 0;
// scene_num guide:
// 0 = title screen
// 1 = actual game
// 2 = inventory screen
// 3 = pause screen

// game loop
bool running = true;
while (running == true) {
    while (SDL_PollEvent(&ev)) {
        // handle user inputs here (ie: keyboard, mouse, etc.)

    // handle game updates here
    if (scene_num == 0) {
        // handle title screen here
    } else if (scene_num == 1) {
        // handle gameplay updates here
    } else if (scene_num == 2) {
        // handle inventory screen here
    } else {
        // hanlde pause screen here

    // handle rendering here
    if (scene_num == 0) {
        // handle title screen rendering here
    } else if (scene_num == 1) {
        // handle gameplay rendering here
    } else if (scene_num == 2) {
        // handle inventory screen rendering here
    } else {
        // handle pause screen rendering here
  • \$\begingroup\$ Have you encountered a problem yet that this method is not solving to your satisfaction? If not, it might be a bit early to ask here. StackExchange is generally better at solving demonstrated problems, than it is at predicting problems that could arise in future (especially since we don't know what you have planned tor the future trajectory of your game's development!) \$\endgroup\$ – DMGregory Apr 7 '20 at 17:30
  • \$\begingroup\$ @DMGregory The reason why I asked this is because I was concerned about future scalability, as I currently only have a few scenes to manage but this could quickly change in the future to 5-10 scenes. So essentially, I was asking whether or not thsi could be a viable approach to scene management \$\endgroup\$ – RandomName Apr 7 '20 at 17:32
  • 1
    \$\begingroup\$ Try writing the function with 5-10 scenes. Is it still maintainable enough for your needs in that state? If VVVVVV can get away with a switch statement spanning hundreds of cases with no enums to clarify the magic numbers, and still ship successfully, I'd wager your game will survive this. ;) \$\endgroup\$ – DMGregory Apr 7 '20 at 17:39
  • \$\begingroup\$ @DMGregory Thank you so much for your help \$\endgroup\$ – RandomName Apr 7 '20 at 17:41
  • 2
    \$\begingroup\$ At least use an enum so you don't need to wonder in 6 months what state "3" might be. \$\endgroup\$ – Tyyppi_77 Apr 7 '20 at 18:43

Your Answer

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

Browse other questions tagged or ask your own question.