From 2a1c6003fc4e454b81da1aea9413abd62ff81b4a Mon Sep 17 00:00:00 2001 From: Soar Qin Date: Sun, 27 Oct 2019 00:53:54 +0800 Subject: [PATCH] added option 'Analog Stick for Directions' --- src/drivers/dingux-sdl/config.cpp | 1 + src/drivers/dingux-sdl/dingoo-joystick.cpp | 37 +++++++++++++ .../dingux-sdl/gui/control_settings.cpp | 26 ++++++++- src/drivers/dingux-sdl/input.cpp | 55 ++++++++++++++++++- 4 files changed, 115 insertions(+), 4 deletions(-) diff --git a/src/drivers/dingux-sdl/config.cpp b/src/drivers/dingux-sdl/config.cpp index 44f7c98de..584f441e9 100644 --- a/src/drivers/dingux-sdl/config.cpp +++ b/src/drivers/dingux-sdl/config.cpp @@ -189,6 +189,7 @@ Config * InitConfig() { config->addOption("input3", "SDL.Input.2", "Gamepad.2"); config->addOption("input4", "SDL.Input.3", "Gamepad.3"); config->addOption("mergecontrols", "SDL.MergeControls", 0); + config->addOption("analogstick", "SDL.AnalogStick", 0); // allow for input configuration // NOT SUPPORTED diff --git a/src/drivers/dingux-sdl/dingoo-joystick.cpp b/src/drivers/dingux-sdl/dingoo-joystick.cpp index 59aec23a5..0d3590412 100644 --- a/src/drivers/dingux-sdl/dingoo-joystick.cpp +++ b/src/drivers/dingux-sdl/dingoo-joystick.cpp @@ -29,6 +29,11 @@ #include "dingoo.h" +#define MAX_JOYSTICKS 1 +static SDL_Joystick *s_Joysticks[MAX_JOYSTICKS] = {NULL}; + +static int s_jinited = 0; + /** * Tests if the given button is active on the joystick. */ @@ -42,6 +47,19 @@ int DTestButtonJoy(ButtConfig *bc) */ int KillJoysticks() { + int n; // joystick index + + if(!s_jinited) { + return -1; + } + + for(n = 0; n < MAX_JOYSTICKS; n++) { + if (s_Joysticks[n] != 0) { + SDL_JoystickClose(s_Joysticks[n]); + } + s_Joysticks[n]=0; + } + SDL_QuitSubSystem(SDL_INIT_JOYSTICK); return(0); } @@ -50,5 +68,24 @@ int KillJoysticks() */ int InitJoysticks() { + int n; /* joystick index */ + int total; + + SDL_InitSubSystem(SDL_INIT_JOYSTICK); + + total = SDL_NumJoysticks(); + if(total>MAX_JOYSTICKS) { + total = MAX_JOYSTICKS; + } + + for(n = 0; n < total; n++) { + /* Open the joystick under SDL. */ + s_Joysticks[n] = SDL_JoystickOpen(n); + //printf("Could not open joystick %d: %s.\n", + //joy[n] - 1, SDL_GetError()); + continue; + } + SDL_JoystickEventState(SDL_ENABLE); + s_jinited = 1; return(1); } diff --git a/src/drivers/dingux-sdl/gui/control_settings.cpp b/src/drivers/dingux-sdl/gui/control_settings.cpp index 7ffba90a5..a33544bbd 100644 --- a/src/drivers/dingux-sdl/gui/control_settings.cpp +++ b/src/drivers/dingux-sdl/gui/control_settings.cpp @@ -1,7 +1,7 @@ // Externals extern Config *g_config; -#define CONTROL_MENUSIZE 6 +#define CONTROL_MENUSIZE 7 /* MENU COMMANDS */ @@ -54,6 +54,18 @@ static void MergeControls(unsigned long key) g_config->setOption("SDL.MergeControls", val); } +static void setAnalogStick(unsigned long key) +{ + int val; + + if (key == DINGOO_RIGHT) + val = 1; + if (key == DINGOO_LEFT) + val = 0; + + g_config->setOption("SDL.AnalogStick", val); +} + static void resetMappings(unsigned long key) { g_config->setOption("SDL.Input.GamePad.0A", DefaultGamePad[0][0]); @@ -61,6 +73,7 @@ static void resetMappings(unsigned long key) g_config->setOption("SDL.Input.GamePad.0TurboA", DefaultGamePad[0][8]); g_config->setOption("SDL.Input.GamePad.0TurboB", DefaultGamePad[0][9]); g_config->setOption("SDL.MergeControls", 0); + g_config->setOption("SDL.AnalogStick", 0); UpdateInput(g_config); } /* CONTROL SETTING MENU */ @@ -72,6 +85,7 @@ static SettingEntry cm_menu[] = {"Turbo B", "Map input for Turbo B", "SDL.Input.GamePad.0TurboB", setTurboB}, {"Turbo A", "Map input for Turbo A", "SDL.Input.GamePad.0TurboA", setTurboA}, {"Merge P1/P2", "Control both players at once", "SDL.MergeControls", MergeControls}, + {"Analog Stick", "Analog Stick for Directions", "SDL.AnalogStick", setAnalogStick}, {"Reset defaults", "Reset default control mappings", "", resetMappings}, }; @@ -146,13 +160,13 @@ int RunControlSettings() } if (parsekey(DINGOO_LEFT, 1)) { - if (index == 4) { + if (index == 4 || index == 5) { cm_menu[index].update(g_key); } } if (parsekey(DINGOO_RIGHT, 1)) { - if (index == 4) { + if (index == 4 || index == 5) { cm_menu[index].update(g_key); } } @@ -201,6 +215,12 @@ int RunControlSettings() if (i == CONTROL_MENUSIZE-1) sprintf(cBtn, "%s", ""); else if (i == CONTROL_MENUSIZE-2) + { + int mergeValue; + g_config->getOption("SDL.AnalogStick", &mergeValue); + sprintf(cBtn, "%s", mergeValue ? "on" : "off"); + } + else if (i == CONTROL_MENUSIZE-3) { int mergeValue; g_config->getOption("SDL.MergeControls", &mergeValue); diff --git a/src/drivers/dingux-sdl/input.cpp b/src/drivers/dingux-sdl/input.cpp index bfb9f5ff9..564c3f4f8 100644 --- a/src/drivers/dingux-sdl/input.cpp +++ b/src/drivers/dingux-sdl/input.cpp @@ -62,6 +62,18 @@ static bool MenuRequested = false; static int frameAdvanceKey = 0; +static unsigned short analog1 = 0; + +#define joy_commit_range 3276 +enum +{ + ANALOG_UP = 1, + ANALOG_DOWN = 2, + ANALOG_LEFT = 4, + ANALOG_RIGHT = 8 +}; + + /** * Necessary for proper GUI functioning (configuring when a game isn't loaded). */ @@ -300,9 +312,10 @@ static void KeyboardCommands() { if (_keyonly(DINGOO_L2) || MenuRequested) { SilenceSound(1); - MenuRequested = false; FCEUGUI_Run(); SilenceSound(0); + analog1 = 0; + MenuRequested = false; return; } @@ -657,6 +670,35 @@ static void UpdatePhysicalInput() // Keep a record of it. MenuRequested = true; break; + case SDL_JOYAXISMOTION: + { + int axisval = event.jaxis.value; + if (event.jaxis.axis == 0) + {// X axis + analog1 &= ~(ANALOG_LEFT | ANALOG_RIGHT); + if (axisval > joy_commit_range) + { + analog1 |= ANALOG_RIGHT; + } + else if (axisval < -joy_commit_range) + { + analog1 |= ANALOG_LEFT; + } + } + else if (event.jaxis.axis == 1) + {// Y axis + analog1 &= ~(ANALOG_UP | ANALOG_DOWN); + if (axisval > joy_commit_range) + { + analog1 |= ANALOG_DOWN; + } + else if (axisval < -joy_commit_range) + { + analog1 |= ANALOG_UP; + } + } + break; + } default: // do nothing break; @@ -727,6 +769,7 @@ static void UpdateGamepad(void) { int x; int wg; int merge; + int use_analog; rapid ^= 1; @@ -759,6 +802,16 @@ static void UpdateGamepad(void) { } } + // add analog direction + g_config->getOption("SDL.AnalogStick", &use_analog); + if (use_analog) + { + if (analog1 & ANALOG_UP) JS |= (1 << 4); + if (analog1 & ANALOG_DOWN) JS |= (1 << 5); + if (analog1 & ANALOG_LEFT) JS |= (1 << 6); + if (analog1 & ANALOG_RIGHT) JS |= (1 << 7); + } + // for(x=0;x<32;x+=8) { /* Now, test to see if anything weird(up+down at same time) // is happening, and correct */ // if((JS & (0xC0<