2 // gamepad.cpp - Host joystick handling (using SDL)
5 // (C) 2013 Underground Software
7 // JLH = James Hammons <jlhamm@acm.org>
10 // --- ---------- -------------------------------------------------------------
11 // JLH 01/05/2013 Created this file
18 // Class member initialization
19 /*static*/ int Gamepad::numJoysticks = 0;
20 /*static*/ SDL_Joystick * Gamepad::pad[8];
21 /*static*/ int Gamepad::numButtons[8];
22 /*static*/ int Gamepad::numHats[8];
23 /*static*/ bool Gamepad::button[8][256];
24 /*static*/ uint8_t Gamepad::hat[8][32];
27 Gamepad::Gamepad(void)//: numJoysticks(0)
33 Gamepad::~Gamepad(void)
35 DeallocateJoysticks();
39 void Gamepad::AllocateJoysticks(void)
41 // DeallocateJoysticks();
42 numJoysticks = SDL_NumJoysticks();
48 for(int i=0; i<numJoysticks; i++)
50 pad[i] = SDL_JoystickOpen(i);
51 numButtons[i] = numHats[i] = 0;
55 numButtons[i] = SDL_JoystickNumButtons(pad[i]);
56 numHats[i] = SDL_JoystickNumHats(pad[i]);
60 WriteLog("Gamepad: Found %u joystick%s.\n", numJoysticks, (numJoysticks == 1 ? "" : "s"));
64 void Gamepad::DeallocateJoysticks(void)
66 for(int i=0; i<numJoysticks; i++)
67 SDL_JoystickClose(pad[i]);
71 bool Gamepad::GetState(int joystickID, int buttonID)
73 uint8_t hatMask[8] = { 1, 2, 4, 8, 16, 32, 64, 128 };
75 if (buttonID & JOY_BUTTON)
78 int buttonNum = (buttonID & JOY_BUTTON_MASK);
79 // SDL_JoystickGetButton(pad[joystickID]);
80 return button[joystickID][buttonNum];
82 else if (buttonID & JOY_HAT)
85 int hatNumber = (buttonID & JOY_HATNUM_MASK) >> 3;
86 uint8_t hatDirection = hatMask[buttonID & JOY_HATBUT_MASK];
87 // uint8 direction = SDL_JoystickGetHat(pad[joystickID], hatNumber);
89 return (hat[joystickID][hatNumber] & hatDirection ? true : false);
97 int Gamepad::CheckButtonPressed(void)
99 // This translates the hat direction to a mask index.
100 int hatNum[16] = { -1, 0, 1, -1, 2, -1, -1, -1,
101 3, -1, -1, -1, -1, -1, -1, -1 };
103 // Return single button ID being pressed (if any)
104 for(int i=0; i<numJoysticks; i++)
106 for(int j=0; j<numButtons[i]; j++)
109 return (JOY_BUTTON | j);
112 for(int j=0; j<numHats[i]; j++)
115 return (JOY_HAT | hatNum[hat[i][j]]);
123 int Gamepad::GetButtonID(void)
125 // Return single button ID being pressed (if any)
130 int Gamepad::GetJoystickID(void)
132 // Return joystick ID of button being pressed (if any)
137 void Gamepad::Update(void)
139 // SDL_PollEvent(&event);
140 SDL_JoystickUpdate();
142 for(int i=0; i<numJoysticks; i++)
144 for(int j=0; j<numButtons[i]; j++)
145 button[i][j] = SDL_JoystickGetButton(pad[i], j);
147 for(int j=0; j<numHats[i]; j++)
148 hat[i][j] = SDL_JoystickGetHat(pad[i], j);
154 // Need to test this. It may be that the only time joysticks are detected is
155 // when the program is first run. That would suck.
156 void Gamepad::CheckConsistency(void)
158 int currentNumJoysticks = SDL_NumJoysticks();
160 // Check to see if the # of joysticks reported by SDL changed
161 if (currentNumJoysticks == numJoysticks)
164 // Either one or more joysticks were plugged in, or removed. Fix up our
165 // internal states to reflect this.