O'Reilly logo

SFML Game Development By Example by Raimondas Pupius

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The most simplistic approach

Let's begin by illustrating the most common approach newcomers take in order to solve this problem. It starts by enumerating all the possible states a game could have:

enum class StateType{
    Intro = 1, MainMenu, Game, Paused, GameOver, Credits
};

Good start. Now let's put it to work by simply using a switch statement:

void Game::Update(){
  switch(m_state){
    case(StateType::Intro):
      UpdateIntro();
      break;
    case(StateType::Game):
      UpdateGame();
      break;
    case(StateType::MainMenu):
      UpdateMenu();
      break;
    ...
  }
}

The same goes for drawing it on screen:

void Game::Render(){
  switch(m_state){
    case(StateType::Intro):
      DrawIntro();
      break;
    case(StateType::Game):
      DrawGame();
      break;
    case(StateType::MainMenu):
      DrawMenu();
      break;
    ...
  }
}

While this ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required