made work in emscripten

This commit is contained in:
DaniTheSkunk 2023-01-20 10:54:16 +00:00
parent 647c6778e2
commit 131031a4f9
2 changed files with 60 additions and 0 deletions

View File

@ -3,6 +3,38 @@
#include "types.h" #include "types.h"
#ifdef __EMSCRIPTEN__
#define SW_MAX_GAMEPADS 1
#define SW_MAX_GAMEPAD_AXES 6
#define SW_MAX_GAMEPAD_BUTTONS 16
#define SW_GAMEPAD_BUTTON_FACE_DOWN 0
#define SW_GAMEPAD_BUTTON_FACE_RIGHT 1
#define SW_GAMEPAD_BUTTON_FACE_LEFT 2
#define SW_GAMEPAD_BUTTON_FACE_UP 3
#define SW_GAMEPAD_BUTTON_LEFT_SHOULDER 4
#define SW_GAMEPAD_BUTTON_RIGHT_SHOULDER 5
#define SW_GAMEPAD_BUTTON_LEFT_TRIGGER 6
#define SW_GAMEPAD_BUTTON_RIGHT_TRIGGER 7
#define SW_GAMEPAD_BUTTON_BACK 8
#define SW_GAMEPAD_BUTTON_START 9
#define SW_GAMEPAD_BUTTON_LEFT_STICK 11
#define SW_GAMEPAD_BUTTON_RIGHT_STICK 10
#define SW_GAMEPAD_BUTTON_DPAD_UP 12
#define SW_GAMEPAD_BUTTON_DPAD_RIGHT 15
#define SW_GAMEPAD_BUTTON_DPAD_DOWN 13
#define SW_GAMEPAD_BUTTON_DPAD_LEFT 14
#define SW_GAMEPAD_AXIS_LEFT_X 0
#define SW_GAMEPAD_AXIS_RIGHT_X 1
#define SW_GAMEPAD_AXIS_LEFT_Y 2
#define SW_GAMEPAD_AXIS_RIGHT_Y 3
#define SW_GAMEPAD_AXIS_LEFT_TRIGGER 4
#define SW_GAMEPAD_AXIS_RIGHT_TRIGGER 5
#else /* __EMSCRIPTEN__ */
#define SW_MAX_GAMEPADS 4 #define SW_MAX_GAMEPADS 4
#define SW_MAX_GAMEPAD_AXES 6 #define SW_MAX_GAMEPAD_AXES 6
#define SW_MAX_GAMEPAD_BUTTONS 15 #define SW_MAX_GAMEPAD_BUTTONS 15
@ -30,6 +62,8 @@
#define SW_GAMEPAD_AXIS_LEFT_TRIGGER 4 #define SW_GAMEPAD_AXIS_LEFT_TRIGGER 4
#define SW_GAMEPAD_AXIS_RIGHT_TRIGGER 5 #define SW_GAMEPAD_AXIS_RIGHT_TRIGGER 5
#endif /* __EMSCRIPTEN__ */
struct sw_gamepad { struct sw_gamepad {
f32 axes[SW_MAX_GAMEPAD_AXES]; f32 axes[SW_MAX_GAMEPAD_AXES];
bool button_down[SW_MAX_GAMEPAD_BUTTONS]; bool button_down[SW_MAX_GAMEPAD_BUTTONS];

View File

@ -10,6 +10,31 @@ void sw_gamepad_static_init() {
} }
void sw_gamepad_tick() { void sw_gamepad_tick() {
#ifdef __EMSCRIPTEN__
i32 i, j, count;
const f32 *axes;
const unsigned char *buttons;
for(i = 0; i < SW_MAX_GAMEPADS; ++i) {
if(glfwJoystickPresent(i)) {
axes = glfwGetJoystickAxes(i, &count);
count = i32_min(count, SW_MAX_GAMEPAD_AXES);
for(j = 0; j < count; ++j) {
sw_gamepads[i].axes[j] = axes[j];
}
buttons = glfwGetJoystickButtons(i, &count);
count = i32_min(count, SW_MAX_GAMEPAD_BUTTONS);
for(j = 0; j < SW_MAX_GAMEPAD_BUTTONS; ++j) {
sw_gamepads[i].button_pressed[j] =
!sw_gamepads[i].button_pressed[j] && buttons[j];
sw_gamepads[i].button_down[j] = buttons[j];
}
}
}
#else /* __EMSCRIPTEN__ */
i32 i, j; i32 i, j;
GLFWgamepadstate state; GLFWgamepadstate state;
@ -26,4 +51,5 @@ void sw_gamepad_tick() {
} }
} }
} }
#endif /* __EMSCRIPTEN__ */
} }