First gamepad stuff

This commit is contained in:
Reece Mackie 2019-04-27 20:49:33 +01:00
parent f37e55a77b
commit b911cefab3
3 changed files with 162 additions and 24 deletions

View File

@ -167,12 +167,13 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ProjectReference Include="..\raylib\raylib.vcxproj">
<Project>{e89d61ac-55de-4482-afd4-df7242ebc859}</Project>
</ProjectReference>
<ClCompile Include="..\..\..\examples\core\core_basic_window.cpp" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\..\examples\core\core_basic_window.cpp" />
<ProjectReference Include="..\raylib\raylib.vcxproj">
<Project>{e89d61ac-55de-4482-afd4-df7242ebc859}</Project>
<UseLibraryDependencyInputs>true</UseLibraryDependencyInputs>
</ProjectReference>
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">

View File

@ -3050,6 +3050,86 @@ static bool GetMouseButtonStatus(int button)
#endif
}
static GamepadButton GetGamepadButton(int button)
{
GamepadButton b = GAMEPAD_BUTTON_UNKNOWN;
#if defined(PLATFORM_DESKTOP)
switch (button)
{
case GLFW_GAMEPAD_BUTTON_Y: b = GAMEPAD_BUTTON_RIGHT_FACE_UP; break;
case GLFW_GAMEPAD_BUTTON_B: b = GAMEPAD_BUTTON_RIGHT_FACE_RIGHT; break;
case GLFW_GAMEPAD_BUTTON_A: b = GAMEPAD_BUTTON_RIGHT_FACE_DOWN; break;
case GLFW_GAMEPAD_BUTTON_X: b = GAMEPAD_BUTTON_RIGHT_FACE_LEFT; break;
case GLFW_GAMEPAD_BUTTON_LEFT_BUMPER: b = GAMEPAD_BUTTON_LEFT_TRIGGER_1; break;
case GLFW_GAMEPAD_BUTTON_RIGHT_BUMPER: b = GAMEPAD_BUTTON_RIGHT_TRIGGER_1; break;
case GLFW_GAMEPAD_BUTTON_BACK: b = GAMEPAD_BUTTON_MIDDLE_LEFT; break;
case GLFW_GAMEPAD_BUTTON_GUIDE: b = GAMEPAD_BUTTON_MIDDLE; break;
case GLFW_GAMEPAD_BUTTON_START: b = GAMEPAD_BUTTON_MIDDLE_RIGHT; break;
case GLFW_GAMEPAD_BUTTON_DPAD_UP: b = GAMEPAD_BUTTON_LEFT_FACE_UP; break;
case GLFW_GAMEPAD_BUTTON_DPAD_RIGHT: b = GAMEPAD_BUTTON_LEFT_FACE_RIGHT; break;
case GLFW_GAMEPAD_BUTTON_DPAD_DOWN: b = GAMEPAD_BUTTON_LEFT_FACE_DOWN; break;
case GLFW_GAMEPAD_BUTTON_DPAD_LEFT: b = GAMEPAD_BUTTON_LEFT_FACE_LEFT; break;
case GLFW_GAMEPAD_BUTTON_LEFT_THUMB: b = GAMEPAD_BUTTON_LEFT_THUMB; break;
case GLFW_GAMEPAD_BUTTON_RIGHT_THUMB: b = GAMEPAD_BUTTON_RIGHT_THUMB; break;
}
#endif
#if defined(PLATFORM_WEB)
//TODO: TEST
//https://www.w3.org/TR/gamepad/#gamepad-interface
switch (button)
{
case 0: b = GAMEPAD_BUTTON_RIGHT_FACE_DOWN; break;
case 1: b = GAMEPAD_BUTTON_RIGHT_FACE_RIGHT; break;
case 2: b = GAMEPAD_BUTTON_RIGHT_FACE_LEFT; break;
case 3: b = GAMEPAD_BUTTON_RIGHT_FACE_UP; break;
case 4: b = GAMEPAD_BUTTON_LEFT_TRIGGER_1; break;
case 5: b = GAMEPAD_BUTTON_RIGHT_TRIGGER_1; break;
case 6: b = GAMEPAD_BUTTON_LEFT_TRIGGER_2; break;
case 7: b = GAMEPAD_BUTTON_RIGHT_TRIGGER_2; break;
case 8: b = GAMEPAD_BUTTON_MIDDLE_LEFT; break;
case 9: b = GAMEPAD_BUTTON_MIDDLE_RIGHT; break;
case 10: b = GAMEPAD_BUTTON_LEFT_THUMB; break;
case 11: b = GAMEPAD_BUTTON_RIGHT_THUMB; break;
case 12: b = GAMEPAD_BUTTON_LEFT_FACE_UP; break;
case 13: b = GAMEPAD_BUTTON_LEFT_FACE_DOWN; break;
case 14: b = GAMEPAD_BUTTON_LEFT_FACE_LEFT; break;
case 15: b = GAMEPAD_BUTTON_LEFT_FACE_RIGHT; break;
}
#endif
return b;
}
static GamepadAxis GetGamepadAxis(int axis)
{
GamepadAxis a = GAMEPAD_AXIS_UNKNOWN;
#if defined(PLATFORM_DESKTOP)
switch(axis)
{
case GLFW_GAMEPAD_AXIS_LEFT_X: a = GAMEPAD_AXIS_LEFT_X; break;
case GLFW_GAMEPAD_AXIS_LEFT_Y: a = GAMEPAD_AXIS_LEFT_Y; break;
case GLFW_GAMEPAD_AXIS_RIGHT_X: a = GAMEPAD_AXIS_RIGHT_X; break;
case GLFW_GAMEPAD_AXIS_RIGHT_Y: a = GAMEPAD_AXIS_RIGHT_Y; break;
case GLFW_GAMEPAD_AXIS_LEFT_TRIGGER: a = GAMEPAD_AXIS_LEFT_TRIGGER; break;
case GLFW_GAMEPAD_AXIS_RIGHT_TRIGGER: a = GAMEPAD_AXIS_RIGHT_TRIGGER; break;
}
#endif
#if defined(PLATFORM_WEB)
//TODO: TEST
switch(axis)
{
case 0: a = GAMEPAD_AXIS_LEFT_X;
case 1: a = GAMEPAD_AXIS_LEFT_Y;
case 2: a = GAMEPAD_AXIS_RIGHT_X;
case 3: a = GAMEPAD_AXIS_RIGHT_X;
}
#endif
return a;
}
// Poll (store) all input events
static void PollInputEvents(void)
{
@ -3114,7 +3194,7 @@ static void PollInputEvents(void)
#if defined(PLATFORM_DESKTOP)
// Check if gamepads are ready
// NOTE: We do it here in case of disconection
// NOTE: We do it here in case of disconnection
for (int i = 0; i < MAX_GAMEPADS; i++)
{
if (glfwJoystickPresent(i)) gamepadReady[i] = true;
@ -3131,33 +3211,37 @@ static void PollInputEvents(void)
// Get current gamepad state
// NOTE: There is no callback available, so we get it manually
const unsigned char *buttons;
int buttonsCount;
//Get remapped buttons
GLFWgamepadstate state;
glfwGetGamepadState(i, &state); //This remapps all gamepads so they work the same
const unsigned char *buttons = state.buttons;
buttons = glfwGetJoystickButtons(i, &buttonsCount);
for (int k = 0; (buttons != NULL) && (k < buttonsCount) && (buttonsCount < MAX_GAMEPAD_BUTTONS); k++)
for (int k = 0; (buttons != NULL) && (k < GLFW_GAMEPAD_BUTTON_DPAD_LEFT + 1) && (k < MAX_GAMEPAD_BUTTONS); k++)
{
const GamepadButton button = GetGamepadButton(k);
if (buttons[k] == GLFW_PRESS)
{
currentGamepadState[i][k] = 1;
lastGamepadButtonPressed = k;
currentGamepadState[i][button] = 1;
lastGamepadButtonPressed = button;
}
else currentGamepadState[i][k] = 0;
else currentGamepadState[i][button] = 0;
}
// Get current axis state
const float *axes;
int axisCount = 0;
const float *axes = state.axes;
axes = glfwGetJoystickAxes(i, &axisCount);
for (int k = 0; (axes != NULL) && (k < axisCount) && (k < MAX_GAMEPAD_AXIS); k++)
for (int k = 0; (axes != NULL) && (k < GLFW_GAMEPAD_AXIS_LAST + 1) && (k < MAX_GAMEPAD_AXIS); k++)
{
gamepadAxisState[i][k] = axes[k];
const GamepadAxis axis = GetGamepadAxis(k);
gamepadAxisState[i][axis] = axes[k];
}
gamepadAxisCount = axisCount;
//Register buttons for 2nd triggers
currentGamepadState[i][GAMEPAD_BUTTON_LEFT_TRIGGER_2] = (char)(gamepadAxisState[i][GAMEPAD_AXIS_LEFT_TRIGGER] > 0.1);
currentGamepadState[i][GAMEPAD_BUTTON_RIGHT_TRIGGER_2] = (char)(gamepadAxisState[i][GAMEPAD_AXIS_RIGHT_TRIGGER] > 0.1);
gamepadAxisCount = GLFW_GAMEPAD_AXIS_LAST;
}
}
@ -3191,12 +3275,13 @@ static void PollInputEvents(void)
// Register buttons data for every connected gamepad
for (int j = 0; (j < gamepadState.numButtons) && (j < MAX_GAMEPAD_BUTTONS); j++)
{
const GamepadButton button = GetGamepadButton(j);
if (gamepadState.digitalButton[j] == 1)
{
currentGamepadState[i][j] = 1;
lastGamepadButtonPressed = j;
currentGamepadState[i][button] = 1;
lastGamepadButtonPressed = button;
}
else currentGamepadState[i][j] = 0;
else currentGamepadState[i][button] = 0;
//TraceLog(LOG_DEBUG, "Gamepad %d, button %d: Digital: %d, Analog: %g", gamepadState.index, j, gamepadState.digitalButton[j], gamepadState.analogButton[j]);
}
@ -3204,7 +3289,8 @@ static void PollInputEvents(void)
// Register axis data for every connected gamepad
for (int j = 0; (j < gamepadState.numAxes) && (j < MAX_GAMEPAD_AXIS); j++)
{
gamepadAxisState[i][j] = gamepadState.axis[j];
const GamepadAxis axis = GetGamepadAxis(k);
gamepadAxisState[i][axis] = gamepadState.axis[j];
}
gamepadAxisCount = gamepadState.numAxes;

View File

@ -619,6 +619,57 @@ typedef enum {
GAMEPAD_PLAYER4 = 3
} GamepadNumber;
// Gamepad Buttons
typedef enum
{
//This is here just for error checking
GAMEPAD_BUTTON_UNKNOWN = 0,
//This is normally ABXY/Circle, Triangle, Square, Cross. No support for 6 button controllers though..
GAMEPAD_BUTTON_LEFT_FACE_UP,
GAMEPAD_BUTTON_LEFT_FACE_RIGHT,
GAMEPAD_BUTTON_LEFT_FACE_DOWN,
GAMEPAD_BUTTON_LEFT_FACE_LEFT,
//This is normally a DPAD
GAMEPAD_BUTTON_RIGHT_FACE_UP,
GAMEPAD_BUTTON_RIGHT_FACE_RIGHT,
GAMEPAD_BUTTON_RIGHT_FACE_DOWN,
GAMEPAD_BUTTON_RIGHT_FACE_LEFT,
//Triggers
GAMEPAD_BUTTON_LEFT_TRIGGER_1,
GAMEPAD_BUTTON_LEFT_TRIGGER_2,
GAMEPAD_BUTTON_RIGHT_TRIGGER_1,
GAMEPAD_BUTTON_RIGHT_TRIGGER_2,
//These are buttons in the center of the gamepad
GAMEPAD_BUTTON_MIDDLE_LEFT, //PS3 Select
GAMEPAD_BUTTON_MIDDLE, //PS Button/XBOX Button
GAMEPAD_BUTTON_MIDDLE_RIGHT, //PS3 Start
//These are the joystick press in buttons
GAMEPAD_BUTTON_LEFT_THUMB,
GAMEPAD_BUTTON_RIGHT_THUMB
} GamepadButton;
typedef enum
{
GAMEPAD_AXIS_UNKNOWN = 0,
//Left stick
GAMEPAD_AXIS_LEFT_X,
GAMEPAD_AXIS_LEFT_Y,
//Right stick
GAMEPAD_AXIS_RIGHT_X,
GAMEPAD_AXIS_RIGHT_Y,
//Pressure levels
GAMEPAD_AXIS_LEFT_TRIGGER, // [1..-1] (pressure-level)
GAMEPAD_AXIS_RIGHT_TRIGGER // [1..-1] (pressure-level)
} GamepadAxis;
// PS3 USB Controller Buttons
// TODO: Provide a generic way to list gamepad controls schemes,
// defining specific controls schemes is not a good option