The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
esc detector wont run -- 'out of scope'
speartip


Joined: 06 Feb 2017
Posts: 28
For what ever reason I get an out of scope error with the following key presser
Code:

 while (gameRunning)
   {
      if (SDL_PollEvent(&event))
      {
         if (event.type == SDL_QUIT)
         {
            gameRunning = false;
         }

         if (event.type == SDL_KEYDOWN)
         {
            SDL_Key keyPressed = event.key.keysym.sym;

            switch (keyPressed)
            {
               case SDLK_ESCAPE:
                  gameRunning = false;
                  break;
            }
         }
      }
   }


Code:

..\src\drawLine.cpp:39:13: error: 'SDLKey' was not declared in this scope
             SDLKey keyPressed = event.key.keysym.sym;
             ^
..\src\drawLine.cpp:41:21: error: 'keyPressed' was not declared in this scope
             switch (keyPressed)


Got to be something simple!
capehill


Joined: 13 Feb 2016
Posts: 18
If this is SDL2, then check: https://wiki.libsdl.org/SDL_Keysym

Your type should probably be SDL_Keycode.
speartip


Joined: 06 Feb 2017
Posts: 28
Thanks! That fixed it so it will compile. No more errors. The code still doesn't do what it is supposed to: abort when you press esc. I flagged it up. It never gets passed flag 1.

Code:

#include<iostream>
#include"SDL.h"
using namespace std;

const int WINDOW_WIDTH = 640;
const int WINDOW_HEIGHT = 480;
const char* WINDOW_TITLE = "SDL Start";

int main(int argc, char **argv)
{ cout<<"flag 0";
   SDL_Init( SDL_INIT_VIDEO );

   SDL_Event event;

   bool gameRunning = true;

   while (gameRunning)
   {
      cout<<"flag 1"<<endl;
      if (SDL_PollEvent(&event))
      {
         cout<<"flag 2";
         if (event.type == SDL_QUIT)
         {
            cout<<"flag 3";
            gameRunning = false;
         }

         if (event.type == SDL_KEYDOWN)
         {

            SDL_Keycode keyPressed = event.key.keysym.sym;

            switch (keyPressed)
            {
               case SDLK_ESCAPE:
                  gameRunning = false;
                  break;
            }
         }

      }

   }

   SDL_Quit();

   cout<<endl<<"end flag";
   return 0;



}
speartip


Joined: 06 Feb 2017
Posts: 28
Sorry: forgot to post output:
flag 1
flag 1
flag 1
flag 1
flag 1
flag 1
flag 1
.....ad infinitum

gameRunning is never set to false
capehill


Joined: 13 Feb 2016
Posts: 18
Do you have a window open? Does Control-C work?
DLudwig


Joined: 09 Feb 2012
Posts: 179
capehill wrote:
Do you have a window open? Does Control-C work?


I'd like to second the above.

Many platforms need a window open, before input can be received. Using either SDL_CreateWindowAndRenderer, or just SDL_CreateWindow, once beforehand at app init, may be sufficient to fix this.

-- David L.
speartip


Joined: 06 Feb 2017
Posts: 28
Thanks every one. In addition to having a window in context I added this small code:

Code:

 const Uint8 *keys = SDL_GetKeyboardState(NULL);
        SDL_Event e;
.
.
.

 while (SDL_PollEvent(&e)){
                    if (e.type == SDL_QUIT){
                        done = SDL_TRUE;
                    }
                    if (keys[SDL_SCANCODE_ESCAPE]){
                        done = SDL_TRUE;}
                    }
capehill


Joined: 13 Feb 2016
Posts: 18
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);
}