Skip to content

Instantly share code, notes, and snippets.

@i8degrees
Created May 24, 2013 12:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save i8degrees/5643148 to your computer and use it in GitHub Desktop.
Save i8degrees/5643148 to your computer and use it in GitHub Desktop.
GameState && SDLInput Interfaces
class SDLInput
{
virtual void Input ( SDL_Event *input )
{
// while ( SDL_PollEvent ( input ) )
switch ( input->type )
{
case SDL_KEYDOWN:
onKeyDown ( input->key.keysym.sym, input->key.keysym.mod );
break;
case SDL_KEYUP:
// Temporary state crash workaround by commenting below line out of exec:
//onKeyUp ( input->key.keysym.sym, input->key.keysym.mod );
break;
case SDL_MOUSEMOTION:
// Temporary state crash workaround by commenting below line out of exec:
//onMouseMotion ( input->motion.x, input->motion.y );
break;
// ...
}
}
virtual void onKeyDown ( SDLKey key, SDLMod mod );
{
// empty implement
}
virtual void onKeyUp ( SDLKey key, SDLMod mod )
{
// empty implement
}
};
class GameState: public SDLInput
{
};
class GameEngine
{
void GameEngine::HandleInput ( void )
{
// let the state handle events
states.back()->HandleInput ();
}
};
class CardsMenuState: public GameState
{
void HandleInput ( void )
{
SDL_Event event;
while ( SDL_PollEvent ( &event ) )
SDLInput::Input ( &event );
// Previously:
// SDLInput::Input ();
}
void onKeyDown ( SDLKey key, SDLMod mod )
{
switch ( key )
{
// ...
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment