Hello everyone. I am writing a doodle jump game now and I forced to use the framework written by someone other than me. it contains the event handlers I need to implement. Its event-checking loop looks like this:
enum class FRKey {
RIGHT,
LEFT,
DOWN,
UP,
COUNT
};
enum class FRMouseButton {
LEFT,
MIDDLE,
RIGHT,
COUNT
};
bool GKeyState[(int)FRKey::COUNT] = {};
FRAMEWORK_API int run(Framework* framework)
{
int done;
SDL_Event event;
for (int i = 0; i < (int)FRKey::COUNT; ++i)
{
GKeyState[i] = false;
}
Framework* GFramework = framework;
/* some unrelated staff */
done = 0;
while ( ! done ) {
while ( SDL_PollEvent(&event) ) {
switch (event.type) {
case SDL_KEYUP:
switch (event.key.keysym.sym) {
case SDLK_RIGHT:
case SDLK_LEFT:
case SDLK_DOWN:
case SDLK_UP:
{
int key_index = (event.key.keysym.sym - SDLK_RIGHT);
if (GKeyState[key_index])
{
GFramework->onKeyReleased((FRKey)key_index);
GKeyState[key_index] = false;
}
break;
}
case SDLK_ESCAPE:
done = 1;
break;
default:
break;
}
break;
case SDL_KEYDOWN:
switch (event.key.keysym.sym) {
case SDLK_RIGHT:
case SDLK_LEFT:
case SDLK_DOWN:
case SDLK_UP:
{
int key_index = (event.key.keysym.sym - SDLK_RIGHT);
if (!GKeyState[key_index])
{
GFramework->onKeyPressed((FRKey)key_index);
GKeyState[key_index] = true;
}
//GFramework->onKeyPressed((FRKey)key_index);
}
break;
default:
break;
}
break;
case SDL_QUIT:
done = 1;
break;
default:
break;
}
}
}
}
SDL_Quit();
return(0);
}
And with this code, when I press a key, the event only happens once even with the key held down. And I need to move the doodle by pressing the left and right arrows. That’s the problem.
In general, this code looks as if it purposely prevents the possibility of responding to a key press. But what should I do then? Is there any way to solve the problem without changing the above code, just by changing the onKeyPressed() and onKeyReleased() methods?
Does anyone have any ideas what is wrong here?