0
\$\begingroup\$

I want to know to code structure of a game that has different levels to choose from on the main screen. More precisely, I want to know how does the main loop hands off the rendering to another loop that exist in the level code.

I could think of a solution but I would like to know how this is done by game developers.

Solution:

// Level.cpp
void Level::LevelLoop() {
    while(LevelNotDone){
        handleInput();
        update();
        render();
    }
}

// Main.cpp
void MainLoop() {
    while(GameNotDone){
        handleInput();

        if (Condition to start level)
            level.LevelLoop();

        // wait until level loop exits

        update();
        render();
    }
}
\$\endgroup\$

4 Answers 4

2
\$\begingroup\$

Lookup "data driven programming", "virtual functions" and "function pointers"

http://en.wikipedia.org/wiki/Data-driven_programming

http://en.wikipedia.org/wiki/Virtual_function

short virtual function example:

bool quit = false;

class GameMode {
public:
  virtual void Update() = 0;
};

class GameModePlatformer : public GameMode
{
public:
  virtual void Update() {
    // do stuff in platformer mode
  }
};

class GameModeMenu : public GameMode
{
public:
  virtual void Update() {
    // do stuff in menu mode
  }
};

GameModePlatformer game_mode_platformer;
GameModeMenu game_mode_menu;

void main()
{
  GameMode *current_mode = &game_mode_menu;

  while(!quit){
    current_mode->Update();

    render();
  }
}

An actual game would have init and free functions and other details such as a convenient way to switch modes but that's the gist of it.

\$\endgroup\$
1
\$\begingroup\$

This can be done with game states.

Pseudo-code example:

STATE = "Menu";
LEVEL_NUMBER = 0;
QUIT = false;
while(!QUIT) {
     if (state is "Menu"){
          Menu.update();
     }
     if (state is "Game"){
          Levels[LEVEL_NUMBER].tick();
     }
}
\$\endgroup\$
1
\$\begingroup\$

In my small game, I've done it this way: (init_level() will start a new level)

srand(time(NULL));
Env e;
Game g(e);
g.init_window();
g.init();
bool exit1 = false;
for(int i=0;i<8;i++)
  {
    g.init_level(i);
    while(1) {
       SDL_Delay(10);
       bool b1 = g.step(); // next level if b1==false
       bool b2= g.event_handling();
       if (!b1) break;
       if (!b2) {exit1=true; break; }
     }
     if (exit1==true) break;
  }
\$\endgroup\$
1
\$\begingroup\$

We may be running into the XY problem here.

More precisely, I want to know how does the main loop hands off the rendering to another loop that exist in the level code.

Most games don't do this. Whether you're in level 1 or level 2, the code rendering is the exact same. The only thing that changes is which objects are loaded up into the scene manager and where the current camera is looking.

Likewise, game logic doesn't necessarily change between levels, either. The actual game logic is generally identical and all that changes is which data is loaded up. If you make your game logic data-driven enough, you can even have vastly different types of levels without needing any specific code added for them.

Menus can also just be another type of level with menu objects instead of characters. Or they might be handled by an entirely different system like a third-party GUI library (which needs to be able to run at the same time as a level for pause and settings menus).

I could think of a solution but I would like to know how this is done by game developers.

They wouldn't have per-level rendering, and probably wouldn't have per-level hard-coded logic.

However, hard-coded level logic is easier and a good first step for a beginner. This is how you should start. The other answers cover the details of how that is done.

I would definitely avoid per-level hard-coded rendering, at least outside of menus.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .