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!
Try “SDL_Keycode”.
-H-----
“After you finish the first 90% of a project, you have to finish the
other 90%.” - Michael Abrash
If this is SDL2, then check: https://wiki.libsdl.org/SDL_Keysym
Your type should probably be SDL_Keycode.
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”<<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;
}
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
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.
Do you have a window open? Does Control-C work?
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
@Clangray
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
@Clangray
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.
I agree also. I thought there was some caveat that you needed a window open
to receive input events.
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
@Clangray
JonnyD
March 18, 2017, 5:32pm
13
As typed, your while(event.type == SDL_KEYDOWN)
is an infinite loop.
Jonny D
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
@Clangray
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;}
}
JonnyD
March 24, 2017, 9:25pm
16
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
thx a bunch, i get that now