esc detector wont run -- 'out of scope'

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
19 messages Options
Reply | Threaded
Open this post in threaded view
|

esc detector wont run -- 'out of scope'

speartip
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!

_______________________________________________
SDL mailing list
[hidden email]
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
Reply | Threaded
Open this post in threaded view
|

Re: esc detector wont run -- 'out of scope'

Evan Ramos
Try "SDL_Keycode".

-H
-----
"After you finish the first 90% of a project, you have to finish the
other 90%." - Michael Abrash


On Wed, Mar 15, 2017 at 4:32 PM, speartip <[hidden email]> wrote:

> 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!
>
> _______________________________________________
> SDL mailing list
> [hidden email]
> http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
>
_______________________________________________
SDL mailing list
[hidden email]
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
Reply | Threaded
Open this post in threaded view
|

Re: esc detector wont run -- 'out of scope'

capehill
In reply to this post by speartip
If this is SDL2, then check: https://wiki.libsdl.org/SDL_Keysym

Your type should probably be SDL_Keycode.

_______________________________________________
SDL mailing list
[hidden email]
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
Reply | Threaded
Open this post in threaded view
|

Re: esc detector wont run -- 'out of scope'

speartip
In reply to this post by speartip
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
#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"<       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<    return 0;



}

_______________________________________________
SDL mailing list
[hidden email]
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
Reply | Threaded
Open this post in threaded view
|

Re: esc detector wont run -- 'out of scope'

speartip
In reply to this post by speartip
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

_______________________________________________
SDL mailing list
[hidden email]
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
Reply | Threaded
Open this post in threaded view
|

Re: esc detector wont run -- 'out of scope'

Brian Puthuff
Try:

while(SDL_PollEvent(&event)) ...

Instead of using an if statement.

That will loop through the event queue on each iteration of the main while loop.


On Mar 17, 2017 3:44 PM, "speartip" <[hidden email]> wrote:
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

_______________________________________________
SDL mailing list
[hidden email]
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org



_______________________________________________
SDL mailing list
[hidden email]
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
Reply | Threaded
Open this post in threaded view
|

Re: esc detector wont run -- 'out of scope'

capehill
In reply to this post by speartip
Do you have a window open? Does Control-C work?

_______________________________________________
SDL mailing list
[hidden email]
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
Reply | Threaded
Open this post in threaded view
|

Re: esc detector wont run -- 'out of scope'

speartip
My console window is open and its just producing flag 1.  I changed the if to while as suggested -- no changed.
Cntrl - C no change.
--
  Gray Family



On Sat, Mar 18, 2017, at 04:01 AM, capehill wrote:
Do you have a window open? Does Control-C work?
_______________________________________________
SDL mailing list


_______________________________________________
SDL mailing list
[hidden email]
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
Reply | Threaded
Open this post in threaded view
|

Re: esc detector wont run -- 'out of scope'

speartip
And the other behavior is that if I don't click - 'focus' into the console my keypresses will show up in my IDE.

--
  Gray Family



On Sat, Mar 18, 2017, at 11:36 AM, Clangray wrote:
My console window is open and its just producing flag 1.  I changed the if to while as suggested -- no changed.
Cntrl - C no change.
--
  Gray Family



On Sat, Mar 18, 2017, at 04:01 AM, capehill wrote:
Do you have a window open? Does Control-C work?
_______________________________________________
SDL mailing list

_______________________________________________
SDL mailing list


_______________________________________________
SDL mailing list
[hidden email]
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
Reply | Threaded
Open this post in threaded view
|

Re: esc detector wont run -- 'out of scope'

DLudwig
In reply to this post by speartip







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.

_______________________________________________
SDL mailing list
[hidden email]
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
Reply | Threaded
Open this post in threaded view
|

Re: esc detector wont run -- 'out of scope'

Brian Puthuff
I agree also. I thought there was some caveat that you needed a window open to receive input events.


On Mar 18, 2017 9:17 AM, "DLudwig" <[hidden email]> wrote:



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.

_______________________________________________
SDL mailing list
[hidden email]
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org



_______________________________________________
SDL mailing list
[hidden email]
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
Reply | Threaded
Open this post in threaded view
|

Re: esc detector wont run -- 'out of scope'

speartip
Ok, I introduced a window

#include"SDL.h"
using namespace std;

const char* WINDOW_TITLE = "SDL Start";

int main(int argc, char **argv)
{
SDL_Init(SDL_INIT_EVERYTHING);

SDL_Window * window = nullptr;
        window = SDL_CreateWindow("Hello World!",
                                  SDL_WINDOWPOS_CENTERED,
                                  SDL_WINDOWPOS_CENTERED,
                                  640,480,
                                  SDL_WINDOW_SHOWN);

        SDL_Delay(2000); // Delay so that we can see the

SDL_Event event;

cout<<"flag 0";

   bool gameRunning = true;

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

         while (event.type == SDL_KEYDOWN)
         {
        cout<<"flag 2";
            SDL_Keycode keyPressed = event.key.keysym.sym;

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

      }

   }

   SDL_Quit();

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



}

I'm getting flag2 constantly.
Esc and other keys seem to have no effect

**if I substitute if for while (below)

change while back to if
if (SDL_PollEvent(&event))

output:

flag 0flag 1
flag 2
end flag


--
  Gray Family



On Sat, Mar 18, 2017, at 12:44 PM, Brian Puthuff wrote:
I agree also. I thought there was some caveat that you needed a window open to receive input events.


On Mar 18, 2017 9:17 AM, "DLudwig" <[hidden email]> wrote:




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.

_______________________________________________
SDL mailing list

_______________________________________________
SDL mailing list


_______________________________________________
SDL mailing list
[hidden email]
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
Reply | Threaded
Open this post in threaded view
|

Re: esc detector wont run -- 'out of scope'

Jonathan Dearborn-2
As typed, your `while(event.type == SDL_KEYDOWN)` is an infinite loop.

Jonny D


On Sat, Mar 18, 2017 at 1:23 PM, Clangray <[hidden email]> wrote:
Ok, I introduced a window

#include"SDL.h"
using namespace std;

const char* WINDOW_TITLE = "SDL Start";

int main(int argc, char **argv)
{
SDL_Init(SDL_INIT_EVERYTHING);

SDL_Window * window = nullptr;
        window = SDL_CreateWindow("Hello World!",
                                  SDL_WINDOWPOS_CENTERED,
                                  SDL_WINDOWPOS_CENTERED,
                                  640,480,
                                  SDL_WINDOW_SHOWN);

        SDL_Delay(2000); // Delay so that we can see the

SDL_Event event;

cout<<"flag 0";

   bool gameRunning = true;

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

         while (event.type == SDL_KEYDOWN)
         {
        cout<<"flag 2";
            SDL_Keycode keyPressed = event.key.keysym.sym;

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

      }

   }

   SDL_Quit();

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



}

I'm getting flag2 constantly.
Esc and other keys seem to have no effect

**if I substitute if for while (below)

change while back to if
if (SDL_PollEvent(&event))

output:

flag 0flag 1
flag 2
end flag


--
  Gray Family



On Sat, Mar 18, 2017, at 12:44 PM, Brian Puthuff wrote:
I agree also. I thought there was some caveat that you needed a window open to receive input events.


On Mar 18, 2017 9:17 AM, "DLudwig" <[hidden email]> wrote:




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.

_______________________________________________
SDL mailing list

_______________________________________________
SDL mailing list


_______________________________________________
SDL mailing list
[hidden email]
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org



_______________________________________________
SDL mailing list
[hidden email]
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
Reply | Threaded
Open this post in threaded view
|

Re: esc detector wont run -- 'out of scope'

speartip
Thx. Its infinite only under certain circumstances, which is confounding. What are you seeing and how can I fix it.
If I use "if":

if (SDL_PollEvent(&event))

its not infinite

if I use while

while(SDL_PollEvent(&event))

if is

flag 2, flag 2 ad infinitum

Does the window  have to be in the loop to get the benefit of the key stroke?




--
  Gray Family



On Sat, Mar 18, 2017, at 01:32 PM, Jonathan Dearborn wrote:
As typed, your `while(event.type == SDL_KEYDOWN)` is an infinite loop.

Jonny D


On Sat, Mar 18, 2017 at 1:23 PM, Clangray <[hidden email]> wrote:

Ok, I introduced a window


#include"SDL.h"
using namespace std;


const char* WINDOW_TITLE = "SDL Start";

int main(int argc, char **argv)
{

SDL_Init(SDL_INIT_EVERYTHING);

SDL_Window * window = nullptr;
        window = SDL_CreateWindow("Hello World!",
                                  SDL_WINDOWPOS_CENTERED,
                                  SDL_WINDOWPOS_CENTERED,
                                  640,480,
                                  SDL_WINDOW_SHOWN);

        SDL_Delay(2000); // Delay so that we can see the

SDL_Event event;

cout<<"flag 0";


   bool gameRunning = true;

   while (gameRunning)
   {

   cout<<"flag 1"<<endl;
      while (SDL_PollEvent(&event))
      {
      gameRunning = false;

      cout<<"flag 2";
      if (event.type == SDL_QUIT)
        {
    cout<<"flag 3";
    gameRunning = false;
      }


         while (event.type == SDL_KEYDOWN)
         {
        cout<<"flag 2";
            SDL_Keycode keyPressed = event.key.keysym.sym;


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

      }

   }


   SDL_Quit();

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



}

I'm getting flag2 constantly.
Esc and other keys seem to have no effect

**if I substitute if for while (below)

change while back to if
if (SDL_PollEvent(&event))

output:

flag 0flag 1
flag 2
end flag


--
  Gray Family



On Sat, Mar 18, 2017, at 12:44 PM, Brian Puthuff wrote:
I agree also. I thought there was some caveat that you needed a window open to receive input events.


On Mar 18, 2017 9:17 AM, "DLudwig" <[hidden email]> wrote:




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.

_______________________________________________
SDL mailing list

_______________________________________________
SDL mailing list


_______________________________________________
SDL mailing list

_______________________________________________
SDL mailing list


_______________________________________________
SDL mailing list
[hidden email]
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
Reply | Threaded
Open this post in threaded view
|

Re: esc detector wont run -- 'out of scope'

speartip
In reply to this post by speartip
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;}
                    }

_______________________________________________
SDL mailing list
[hidden email]
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
Reply | Threaded
Open this post in threaded view
|

Re: esc detector wont run -- 'out of scope'

Jonathan Dearborn-2
Checking scancodes is a different thing than handling events.  Unless necessary to achieve an effect (e.g. holding a key while moving the mouse), you should only be using the event structure when in the SDL_PollEvent loop.

Jonny D

On Fri, Mar 24, 2017 at 4:57 PM, speartip <[hidden email]> wrote:
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;}
                    }



_______________________________________________
SDL mailing list
[hidden email]
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org



_______________________________________________
SDL mailing list
[hidden email]
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
Reply | Threaded
Open this post in threaded view
|

Re: esc detector wont run -- 'out of scope'

speartip
Jonny D is this an appropriate way to handle a keypress and exit the program?

--
  Gray Family



On Fri, Mar 24, 2017, at 05:25 PM, Jonathan Dearborn wrote:
Checking scancodes is a different thing than handling events.  Unless necessary to achieve an effect (e.g. holding a key while moving the mouse), you should only be using the event structure when in the SDL_PollEvent loop.

Jonny D

On Fri, Mar 24, 2017 at 4:57 PM, speartip <[hidden email]> wrote:

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;}
                    }



_______________________________________________
SDL mailing list

_______________________________________________
SDL mailing list


_______________________________________________
SDL mailing list
[hidden email]
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
Reply | Threaded
Open this post in threaded view
|

Re: esc detector wont run -- 'out of scope'

capehill
In reply to this post by speartip
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
[hidden email]
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
Reply | Threaded
Open this post in threaded view
|

Re: esc detector wont run -- 'out of scope'

Jonathan Dearborn-2
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 <[hidden email]> 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
[hidden email]
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org



_______________________________________________
SDL mailing list
[hidden email]
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org