I've started writing an SDL2 program. I want integer count
to go up one when the user presses the right arrow key, down one when user presses left.
#include <iostream>
#include <SDL2/SDL.h>
int main(){
SDL_Window *window= SDL_CreateWindow("test", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN);
int count= 0;
bool isRunning= true;
SDL_Event ev;
while(isRunning){
if(SDL_PollEvent(&ev)){
if(ev.type == SDL_QUIT || ev.key.keysym.scancode == SDL_SCANCODE_ESCAPE)
return 0;
}
const Uint8 *keystate= SDL_GetKeyboardState(NULL);
if(keystate[SDL_SCANCODE_LEFT])
--count;
else if(keystate[SDL_SCANCODE_RIGHT])
++count;
std::cout << count << std::endl;
}
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
Here's a sample of what's printed when I start the program and briefly tap right -- all within a second or two:
0
0
0
0
0
0
0
0
0
0
0
1
2
3
4
4
4
4
4
When I do a quick tap on the right arrow key, I want count
to go up by just one, but instead it went from 0
to 4
.
Why?
How do I fix this problem?