[SDL] esc detector wont run -- 'out of scope'

Jonathan Dearborn grimfang4 at gmail.com
Sun Mar 26 04:58:02 PDT 2017


That's a bit closer.  Here's a little edit to give you an idea of what I
was talking about:

static void readKeyboard(InputState * inputState)
{
   const Uint8 *state = SDL_GetKeyboardState(NULL);

   inputState->fireButtonDown = state[SDL_SCANCODE_SPACE];
   inputState->leftButtonDown = state[SDL_SCANCODE_LEFT];
   inputState->rightButtonDown = state[SDL_SCANCODE_RIGHT];
   inputState->upButtonDown = state[SDL_SCANCODE_UP];
   inputState->downButtonDown = state[SDL_SCANCODE_DOWN];
   inputState->rotateLeftButtonDown = state[SDL_SCANCODE_Z];
   inputState->rotateRightButtonDown = state[SDL_SCANCODE_X];
}

void handleEvents(InputState * inputState)
{
   SDL_Event event;

   while (SDL_PollEvent(&event))
   {
      switch (event.type)
      {
         case SDL_QUIT:
            inputState->quitButtonPressed = SDL_TRUE;
            break;
         case SDL_KEYDOWN:
            if(event.key.keysym.sym == SDLK_ESCAPE)
            {
                gameState->doQuit();  // Do whatever you need to quit the
app
                break;
            }
      }
   }

   readKeyboard(inputState);
}


On Sat, Mar 25, 2017 at 3:07 PM, capehill <juha.niemimaki at gmail.com> wrote:

> Here is something that I use in a puzzle game. First I poll for events
> which triggers the event pump, and then I check the keyboard state. This
> happens at 60 Hz rate, each time game logic is ran.
>
>
>
>
> Code:
>
>
> static void readKeyboard(InputState * inputState)
> {
>    const Uint8 *state = SDL_GetKeyboardState(NULL);
>
>    inputState->fireButtonPressed = state[SDL_SCANCODE_SPACE];
>    inputState->leftButtonPressed = state[SDL_SCANCODE_LEFT];
>    inputState->rightButtonPressed = state[SDL_SCANCODE_RIGHT];
>    inputState->upButtonPressed = state[SDL_SCANCODE_UP];
>    inputState->downButtonPressed = state[SDL_SCANCODE_DOWN];
>    inputState->rotateLeftButtonPressed = state[SDL_SCANCODE_Z];
>    inputState->rotateRightButtonPressed = state[SDL_SCANCODE_X];
>
>    if (state[SDL_SCANCODE_ESCAPE])
>    {
>       inputState->quitButtonPressed = SDL_TRUE;
>    }
> }
>
> void handleEvents(InputState * inputState)
> {
>    SDL_Event event;
>
>    while (SDL_PollEvent(&event))
>    {
>       switch (event.type)
>       {
>          case SDL_QUIT:
>             inputState->quitButtonPressed = SDL_TRUE;
>             break;
>       }
>    }
>
>    readKeyboard(inputState);
> }
>
>
>
> _______________________________________________
> SDL mailing list
> SDL at lists.libsdl.org
> http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20170326/7b98cd45/attachment-0001.htm>


More information about the SDL mailing list