3232#include < string.h>
3333#include " debug.h"
3434
35+ // SDL Gamepad
36+ #include < SDL_gamecontroller.h>
3537
3638// general definitions
3739#include " general.h"
6668// maximum number of joystick devices we support ------------------------------
6769//
6870#define MAX_JOYSTICK_DEVICES 4
71+ #define NUM_IL_JOY_INT_COMMANDS CALC_NUM_ARRAY_ENTRIES ( il_joy_int_commands )
6972
7073// external variables ---------------------------------------------------------
7174//
@@ -83,6 +86,8 @@ extern joystate_s JoyState; // generic joystick data
8386//
8487int isdl_joyOutput; // When enabled output joy button states to console
8588SDL_Joystick* isdl_joyHandle; // handle to the open joystick
89+ SDL_GameController* isdl_controllerHandle; // Handle for Game Controller type
90+ SDL_JoystickGUID isdl_controllerGUID;
8691byte isdl_NumAxes; // number of axes for this joystick
8792byte isdl_NumButtons; // number of buttons for this joystick
8893int isdl_FireGun;
@@ -129,7 +134,7 @@ void ISDL_JoyInitHandler()
129134 isdl_nJoystickFound = SDL_NumJoysticks ();
130135 MSGOUT (" isdl_joy: Found %d joysticks.\n " , isdl_nJoystickFound);
131136 if (isdl_nJoystickFound > 0 )
132- {
137+ {
133138 // TODO: Implement support for more than one stick when we have more flexible input layer stuff
134139 MSGOUT (" isdl_joy: ... but we don't care, because Parsec only has one JoyState\n " );
135140 isdl_joyHandle = SDL_JoystickOpen (0 );
@@ -139,14 +144,51 @@ void ISDL_JoyInitHandler()
139144 MSGOUT (" isdl_joy: Joystick%d has %d axes and %d buttons\n " , 0 , isdl_NumAxes, isdl_NumButtons);
140145 QueryJoystick = TRUE ;
141146 JoystickDisabled = FALSE ;
142- }
147+ if (!SDL_GameControllerAddMappingsFromFile (" gamecontrollerdb.txt" )) MSGOUT (" FAILED TO LOAD CONTROLLER DATABASE" ); // Load game controller mapping database
148+ else {
149+ MSGOUT (" LOADED CONTROLLER DATABASE" );
150+ isdl_controllerHandle = SDL_GameControllerOpen (0 ); // Open Game controller object
151+ isdl_controllerGUID = SDL_JoystickGetDeviceGUID (0 );
152+ MSGOUT (" Device mapping in system: %s" , SDL_GameControllerMapping (isdl_controllerHandle)); // Log mapping for debugging
153+ if (SDL_GameControllerMapping (isdl_controllerHandle)) { // If a mapping exists proceed to map controller
154+ SDL_GameControllerAddMapping (SDL_GameControllerMappingForGUID (isdl_controllerGUID));
155+ // Default mappings for fire, missle, accel, decel, next gun, next missle and dpad based on controller mapping
156+ if (SDL_GameControllerHasButton (isdl_controllerHandle,SDL_CONTROLLER_BUTTON_A))
157+ isdl_FireGun = SDL_GameControllerGetBindForButton (isdl_controllerHandle,SDL_CONTROLLER_BUTTON_A).value .button ; // A for shoot
158+ if (SDL_GameControllerHasButton (isdl_controllerHandle,SDL_CONTROLLER_BUTTON_B))
159+ isdl_FireMissile = SDL_GameControllerGetBindForButton (isdl_controllerHandle,SDL_CONTROLLER_BUTTON_B).value .button ; // B for missile
160+ if (SDL_GameControllerHasButton (isdl_controllerHandle,SDL_CONTROLLER_BUTTON_X))
161+ isdl_Accelerate = SDL_GameControllerGetBindForButton (isdl_controllerHandle,SDL_CONTROLLER_BUTTON_X).value .button ; // X for accelerate
162+ if (SDL_GameControllerHasButton (isdl_controllerHandle,SDL_CONTROLLER_BUTTON_Y))
163+ isdl_Deccelerate = SDL_GameControllerGetBindForButton (isdl_controllerHandle,SDL_CONTROLLER_BUTTON_Y).value .button ; // Y for Deccelerate
164+ if (SDL_GameControllerHasButton (isdl_controllerHandle,SDL_CONTROLLER_BUTTON_LEFTSHOULDER))
165+ isdl_NextGun = SDL_GameControllerGetBindForButton (isdl_controllerHandle,SDL_CONTROLLER_BUTTON_LEFTSHOULDER).value .button ; // Select gun
166+ if (SDL_GameControllerHasButton (isdl_controllerHandle,SDL_CONTROLLER_BUTTON_RIGHTSHOULDER))
167+ isdl_NextMissile = SDL_GameControllerGetBindForButton (isdl_controllerHandle,SDL_CONTROLLER_BUTTON_RIGHTSHOULDER).value .button ; // Select Missile
168+ if (SDL_GameControllerHasButton (isdl_controllerHandle,SDL_CONTROLLER_BUTTON_BACK))
169+ isdl_Target = SDL_GameControllerGetBindForButton (isdl_controllerHandle,SDL_CONTROLLER_BUTTON_BACK).value .button ; // Target somethng
170+ if (SDL_GameControllerHasButton (isdl_controllerHandle,SDL_CONTROLLER_BUTTON_START))
171+ isdl_Emp = SDL_GameControllerGetBindForButton (isdl_controllerHandle,SDL_CONTROLLER_BUTTON_START).value .button ; // Activate EMP
172+ /* if(SDL_GameControllerHasButton(isdl_controllerHandle,SDL_CONTROLLER_BUTTON_DPAD_UP))
173+ isdl_Dup = SDL_GameControllerGetBindForButton(isdl_controllerHandle,SDL_CONTROLLER_BUTTON_DPAD_UP).value.button;
174+ if(SDL_GameControllerHasButton(isdl_controllerHandle,SDL_CONTROLLER_BUTTON_DPAD_DOWN))
175+ isdl_Ddown = SDL_GameControllerGetBindForButton(isdl_controllerHandle,SDL_CONTROLLER_BUTTON_DPAD_DOWN).value.button;
176+ if(SDL_GameControllerHasButton(isdl_controllerHandle,SDL_CONTROLLER_BUTTON_DPAD_LEFT))
177+ isdl_Dleft = SDL_GameControllerGetBindForButton(isdl_controllerHandle,SDL_CONTROLLER_BUTTON_DPAD_LEFT).value.button;
178+ if(SDL_GameControllerHasButton(isdl_controllerHandle,SDL_CONTROLLER_BUTTON_DPAD_RIGHT))
179+ isdl_Dright = SDL_GameControllerGetBindForButton(isdl_controllerHandle,SDL_CONTROLLER_BUTTON_DPAD_RIGHT).value.button;
180+ */
181+ }
182+ }
183+ }
143184}
144185
145186
146187// close joystick device ------------------------------------------------------
147188//
148189void ISDL_JoyKillHandler ()
149190{
191+ SDL_GameControllerClose (isdl_controllerHandle);
150192 SDL_JoystickClose (isdl_joyHandle);
151193 return ;
152194 /*
@@ -228,6 +270,7 @@ int ISDL_ApplyDZ(int value, int axis)
228270//
229271void ISDL_JoyCollect ()
230272{
273+ SDL_GameControllerButtonBind gcDebug;
231274 if ( !QueryJoystick )
232275 return ;
233276
@@ -273,8 +316,12 @@ void ISDL_JoyCollect()
273316 ASSERT ( KeyAdditional->size <= KEY_ADDITIONS_MAX );
274317 for (int button = 0 ; button < isdl_NumButtons; button++) {
275318 JoyState.Buttons [button] = SDL_JoystickGetButton (isdl_joyHandle, button);
276- if (isdl_joyOutput && JoyState.Buttons [button])
319+ if (isdl_joyOutput && JoyState.Buttons [button]) {
277320 MSGOUT (" Joy button %d pressed" ,button);
321+ gcDebug = SDL_GameControllerGetBindForButton (isdl_controllerHandle,SDL_CONTROLLER_BUTTON_X);
322+ MSGOUT (" Game Controller bind %d" ,SDL_GameControllerGetBindForButton (isdl_controllerHandle,SDL_CONTROLLER_BUTTON_X).value .button );
323+
324+ }
278325
279326#ifdef JOY_AKC_SUPPORT
280327 if ( _OldJoyState.Buttons [button] != JoyState.Buttons [button]) {
@@ -321,14 +368,14 @@ int_command_s il_joy_int_commands[] = {
321368 { 0x01 , " isdl.deadzone_max_axis_2" , 0 , 32767 , &isdl_joyDeadZone_Max[2 ] , NULL , NULL },
322369 { 0x01 , " isdl.deadzone_max_axis_3" , 0 , 32767 , &isdl_joyDeadZone_Max[3 ] , NULL , NULL },
323370 { 0x01 , " isdl.showjoy_input" , 0 , 1 , &isdl_joyOutput , NULL , NULL ,0 }, // default off
324- { 0x01 , " isdl.jbind_gun" , -1 , 64 ,&isdl_FireGun , NULL , NULL ,2 }, // button 2 default
325- { 0x01 , " isdl.jbind_missile" , -1 , 64 ,&isdl_FireMissile , NULL , NULL ,1 }, // button 1 default
326- { 0x01 , " isdl.jbind_accel" , -1 , 64 ,&isdl_Accelerate , NULL , NULL ,0 }, // button 0 default
327- { 0x01 , " isdl.jbind_decel" , -1 , 64 ,&isdl_Deccelerate , NULL , NULL ,3 }, // button 3 default
371+ { 0x01 , " isdl.jbind_gun" , -1 , 64 ,&isdl_FireGun , NULL , NULL ,- 1 }, // button 2 default
372+ { 0x01 , " isdl.jbind_missile" , -1 , 64 ,&isdl_FireMissile , NULL , NULL ,- 1 }, // button 1 default
373+ { 0x01 , " isdl.jbind_accel" , -1 , 64 ,&isdl_Accelerate , NULL , NULL ,- 1 }, // button 0 default
374+ { 0x01 , " isdl.jbind_decel" , -1 , 64 ,&isdl_Deccelerate , NULL , NULL ,- 1 }, // button 3 default
328375 { 0x01 , " isdl.jbind_rollleft" , -1 , 64 ,&isdl_Rollleft , NULL , NULL ,-1 }, // disabled until bound
329376 { 0x01 , " isdl.jbind_rollright" , -1 , 64 ,&isdl_RollRight , NULL , NULL ,-1 },
330- { 0x01 , " isdl.jbind_nextgun" , -1 , 64 ,&isdl_NextGun , NULL , NULL ,4 }, // button 4 default
331- { 0x01 , " isdl.jbind_nextmissile" , -1 , 64 ,&isdl_NextMissile , NULL , NULL ,5 }, // button 5 default
377+ { 0x01 , " isdl.jbind_nextgun" , -1 , 64 ,&isdl_NextGun , NULL , NULL ,- 1 }, // button 4 default
378+ { 0x01 , " isdl.jbind_nextmissile" , -1 , 64 ,&isdl_NextMissile , NULL , NULL ,- 1 }, // button 5 default
332379 { 0x01 , " isdl.analogthrottle" , 0 , 1 , &isdl_RudderToggle , NULL , NULL ,0 }, // default off
333380 { 0x01 , " isdl.analogrudder" , 0 , 1 , &isdl_ThrottleToggle , NULL , NULL ,0 },
334381 { 0x01 , " isdl.jbind_aburn" , -1 , 64 ,&isdl_Aburn , NULL , NULL ,-1 },
@@ -353,9 +400,6 @@ int_command_s il_joy_int_commands[] = {
353400
354401};
355402
356- #define NUM_IL_JOY_INT_COMMANDS CALC_NUM_ARRAY_ENTRIES ( il_joy_int_commands )
357-
358-
359403// module registration function -----------------------------------------------
360404//
361405REGISTER_MODULE ( IL_JOY )
0 commit comments