Discussion:
esc detector wont run -- 'out of scope'
(too old to reply)
speartip
2017-03-15 21:32:47 UTC
Permalink
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!
Evan Ramos
2017-03-15 22:29:40 UTC
Permalink
Try "SDL_Keycode".

-H
-----
"After you finish the first 90% of a project, you have to finish the
other 90%." - Michael Abrash
Post by speartip
For what ever reason I get an out of scope error with the following key presser
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)
{
gameRunning = false;
break;
}
}
}
}
..\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
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
capehill
2017-03-16 18:21:36 UTC
Permalink
If this is SDL2, then check: https://wiki.libsdl.org/SDL_Keysym

Your type should probably be SDL_Keycode.
speartip
2017-03-17 22:41:16 UTC
Permalink
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
2017-03-17 22:44:31 UTC
Permalink
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
Brian Puthuff
2017-03-17 22:58:33 UTC
Permalink
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" <***@fastmail.com> 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
capehill
2017-03-18 08:01:26 UTC
Permalink
Do you have a window open? Does Control-C work?
Clangray
2017-03-18 15:36:18 UTC
Permalink
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
Post by capehill
Do you have a window open? Does Control-C work?
_________________________________________________
SDL mailing list
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
Clangray
2017-03-18 15:41:29 UTC
Permalink
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
Post by Clangray
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
Post by capehill
Do you have a window open? Does Control-C work?
_________________________________________________
SDL mailing list
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
_________________________________________________
SDL mailing list
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
DLudwig
2017-03-18 16:17:36 UTC
Permalink
Post by capehill
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.
Brian Puthuff
2017-03-18 16:44:46 UTC
Permalink
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" <***@pobox.com> 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.
Clangray
2017-03-18 17:23:07 UTC
Permalink
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
Post by Brian Puthuff
I agree also. I thought there was some caveat that you needed a window
open to receive input events.
Post by capehill
__
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
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
_________________________________________________
SDL mailing list
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
Jonathan Dearborn
2017-03-18 17:32:55 UTC
Permalink
As typed, your `while(event.type == SDL_KEYDOWN)` is an infinite loop.

Jonny D
Post by Clangray
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)
{
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))
flag 0flag 1
flag 2
end flag
--
Gray Family
I agree also. I thought there was some caveat that you needed a window
open to receive input events.
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
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
*_______________________________________________*
SDL mailing list
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
_______________________________________________
SDL mailing list
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
Clangray
2017-03-18 17:57:35 UTC
Permalink
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
Post by Jonathan Dearborn
As typed, your `while(event.type == SDL_KEYDOWN)` is an infinite loop.
Jonny D
On Sat, Mar 18, 2017 at 1:23 PM, Clangray
Post by Clangray
__
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)
{
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))
flag 0flag 1
flag 2
end flag
--
Gray Family
Post by Brian Puthuff
I agree also. I thought there was some caveat that you needed a
window open to receive input events.
Post by capehill
__
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
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
_________________________________________________
SDL mailing list
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
_______________________________________________
SDL mailing list
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
_________________________________________________
SDL mailing list
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
speartip
2017-03-24 20:57:06 UTC
Permalink
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;}
}
Jonathan Dearborn
2017-03-24 21:25:54 UTC
Permalink
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
Post by speartip
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
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
Clangray
2017-03-25 18:23:36 UTC
Permalink
Jonny D is this an appropriate way to handle a keypress and exit
the program?


--

Gray Family
Post by Jonathan Dearborn
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
Post by speartip
__
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
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
_________________________________________________
SDL mailing list
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
capehill
2017-03-25 19:07:10 UTC
Permalink
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);
}
Jonathan Dearborn
2017-03-26 11:58:02 UTC
Permalink
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);
}
Post by capehill
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.
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)
{
inputState->quitButtonPressed = SDL_TRUE;
break;
}
}
readKeyboard(inputState);
}
_______________________________________________
SDL mailing list
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
Loading...