SDL - Getting a single keypress event instead of a keystate?
Posted
by
MrKatSwordfish
on Game Development
See other posts from Game Development
or by MrKatSwordfish
Published on 2012-09-10T04:05:25Z
Indexed on
2012/09/10
9:49 UTC
Read the original article
Hit count: 224
Right now I'm working on a simple SDL project, but I've hit an issue when trying to get a single keypress event to skip past a splash screen. Right now, there are 4 start-up splash screens that I would like to be able to skip with a single keypress (of any key). My issue is that, as of now, if I hold down a key, it skips through each splash screen to the very last one immediately.
The splash screens are stored as an array of SDL surfaces which are all loaded at the initialization of the state. I have an variable called currentSplashImage that controls which element of the array is being rendered on the screen. I've set it up so that whenever there's a SDL_KEYDOWN event, it triggers a single incrementation of the currentSplashImage variable.
So, I'm really not sure why my code isn't working correctly. For some reason, when I hold down a button, it seems to be treating the held button as a new key press event every time it ticks through the code. Does anyone know how I can go about fixing this issue?
[Here's a snippet of code that I've been using...]
void SplashScreenState::handleEvents()
{
SDL_PollEvent( &localEvent );
if ( localEvent.type == SDL_KEYDOWN )
{
if ( currentSplashImage < 3 && currentSplashImage >= 0)
{ currentSplashImage++; }
}
else if ( localEvent.type == SDL_QUIT )
{
smgaEngine.setRunning(false);
}
}
I should also mention that the SDL_Event 'localEvent' is part of the GameState parent class, while this event handling code is part of a SplashScreenState subclass.
If anyone knows why this is happening, or if there is any way to improve my code, It'd be helpful to me! :D I'm still a very new programmer, trying to learn.
UPDATE:
I added a std::cout line to that the code runs multiple times with a single KEYDOWN event. I also tried disabling SDL_EnableKeyRepeat, but it didn't fix the issue.
void SplashScreenState::handleEvents()
{
SDL_PollEvent( &localEvent );
if ( localEvent.type == SDL_KEYDOWN )
{
if ( currentSplashImage < 3 && currentSplashImage >= 0)
{
currentSplashImage++;
std::cout << "KEYDOWN.."; //<---- test cout line
}
}
else if ( localEvent.type == SDL_QUIT )
{
smgaEngine.setRunning(false);
}
}
This prints out "KEYDOWN..KEYDOWN..KEYDOWN.." in the cout stream when a button is held.
© Game Development or respective owner