Space Invaders – Key input

The Arcanoid code we use as the structure for the space invaders clone used mouse input to control the paddle. We wanted to use keyboard input instead so this had to be changed. This task should actually be rather simple, but I had problems to make it work. I spent a lot of time looking at the Arcanoid to figure out where the changes should be made and how to make them.

The problem lied in how I had written SDL_KEYDOWN and SDL_KEYUP in Engine::HandleEvents(). I didn’t understand how the SDL mouse input code worked and when I tried to look at that code when writing the keyboard input.

case SDL_MOUSEBUTTONDOWN:
{
int index = event.button.button - 1;
if (index 2)
index = 2;
m_input_manager->SetMouseButton(index, true);

}
I wrote code in the SDL keydown/up like: if(index <0), that resulted in that no keyboard input was registered. After reading code from earlier during this course, Pong, I finally realized the mistakes and wrote a SDL_Event that worked.

case SDL_KEYDOWN:
{
int index = event.key.keysym.sym;
m_input_manager->SetKeyboard(index, true);
}

About Sebastian Ringvold

2014  Programming