Skip to content

Commit

Permalink
Use SDL2 GameController instead of Joystick where possible, use moder…
Browse files Browse the repository at this point in the history
…n FPS mapping for control
  • Loading branch information
lazd committed Oct 17, 2018
1 parent c567654 commit 2ea4cd4
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 30 deletions.
52 changes: 41 additions & 11 deletions id_in.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ static KeyboardDef KbdDefs = {
};

static SDL_Joystick *Joystick;
static SDL_GameController *GameController;
int JoyNumButtons;
static int JoyNumHats;

Expand Down Expand Up @@ -143,21 +144,22 @@ INL_GetMouseButtons(void)
// joystick (from +/-127)
//
///////////////////////////////////////////////////////////////////////////
void IN_GetJoyDelta(int *dx,int *dy)
void IN_GetJoyDelta(int *dx,int *dy, SDL_GameControllerAxis xaxis, SDL_GameControllerAxis yaxis)
{
if(!Joystick)
if(!GameController)
{
*dx = *dy = 0;
return;
}

SDL_JoystickUpdate();
SDL_GameControllerUpdate();
#ifdef _arch_dreamcast
int x = 0;
int y = 0;
#else
int x = SDL_JoystickGetAxis(Joystick, 0) >> 8;
int y = SDL_JoystickGetAxis(Joystick, 1) >> 8;
int x = SDL_GameControllerGetAxis(GameController, xaxis) >> 8;
int y = SDL_GameControllerGetAxis(GameController, yaxis) >> 8;
#endif

if(param_joystickhat != -1)
Expand All @@ -179,6 +181,10 @@ void IN_GetJoyDelta(int *dx,int *dy)
else if(y > 127) y = 127;
}

// Apply expo
x = (int) (pow(x / 127.0, JOYEXPO) * (x > 0 ? 127 : -127));
y = (int) (pow(y / 127.0, JOYEXPO) * (y > 0 ? 127 : -127));

*dx = x;
*dy = y;
}
Expand All @@ -199,8 +205,9 @@ void IN_GetJoyFineDelta(int *dx, int *dy)
}

SDL_JoystickUpdate();
int x = SDL_JoystickGetAxis(Joystick, 0);
int y = SDL_JoystickGetAxis(Joystick, 1);
SDL_GameControllerUpdate();
int x = SDL_GameControllerGetAxis(GameController, SDL_CONTROLLER_AXIS_LEFTX);
int y = SDL_GameControllerGetAxis(GameController, SDL_CONTROLLER_AXIS_LEFTY);

if(x < -128) x = -128;
else if(x > 127) x = 127;
Expand All @@ -222,19 +229,33 @@ void IN_GetJoyFineDelta(int *dx, int *dy)

int IN_JoyButtons()
{
if(!Joystick) return 0;
if(!GameController) return 0;

SDL_JoystickUpdate();
SDL_GameControllerUpdate();

// Read triggers in as buttons
int leftTrigger = (SDL_GameControllerGetAxis(GameController, SDL_CONTROLLER_AXIS_TRIGGERLEFT) >> 8) > JOYDEADZONE;
int rightTrigger = (SDL_GameControllerGetAxis(GameController, SDL_CONTROLLER_AXIS_TRIGGERRIGHT) >> 8) > JOYDEADZONE;

// Read in each button in the order we expect to define it
int res = 0;
for(int i = 0; i < JoyNumButtons && i < 32; i++)
res |= SDL_JoystickGetButton(Joystick, i) << i;
res |= (SDL_GameControllerGetButton(GameController, SDL_CONTROLLER_BUTTON_A) || rightTrigger) << 0;
res |= (SDL_GameControllerGetButton(GameController, SDL_CONTROLLER_BUTTON_B) || leftTrigger) << 1;
res |= SDL_GameControllerGetButton(GameController, SDL_CONTROLLER_BUTTON_X) << 2;
res |= SDL_GameControllerGetButton(GameController, SDL_CONTROLLER_BUTTON_Y) << 3;
res |= SDL_GameControllerGetButton(GameController, SDL_CONTROLLER_BUTTON_LEFTSHOULDER) << 4;
res |= SDL_GameControllerGetButton(GameController, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER) << 5;
res |= SDL_GameControllerGetButton(GameController, SDL_CONTROLLER_BUTTON_LEFTSTICK) << 6;
res |= SDL_GameControllerGetButton(GameController, SDL_CONTROLLER_BUTTON_RIGHTSTICK) << 7;
res |= SDL_GameControllerGetButton(GameController, SDL_CONTROLLER_BUTTON_BACK) << 8;
res |= SDL_GameControllerGetButton(GameController, SDL_CONTROLLER_BUTTON_START) << 9;
res |= SDL_GameControllerGetButton(GameController, SDL_CONTROLLER_BUTTON_GUIDE) << 10;
return res;
}

boolean IN_JoyPresent()
{
return Joystick != NULL;
return GameController != NULL;
}

static void processEvent(SDL_Event *event)
Expand Down Expand Up @@ -397,6 +418,8 @@ IN_Startup(void)
if(param_joystickindex >= 0 && param_joystickindex < SDL_NumJoysticks())
{
Joystick = SDL_JoystickOpen(param_joystickindex);
GameController = SDL_GameControllerOpen(param_joystickindex);

if(Joystick)
{
JoyNumButtons = SDL_JoystickNumButtons(Joystick);
Expand Down Expand Up @@ -441,6 +464,9 @@ IN_Shutdown(void)
if(Joystick)
SDL_JoystickClose(Joystick);

if(GameController)
SDL_GameControllerClose(GameController);

IN_Started = false;
}

Expand Down Expand Up @@ -512,6 +538,10 @@ IN_ReadControl(int player,ControlInfo *info)
info->button1 = (buttons & (1 << 1)) != 0;
info->button2 = (buttons & (1 << 2)) != 0;
info->button3 = (buttons & (1 << 3)) != 0;
// info->button0 = (buttons & (1 << 0)) != 0 || SDL_GameControllerGetButton(GameController, SDL_CONTROLLER_BUTTON_A);
// info->button1 = (buttons & (1 << 1)) != 0 || SDL_GameControllerGetButton(GameController, SDL_CONTROLLER_BUTTON_B);
// info->button2 = (buttons & (1 << 2)) != 0 || SDL_GameControllerGetButton(GameController, SDL_CONTROLLER_BUTTON_X);
// info->button3 = (buttons & (1 << 3)) != 0 || SDL_GameControllerGetButton(GameController, SDL_CONTROLLER_BUTTON_Y);
info->dir = DirTable[((my + 1) * 3) + (mx + 1)];
}

Expand Down
2 changes: 1 addition & 1 deletion id_in.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ int IN_MouseButtons (void);
boolean IN_JoyPresent();
void IN_SetJoyCurrent(int joyIndex);
int IN_JoyButtons (void);
void IN_GetJoyDelta(int *dx,int *dy);
void IN_GetJoyDelta(int *dx,int *dy, SDL_GameControllerAxis xaxis, SDL_GameControllerAxis yaxis);
void IN_GetJoyFineDelta(int *dx, int *dy);

void IN_StartAck(void);
Expand Down
18 changes: 12 additions & 6 deletions wl_agent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,32 +166,38 @@ void ControlMovement (objtype *ob)
int32_t oldx,oldy;
int angle;
int angleunits;
float joyfactor = 1;

thrustspeed = 0;

oldx = player->x;
oldy = player->y;

if(buttonstate[bt_strafeleft])
int joyx, joyy;
IN_GetJoyDelta (&joyx, &joyy, SDL_CONTROLLER_AXIS_LEFTX, SDL_CONTROLLER_AXIS_RIGHTY);

if(buttonstate[bt_strafeleft] || joyx < -JOYDEADZONE)
{
float joyfactor = abs(joyx) / 127.0;
angle = ob->angle + ANGLES/4;
if(angle >= ANGLES)
angle -= ANGLES;
if(buttonstate[bt_run])
Thrust(angle, RUNMOVE * MOVESCALE * tics);
Thrust(angle, (int32_t) (joyfactor * RUNMOVE * MOVESCALE * tics));
else
Thrust(angle, BASEMOVE * MOVESCALE * tics);
Thrust(angle, (int32_t) (joyfactor * BASEMOVE * MOVESCALE * tics));
}

if(buttonstate[bt_straferight])
if(buttonstate[bt_straferight] || joyx > JOYDEADZONE)
{
float joyfactor = abs(joyx) / 127.0;
angle = ob->angle - ANGLES/4;
if(angle < 0)
angle += ANGLES;
if(buttonstate[bt_run])
Thrust(angle, RUNMOVE * MOVESCALE * tics );
Thrust(angle, (int32_t) (joyfactor * RUNMOVE * MOVESCALE * tics));
else
Thrust(angle, BASEMOVE * MOVESCALE * tics);
Thrust(angle, (int32_t) (joyfactor * BASEMOVE * MOVESCALE * tics));
}

//
Expand Down
2 changes: 2 additions & 0 deletions wl_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -1002,6 +1002,8 @@ void UpdateSoundLoc(void);
#define RUNTURN 70

#define JOYSCALE 2
#define JOYEXPO 2
#define JOYDEADZONE 12

extern byte tilemap[MAPSIZE][MAPSIZE]; // wall values only
extern byte spotvis[MAPSIZE][MAPSIZE];
Expand Down
2 changes: 1 addition & 1 deletion wl_menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3787,7 +3787,7 @@ ReadAnyControl (ControlInfo * ci)
{
int jx, jy, jb;

IN_GetJoyDelta (&jx, &jy);
IN_GetJoyDelta (&jx, &jy, SDL_CONTROLLER_AXIS_LEFTX, SDL_CONTROLLER_AXIS_LEFTY);
if (jy < -SENSITIVE)
ci->dir = dir_North;
else if (jy > SENSITIVE)
Expand Down
32 changes: 21 additions & 11 deletions wl_play.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ int buttonjoy[32] = {
bt_attack, bt_strafe, bt_use, bt_run, bt_esc, bt_prevweapon, bt_nobutton, bt_nextweapon,
bt_pause, bt_strafeleft, bt_straferight, bt_nobutton, bt_nobutton, bt_nobutton, bt_nobutton, bt_nobutton,
#else
bt_attack, bt_strafe, bt_use, bt_run, bt_strafeleft, bt_straferight, bt_esc, bt_pause,
bt_prevweapon, bt_nextweapon, bt_nobutton, bt_nobutton, bt_nobutton, bt_nobutton, bt_nobutton, bt_nobutton,
bt_attack, bt_use, bt_strafe, bt_run, bt_prevweapon, bt_nextweapon, bt_run, bt_run,
bt_pause, bt_esc, bt_esc, bt_nobutton, bt_nobutton, bt_nobutton, bt_nobutton, bt_nobutton,
#endif
bt_nobutton, bt_nobutton, bt_nobutton, bt_nobutton, bt_nobutton, bt_nobutton, bt_nobutton, bt_nobutton,
bt_nobutton, bt_nobutton, bt_nobutton, bt_nobutton, bt_nobutton, bt_nobutton, bt_nobutton, bt_nobutton
Expand Down Expand Up @@ -364,18 +364,28 @@ void PollJoystickMove (void)
{
int joyx, joyy;

IN_GetJoyDelta (&joyx, &joyy);
IN_GetJoyDelta (&joyx, &joyy, SDL_CONTROLLER_AXIS_RIGHTX, SDL_CONTROLLER_AXIS_LEFTY);

int delta = buttonstate[bt_run] ? RUNMOVE * tics : BASEMOVE * tics;

if (joyx > 64 || buttonstate[bt_turnright])
controlx += delta;
else if (joyx < -64 || buttonstate[bt_turnleft])
controlx -= delta;
if (joyy > 64 || buttonstate[bt_movebackward])
controly += delta;
else if (joyy < -64 || buttonstate[bt_moveforward])
controly -= delta;
int newcontrolx = delta;
int newcontroly = delta;

if (joyx != 0) {
newcontrolx = (int) ((abs(joyx) / 127.0) * delta);
}
if (joyy != 0) {
newcontroly = (int) ((abs(joyy) / 127.0) * delta);
}

if (joyx > JOYDEADZONE || buttonstate[bt_turnright])
controlx += newcontrolx;
else if (joyx < -JOYDEADZONE || buttonstate[bt_turnleft])
controlx -= newcontrolx;
if (joyy > JOYDEADZONE || buttonstate[bt_movebackward])
controly += newcontroly;
else if (joyy < -JOYDEADZONE || buttonstate[bt_moveforward])
controly -= newcontroly;
}

/*
Expand Down

0 comments on commit 2ea4cd4

Please sign in to comment.