LD24: A Second Dinner Today? #LD48

So while the pasta was ok, apparently I was still hungry.

Cheeseless pizza

As for the game, I realized that my code that wraps libSDL and allows the rest of the code to be easily unit tested has a problem. It only knows if a key is currently being pressed or is currently released. It has no easy way to tell if the key has just been pressed or released.


case SDL_KEYDOWN:
{
m_keyboardState.setKeyDown(event.key.keysym.sym);
}
break;
case SDL_KEYUP:
{
m_keyboardState.setKeyUp(event.key.keysym.sym);
}
break;

Typically you would check SDL_KEYDOWN/SDL_KEYUP events in the event pump, and if those events occurred, you could handle them directly. Since I wanted my code to be unit tested, I created a HardwareLayer that wraps all of this functionality, and I used a KeyboardState class (that’s m_keyboardState) to track key status. Then, in my game, I can check the keyboard state for individual keys to see if anything is being pressed.

What this means in practice is that SDL is already creating an event for a key press/release, but then I ignore the existence of the event, tracking only the status. If I want to know if a key has been pressed this update, I essentially have to write code to track the state of the key and do a few if statements to know if I’ve already processed it or not.

But why do that when SDL has already done the work for me?

So I’m modifying my keyboard state to track not only the status of a key, but to also track the fact that it has been pressed or released this update. Then my game can check for this list of events, do whatever it needs, and at the end of the update, the keyboard state will clear out the list.

And this is why LD is fun.

Comments are closed.