Skip to content

Commit 2ea4cd4

Browse files
committed
Use SDL2 GameController instead of Joystick where possible, use modern FPS mapping for control
1 parent c567654 commit 2ea4cd4

6 files changed

Lines changed: 78 additions & 30 deletions

File tree

id_in.cpp

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ static KeyboardDef KbdDefs = {
5757
};
5858

5959
static SDL_Joystick *Joystick;
60+
static SDL_GameController *GameController;
6061
int JoyNumButtons;
6162
static int JoyNumHats;
6263

@@ -143,21 +144,22 @@ INL_GetMouseButtons(void)
143144
// joystick (from +/-127)
144145
//
145146
///////////////////////////////////////////////////////////////////////////
146-
void IN_GetJoyDelta(int *dx,int *dy)
147+
void IN_GetJoyDelta(int *dx,int *dy, SDL_GameControllerAxis xaxis, SDL_GameControllerAxis yaxis)
147148
{
148-
if(!Joystick)
149+
if(!GameController)
149150
{
150151
*dx = *dy = 0;
151152
return;
152153
}
153154

154155
SDL_JoystickUpdate();
156+
SDL_GameControllerUpdate();
155157
#ifdef _arch_dreamcast
156158
int x = 0;
157159
int y = 0;
158160
#else
159-
int x = SDL_JoystickGetAxis(Joystick, 0) >> 8;
160-
int y = SDL_JoystickGetAxis(Joystick, 1) >> 8;
161+
int x = SDL_GameControllerGetAxis(GameController, xaxis) >> 8;
162+
int y = SDL_GameControllerGetAxis(GameController, yaxis) >> 8;
161163
#endif
162164

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

184+
// Apply expo
185+
x = (int) (pow(x / 127.0, JOYEXPO) * (x > 0 ? 127 : -127));
186+
y = (int) (pow(y / 127.0, JOYEXPO) * (y > 0 ? 127 : -127));
187+
182188
*dx = x;
183189
*dy = y;
184190
}
@@ -199,8 +205,9 @@ void IN_GetJoyFineDelta(int *dx, int *dy)
199205
}
200206

201207
SDL_JoystickUpdate();
202-
int x = SDL_JoystickGetAxis(Joystick, 0);
203-
int y = SDL_JoystickGetAxis(Joystick, 1);
208+
SDL_GameControllerUpdate();
209+
int x = SDL_GameControllerGetAxis(GameController, SDL_CONTROLLER_AXIS_LEFTX);
210+
int y = SDL_GameControllerGetAxis(GameController, SDL_CONTROLLER_AXIS_LEFTY);
204211

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

223230
int IN_JoyButtons()
224231
{
225-
if(!Joystick) return 0;
232+
if(!GameController) return 0;
226233

227-
SDL_JoystickUpdate();
234+
SDL_GameControllerUpdate();
228235

236+
// Read triggers in as buttons
237+
int leftTrigger = (SDL_GameControllerGetAxis(GameController, SDL_CONTROLLER_AXIS_TRIGGERLEFT) >> 8) > JOYDEADZONE;
238+
int rightTrigger = (SDL_GameControllerGetAxis(GameController, SDL_CONTROLLER_AXIS_TRIGGERRIGHT) >> 8) > JOYDEADZONE;
239+
240+
// Read in each button in the order we expect to define it
229241
int res = 0;
230-
for(int i = 0; i < JoyNumButtons && i < 32; i++)
231-
res |= SDL_JoystickGetButton(Joystick, i) << i;
242+
res |= (SDL_GameControllerGetButton(GameController, SDL_CONTROLLER_BUTTON_A) || rightTrigger) << 0;
243+
res |= (SDL_GameControllerGetButton(GameController, SDL_CONTROLLER_BUTTON_B) || leftTrigger) << 1;
244+
res |= SDL_GameControllerGetButton(GameController, SDL_CONTROLLER_BUTTON_X) << 2;
245+
res |= SDL_GameControllerGetButton(GameController, SDL_CONTROLLER_BUTTON_Y) << 3;
246+
res |= SDL_GameControllerGetButton(GameController, SDL_CONTROLLER_BUTTON_LEFTSHOULDER) << 4;
247+
res |= SDL_GameControllerGetButton(GameController, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER) << 5;
248+
res |= SDL_GameControllerGetButton(GameController, SDL_CONTROLLER_BUTTON_LEFTSTICK) << 6;
249+
res |= SDL_GameControllerGetButton(GameController, SDL_CONTROLLER_BUTTON_RIGHTSTICK) << 7;
250+
res |= SDL_GameControllerGetButton(GameController, SDL_CONTROLLER_BUTTON_BACK) << 8;
251+
res |= SDL_GameControllerGetButton(GameController, SDL_CONTROLLER_BUTTON_START) << 9;
252+
res |= SDL_GameControllerGetButton(GameController, SDL_CONTROLLER_BUTTON_GUIDE) << 10;
232253
return res;
233254
}
234255

235256
boolean IN_JoyPresent()
236257
{
237-
return Joystick != NULL;
258+
return GameController != NULL;
238259
}
239260

240261
static void processEvent(SDL_Event *event)
@@ -397,6 +418,8 @@ IN_Startup(void)
397418
if(param_joystickindex >= 0 && param_joystickindex < SDL_NumJoysticks())
398419
{
399420
Joystick = SDL_JoystickOpen(param_joystickindex);
421+
GameController = SDL_GameControllerOpen(param_joystickindex);
422+
400423
if(Joystick)
401424
{
402425
JoyNumButtons = SDL_JoystickNumButtons(Joystick);
@@ -441,6 +464,9 @@ IN_Shutdown(void)
441464
if(Joystick)
442465
SDL_JoystickClose(Joystick);
443466

467+
if(GameController)
468+
SDL_GameControllerClose(GameController);
469+
444470
IN_Started = false;
445471
}
446472

@@ -512,6 +538,10 @@ IN_ReadControl(int player,ControlInfo *info)
512538
info->button1 = (buttons & (1 << 1)) != 0;
513539
info->button2 = (buttons & (1 << 2)) != 0;
514540
info->button3 = (buttons & (1 << 3)) != 0;
541+
// info->button0 = (buttons & (1 << 0)) != 0 || SDL_GameControllerGetButton(GameController, SDL_CONTROLLER_BUTTON_A);
542+
// info->button1 = (buttons & (1 << 1)) != 0 || SDL_GameControllerGetButton(GameController, SDL_CONTROLLER_BUTTON_B);
543+
// info->button2 = (buttons & (1 << 2)) != 0 || SDL_GameControllerGetButton(GameController, SDL_CONTROLLER_BUTTON_X);
544+
// info->button3 = (buttons & (1 << 3)) != 0 || SDL_GameControllerGetButton(GameController, SDL_CONTROLLER_BUTTON_Y);
515545
info->dir = DirTable[((my + 1) * 3) + (mx + 1)];
516546
}
517547

id_in.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ int IN_MouseButtons (void);
173173
boolean IN_JoyPresent();
174174
void IN_SetJoyCurrent(int joyIndex);
175175
int IN_JoyButtons (void);
176-
void IN_GetJoyDelta(int *dx,int *dy);
176+
void IN_GetJoyDelta(int *dx,int *dy, SDL_GameControllerAxis xaxis, SDL_GameControllerAxis yaxis);
177177
void IN_GetJoyFineDelta(int *dx, int *dy);
178178

179179
void IN_StartAck(void);

wl_agent.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -166,32 +166,38 @@ void ControlMovement (objtype *ob)
166166
int32_t oldx,oldy;
167167
int angle;
168168
int angleunits;
169+
float joyfactor = 1;
169170

170171
thrustspeed = 0;
171172

172173
oldx = player->x;
173174
oldy = player->y;
174175

175-
if(buttonstate[bt_strafeleft])
176+
int joyx, joyy;
177+
IN_GetJoyDelta (&joyx, &joyy, SDL_CONTROLLER_AXIS_LEFTX, SDL_CONTROLLER_AXIS_RIGHTY);
178+
179+
if(buttonstate[bt_strafeleft] || joyx < -JOYDEADZONE)
176180
{
181+
float joyfactor = abs(joyx) / 127.0;
177182
angle = ob->angle + ANGLES/4;
178183
if(angle >= ANGLES)
179184
angle -= ANGLES;
180185
if(buttonstate[bt_run])
181-
Thrust(angle, RUNMOVE * MOVESCALE * tics);
186+
Thrust(angle, (int32_t) (joyfactor * RUNMOVE * MOVESCALE * tics));
182187
else
183-
Thrust(angle, BASEMOVE * MOVESCALE * tics);
188+
Thrust(angle, (int32_t) (joyfactor * BASEMOVE * MOVESCALE * tics));
184189
}
185190

186-
if(buttonstate[bt_straferight])
191+
if(buttonstate[bt_straferight] || joyx > JOYDEADZONE)
187192
{
193+
float joyfactor = abs(joyx) / 127.0;
188194
angle = ob->angle - ANGLES/4;
189195
if(angle < 0)
190196
angle += ANGLES;
191197
if(buttonstate[bt_run])
192-
Thrust(angle, RUNMOVE * MOVESCALE * tics );
198+
Thrust(angle, (int32_t) (joyfactor * RUNMOVE * MOVESCALE * tics));
193199
else
194-
Thrust(angle, BASEMOVE * MOVESCALE * tics);
200+
Thrust(angle, (int32_t) (joyfactor * BASEMOVE * MOVESCALE * tics));
195201
}
196202

197203
//

wl_def.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,6 +1002,8 @@ void UpdateSoundLoc(void);
10021002
#define RUNTURN 70
10031003

10041004
#define JOYSCALE 2
1005+
#define JOYEXPO 2
1006+
#define JOYDEADZONE 12
10051007

10061008
extern byte tilemap[MAPSIZE][MAPSIZE]; // wall values only
10071009
extern byte spotvis[MAPSIZE][MAPSIZE];

wl_menu.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3787,7 +3787,7 @@ ReadAnyControl (ControlInfo * ci)
37873787
{
37883788
int jx, jy, jb;
37893789

3790-
IN_GetJoyDelta (&jx, &jy);
3790+
IN_GetJoyDelta (&jx, &jy, SDL_CONTROLLER_AXIS_LEFTX, SDL_CONTROLLER_AXIS_LEFTY);
37913791
if (jy < -SENSITIVE)
37923792
ci->dir = dir_North;
37933793
else if (jy > SENSITIVE)

wl_play.cpp

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ int buttonjoy[32] = {
5959
bt_attack, bt_strafe, bt_use, bt_run, bt_esc, bt_prevweapon, bt_nobutton, bt_nextweapon,
6060
bt_pause, bt_strafeleft, bt_straferight, bt_nobutton, bt_nobutton, bt_nobutton, bt_nobutton, bt_nobutton,
6161
#else
62-
bt_attack, bt_strafe, bt_use, bt_run, bt_strafeleft, bt_straferight, bt_esc, bt_pause,
63-
bt_prevweapon, bt_nextweapon, bt_nobutton, bt_nobutton, bt_nobutton, bt_nobutton, bt_nobutton, bt_nobutton,
62+
bt_attack, bt_use, bt_strafe, bt_run, bt_prevweapon, bt_nextweapon, bt_run, bt_run,
63+
bt_pause, bt_esc, bt_esc, bt_nobutton, bt_nobutton, bt_nobutton, bt_nobutton, bt_nobutton,
6464
#endif
6565
bt_nobutton, bt_nobutton, bt_nobutton, bt_nobutton, bt_nobutton, bt_nobutton, bt_nobutton, bt_nobutton,
6666
bt_nobutton, bt_nobutton, bt_nobutton, bt_nobutton, bt_nobutton, bt_nobutton, bt_nobutton, bt_nobutton
@@ -364,18 +364,28 @@ void PollJoystickMove (void)
364364
{
365365
int joyx, joyy;
366366

367-
IN_GetJoyDelta (&joyx, &joyy);
367+
IN_GetJoyDelta (&joyx, &joyy, SDL_CONTROLLER_AXIS_RIGHTX, SDL_CONTROLLER_AXIS_LEFTY);
368368

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

371-
if (joyx > 64 || buttonstate[bt_turnright])
372-
controlx += delta;
373-
else if (joyx < -64 || buttonstate[bt_turnleft])
374-
controlx -= delta;
375-
if (joyy > 64 || buttonstate[bt_movebackward])
376-
controly += delta;
377-
else if (joyy < -64 || buttonstate[bt_moveforward])
378-
controly -= delta;
371+
int newcontrolx = delta;
372+
int newcontroly = delta;
373+
374+
if (joyx != 0) {
375+
newcontrolx = (int) ((abs(joyx) / 127.0) * delta);
376+
}
377+
if (joyy != 0) {
378+
newcontroly = (int) ((abs(joyy) / 127.0) * delta);
379+
}
380+
381+
if (joyx > JOYDEADZONE || buttonstate[bt_turnright])
382+
controlx += newcontrolx;
383+
else if (joyx < -JOYDEADZONE || buttonstate[bt_turnleft])
384+
controlx -= newcontrolx;
385+
if (joyy > JOYDEADZONE || buttonstate[bt_movebackward])
386+
controly += newcontroly;
387+
else if (joyy < -JOYDEADZONE || buttonstate[bt_moveforward])
388+
controly -= newcontroly;
379389
}
380390

381391
/*

0 commit comments

Comments
 (0)