Skip to content

Commit

Permalink
added option 'Analog Stick for Directions'
Browse files Browse the repository at this point in the history
  • Loading branch information
soarqin committed Nov 23, 2019
1 parent 6973bc1 commit 2a1c600
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/drivers/dingux-sdl/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 37 additions & 0 deletions src/drivers/dingux-sdl/dingoo-joystick.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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);
}

Expand All @@ -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);
}
26 changes: 23 additions & 3 deletions src/drivers/dingux-sdl/gui/control_settings.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Externals
extern Config *g_config;

#define CONTROL_MENUSIZE 6
#define CONTROL_MENUSIZE 7

/* MENU COMMANDS */

Expand Down Expand Up @@ -54,13 +54,26 @@ 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]);
g_config->setOption("SDL.Input.GamePad.0B", DefaultGamePad[0][1]);
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 */
Expand All @@ -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},
};

Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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);
Expand Down
55 changes: 54 additions & 1 deletion src/drivers/dingux-sdl/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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).
*/
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -727,6 +769,7 @@ static void UpdateGamepad(void) {
int x;
int wg;
int merge;
int use_analog;

rapid ^= 1;

Expand Down Expand Up @@ -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<<x) ) == (0xC0<<x) ) JS&=~(0xC0<<x);
Expand Down

0 comments on commit 2a1c600

Please sign in to comment.