mirror of
https://github.com/raysan5/raylib.git
synced 2025-09-06 19:38:15 +00:00
Improved gamepad system
- Support up to 4 gamepads - Unified system between platforms - Corrected some bugs
This commit is contained in:
161
src/core.c
161
src/core.c
@@ -128,12 +128,12 @@
|
|||||||
//#define DEFAULT_GAMEPAD_DEV "/dev/input/eventN"
|
//#define DEFAULT_GAMEPAD_DEV "/dev/input/eventN"
|
||||||
|
|
||||||
#define MOUSE_SENSITIVITY 0.8f
|
#define MOUSE_SENSITIVITY 0.8f
|
||||||
|
|
||||||
#define MAX_GAMEPADS 2 // Max number of gamepads supported
|
|
||||||
#define MAX_GAMEPAD_BUTTONS 11 // Max bumber of buttons supported (per gamepad)
|
|
||||||
#define MAX_GAMEPAD_AXIS 8 // Max number of axis supported (per gamepad)
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#define MAX_GAMEPADS 4 // Max number of gamepads supported
|
||||||
|
#define MAX_GAMEPAD_BUTTONS 11 // Max bumber of buttons supported (per gamepad)
|
||||||
|
#define MAX_GAMEPAD_AXIS 8 // Max number of axis supported (per gamepad)
|
||||||
|
|
||||||
#define RL_LOAD_DEFAULT_FONT // Load default font on window initialization (module: text)
|
#define RL_LOAD_DEFAULT_FONT // Load default font on window initialization (module: text)
|
||||||
|
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
@@ -174,15 +174,11 @@ static int defaultKeyboardMode; // Used to store default keyboar
|
|||||||
// Mouse input variables
|
// Mouse input variables
|
||||||
static int mouseStream = -1; // Mouse device file descriptor
|
static int mouseStream = -1; // Mouse device file descriptor
|
||||||
static bool mouseReady = false; // Flag to know if mouse is ready
|
static bool mouseReady = false; // Flag to know if mouse is ready
|
||||||
pthread_t mouseThreadId; // Mouse reading thread id
|
static pthread_t mouseThreadId; // Mouse reading thread id
|
||||||
|
|
||||||
// Gamepad input variables
|
// Gamepad input variables
|
||||||
static int gamepadStream[MAX_GAMEPADS] = { -1 }; // Gamepad device file descriptor (two gamepads supported)
|
static int gamepadStream[MAX_GAMEPADS] = { -1 };// Gamepad device file descriptor
|
||||||
static bool gamepadReady[MAX_GAMEPADS] = { false }; // Flag to know if gamepad is ready (two gamepads supported)
|
static pthread_t gamepadThreadId; // Gamepad reading thread id
|
||||||
pthread_t gamepadThreadId; // Gamepad reading thread id
|
|
||||||
|
|
||||||
int gamepadButtons[MAX_GAMEPADS][MAX_GAMEPAD_BUTTONS]; // Gamepad buttons state
|
|
||||||
float gamepadAxisValues[MAX_GAMEPADS][MAX_GAMEPAD_AXIS]; // Gamepad axis state
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(PLATFORM_ANDROID) || defined(PLATFORM_RPI)
|
#if defined(PLATFORM_ANDROID) || defined(PLATFORM_RPI)
|
||||||
@@ -207,18 +203,23 @@ static Matrix downscaleView; // Matrix to downscale view (in case
|
|||||||
static const char *windowTitle; // Window text title...
|
static const char *windowTitle; // Window text title...
|
||||||
static bool cursorOnScreen = false; // Tracks if cursor is inside client area
|
static bool cursorOnScreen = false; // Tracks if cursor is inside client area
|
||||||
|
|
||||||
static char previousKeyState[512] = { 0 }; // Required to check if key pressed/released once
|
// Register keyboard states
|
||||||
static char currentKeyState[512] = { 0 }; // Required to check if key pressed/released once
|
static char previousKeyState[512] = { 0 }; // Registers previous frame key state
|
||||||
|
static char currentKeyState[512] = { 0 }; // Registers current frame key state
|
||||||
|
|
||||||
static char previousGamepadState[32] = {0}; // Required to check if gamepad btn pressed/released once
|
// Register mouse states
|
||||||
static char currentGamepadState[32] = {0}; // Required to check if gamepad btn pressed/released once
|
static char previousMouseState[3] = { 0 }; // Registers previous mouse button state
|
||||||
|
static char currentMouseState[3] = { 0 }; // Registers current mouse button state
|
||||||
|
static int previousMouseWheelY = 0; // Registers previous mouse wheel variation
|
||||||
|
static int currentMouseWheelY = 0; // Registers current mouse wheel variation
|
||||||
|
|
||||||
static char previousMouseState[3] = { 0 }; // Required to check if mouse btn pressed/released once
|
// Register gamepads states
|
||||||
static char currentMouseState[3] = { 0 }; // Required to check if mouse btn pressed/released once
|
static bool gamepadReady[MAX_GAMEPADS] = { false }; // Flag to know if gamepad is ready
|
||||||
|
static float gamepadAxisState[MAX_GAMEPADS][MAX_GAMEPAD_AXIS]; // Gamepad axis state
|
||||||
static int previousMouseWheelY = 0; // Required to track mouse wheel variation
|
static char previousGamepadState[MAX_GAMEPADS][MAX_GAMEPAD_BUTTONS] = { 0 };
|
||||||
static int currentMouseWheelY = 0; // Required to track mouse wheel variation
|
static char currentGamepadState[MAX_GAMEPADS][MAX_GAMEPAD_BUTTONS] = { 0 };
|
||||||
|
|
||||||
|
// Keyboard configuration
|
||||||
static int exitKey = KEY_ESCAPE; // Default exit key (ESC)
|
static int exitKey = KEY_ESCAPE; // Default exit key (ESC)
|
||||||
static int lastKeyPressed = -1; // Register last key pressed
|
static int lastKeyPressed = -1; // Register last key pressed
|
||||||
|
|
||||||
@@ -1157,11 +1158,7 @@ bool IsGamepadAvailable(int gamepad)
|
|||||||
{
|
{
|
||||||
bool result = false;
|
bool result = false;
|
||||||
|
|
||||||
#if defined(PLATFORM_RPI)
|
|
||||||
if ((gamepad < MAX_GAMEPADS) && gamepadReady[gamepad]) result = true;
|
if ((gamepad < MAX_GAMEPADS) && gamepadReady[gamepad]) result = true;
|
||||||
#else
|
|
||||||
if (glfwJoystickPresent(gamepad) == 1) result = true;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@@ -1182,19 +1179,10 @@ float GetGamepadAxisMovement(int gamepad, int axis)
|
|||||||
{
|
{
|
||||||
float value = 0;
|
float value = 0;
|
||||||
|
|
||||||
#if defined(PLATFORM_RPI)
|
|
||||||
if ((gamepad < MAX_GAMEPADS) && gamepadReady[gamepad])
|
if ((gamepad < MAX_GAMEPADS) && gamepadReady[gamepad])
|
||||||
{
|
{
|
||||||
if (axis < MAX_GAMEPAD_AXIS) value = gamepadAxisValues[gamepad][axis];
|
if (axis < MAX_GAMEPAD_AXIS) value = gamepadAxisState[gamepad][axis];
|
||||||
}
|
}
|
||||||
#else
|
|
||||||
const float *axes;
|
|
||||||
int axisCount = 0;
|
|
||||||
|
|
||||||
axes = glfwGetJoystickAxes(gamepad, &axisCount);
|
|
||||||
|
|
||||||
if (axis < axisCount) value = axes[axis];
|
|
||||||
#endif
|
|
||||||
|
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
@@ -1204,19 +1192,9 @@ bool IsGamepadButtonPressed(int gamepad, int button)
|
|||||||
{
|
{
|
||||||
bool pressed = false;
|
bool pressed = false;
|
||||||
|
|
||||||
if ((currentGamepadState[button] != previousGamepadState[button]) && (currentGamepadState[button] == 1)) pressed = true;
|
if ((gamepad < MAX_GAMEPADS) && gamepadReady[gamepad] &&
|
||||||
else pressed = false;
|
(currentGamepadState[gamepad][button] != previousGamepadState[gamepad][button]) &&
|
||||||
|
(currentGamepadState[gamepad][button] == 1)) pressed = true;
|
||||||
/*
|
|
||||||
currentGamepadState[button] = IsGamepadButtonDown(gamepad, button);
|
|
||||||
|
|
||||||
if (currentGamepadState[button] != previousGamepadState[button])
|
|
||||||
{
|
|
||||||
if (currentGamepadState[button]) pressed = true;
|
|
||||||
previousGamepadState[button] = currentGamepadState[button];
|
|
||||||
}
|
|
||||||
else pressed = false;
|
|
||||||
*/
|
|
||||||
|
|
||||||
return pressed;
|
return pressed;
|
||||||
}
|
}
|
||||||
@@ -1226,21 +1204,8 @@ bool IsGamepadButtonDown(int gamepad, int button)
|
|||||||
{
|
{
|
||||||
bool result = false;
|
bool result = false;
|
||||||
|
|
||||||
#if defined(PLATFORM_RPI)
|
if ((gamepad < MAX_GAMEPADS) && gamepadReady[gamepad] &&
|
||||||
// Get gamepad buttons information
|
(currentGamepadState[gamepad][button] == 1)) result = true;
|
||||||
if ((gamepad < MAX_GAMEPADS) && gamepadReady[gamepad] && (gamepadButtons[gamepad][button] == 1)) result = true;
|
|
||||||
else result = false;
|
|
||||||
#else
|
|
||||||
const unsigned char *buttons;
|
|
||||||
int buttonsCount;
|
|
||||||
|
|
||||||
buttons = glfwGetJoystickButtons(gamepad, &buttonsCount);
|
|
||||||
|
|
||||||
if ((buttons != NULL) && (buttons[button] == GLFW_PRESS)) result = true;
|
|
||||||
else result = false;
|
|
||||||
|
|
||||||
//result = currentGamepadState[button];
|
|
||||||
#endif
|
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@@ -1250,19 +1215,9 @@ bool IsGamepadButtonReleased(int gamepad, int button)
|
|||||||
{
|
{
|
||||||
bool released = false;
|
bool released = false;
|
||||||
|
|
||||||
currentGamepadState[button] = IsGamepadButtonUp(gamepad, button);
|
if ((gamepad < MAX_GAMEPADS) && gamepadReady[gamepad] &&
|
||||||
|
(currentGamepadState[gamepad][button] != previousGamepadState[gamepad][button]) &&
|
||||||
if ((currentGamepadState[button] != previousGamepadState[button]) && (currentGamepadState[button] == 0)) released = true;
|
(currentGamepadState[gamepad][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;
|
return released;
|
||||||
}
|
}
|
||||||
@@ -1272,19 +1227,8 @@ bool IsGamepadButtonUp(int gamepad, int button)
|
|||||||
{
|
{
|
||||||
bool result = false;
|
bool result = false;
|
||||||
|
|
||||||
#if defined(PLATFORM_RPI)
|
if ((gamepad < MAX_GAMEPADS) && gamepadReady[gamepad] &&
|
||||||
// Get gamepad buttons information
|
(currentGamepadState[gamepad][button] == 0)) result = true;
|
||||||
if ((gamepad < MAX_GAMEPADS) && gamepadReady[gamepad] && (gamepadButtons[gamepad][button] == 0)) result = true;
|
|
||||||
else result = false;
|
|
||||||
#else
|
|
||||||
const unsigned char *buttons;
|
|
||||||
int buttonsCount;
|
|
||||||
|
|
||||||
buttons = glfwGetJoystickButtons(gamepad, &buttonsCount);
|
|
||||||
|
|
||||||
if ((buttons != NULL) && (buttons[button] == GLFW_RELEASE)) result = true;
|
|
||||||
else result = false;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@@ -2008,22 +1952,41 @@ static void PollInputEvents(void)
|
|||||||
previousMouseWheelY = currentMouseWheelY;
|
previousMouseWheelY = currentMouseWheelY;
|
||||||
currentMouseWheelY = 0;
|
currentMouseWheelY = 0;
|
||||||
|
|
||||||
// Register previous gamepad states
|
// Register gamepads buttons events
|
||||||
for (int i = 0; i < 32; i++) previousGamepadState[i] = currentGamepadState[i];
|
for (int i = 0; i < MAX_GAMEPADS; i++)
|
||||||
|
|
||||||
// Get current gamepad state (no callback)
|
|
||||||
if (glfwJoystickPresent(GAMEPAD_PLAYER1))
|
|
||||||
{
|
{
|
||||||
|
if (glfwJoystickPresent(i)) // Check if gamepad is available
|
||||||
|
{
|
||||||
|
gamepadReady[i] = true;
|
||||||
|
|
||||||
|
// Register previous gamepad states
|
||||||
|
for (int k = 0; k < MAX_GAMEPAD_BUTTONS; k++) previousGamepadState[i][k] = currentGamepadState[i][k];
|
||||||
|
|
||||||
|
// Get current gamepad state
|
||||||
|
// NOTE: There is no callback available, so we get it manually
|
||||||
const unsigned char *buttons;
|
const unsigned char *buttons;
|
||||||
int buttonsCount;
|
int buttonsCount;
|
||||||
|
|
||||||
buttons = glfwGetJoystickButtons(GAMEPAD_PLAYER1, &buttonsCount);
|
buttons = glfwGetJoystickButtons(i, &buttonsCount);
|
||||||
|
|
||||||
for (int i = 0; (buttons != NULL) && (buttonsCount < 32) && (i < buttonsCount); i++)
|
for (int k = 0; (buttons != NULL) && (k < buttonsCount) && (buttonsCount < MAX_GAMEPAD_BUTTONS); k++)
|
||||||
{
|
{
|
||||||
if (buttons[i] == GLFW_PRESS) currentGamepadState[i] = true;
|
if (buttons[i] == GLFW_PRESS) currentGamepadState[i][k] = 1;
|
||||||
else currentGamepadState[i] = false;
|
else currentGamepadState[i][k] = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get current axis state
|
||||||
|
const float *axes;
|
||||||
|
int axisCount = 0;
|
||||||
|
|
||||||
|
axes = glfwGetJoystickAxes(i, &axisCount);
|
||||||
|
|
||||||
|
for (int k = 0; (axes != NULL) && (k < axisCount) && (k < MAX_GAMEPAD_AXIS); k++)
|
||||||
|
{
|
||||||
|
gamepadAxisState[i][k] = axes[k];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else gamepadReady[i] = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
glfwPollEvents(); // Register keyboard/mouse events (callbacks)... and window events!
|
glfwPollEvents(); // Register keyboard/mouse events (callbacks)... and window events!
|
||||||
@@ -2854,7 +2817,7 @@ static void *GamepadThread(void *arg)
|
|||||||
if (gamepadEvent.number < MAX_GAMEPAD_BUTTONS)
|
if (gamepadEvent.number < MAX_GAMEPAD_BUTTONS)
|
||||||
{
|
{
|
||||||
// 1 - button pressed, 0 - button released
|
// 1 - button pressed, 0 - button released
|
||||||
gamepadButtons[i][gamepadEvent.number] = (int)gamepadEvent.value;
|
currentGamepadState[i][gamepadEvent.number] = (int)gamepadEvent.value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (gamepadEvent.type == JS_EVENT_AXIS)
|
else if (gamepadEvent.type == JS_EVENT_AXIS)
|
||||||
@@ -2864,7 +2827,7 @@ static void *GamepadThread(void *arg)
|
|||||||
if (gamepadEvent.number < MAX_GAMEPAD_AXIS)
|
if (gamepadEvent.number < MAX_GAMEPAD_AXIS)
|
||||||
{
|
{
|
||||||
// NOTE: Scaling of gamepadEvent.value to get values between -1..1
|
// NOTE: Scaling of gamepadEvent.value to get values between -1..1
|
||||||
gamepadAxisValues[i][gamepadEvent.number] = (float)gamepadEvent.value/32768;
|
gamepadAxisState[i][gamepadEvent.number] = (float)gamepadEvent.value/32768;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -185,8 +185,8 @@
|
|||||||
// Gamepad Number
|
// Gamepad Number
|
||||||
#define GAMEPAD_PLAYER1 0
|
#define GAMEPAD_PLAYER1 0
|
||||||
#define GAMEPAD_PLAYER2 1
|
#define GAMEPAD_PLAYER2 1
|
||||||
#define GAMEPAD_PLAYER3 2 // Not supported
|
#define GAMEPAD_PLAYER3 2
|
||||||
#define GAMEPAD_PLAYER4 3 // Not supported
|
#define GAMEPAD_PLAYER4 3
|
||||||
|
|
||||||
// Gamepad Buttons
|
// Gamepad Buttons
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user