I have a problem with crashing my programme when handling events in SDL2.
I handle events like this:
const Uint8 *key_buttons;
SDL_Event e;
while (SDL_PollEvent(&e))
{
switch(e.type)
{
case SDL_QUIT:
running = false;
break;
case SDL_KEYDOWN:
key_buttons = SDL_GetKeyboardState(0);
break;
case SDL_KEYUP:
key_buttons = SDL_GetKeyboardState(0);
break;
}
}
And then, when I call this function:
bool Get_KeyButtonState(SDL_Scancode key)
{
if (!key_buttons)
return false;
if (key_buttons[key])
return true;
else
return false;
}
My programme crashes.
If I call SDL_GetKeyboardState()
at start of the while loop and stop using SDL_KEYDOWN
and SDL_KEYUP
, then my programme works.
So, my question is: why it happens and what's the proper solution? And also, is there a way how to handle just change of a state without needing to save previous state of all buttons (cause I couldn't find any function for this)?