From 7c43155a90b2f66b89b102bf5cee66e16a3e1a2f Mon Sep 17 00:00:00 2001 From: Steven Fuller Date: Sat, 7 Sep 2002 03:03:52 +0000 Subject: [PATCH] basic joystick support --- src/avp/win95/usr_io.c | 7 +-- src/fixer.h | 17 +++++++ src/main.c | 100 +++++++++++++++++++++++++++++++++-------- 3 files changed, 100 insertions(+), 24 deletions(-) diff --git a/src/avp/win95/usr_io.c b/src/avp/win95/usr_io.c index 9c8671d..4ba2ec9 100644 --- a/src/avp/win95/usr_io.c +++ b/src/avp/win95/usr_io.c @@ -856,6 +856,7 @@ void ReadPlayerGameInput(STRATEGYBLOCK* sbPtr) switch (AvP.PlayerType) { + default: case I_Marine: { primaryInput = &MarineInputPrimaryConfig; @@ -1351,10 +1352,7 @@ void ReadPlayerGameInput(STRATEGYBLOCK* sbPtr) /* KJL 18:27:34 04/29/97 - joystick control */ if (GotJoystick) { - fprintf(stderr, "ReadPlayerGameInput: GotJoystick\n"); -#if 0 #define JOYSTICK_DEAD_ZONE 12000 - extern int GotJoystick; extern JOYINFOEX JoystickData; extern JOYCAPS JoystickCaps; @@ -1580,7 +1578,7 @@ void ReadPlayerGameInput(STRATEGYBLOCK* sbPtr) } - #if 1 + #if 0 textprint("%d\n%d\n%d\n%d\n%d\n%d\n%d\n%d\n", JoystickData.dwXpos, JoystickData.dwYpos, @@ -1591,7 +1589,6 @@ void ReadPlayerGameInput(STRATEGYBLOCK* sbPtr) JoystickData.dwButtons, JoystickData.dwPOV); #endif -#endif } /* KJL 16:03:06 05/11/97 - Handle map options */ diff --git a/src/fixer.h b/src/fixer.h index df32540..2766cf7 100644 --- a/src/fixer.h +++ b/src/fixer.h @@ -205,6 +205,23 @@ typedef struct DPMSG_DESTROYPLAYERORGROUP } DPMSG_DESTROYPLAYERORGROUP; typedef DPMSG_DESTROYPLAYERORGROUP * LPDPMSG_DESTROYPLAYERORGROUP; +#define JOYCAPS_HASR 1 + +typedef struct JOYINFOEX +{ + int dwXpos; + int dwYpos; + int dwRpos; + int dwUpos; + int dwVpos; + int dwPOV; +} JOYINFOEX; + +typedef struct JOYCAPS +{ + int wCaps; +} JOYCAPS; + #ifdef __cplusplus }; #endif diff --git a/src/main.c b/src/main.c index e89d1db..b04426e 100644 --- a/src/main.c +++ b/src/main.c @@ -58,15 +58,16 @@ extern unsigned char GotAnyKey; extern int NormalFrameTime; SDL_Surface *surface; -/* SDL_Joystick *joy; */ + +SDL_Joystick *joy; +JOYINFOEX JoystickData; +JOYCAPS JoystickCaps; /* defaults */ static int WantFullscreen = 1; int WantSound = 1; static int WantCDRom = 1; -#if 0 static int WantJoystick = 1; -#endif #if 0 /* NVIDIA gl.h breaks this */ #if GL_EXT_secondary_color @@ -104,6 +105,67 @@ void DirectReadMouse() void ReadJoysticks() { + int axes, balls, hats; + Uint8 hat; + + JoystickData.dwXpos = 0; + JoystickData.dwYpos = 0; + JoystickData.dwRpos = 0; + JoystickData.dwUpos = 0; + JoystickData.dwVpos = 0; + JoystickData.dwPOV = -1; + + if (joy == NULL || !GotJoystick) { + return; + } + + SDL_JoystickUpdate(); + + axes = SDL_JoystickNumAxes(joy); + balls = SDL_JoystickNumBalls(joy); + hats = SDL_JoystickNumHats(joy); + + if (axes > 0) { + JoystickData.dwXpos = SDL_JoystickGetAxis(joy, 0) + 32768; + } + if (axes > 1) { + JoystickData.dwYpos = SDL_JoystickGetAxis(joy, 1) + 32768; + } + + if (hats > 0) { + hat = SDL_JoystickGetHat(joy, 0); + + switch (hat) { + default: + case SDL_HAT_CENTERED: + JoystickData.dwPOV = -1; + break; + case SDL_HAT_UP: + JoystickData.dwPOV = 0; + break; + case SDL_HAT_RIGHT: + JoystickData.dwPOV = 9000; + break; + case SDL_HAT_DOWN: + JoystickData.dwPOV = 18000; + break; + case SDL_HAT_LEFT: + JoystickData.dwPOV = 27000; + break; + case SDL_HAT_RIGHTUP: + JoystickData.dwPOV = 4500; + break; + case SDL_HAT_RIGHTDOWN: + JoystickData.dwPOV = 13500; + break; + case SDL_HAT_LEFTUP: + JoystickData.dwPOV = 31500; + break; + case SDL_HAT_LEFTDOWN: + JoystickData.dwPOV = 22500; + break; + } + } } /* ** */ @@ -352,18 +414,27 @@ int InitSDL() LoadDeviceAndVideoModePreferences(); -#if 0 if (WantJoystick) { SDL_Init(SDL_INIT_JOYSTICK); if (SDL_NumJoysticks() > 0) { + /* TODO: make joystick number a configuration parameter */ + joy = SDL_JoystickOpen(0); if (joy) { GotJoystick = 1; } + + JoystickCaps.wCaps = 0; /* no rudder... ? */ + + JoystickData.dwXpos = 0; + JoystickData.dwYpos = 0; + JoystickData.dwRpos = 0; + JoystickData.dwUpos = 0; + JoystickData.dwVpos = 0; + JoystickData.dwPOV = -1; } } -#endif surface = NULL; @@ -544,11 +615,10 @@ int InitialiseWindowsSystem(HANDLE hInstance, int nCmdShow, int WinInitMode) int ExitWindowsSystem() { -#if 0 if (joy) { SDL_JoystickClose(joy); } -#endif + /* SDL_Quit(); */ if (surface) SDL_FreeSurface(surface); @@ -944,12 +1014,6 @@ void CheckForWindowsMessages() MouseVelY = 0; } -/* -This is half of the necessary joystick code, the rest of the changes -involve avp/win95/usr_io.c. I don't own a joystick so I have no idea -how things should be implemented. -*/ -#if 0 if (GotJoystick) { int numbuttons; @@ -970,7 +1034,7 @@ how things should be implemented. } } } -#endif + if ((KeyboardInput[KEY_LEFTALT]||KeyboardInput[KEY_RIGHTALT]) && DebouncedKeyboardInput[KEY_CR]) { SDL_GrabMode gm; @@ -1069,7 +1133,7 @@ static struct option getopt_long_options[] = { { "windowed", 0, NULL, 'w' }, { "nosound", 0, NULL, 's' }, { "nocdrom", 0, NULL, 'c' }, -/* { "nojoy", 0, NULL, 'j' }, */ +{ "nojoy", 0, NULL, 'j' }, { "debug", 0, NULL, 'd' }, /* { "loadrifs", 1, NULL, 'l' }, @@ -1088,7 +1152,7 @@ static const char *usage_string = " [-w | --windowed] Run the game in a window\n" " [-s | --nosound] Do not access the soundcard\n" " [-c | --nocdrom] Do not access the CD-ROM\n" -/* " [-j | --nojoy] Do not access the joystick\n" */ +" [-j | --nojoy] Do not access the joystick\n" ; int main(int argc, char *argv[]) @@ -1116,11 +1180,9 @@ int main(int argc, char *argv[]) case 'c': WantCDRom = 0; break; - /* case 'j': WantJoystick = 0; - break; - */ + break; case 'd': { extern int DebuggingCommandsActive; DebuggingCommandsActive = 1;