Programming States

From Esenthel

Jump to: navigation, search

States (State class) are used for different 'Application States'

For example there can be:

  • intro state
  • menu state
  • game state


Each state has it's own:

  • initializing method
  • shut down method
  • updating method
  • drawing method


Initialize and Shut Down are optional methods and can be set to NULL.

There is only one active application state.

Once active state is changed to another, first is called the old state shut down method (if any), then there is called the new state init method (if any).

Init methods must return true if state has loaded succesfully, and false if not - if false is returned then application will exit immediately.

Once a state is activated, its update and draw methods are called each frame by the engine.


Let's assume we have following states defined with their own methods:

  • StateIntro (with InitIntro, ShutIntro, UpdateIntro, DrawIntro methods)
  • StateMenu (with InitMenu, ShutMenu, UpdateMenu, DrawMenu methods)
  • StateGame (with InitGame, ShutGame, UpdateGame, DrawGame methods)


Now let's assume that those states will be called in following order:

  • StateIntro - player watches the introduction
  • StateMenu - player enters the menu, and selects new game
  • StateGame - player plays the game, then exits to the menu
  • StateMenu - in the menu exit is selected and program quits to system


In previously described scenario Esenthel Engine will call methods in following order:

  • InitPre (this is the pre-initializing method for setting up engine options, called only once)
  • Init (this is the global initializing method for loading data, called only once)
    • InitIntro (state has changed to StateIntro so initializing method of intro is called)
      • UpdateIntro (from now on update and draw methods of the state are called in each frame)
      • DrawIntro
      • .. (repeat)
      • UpdateIntro
      • DrawIntro
    • ShutIntro (state is changing to StateMenu so shut down intro state)
    • InitMenu
      • UpdateMenu
      • DrawMenu
      • ..
      • UpdateMenu
      • DrawMenu
    • ShutMenu
    • InitGame
      • UpdateGame
      • DrawGame
      • ..
      • UpdateGame
      • DrawGame
    • ShutGame
    • InitMenu
      • UpdateMenu
      • DrawMenu
      • ..
      • UpdateMenu
      • DrawMenu
    • ShutMenu
  • Shut (this is the global shut down method, called only once)