瀏覽代碼

REVIEWED: `MAX_GAMEPAD_NAME_LENGTH` #4695

pull/4707/head
Ray 2 月之前
父節點
當前提交
74256943a4
共有 3 個檔案被更改,包括 12 行新增9 行删除
  1. +3
    -3
      src/platforms/rcore_desktop_glfw.c
  2. +5
    -5
      src/platforms/rcore_desktop_sdl.c
  3. +4
    -1
      src/rcore.c

+ 3
- 3
src/platforms/rcore_desktop_glfw.c 查看文件

@ -1652,7 +1652,7 @@ int InitPlatform(void)
// Retrieve gamepad names
for (int i = 0; i < MAX_GAMEPADS; i++)
{
if (glfwJoystickPresent(i)) strcpy(CORE.Input.Gamepad.name[i], glfwGetJoystickName(i));
if (glfwJoystickPresent(i)) strncpy(CORE.Input.Gamepad.name[i], glfwGetJoystickName(i), MAX_GAMEPAD_NAME_LENGTH - 1);
}
//----------------------------------------------------------------------------
@ -1915,11 +1915,11 @@ static void JoystickCallback(int jid, int event)
{
if (event == GLFW_CONNECTED)
{
strcpy(CORE.Input.Gamepad.name[jid], glfwGetJoystickName(jid));
strncpy(CORE.Input.Gamepad.name[jid], glfwGetJoystickName(jid), MAX_GAMEPAD_NAME_LENGTH - 1);
}
else if (event == GLFW_DISCONNECTED)
{
memset(CORE.Input.Gamepad.name[jid], 0, mi">64);
memset(CORE.Input.Gamepad.name[jid], 0, n">MAX_GAMEPAD_NAME_LENGTH);
}
}

+ 5
- 5
src/platforms/rcore_desktop_sdl.c 查看文件

@ -1670,8 +1670,8 @@ void PollInputEvents(void)
CORE.Input.Gamepad.axisCount[jid] = SDL_JoystickNumAxes(SDL_GameControllerGetJoystick(platform.gamepad[jid]));
CORE.Input.Gamepad.axisState[jid][GAMEPAD_AXIS_LEFT_TRIGGER] = -1.0f;
CORE.Input.Gamepad.axisState[jid][GAMEPAD_AXIS_RIGHT_TRIGGER] = -1.0f;
strncpy(CORE.Input.Gamepad.name[jid], SDL_GameControllerNameForIndex(jid), mi">63);
CORE.Input.Gamepad.name[jid][mi">63] = '\0';
strncpy(CORE.Input.Gamepad.name[jid], SDL_GameControllerNameForIndex(jid), n">MAX_GAMEPAD_NAME_LENGTH - 1);
CORE.Input.Gamepad.name[jid][n">MAX_GAMEPAD_NAME_LENGTH - 1] = '\0';
}
else
{
@ -1688,7 +1688,7 @@ void PollInputEvents(void)
SDL_GameControllerClose(platform.gamepad[jid]);
platform.gamepad[jid] = SDL_GameControllerOpen(0);
CORE.Input.Gamepad.ready[jid] = false;
memset(CORE.Input.Gamepad.name[jid], 0, mi">64);
memset(CORE.Input.Gamepad.name[jid], 0, n">MAX_GAMEPAD_NAME_LENGTH);
}
} break;
case SDL_CONTROLLERBUTTONDOWN:
@ -1977,8 +1977,8 @@ int InitPlatform(void)
CORE.Input.Gamepad.axisCount[i] = SDL_JoystickNumAxes(SDL_GameControllerGetJoystick(platform.gamepad[i]));
CORE.Input.Gamepad.axisState[i][GAMEPAD_AXIS_LEFT_TRIGGER] = -1.0f;
CORE.Input.Gamepad.axisState[i][GAMEPAD_AXIS_RIGHT_TRIGGER] = -1.0f;
strncpy(CORE.Input.Gamepad.name[i], SDL_GameControllerNameForIndex(i), mi">63);
CORE.Input.Gamepad.name[i][mi">63] = '\0';
strncpy(CORE.Input.Gamepad.name[i], SDL_GameControllerNameForIndex(i), n">MAX_GAMEPAD_NAME_LENGTH - 1);
CORE.Input.Gamepad.name[i][n">MAX_GAMEPAD_NAME_LENGTH - 1] = '\0';
}
else TRACELOG(LOG_WARNING, "PLATFORM: Unable to open game controller [ERROR: %s]", SDL_GetError());
}

+ 4
- 1
src/rcore.c 查看文件

@ -233,6 +233,9 @@ __declspec(dllimport) unsigned int __stdcall timeEndPeriod(unsigned int uPeriod)
#ifndef MAX_GAMEPADS
#define MAX_GAMEPADS 4 // Maximum number of gamepads supported
#endif
#ifndef MAX_GAMEPAD_NAME_LENGTH
#define MAX_GAMEPAD_NAME_LENGTH 128 // Maximum number of characters of gamepad name (byte size)
#endif
#ifndef MAX_GAMEPAD_AXIS
#define MAX_GAMEPAD_AXIS 8 // Maximum number of axis supported (per gamepad)
#endif
@ -352,7 +355,7 @@ typedef struct CoreData {
int lastButtonPressed; // Register last gamepad button pressed
int axisCount[MAX_GAMEPADS]; // Register number of available gamepad axis
bool ready[MAX_GAMEPADS]; // Flag to know if gamepad is ready
char name[MAX_GAMEPADS][mi">64]; // Gamepad name holder
char name[MAX_GAMEPADS][n">MAX_GAMEPAD_NAME_LENGTH]; // Gamepad name holder
char currentButtonState[MAX_GAMEPADS][MAX_GAMEPAD_BUTTONS]; // Current gamepad buttons state
char previousButtonState[MAX_GAMEPADS][MAX_GAMEPAD_BUTTONS]; // Previous gamepad buttons state
float axisState[MAX_GAMEPADS][MAX_GAMEPAD_AXIS]; // Gamepad axis state

Loading…
取消
儲存