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 return button[joystickID][buttonNum];
81 else if (buttonID & JOY_HAT)
84 int hatNumber = (buttonID & JOY_HATNUM_MASK) >> 3;
85 uint8_t hatDirection = hatMask[buttonID & JOY_HATBUT_MASK];
86 return (hat[joystickID][hatNumber] & hatDirection ? true : false);
94 int Gamepad::CheckButtonPressed(void)
96 // This translates the hat direction to a mask index.
97 int hatNum[16] = { -1, 0, 1, -1, 2, -1, -1, -1,
98 3, -1, -1, -1, -1, -1, -1, -1 };
100 // Return single button ID being pressed (if any)
101 for(int i=0; i<numJoysticks; i++)
103 for(int j=0; j<numButtons[i]; j++)
106 return (JOY_BUTTON | j);
109 for(int j=0; j<numHats[i]; j++)
112 return (JOY_HAT | hatNum[hat[i][j]]);
120 int Gamepad::GetButtonID(void)
122 // Return single button ID being pressed (if any)
127 int Gamepad::GetJoystickID(void)
129 // Return joystick ID of button being pressed (if any)
134 void Gamepad::Update(void)
136 // SDL_PollEvent(&event);
137 SDL_JoystickUpdate();
139 for(int i=0; i<numJoysticks; i++)
141 for(int j=0; j<numButtons[i]; j++)
142 button[i][j] = SDL_JoystickGetButton(pad[i], j);
144 for(int j=0; j<numHats[i]; j++)
145 hat[i][j] = SDL_JoystickGetHat(pad[i], j);
151 // Need to test this. It may be that the only time joysticks are detected is
152 // when the program is first run. That would suck.
153 // Well, it turns out that SDL doesn't do hot plugging. :-(
154 void Gamepad::CheckConsistency(void)
156 int currentNumJoysticks = SDL_NumJoysticks();
158 // Check to see if the # of joysticks reported by SDL changed
159 if (currentNumJoysticks == numJoysticks)
162 // Either one or more joysticks were plugged in, or removed. Fix up our
163 // internal states to reflect this.