Skip to main content
added 718 characters in body
Source Link
Stephane Hockenhull
  • 12.1k
  • 1
  • 26
  • 44

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.

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

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

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

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.

Source Link
Stephane Hockenhull
  • 12.1k
  • 1
  • 26
  • 44

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

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

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