Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/test/browser/test_glfw_joystick.c
4150 views
1
/*
2
* Copyright 2017 The Emscripten Authors. All rights reserved.
3
* Emscripten is available under two separate licenses, the MIT license and the
4
* University of Illinois/NCSA Open Source License. Both these licenses can be
5
* found in the LICENSE file.
6
*/
7
8
#include <stdio.h>
9
#include <assert.h>
10
#include <string.h>
11
#ifdef __EMSCRIPTEN__
12
#include <emscripten.h>
13
#endif
14
#define GLFW_INCLUDE_ES2
15
#include <GLFW/glfw3.h>
16
17
GLFWwindow* g_window;
18
19
void error_callback(int error, const char* description);
20
21
int joy_connected = -1;
22
23
void joystick_callback(int joy, int event) {
24
if (event == GLFW_CONNECTED) {
25
printf("Joystick %d was connected: %s\n", joy, glfwGetJoystickName(joy));
26
joy_connected = joy; // use the most recently connected joystick
27
} else if (event == GLFW_DISCONNECTED) {
28
printf("Joystick %d was disconnected\n", joy);
29
if (joy == joy_connected) joy_connected = -1;
30
}
31
}
32
33
void main_2(void *arg) {
34
printf("Testing adding new gamepads\n");
35
emscripten_run_script("window.addNewGamepad('Pad Thai', 4, 16)");
36
emscripten_run_script("window.addNewGamepad('Pad Kee Mao', 0, 4)");
37
// Check that the joysticks exist properly.
38
assert(glfwJoystickPresent(GLFW_JOYSTICK_1));
39
assert(glfwJoystickPresent(GLFW_JOYSTICK_2));
40
41
assert(strcmp(glfwGetJoystickName(GLFW_JOYSTICK_1), "Pad Thai") == 0);
42
assert(strcmp(glfwGetJoystickName(GLFW_JOYSTICK_2), "Pad Kee Mao") == 0);
43
44
int axes_count = 0;
45
int buttons_count = 0;
46
47
glfwGetJoystickAxes(GLFW_JOYSTICK_1, &axes_count);
48
glfwGetJoystickButtons(GLFW_JOYSTICK_1, &buttons_count);
49
assert(axes_count == 4);
50
assert(buttons_count == 16);
51
52
glfwGetJoystickAxes(GLFW_JOYSTICK_2, &axes_count);
53
glfwGetJoystickButtons(GLFW_JOYSTICK_2, &buttons_count);
54
assert(axes_count == 0);
55
assert(buttons_count == 4);
56
57
// Buttons
58
printf("Testing buttons\n");
59
const unsigned char *buttons = glfwGetJoystickButtons(GLFW_JOYSTICK_1, &buttons_count);
60
assert(buttons_count == 16);
61
assert(buttons[0] == 0);
62
emscripten_run_script("window.simulateGamepadButtonDown(0, 1)");
63
buttons = glfwGetJoystickButtons(GLFW_JOYSTICK_1, &buttons_count);
64
assert(buttons_count == 16);
65
assert(buttons[1] == 1);
66
67
emscripten_run_script("window.simulateGamepadButtonUp(0, 1)");
68
buttons = glfwGetJoystickButtons(GLFW_JOYSTICK_1, &buttons_count);
69
assert(buttons_count == 16);
70
assert(buttons[1] == 0);
71
72
73
emscripten_run_script("window.simulateGamepadButtonDown(1, 0)");
74
buttons = glfwGetJoystickButtons(GLFW_JOYSTICK_2, &buttons_count);
75
assert(buttons_count == 4);
76
assert(buttons[0] == 1);
77
78
emscripten_run_script("window.simulateGamepadButtonUp(1, 0)");
79
buttons = glfwGetJoystickButtons(GLFW_JOYSTICK_2, &buttons_count);
80
assert(buttons_count == 4);
81
assert(buttons[1] == 0);
82
83
// Joystick wiggling
84
printf("Testing joystick axes\n");
85
emscripten_run_script("window.simulateAxisMotion(0, 0, 1)");
86
const float *axes = glfwGetJoystickAxes(GLFW_JOYSTICK_1, &axes_count);
87
assert(axes_count == 4);
88
assert(axes[0] == 1);
89
90
emscripten_run_script("window.simulateAxisMotion(0, 0, 0)");
91
axes = glfwGetJoystickAxes(GLFW_JOYSTICK_1, &axes_count);
92
assert(axes_count == 4);
93
assert(axes[0] == 0);
94
95
emscripten_run_script("window.simulateAxisMotion(0, 1, -1)");
96
axes = glfwGetJoystickAxes(GLFW_JOYSTICK_1, &axes_count);
97
assert(axes_count == 4);
98
assert(axes[1] == -1);
99
100
// End test.
101
printf("Test passed!\n");
102
emscripten_force_exit(0);
103
}
104
105
int main() {
106
if (!glfwInit()) {
107
printf("Could not create window. Test failed.\n");
108
return 1;
109
}
110
glfwWindowHint(GLFW_RESIZABLE , 1);
111
g_window = glfwCreateWindow(600, 450, "GLFW joystick test", NULL, NULL);
112
if (!g_window) {
113
printf("Could not create window. Test failed.\n");
114
glfwTerminate();
115
return 1;
116
}
117
glfwMakeContextCurrent(g_window);
118
glfwSetJoystickCallback(joystick_callback);
119
120
emscripten_async_call(main_2, NULL, 3000); // avoid startup delays and intermittent errors
121
122
#ifndef __EMSCRIPTEN__
123
while (!glfwWindowShouldClose(window)) {
124
glfwPollEvents();
125
}
126
#endif
127
128
glfwTerminate();
129
return 99;
130
}
131
132