Completely untested controller mappings.

This commit is contained in:
Linus Probert 2018-10-11 15:20:28 +02:00
parent bb7f9effd3
commit 3143236908
2 changed files with 36 additions and 19 deletions

View File

@ -79,14 +79,6 @@ get_event_key(SDL_Event *event)
key = KEY_NUM4; break; key = KEY_NUM4; break;
case SDLK_5: case SDLK_5:
key = KEY_NUM5; break; key = KEY_NUM5; break;
case SDLK_6:
key = KEY_NUM6; break;
case SDLK_7:
key = KEY_NUM7; break;
case SDLK_8:
key = KEY_NUM8; break;
case SDLK_9:
key = KEY_NUM9; break;
case SDLK_ESCAPE: case SDLK_ESCAPE:
key = KEY_ESC; break; key = KEY_ESC; break;
case SDLK_RETURN: case SDLK_RETURN:
@ -104,6 +96,14 @@ get_event_button(SDL_Event *event)
{ {
Uint64 key; Uint64 key;
switch (event->jbutton.button) { switch (event->jbutton.button) {
case SDL_CONTROLLER_BUTTON_DPAD_UP:
key = KEY_UP; break;
case SDL_CONTROLLER_BUTTON_DPAD_DOWN:
key = KEY_DOWN; break;
case SDL_CONTROLLER_BUTTON_DPAD_LEFT:
key = KEY_LEFT; break;
case SDL_CONTROLLER_BUTTON_DPAD_RIGHT:
key = KEY_RIGHT; break;
case SDL_CONTROLLER_BUTTON_A: case SDL_CONTROLLER_BUTTON_A:
key = KEY_NUM1 & KEY_ENTER; break; key = KEY_NUM1 & KEY_ENTER; break;
case SDL_CONTROLLER_BUTTON_X: case SDL_CONTROLLER_BUTTON_X:
@ -116,9 +116,6 @@ get_event_button(SDL_Event *event)
key = KEY_NUM5; break; key = KEY_NUM5; break;
case SDL_CONTROLLER_BUTTON_START: case SDL_CONTROLLER_BUTTON_START:
key = KEY_ESC; break; key = KEY_ESC; break;
case SDL_CONTROLLER_AXIS_TRIGGERRIGHT:
key = KEY_SPACE; break;
case SDL_CONTROLLER_BUTTON_INVALID:
default: default:
key = 0; break; key = 0; break;
} }
@ -128,6 +125,19 @@ get_event_button(SDL_Event *event)
static Uint64 static Uint64
get_axis_motion(SDL_Event *event) get_axis_motion(SDL_Event *event)
{ {
Uint64 key;
int angle = event->caxis.value;
switch (event->caxis.axis) {
case SDL_CONTROLLER_AXIS_TRIGGERRIGHT:
key = KEY_SPACE; break;
case SDL_CONTROLLER_AXIS_LEFTX:
key = angle < 0 ? KEY_LEFT : KEY_RIGHT; break;
case SDL_CONTROLLER_AXIS_LEFTY:
key = angle < 0 ? KEY_UP : KEY_DOWN; break;
default:
key = 0;
}
return key;
} }
static Uint32 static Uint32
@ -210,6 +220,12 @@ input_handle_event(Input *input, SDL_Event *event)
else if (event->type == SDL_CONTROLLERBUTTONUP) { else if (event->type == SDL_CONTROLLERBUTTONUP) {
input->keyState &= ~get_event_button(event); input->keyState &= ~get_event_button(event);
} }
else if (event->type == SDL_CONTROLLERAXISMOTION) {
if (event->caxis.value > 8000 || event->caxis.value < -8000)
input->keyState |= get_axis_motion(event);
else
input->keyState &= ~get_axis_motion(event);
}
} }
bool bool

View File

@ -210,15 +210,16 @@ bool initSDL(void)
return false; return false;
} }
if (SDL_NumJoysticks() > 0) { for (Uint8 i = 0; i < SDL_NumJoysticks(); ++i) {
gController = SDL_JoystickOpen(0); if (!SDL_IsGameController(i))
if (!gController) { continue;
error("Unable to open controller: %s", SDL_GetError());
gController = SDL_GameControllerOpen(i);
if (gController) {
info("Game controller connected");
break;
} }
} }
else {
error("No controller found");
}
mixer_init(); mixer_init();
@ -1119,7 +1120,7 @@ void close(void)
#endif // STEAM_BUILD #endif // STEAM_BUILD
if (gController) if (gController)
SDL_JoystickClose(gController); SDL_GameControllerClose(gController);
SDL_DestroyRenderer(gRenderer); SDL_DestroyRenderer(gRenderer);
SDL_DestroyWindow(gWindow); SDL_DestroyWindow(gWindow);
gWindow = NULL; gWindow = NULL;