change gamepad state callback to use the state. post empty event after joystick polling to make waitEvents return

This commit is contained in:
Enthuin 2022-10-20 20:37:48 -05:00
parent 712942e596
commit e65723069f
3 changed files with 111 additions and 118 deletions

View File

@ -1964,107 +1964,6 @@ typedef void (* GLFWmonitorfun)(GLFWmonitor* monitor, int event);
*/ */
typedef void (* GLFWjoystickfun)(int jid, int event); typedef void (* GLFWjoystickfun)(int jid, int event);
/*! @brief The function pointer type for joystick button callbacks.
*
* This is the function pointer type for joystick button callbacks. A joystick
* button callback function has the following signature:
* @code
* void function_name(int jid, int button, int action)
* @endcode
*
* @param[in] jid The joystick that had a button pressed or released.
* @param[in] button The [joystick button](@ref buttons) that was pressed or released.
* @param[in] action `GLFW_PRESS` or `GLFW_RELEASE`. Future
* releases may add more actions.
*
* @sa @ref input_joystick_button
* @sa @ref glfwSetJoystickButonCallback
*
* @since Added in version 3.4.
* @ingroup input
*/
typedef void (* GLFWjoystickbuttonfun)(int,int,int);
/*! @brief The function pointer type for joystick axis movement callbacks.
*
* This is the function pointer type for joystick axis movement callbacks. A joystick
* axis movement callback function has the following signature:
* @code
* void function_name(int jid, int axis, float position)
* @endcode
*
* @param[in] jid The joystick that had an axis moved.
* @param[in] axis The [joystick axis](@ref gamepad axes) that was moved.
* @param[in] position A value between -1.0 and 1.0 that indicates the position of the axis.
*
* @sa @ref input_gamepad_axis
* @sa @ref glfwSetJoystickAxisCallback
*
* @since Added in version 3.4.
* @ingroup input
*/
typedef void (* GLFWjoystickaxisfun)(int,int,float);
/*! @brief The function pointer type for joystick hat movement callbacks.
*
* This is the function pointer type for joystick hat movement callbacks. A joystick
* hat movement callback function has the following signature:
* @code
* void function_name(int jid, int hat, int position)
* @endcode
*
* @param[in] jid The joystick that had an axis moved.
* @param[in] hat The [joystick hat](@ref joystick hats) that was moved.
* @param[in] position A value that indicates the position of the hat.
* The position parameter is one of the following values:
*
* Name | Value
* ---- | -----
* `GLFW_HAT_CENTERED` | 0
* `GLFW_HAT_UP` | 1
* `GLFW_HAT_RIGHT` | 2
* `GLFW_HAT_DOWN` | 4
* `GLFW_HAT_LEFT` | 8
* `GLFW_HAT_RIGHT_UP` | `GLFW_HAT_RIGHT` \| `GLFW_HAT_UP`
* `GLFW_HAT_RIGHT_DOWN` | `GLFW_HAT_RIGHT` \| `GLFW_HAT_DOWN`
* `GLFW_HAT_LEFT_UP` | `GLFW_HAT_LEFT` \| `GLFW_HAT_UP`
* `GLFW_HAT_LEFT_DOWN` | `GLFW_HAT_LEFT` \| `GLFW_HAT_DOWN`
*
* The diagonal directions are bitwise combinations of the primary (up, right,
* down and left) directions and you can test for these individually by ANDing
* it with the corresponding direction.
*
* @sa @ref input_joystick_hat
* @sa @ref glfwSetJoystickHatCallback
*
* @since Added in version 3.4.
* @ingroup input
*/
typedef void (* GLFWjoystickhatfun)(int,int,int);
/*! @brief The function pointer type for game pad state changes.
*
* This is the function pointer type for game pad state change callbacks.
* A game pad state change callback function has the following signature:
* @code
* void function_name(int jid, GLFWgamepadstate* state)
* @endcode
*
* @param[in] jid The ID of the game pad that changed state.
* @param[in] buttons The states of each
* [gamepad button](@ref gamepad_buttons),
* `GLFW_PRESS` or `GLFW_RELEASE`.
* @param[in] axes The states of each [gamepad axis](@ref gamepad_axes),
* in the range -1.0 to 1.0 inclusive.
*
* @sa @ref input_gamepad
* @sa @ref glfwSetGamepadStateCallback
*
* @since Added in version 3.4.
* @ingroup input
*/
typedef void (* GLFWgamepadstatefun)(int, unsigned char buttons[15],
float axes[6]);
/*! @brief Video mode type. /*! @brief Video mode type.
* *
* This describes a single video mode. * This describes a single video mode.
@ -2195,6 +2094,103 @@ typedef struct GLFWallocator
void* user; void* user;
} GLFWallocator; } GLFWallocator;
/*! @brief The function pointer type for joystick button callbacks.
*
* This is the function pointer type for joystick button callbacks. A joystick
* button callback function has the following signature:
* @code
* void function_name(int jid, int button, int action)
* @endcode
*
* @param[in] jid The joystick that had a button pressed or released.
* @param[in] button The [joystick button](@ref buttons) that was pressed or released.
* @param[in] action `GLFW_PRESS` or `GLFW_RELEASE`. Future
* releases may add more actions.
*
* @sa @ref input_joystick_button
* @sa @ref glfwSetJoystickButonCallback
*
* @since Added in version 3.4.
* @ingroup input
*/
typedef void (* GLFWjoystickbuttonfun)(int,int,int);
/*! @brief The function pointer type for joystick axis movement callbacks.
*
* This is the function pointer type for joystick axis movement callbacks. A joystick
* axis movement callback function has the following signature:
* @code
* void function_name(int jid, int axis, float position)
* @endcode
*
* @param[in] jid The joystick that had an axis moved.
* @param[in] axis The [joystick axis](@ref gamepad axes) that was moved.
* @param[in] position A value between -1.0 and 1.0 that indicates the position of the axis.
*
* @sa @ref input_gamepad_axis
* @sa @ref glfwSetJoystickAxisCallback
*
* @since Added in version 3.4.
* @ingroup input
*/
typedef void (* GLFWjoystickaxisfun)(int,int,float);
/*! @brief The function pointer type for joystick hat movement callbacks.
*
* This is the function pointer type for joystick hat movement callbacks. A joystick
* hat movement callback function has the following signature:
* @code
* void function_name(int jid, int hat, int position)
* @endcode
*
* @param[in] jid The joystick that had an axis moved.
* @param[in] hat The [joystick hat](@ref joystick hats) that was moved.
* @param[in] position A value that indicates the position of the hat.
* The position parameter is one of the following values:
*
* Name | Value
* ---- | -----
* `GLFW_HAT_CENTERED` | 0
* `GLFW_HAT_UP` | 1
* `GLFW_HAT_RIGHT` | 2
* `GLFW_HAT_DOWN` | 4
* `GLFW_HAT_LEFT` | 8
* `GLFW_HAT_RIGHT_UP` | `GLFW_HAT_RIGHT` \| `GLFW_HAT_UP`
* `GLFW_HAT_RIGHT_DOWN` | `GLFW_HAT_RIGHT` \| `GLFW_HAT_DOWN`
* `GLFW_HAT_LEFT_UP` | `GLFW_HAT_LEFT` \| `GLFW_HAT_UP`
* `GLFW_HAT_LEFT_DOWN` | `GLFW_HAT_LEFT` \| `GLFW_HAT_DOWN`
*
* The diagonal directions are bitwise combinations of the primary (up, right,
* down and left) directions and you can test for these individually by ANDing
* it with the corresponding direction.
*
* @sa @ref input_joystick_hat
* @sa @ref glfwSetJoystickHatCallback
*
* @since Added in version 3.4.
* @ingroup input
*/
typedef void (* GLFWjoystickhatfun)(int,int,int);
/*! @brief The function pointer type for game pad state changes.
*
* This is the function pointer type for game pad state change callbacks.
* A game pad state change callback function has the following signature:
* @code
* void function_name(int jid, GLFWgamepadstate* state)
* @endcode
*
* @param[in] jid The ID of the game pad that changed state.
* @param[in] state the state of the game pad
*
* @sa @ref input_gamepad
* @sa @ref glfwSetGamepadStateCallback
*
* @since Added in version 3.4.
* @ingroup input
*/
typedef void (* GLFWgamepadstatefun)(int jid, GLFWgamepadstate state);
/************************************************************************* /*************************************************************************
* GLFW API functions * GLFW API functions

View File

@ -427,7 +427,7 @@ void _glfwInputGamepad(_GLFWjoystick *js)
GLFWgamepadstate state; GLFWgamepadstate state;
if (glfwGetGamepadState(jid, &state)) if (glfwGetGamepadState(jid, &state))
{ {
_glfw.callbacks.gamepad_state(jid, state.buttons, state.axes); _glfw.callbacks.gamepad_state(jid, state);
} }
} }
} }
@ -464,9 +464,7 @@ void _glfwInputJoystickAxis(_GLFWjoystick *js, int axis, float value)
if (_glfw.callbacks.joystick_axis) if (_glfw.callbacks.joystick_axis)
_glfw.callbacks.joystick_axis(jid, axis, value); _glfw.callbacks.joystick_axis(jid, axis, value);
_glfwInputGamepad(js); _glfwInputGamepad(js);
} } else {
else
{
js->axes[axis] = value; js->axes[axis] = value;
} }
} }
@ -479,17 +477,15 @@ void _glfwInputJoystickButton(_GLFWjoystick *js, int button, char value)
assert(button >= 0); assert(button >= 0);
assert(button < js->buttonCount); assert(button < js->buttonCount);
assert(value == GLFW_PRESS || value == GLFW_RELEASE); assert(value == GLFW_PRESS || value == GLFW_RELEASE);
if (js->buttons[button] != value)
{ if (js->buttons[button] != value) {
const int jid = (int)(js - _glfw.joysticks); const int jid = (int)(js - _glfw.joysticks);
js->buttons[button] = value; js->buttons[button] = value;
if (_glfw.callbacks.joystick_button) if (_glfw.callbacks.joystick_button)
_glfw.callbacks.joystick_button(jid, button, value); _glfw.callbacks.joystick_button(jid, button, value);
_glfwInputGamepad(js); _glfwInputGamepad(js);
} } else {
else
{
js->buttons[button] = value; js->buttons[button] = value;
} }
} }
@ -610,7 +606,8 @@ void _glfwPollAllJoysticks()
{ {
if (_glfw.joysticks[jid].connected == GLFW_TRUE) if (_glfw.joysticks[jid].connected == GLFW_TRUE)
{ {
_glfwPlatformPollJoystick(_glfw.joysticks + jid, _GLFW_POLL_ALL); _glfw.platform.pollJoystick(_glfw.joysticks + jid, _GLFW_POLL_ALL);
glfwPostEmptyEvent();
} }
} }
} }
@ -1310,28 +1307,28 @@ GLFWAPI GLFWjoystickfun glfwSetJoystickCallback(GLFWjoystickfun cbfun)
GLFWAPI GLFWgamepadstatefun glfwSetGamepadStateCallback(GLFWgamepadstatefun cbfun) GLFWAPI GLFWgamepadstatefun glfwSetGamepadStateCallback(GLFWgamepadstatefun cbfun)
{ {
_GLFW_REQUIRE_INIT_OR_RETURN(NULL); _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
_GLFW_SWAP_POINTERS(_glfw.callbacks.gamepad_state, cbfun); _GLFW_SWAP(GLFWgamepadstatefun, _glfw.callbacks.gamepad_state, cbfun);
return cbfun; return cbfun;
} }
GLFWAPI GLFWjoystickbuttonfun glfwSetJoystickButtonCallback(GLFWjoystickbuttonfun cbfun) GLFWAPI GLFWjoystickbuttonfun glfwSetJoystickButtonCallback(GLFWjoystickbuttonfun cbfun)
{ {
_GLFW_REQUIRE_INIT_OR_RETURN(NULL); _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
_GLFW_SWAP_POINTERS(_glfw.callbacks.joystick_button, cbfun); _GLFW_SWAP(GLFWjoystickbuttonfun, _glfw.callbacks.joystick_button, cbfun);
return cbfun; return cbfun;
} }
GLFWAPI GLFWjoystickaxisfun glfwSetJoystickAxisCallback(GLFWjoystickaxisfun cbfun) GLFWAPI GLFWjoystickaxisfun glfwSetJoystickAxisCallback(GLFWjoystickaxisfun cbfun)
{ {
_GLFW_REQUIRE_INIT_OR_RETURN(NULL); _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
_GLFW_SWAP_POINTERS(_glfw.callbacks.joystick_axis, cbfun); _GLFW_SWAP(GLFWjoystickaxisfun, _glfw.callbacks.joystick_axis, cbfun);
return cbfun; return cbfun;
} }
GLFWAPI GLFWjoystickhatfun glfwSetJoystickHatCallback(GLFWjoystickhatfun cbfun) GLFWAPI GLFWjoystickhatfun glfwSetJoystickHatCallback(GLFWjoystickhatfun cbfun)
{ {
_GLFW_REQUIRE_INIT_OR_RETURN(NULL); _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
_GLFW_SWAP_POINTERS(_glfw.callbacks.joystick_hat, cbfun); _GLFW_SWAP(GLFWjoystickhatfun, _glfw.callbacks.joystick_hat, cbfun);
return cbfun; return cbfun;
} }

View File

@ -558,7 +558,7 @@ static void joystick_hat_callback(int jid, int hat, int value) {
value); value);
} }
static void gamepad_state_callback(int jid, unsigned char buttons[15], float axes[6]) { static void gamepad_state_callback(int jid, GLFWgamepadstate state) {
int i = 0; int i = 0;
printf("%08x at %0.3f: Gamepad %i (%s) state:", printf("%08x at %0.3f: Gamepad %i (%s) state:",
counter++, glfwGetTime(), counter++, glfwGetTime(),
@ -567,11 +567,11 @@ static void gamepad_state_callback(int jid, unsigned char buttons[15], float axe
printf("Buttons: "); printf("Buttons: ");
for (i= 0 ; i < 15; i++) { for (i= 0 ; i < 15; i++) {
printf(" %d:%d", i, buttons[i]); printf(" %d:%d", i, state.buttons[i]);
} }
printf("Axes: "); printf("Axes: ");
for (i= 0 ; i < 6; i++) { for (i= 0 ; i < 6; i++) {
printf(" %d:%0.4f", i, axes[i]); printf(" %d:%0.4f", i, state.axes[i]);
} }
printf("\n"); printf("\n");