From 9003ceba28001928e859f73bfe5b6c7db2c761b6 Mon Sep 17 00:00:00 2001 From: GPF Date: Fri, 17 Jan 2025 14:29:17 -0700 Subject: [PATCH] implemented SDL2 gamecontroller for dreamcast --- src/joystick/SDL_gamecontrollerdb.h | 3 + src/joystick/dreamcast/SDL_sysjoystick.c | 90 +++++++++++++++--------- 2 files changed, 58 insertions(+), 35 deletions(-) diff --git a/src/joystick/SDL_gamecontrollerdb.h b/src/joystick/SDL_gamecontrollerdb.h index f41912599f4c0..07a9f17cbb477 100644 --- a/src/joystick/SDL_gamecontrollerdb.h +++ b/src/joystick/SDL_gamecontrollerdb.h @@ -996,6 +996,9 @@ static const char *s_ControllerMappings[] = { #endif #ifdef SDL_JOYSTICK_N3DS "000000004e696e74656e646f20334400,Nintendo 3DS,crc:3210,a:b0,b:b1,back:b2,dpdown:b7,dpleft:b5,dpright:b4,dpup:b6,leftshoulder:b9,lefttrigger:b14,leftx:a0,lefty:a1,rightshoulder:b8,righttrigger:b15,rightx:a2,righty:a3,start:b3,x:b10,y:b11,", +#endif +#ifdef SDL_JOYSTICK_DREAMCAST + "ff0013db5669727475616c2043007601,Dreamcast Controller,platform:Dreamcast,a:b2,b:b1,x:b6,y:b5,start:b3,dpup:h0.1,dpleft:h0.8,dpdown:h0.4,dpright:h0.2,dpup2:h1.1,dpleft2:h1.8,dpdown2:h1.4,dpright2:h1.2,righttrigger:a2~,lefttrigger:a3~,leftx:a0,lefty:a1,rightx:a4,righty:a5,", #endif "hidapi,*,a:b0,b:b1,back:b4,dpdown:b12,dpleft:b13,dpright:b14,dpup:b11,guide:b5,leftshoulder:b9,leftstick:b7,lefttrigger:a4,leftx:a0,lefty:a1,rightshoulder:b10,rightstick:b8,righttrigger:a5,rightx:a2,righty:a3,start:b6,x:b2,y:b3,", NULL diff --git a/src/joystick/dreamcast/SDL_sysjoystick.c b/src/joystick/dreamcast/SDL_sysjoystick.c index 3849d77da8a92..3b252a7d932bc 100644 --- a/src/joystick/dreamcast/SDL_sysjoystick.c +++ b/src/joystick/dreamcast/SDL_sysjoystick.c @@ -127,9 +127,8 @@ static void DREAMCAST_JoystickSetDevicePlayerIndex(int device_index, int player_ static SDL_JoystickGUID DREAMCAST_JoystickGetDeviceGUID(int device_index) { - SDL_JoystickGUID guid; - SDL_zero(guid); - return guid; + const char *guid_string = "ff0013db5669727475616c2043007601"; // Your Dreamcast GUID + return SDL_JoystickGetGUIDFromString(guid_string); } static SDL_JoystickID DREAMCAST_JoystickGetDeviceInstanceID(int device_index) @@ -204,60 +203,81 @@ static void DREAMCAST_JoystickUpdate(SDL_Joystick *joystick) prev_state = &joystick->hwdata->prev_state; changed = buttons ^ prev_state->buttons; - if (changed & (CONT_DPAD_UP | CONT_DPAD_DOWN | CONT_DPAD_LEFT | - CONT_DPAD_RIGHT)) { + // Process D-Pad + if (changed & (CONT_DPAD_UP | CONT_DPAD_DOWN | CONT_DPAD_LEFT | CONT_DPAD_RIGHT)) { hat = SDL_HAT_CENTERED; - - if (buttons & CONT_DPAD_UP) - hat |= SDL_HAT_UP; - if (buttons & CONT_DPAD_DOWN) - hat |= SDL_HAT_DOWN; - if (buttons & CONT_DPAD_LEFT) - hat |= SDL_HAT_LEFT; - if (buttons & CONT_DPAD_RIGHT) - hat |= SDL_HAT_RIGHT; - + if (buttons & CONT_DPAD_UP) hat |= SDL_HAT_UP; + if (buttons & CONT_DPAD_DOWN) hat |= SDL_HAT_DOWN; + if (buttons & CONT_DPAD_LEFT) hat |= SDL_HAT_LEFT; + if (buttons & CONT_DPAD_RIGHT) hat |= SDL_HAT_RIGHT; SDL_PrivateJoystickHat(joystick, 0, hat); } - if (changed & (CONT_DPAD2_UP | CONT_DPAD2_DOWN | CONT_DPAD2_LEFT | - CONT_DPAD2_RIGHT)) { + if (changed & (CONT_DPAD2_UP | CONT_DPAD2_DOWN | CONT_DPAD2_LEFT | CONT_DPAD2_RIGHT)) { hat = SDL_HAT_CENTERED; - - if (buttons & CONT_DPAD2_UP) - hat |= SDL_HAT_UP; - if (buttons & CONT_DPAD2_DOWN) - hat |= SDL_HAT_DOWN; - if (buttons & CONT_DPAD2_LEFT) - hat |= SDL_HAT_LEFT; - if (buttons & CONT_DPAD2_RIGHT) - hat |= SDL_HAT_RIGHT; - + if (buttons & CONT_DPAD2_UP) hat |= SDL_HAT_UP; + if (buttons & CONT_DPAD2_DOWN) hat |= SDL_HAT_DOWN; + if (buttons & CONT_DPAD2_LEFT) hat |= SDL_HAT_LEFT; + if (buttons & CONT_DPAD2_RIGHT) hat |= SDL_HAT_RIGHT; SDL_PrivateJoystickHat(joystick, 1, hat); } + // Process buttons for (i = 0; i < MAX_BUTTONS; ++i) { if (changed & sdl_buttons[i]) { SDL_PrivateJoystickButton(joystick, i, (buttons & sdl_buttons[i]) ? SDL_PRESSED : SDL_RELEASED); } } + // Scale Joystick Axes + #define DC_JOYSTICK_MAX 128 + #define SDL_JOYSTICK_MAX 32767 + int scale_axis(int value) { + return (value * SDL_JOYSTICK_MAX) / DC_JOYSTICK_MAX; + } + if (state->joyx != prev_state->joyx) - SDL_PrivateJoystickAxis(joystick, 0, state->joyx); + SDL_PrivateJoystickAxis(joystick, 0, scale_axis(state->joyx)); if (state->joyy != prev_state->joyy) - SDL_PrivateJoystickAxis(joystick, 1, state->joyy); - if (state->rtrig != prev_state->rtrig) - SDL_PrivateJoystickAxis(joystick, 2, state->rtrig); - if (state->ltrig != prev_state->ltrig) - SDL_PrivateJoystickAxis(joystick, 3, state->ltrig); + SDL_PrivateJoystickAxis(joystick, 1, scale_axis(state->joyy)); if (state->joy2x != prev_state->joy2x) - SDL_PrivateJoystickAxis(joystick, 4, state->joy2x); + SDL_PrivateJoystickAxis(joystick, 4, scale_axis(state->joy2x)); if (state->joy2y != prev_state->joy2y) - SDL_PrivateJoystickAxis(joystick, 5, state->joy2y); + SDL_PrivateJoystickAxis(joystick, 5, scale_axis(state->joy2y)); + + // Debugging: Print raw trigger values (before normalization) + // printf("Raw rtrig: %d, ltrig: %d\n", state->rtrig, state->ltrig); // Debugging output + +#define DC_TRIGGER_MIN 0 +#define DC_TRIGGER_MAX 255 +#define SDL_TRIGGER_MAX 32767 +int normalize_trigger(int value) { + int normalized = SDL_TRIGGER_MAX; + return (value == DC_TRIGGER_MAX) ? DC_TRIGGER_MIN : normalized; // Ensure we never go negative +} + + int rtrig = normalize_trigger(state->rtrig); + int ltrig = normalize_trigger(state->ltrig); + + // Debugging: Print normalized trigger values + // printf("Normalized rtrig: %d, ltrig: %d\n", rtrig, ltrig); // Debugging output + + // If the trigger state is different, send the updated axis value + if (rtrig != prev_state->rtrig) { + // printf("Updating right trigger: %d\n", rtrig); // Debugging output + SDL_PrivateJoystickAxis(joystick, 2, rtrig); // Right trigger mapped as axis 2 + } + if (ltrig != prev_state->ltrig) { + // printf("Updating left trigger: %d\n", ltrig); // Debugging output + SDL_PrivateJoystickAxis(joystick, 3, ltrig); // Left trigger mapped as axis 3 + } + + // Save the current state to compare on the next frame joystick->hwdata->prev_state = *state; } + static void DREAMCAST_JoystickClose(SDL_Joystick *joystick) { if (joystick->hwdata != NULL) {