Skip to content

Commit

Permalink
Handle L and R buttons in code
Browse files Browse the repository at this point in the history
  • Loading branch information
azihassan committed Oct 6, 2024
1 parent dc4e01c commit 72c7e30
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 14 deletions.
15 changes: 3 additions & 12 deletions CMake/platforms/dreamcast.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,6 @@ list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/threads-stub")
list(APPEND DEVILUTIONX_PLATFORM_COMPILE_DEFINITIONS __DREAMCAST__)
add_compile_options(-fpermissive)

#SDL Joystick axis mapping (circle-pad/C-stick)
set(JOY_AXIS_LEFTX 11)
set(JOY_AXIS_LEFTY 9)
set(JOY_AXIS_RIGHTX 12)
set(JOY_AXIS_RIGHTY 10)

#SDL Joystick hat mapping (D-pad)
set(JOY_HAT_DPAD_UP_HAT 0)
set(JOY_HAT_DPAD_RIGHT_HAT 0)
Expand All @@ -39,17 +33,14 @@ set(JOY_HAT_DPAD_UP 1)
set(JOY_HAT_DPAD_RIGHT 2)
set(JOY_HAT_DPAD_DOWN 4)
set(JOY_HAT_DPAD_LEFT 8)
#SDL Joystick button mapping (A / B and X / Y inverted)

#SDL Joystick button mapping
set(JOY_BUTTON_A 2)
set(JOY_BUTTON_B 1)
set(JOY_BUTTON_X 5)
set(JOY_BUTTON_Y 6)
#set(JOY_BUTTON_LEFTSHOULDER 5)
#set(JOY_BUTTON_RIGHTSHOULDER 6)
set(JOY_BUTTON_BACK 7)

set(JOY_BUTTON_START 3)
set(JOY_BUTTON_TRIGGERLEFT 8)
set(JOY_BUTTON_TRIGGERRIGHT 9)

#GPF SDL files
set(SDL_INCLUDE_DIR /usr/include/SDL/)
Expand Down
48 changes: 46 additions & 2 deletions Source/controls/devices/joystick.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ StaticVector<ControllerButtonEvent, 4> Joystick::ToControllerButtonEvents(const
return { ControllerButtonEvent { ControllerButton_BUTTON_RIGHTSTICK, up } };
#endif
#ifdef JOY_BUTTON_LEFTSHOULDER
case JOY_BUTTON_LEFTSHOULDER:
case JOY_BUTTON_LEFTSHOULDER: {
Log("ToControllerButtonEvents JOY_BUTTON_LEFTSHOULDER pressed");
return { ControllerButtonEvent { ControllerButton_BUTTON_LEFTSHOULDER, up } };
}
#endif
#ifdef JOY_BUTTON_RIGHTSHOULDER
case JOY_BUTTON_RIGHTSHOULDER:
Expand Down Expand Up @@ -101,6 +103,20 @@ StaticVector<ControllerButtonEvent, 4> Joystick::ToControllerButtonEvents(const
}
case SDL_JOYAXISMOTION:
case SDL_JOYBALLMOTION:
#ifdef __DREAMCAST__
if(event.jaxis.axis == 3) {
Log("BUTTON_LEFTSHOULDER detected");
Log("event.jbutton.button = {}", event.jbutton.button);
Log("event.jbutton.state == SDL_RELEASED = {}", event.jbutton.state == SDL_RELEASED);
return { ControllerButtonEvent { ControllerButton_BUTTON_LEFTSHOULDER, event.jaxis.value < 255 } };
}
if(event.jaxis.axis == 2) {
Log("BUTTON_RIGHTSHOULDER detected");
Log("event.jbutton.button = {}", event.jbutton.button);
Log("event.jbutton.state == SDL_RELEASED = {}", event.jbutton.state == SDL_RELEASED);
return { ControllerButtonEvent { ControllerButton_BUTTON_RIGHTSHOULDER, event.jaxis.value < 255 } };
}
#endif
// ProcessAxisMotion() requires a ControllerButtonEvent parameter
// so provide one here using ControllerButton_NONE
return { ControllerButtonEvent { ControllerButton_NONE, false } };
Expand Down Expand Up @@ -211,8 +227,10 @@ int Joystick::ToSdlJoyButton(ControllerButton button)
return JOY_BUTTON_RIGHTSTICK;
#endif
#ifdef JOY_BUTTON_LEFTSHOULDER
case ControllerButton_BUTTON_LEFTSHOULDER:
case ControllerButton_BUTTON_LEFTSHOULDER: {
Log("ToSdlJoyButton JOY_BUTTON_LEFTSHOULDER pressed");
return JOY_BUTTON_LEFTSHOULDER;
}
#endif
#ifdef JOY_BUTTON_RIGHTSHOULDER
case ControllerButton_BUTTON_RIGHTSHOULDER:
Expand Down Expand Up @@ -292,6 +310,31 @@ bool Joystick::IsPressed(ControllerButton button) const
return joyButton < numButtons && SDL_JoystickGetButton(sdl_joystick_, joyButton) != 0;
}

#ifdef __DREAMCAST__
bool Joystick::ProcessAxisMotion(const SDL_Event &event)
{
if (event.type != SDL_JOYAXISMOTION)
return false;

Log("ProcessAxisMotion event.jaxis.axis = {}", event.jaxis.axis);
Log("ProcessAxisMotion event.jaxis.value = {}", event.jaxis.value);
Log("ProcessAxisMotion event.jbutton.button = {}", event.jbutton.button);
Log("event.jbutton.state == SDL_RELEASED = {}", event.jbutton.state == SDL_RELEASED);

switch (event.jaxis.axis) {
case 0: //horizontal
leftStickXUnscaled = event.jaxis.value;
leftStickNeedsScaling = true;
return true;
case 1: //vertical
leftStickYUnscaled = event.jaxis.value;
leftStickNeedsScaling = true;
return true;
default:
return false;
}
}
#else //!ifdef __DREAMCAST__
bool Joystick::ProcessAxisMotion(const SDL_Event &event)
{
if (event.type != SDL_JOYAXISMOTION)
Expand Down Expand Up @@ -330,6 +373,7 @@ bool Joystick::ProcessAxisMotion(const SDL_Event &event)
return false;
#endif
}
#endif

void Joystick::Add(int deviceIndex)
{
Expand Down

0 comments on commit 72c7e30

Please sign in to comment.