SDL: Add controller hat support in-game and in the GUI.

This commit is contained in:
Nadia Pedersen 2018-06-03 00:21:43 +02:00
parent 8721a48206
commit 6f1070cccd
2 changed files with 32 additions and 0 deletions

View File

@ -730,6 +730,23 @@ void run_gui(bool is_running)
else if (event.jbutton.button == SDL_CONTROLLER_BUTTON_DPAD_RIGHT) event.key.keysym.scancode = SDL_SCANCODE_RIGHT;
break;
case SDL_JOYHATMOTION: {
uint8_t value = event.jhat.value;
if (value != 0) {
uint32_t scancode =
value == SDL_HAT_UP ? SDL_SCANCODE_UP
: value == SDL_HAT_DOWN ? SDL_SCANCODE_DOWN
: value == SDL_HAT_LEFT ? SDL_SCANCODE_LEFT
: value == SDL_HAT_RIGHT ? SDL_SCANCODE_RIGHT
: 0;
if (scancode != 0) {
event.type = SDL_KEYDOWN;
event.key.keysym.scancode = scancode;
}
}
}
case SDL_JOYAXISMOTION: {
static bool axis_active[2] = {false, false};
event.jaxis.axis = fix_joypad_axis(event.jaxis.axis);

View File

@ -166,6 +166,21 @@ static void handle_events(GB_gameboy_t *gb)
}
break;
case SDL_JOYHATMOTION:
{
uint8_t value = event.jhat.value;
int8_t updown =
value == SDL_HAT_LEFTUP || value == SDL_HAT_UP || value == SDL_HAT_RIGHTUP ? -1 : (value == SDL_HAT_LEFTDOWN || value == SDL_HAT_DOWN || value == SDL_HAT_RIGHTDOWN ? 1 : 0);
int8_t leftright =
value == SDL_HAT_LEFTUP || value == SDL_HAT_LEFT || value == SDL_HAT_LEFTDOWN ? -1 : (value == SDL_HAT_RIGHTUP || value == SDL_HAT_RIGHT || value == SDL_HAT_RIGHTDOWN ? 1 : 0);
GB_set_key_state(gb, GB_KEY_LEFT, leftright == -1);
GB_set_key_state(gb, GB_KEY_RIGHT, leftright == 1);
GB_set_key_state(gb, GB_KEY_UP, updown == -1);
GB_set_key_state(gb, GB_KEY_DOWN, updown == 1);
break;
};
case SDL_KEYDOWN:
switch (event.key.keysym.scancode) {
case SDL_SCANCODE_ESCAPE: {