Add support for joysticks and gamepads.

Tell SDL to listen for joystick events and simply map motion to the cursor keys
and the first 4 buttons to control, space, shift and t.
This commit is contained in:
Guus Sliepen 2013-07-10 21:37:08 +02:00
parent 828b3eb813
commit 1853044891
2 changed files with 41 additions and 2 deletions

View File

@ -162,7 +162,7 @@ void initSystem()
#endif #endif
/* Initialize the SDL library */ /* Initialize the SDL library */
if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO) < 0) { if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO|SDL_INIT_JOYSTICK) < 0) {
printf("Couldn't initialize SDL: %s\n", SDL_GetError()); printf("Couldn't initialize SDL: %s\n", SDL_GetError());
exit(1); exit(1);
} }
@ -209,6 +209,9 @@ void initSystem()
SDL_ShowCursor(SDL_DISABLE); SDL_ShowCursor(SDL_DISABLE);
SDL_EventState(SDL_MOUSEMOTION, SDL_DISABLE); SDL_EventState(SDL_MOUSEMOTION, SDL_DISABLE);
SDL_JoystickEventState(SDL_ENABLE);
SDL_JoystickOpen(0);
} }
/* /*

View File

@ -308,7 +308,7 @@ void getPlayerInput()
engine.cursor_y = y; engine.cursor_y = y;
} }
if (SDL_PollEvent(&engine.event)) while (SDL_PollEvent(&engine.event))
{ {
switch (engine.event.type) switch (engine.event.type)
{ {
@ -340,6 +340,42 @@ void getPlayerInput()
engine.keyState[engine.event.key.keysym.sym] = 0; engine.keyState[engine.event.key.keysym.sym] = 0;
break; break;
case SDL_JOYBUTTONDOWN:
case SDL_JOYBUTTONUP:
switch (engine.event.jbutton.button)
{
case 0:
engine.keyState[SDLK_LCTRL] = engine.event.jbutton.state;
break;
case 1:
engine.keyState[SDLK_SPACE] = engine.event.jbutton.state;
break;
case 2:
engine.keyState[SDLK_LSHIFT] = engine.event.jbutton.state;
break;
case 3:
engine.keyState[SDLK_t] = engine.event.jbutton.state;
break;
}
break;
case SDL_JOYHATMOTION:
engine.keyState[SDLK_UP] = engine.event.jhat.value & SDL_HAT_UP;
engine.keyState[SDLK_DOWN] = engine.event.jhat.value & SDL_HAT_DOWN;
engine.keyState[SDLK_LEFT] = engine.event.jhat.value & SDL_HAT_LEFT;
engine.keyState[SDLK_RIGHT] = engine.event.jhat.value & SDL_HAT_RIGHT;
break;
case SDL_JOYAXISMOTION:
if (engine.event.jaxis.axis & 1) {
engine.keyState[SDLK_UP] = engine.event.jaxis.value < -16384;
engine.keyState[SDLK_DOWN] = engine.event.jaxis.value >= 16384;
} else {
engine.keyState[SDLK_LEFT] = engine.event.jaxis.value < -16384;
engine.keyState[SDLK_RIGHT] = engine.event.jaxis.value >= 16384;
}
break;
default: default:
break; break;
} }