A state machine, as fastcall mentioned really is the best way to go, but the psuedo code presented makes it look like a giant switch statement, which I tend to try to avoid. Switches are fast which is good, but it tends to cluster your code together more than I like.
The way I generally do it is very similar to how XNA structures the game class.
I create a base GameStateNode class with Update/Draw functions, then each specific game state implements those functions however they like.
class GameStateNode
{
virtual void gameStateNode Update(WorldState worldState)
virtual void Draw(WorldState worldState)
}
class TitleScreen:GameStateNode
{
void Update(worldState worldState)
{
...
if (KeyPressed(EnterButton))
worldState.NextState=new MenuState();
...
return
}
void Draw(WOrldState worldState)
{
...
DrawTitle()
...
Return
}
}
Class MenuState(){....}
void main()
{
WorldState world =new WorldState()
world.CurrentNode=new TitleScreen()
while(!world.IsDone)
{
world.CurrentState.Update(world)
world.CurrentState.Draw(world)
if (world.NextState!=null)
{
world.CurrentState.Dispose()
world.CurrentState=world.NextState
}
}
}