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 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
float GetGamepadAxisMovement(int gamepad, int axis)
{
@ -1192,7 +1203,11 @@ float GetGamepadAxisMovement(int gamepad, int axis)
bool IsGamepadButtonPressed(int gamepad, int button)
{
bool pressed = false;
if ((currentGamepadState[button] != previousGamepadState[button]) && (currentGamepadState[button] == 1)) pressed = true;
else pressed = false;
/*
currentGamepadState[button] = IsGamepadButtonDown(gamepad, button);
if (currentGamepadState[button] != previousGamepadState[button])
@ -1201,6 +1216,7 @@ bool IsGamepadButtonPressed(int gamepad, int button)
previousGamepadState[button] = currentGamepadState[button];
}
else pressed = false;
*/
return pressed;
}
@ -1222,6 +1238,8 @@ bool IsGamepadButtonDown(int gamepad, int button)
if ((buttons != NULL) && (buttons[button] == GLFW_PRESS)) result = true;
else result = false;
//result = currentGamepadState[button];
#endif
return result;
@ -1233,14 +1251,19 @@ bool IsGamepadButtonReleased(int gamepad, int button)
bool released = false;
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]) released = true;
previousGamepadState[button] = currentGamepadState[button];
}
else released = false;
*/
return released;
}
@ -1984,8 +2007,26 @@ static void PollInputEvents(void)
previousMouseWheelY = currentMouseWheelY;
currentMouseWheelY = 0;
// 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;
glfwPollEvents(); // Register keyboard/mouse events... and window events!
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
#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 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 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