Working on better gamepad support

This commit is contained in:
Ray 2016-10-14 00:47:43 +02:00
parent 97e3277d58
commit b3bc4b21d1
2 changed files with 44 additions and 2 deletions

View File

@ -1166,6 +1166,17 @@ bool IsGamepadAvailable(int gamepad)
return result; return result;
} }
// Return gamepad internal name id
const char *GetGamepadName(int gamepad)
{
#if defined(PLATFORM_DESKTOP)
if (glfwJoystickPresent(gamepad) == 1) return glfwGetJoystickName(gamepad);
else return NULL;
#else
return NULL;
#endif
}
// Return axis movement vector for a gamepad // Return axis movement vector for a gamepad
float GetGamepadAxisMovement(int gamepad, int axis) float GetGamepadAxisMovement(int gamepad, int axis)
{ {
@ -1193,6 +1204,10 @@ bool IsGamepadButtonPressed(int gamepad, int button)
{ {
bool pressed = false; bool pressed = false;
if ((currentGamepadState[button] != previousGamepadState[button]) && (currentGamepadState[button] == 1)) pressed = true;
else pressed = false;
/*
currentGamepadState[button] = IsGamepadButtonDown(gamepad, button); currentGamepadState[button] = IsGamepadButtonDown(gamepad, button);
if (currentGamepadState[button] != previousGamepadState[button]) if (currentGamepadState[button] != previousGamepadState[button])
@ -1201,6 +1216,7 @@ bool IsGamepadButtonPressed(int gamepad, int button)
previousGamepadState[button] = currentGamepadState[button]; previousGamepadState[button] = currentGamepadState[button];
} }
else pressed = false; else pressed = false;
*/
return pressed; return pressed;
} }
@ -1222,6 +1238,8 @@ bool IsGamepadButtonDown(int gamepad, int button)
if ((buttons != NULL) && (buttons[button] == GLFW_PRESS)) result = true; if ((buttons != NULL) && (buttons[button] == GLFW_PRESS)) result = true;
else result = false; else result = false;
//result = currentGamepadState[button];
#endif #endif
return result; return result;
@ -1234,12 +1252,17 @@ bool IsGamepadButtonReleased(int gamepad, int button)
currentGamepadState[button] = IsGamepadButtonUp(gamepad, button); currentGamepadState[button] = IsGamepadButtonUp(gamepad, button);
if ((currentGamepadState[button] != previousGamepadState[button]) && (currentGamepadState[button] == 0)) released = true;
else released = false;
/*
if (currentGamepadState[button] != previousGamepadState[button]) if (currentGamepadState[button] != previousGamepadState[button])
{ {
if (currentGamepadState[button]) released = true; if (currentGamepadState[button]) released = true;
previousGamepadState[button] = currentGamepadState[button]; previousGamepadState[button] = currentGamepadState[button];
} }
else released = false; else released = false;
*/
return released; return released;
} }
@ -1985,7 +2008,25 @@ static void PollInputEvents(void)
previousMouseWheelY = currentMouseWheelY; previousMouseWheelY = currentMouseWheelY;
currentMouseWheelY = 0; currentMouseWheelY = 0;
glfwPollEvents(); // Register keyboard/mouse events... and window events! // Register previous gamepad states
for (int i = 0; i < 32; i++) previousGamepadState[i] = currentGamepadState[i];
// Get current gamepad state (no callback)
if (glfwJoystickPresent(GAMEPAD_PLAYER1))
{
const unsigned char *buttons;
int buttonsCount;
buttons = glfwGetJoystickButtons(GAMEPAD_PLAYER1, &buttonsCount);
for (int i = 0; (buttons != NULL) && (buttonsCount < 32) && (i < buttonsCount); i++)
{
if (buttons[i] == GLFW_PRESS) currentGamepadState[i] = true;
else currentGamepadState[i] = false;
}
}
glfwPollEvents(); // Register keyboard/mouse events (callbacks)... and window events!
#endif #endif
#if defined(PLATFORM_ANDROID) #if defined(PLATFORM_ANDROID)

View File

@ -657,6 +657,7 @@ RLAPI int GetKeyPressed(void); // Get latest key
RLAPI void SetExitKey(int key); // Set a custom key to exit program (default is ESC) RLAPI void SetExitKey(int key); // Set a custom key to exit program (default is ESC)
RLAPI bool IsGamepadAvailable(int gamepad); // Detect if a gamepad is available RLAPI bool IsGamepadAvailable(int gamepad); // Detect if a gamepad is available
RLAPI const char *GetGamepadName(int gamepad); // Return gamepad internal name id
RLAPI float GetGamepadAxisMovement(int gamepad, int axis); // Return axis movement value for a gamepad axis RLAPI float GetGamepadAxisMovement(int gamepad, int axis); // Return axis movement value for a gamepad axis
RLAPI bool IsGamepadButtonPressed(int gamepad, int button); // Detect if a gamepad button has been pressed once RLAPI bool IsGamepadButtonPressed(int gamepad, int button); // Detect if a gamepad button has been pressed once
RLAPI bool IsGamepadButtonDown(int gamepad, int button); // Detect if a gamepad button is being pressed RLAPI bool IsGamepadButtonDown(int gamepad, int button); // Detect if a gamepad button is being pressed