SDL Forum Index
SDL
Simple DirectMedia Layer Forums
Reply to topic
esc detector wont run -- 'out of scope'
speartip


Joined: 06 Feb 2017
Posts: 28
Reply with quote
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!
View user's profileSend private message
capehill


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

Your type should probably be SDL_Keycode.
View user's profileSend private message
speartip


Joined: 06 Feb 2017
Posts: 28
Reply with quote
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;



}
View user's profileSend private message
speartip


Joined: 06 Feb 2017
Posts: 28
Reply with quote
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
View user's profileSend private message
capehill


Joined: 13 Feb 2016
Posts: 18
Reply with quote
Do you have a window open? Does Control-C work?
View user's profileSend private message
DLudwig


Joined: 09 Feb 2012
Posts: 179
Reply with quote
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.
View user's profileSend private message
speartip


Joined: 06 Feb 2017
Posts: 28
Reply with quote
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;}
                    }
View user's profileSend private message
capehill


Joined: 13 Feb 2016
Posts: 18
Reply with quote
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);
}
View user's profileSend private message
esc detector wont run -- 'out of scope'
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
All times are GMT  
Page 1 of 1  

  
  
 Reply to topic