[SDL] Game event loop and input

David Olofson david at olofson.net
Fri Jan 31 07:04:01 PST 2003

On Friday 31 January 2003 01.42, Joby Bednar wrote:
> I'm starting to map out the various stages of my game, intro,
> tutorial, game play, cut scenes, etc.  and it is getting a little
> ugly when it comes to what I envision as user input and the basic
> event loop.  I'm thinking I need to create a global that stores the
> current game state and use a series of case statements within the
> PollEvent loop.  This seems a bit bulky though and could run pages
> long.

In Kobo Deluxo, I'm using a generic game state class for this. When 
you add a state to the game, you derive from a state class, override 
handlers for events, video frames, game logic frames etc. The state 
manager class has methods for switching and pushing states, so you 
can either replace the current state with another state, or "call" 
the another state, subroutine style.

This makes it easy to enter the options dialog from within the game 
and then return to the current game when leaving it, and that sort of 

> Has anyone had experience in developing a custom class that you can
> use to wrap around the event to simplify the event structure with
> respect to your game states?

Well, yes and no; the event itself is rather irrelevant here, IMHO. 
It's the *state* and whatever manages states that are the actual 
objects here. It's a pretty typical state machine, and the events are 
just one form of input to it.

> Or perhaps other ways that you have
> found especially effective in keeping code clean and simple?

The game_state_manager of Kobo Deluxe does the job pretty well, IMHO, 
but it's perhaps not as clean as it could be. It sort of evolved over 
time, and I haven't looked at it in a while; maybe it's just the game 
code around it that's messy? :-)

//David Olofson - Programmer, Composer, Open Source Advocate

.- The Return of Audiality! --------------------------------.
| Free/Open Source Audio Engine for use in Games or Studio. |
| RT and off-line synth. Scripting. Sample accurate timing. |
`---------------------------> http://olofson.net/audiality -'
   --- http://olofson.net --- http://www.reologica.se ---

More information about the SDL mailing list