Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/test/browser/test_glfw3.c
4150 views
1
/*
2
* Copyright 2014 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 <GLFW/glfw3.h>
9
#include <stdio.h>
10
#include <assert.h>
11
#include <string.h>
12
13
static void errorcb(int error, const char *msg) {
14
}
15
static void monitcb(GLFWmonitor *monitor, int event) {
16
assert(monitor != NULL);
17
}
18
static void wposicb(GLFWwindow *window, int x, int y) {
19
assert(window != NULL);
20
}
21
static void wsizecb(GLFWwindow *window, int w, int h) {
22
assert(window != NULL);
23
}
24
static void wscalcb(GLFWwindow *window, float xscale, float yscale) {
25
assert(window != NULL);
26
}
27
static void wcloscb(GLFWwindow *window) {
28
assert(window != NULL);
29
}
30
static void wrfrscb(GLFWwindow *window) {
31
assert(window != NULL);
32
}
33
static void wfocucb(GLFWwindow *window, int focused) {
34
assert(window != NULL);
35
}
36
static void wiconcb(GLFWwindow *window, int iconified) {
37
assert(window != NULL);
38
}
39
static void wmaxicb(GLFWwindow *window, int maximized) {
40
assert(window != NULL);
41
}
42
static void wfsizcb(GLFWwindow *window, int w, int h) {
43
assert(window != NULL);
44
}
45
static void wkeypcb(GLFWwindow *window, int key, int scancode, int action, int mods) {
46
assert(window != NULL);
47
}
48
static void wcharcb(GLFWwindow *window, unsigned int cp) {
49
assert(window != NULL);
50
}
51
static void wmbutcb(GLFWwindow *window, int button, int action, int mods) {
52
assert(window != NULL);
53
}
54
static void wcurpcb(GLFWwindow *window, double x, double y) {
55
assert(window != NULL);
56
}
57
static void wcurecb(GLFWwindow *window, int entered) {
58
assert(window != NULL);
59
}
60
static void wscrocb(GLFWwindow *window, double x, double y) {
61
assert(window != NULL);
62
}
63
static void wdropcb(GLFWwindow *window, int count, const char **paths) {
64
assert(window != NULL);
65
}
66
67
#define TEST_GLFW_SET_I(Function, Value) \
68
assert(glfwSet##Function(Value) == NULL); /* Default value (no callback was set) */ \
69
assert(glfwSet##Function(Value) == Value); /* The previously set callback */
70
71
#define TEST_GLFW_SET_II(Function, Window, Value) \
72
assert(glfwSet##Function(Window, Value) == NULL); /* Default value (no callback was set) */ \
73
assert(glfwSet##Function(Window, Value) == Value); /* The previously set callback */
74
75
int main() {
76
GLFWwindow *window;
77
char *userptr = "userptr";
78
79
TEST_GLFW_SET_I(ErrorCallback, errorcb)
80
assert(glfwInit() == GL_TRUE);
81
assert(!strcmp(glfwGetVersionString(), "3.2.1 JS WebGL Emscripten"));
82
assert(glfwGetCurrentContext() == NULL);
83
84
{
85
int major, minor, rev;
86
glfwGetVersion(&major, &minor, &rev);
87
assert(major == 3);
88
assert(minor == 2);
89
assert(rev == 1);
90
}
91
92
{
93
int count, x, y, w, h;
94
float xs, ys;
95
GLFWmonitor **monitors = glfwGetMonitors(&count);
96
assert(count == 1);
97
for (int i = 0; i < count; ++i) {
98
assert(monitors[i] != NULL);
99
}
100
101
assert(glfwGetPrimaryMonitor() != NULL);
102
glfwGetMonitorPos(monitors[0], &x, &y);
103
glfwGetMonitorPhysicalSize(monitors[0], &w, &h);
104
glfwGetMonitorWorkarea(monitors[0], &x, &y, &w, &h);
105
glfwGetMonitorContentScale(monitors[0], &xs, &ys);
106
assert(glfwGetMonitorName(monitors[0]) != NULL);
107
TEST_GLFW_SET_I(MonitorCallback, monitcb)
108
109
// XXX: not implemented
110
// assert(glfwGetVideoModes(monitors[0], &count) != NULL);
111
// assert(glfwGetVideoMode(monitors[0]) != NULL);
112
// glfwSetGamma(monitors[0], 1.0f);
113
// assert(glfwGetGammaRamp(monitors[0]) != NULL);
114
// glfwSetGammaRamp(monitors[0], ramp);
115
}
116
117
{
118
int x, y, w, h;
119
float xscale, yscale;
120
glfwDefaultWindowHints();
121
glfwWindowHint(GLFW_CLIENT_API, CLIENT_API);
122
glfwWindowHintString(GLFW_X11_CLASS_NAME, "Will be Ignored :)");
123
window = glfwCreateWindow(640, 480, "glfw3.c", NULL, NULL);
124
assert(window != NULL);
125
126
TEST_GLFW_SET_II(WindowPosCallback, window, wposicb)
127
TEST_GLFW_SET_II(WindowSizeCallback, window, wsizecb)
128
TEST_GLFW_SET_II(WindowContentScaleCallback, window, wscalcb)
129
TEST_GLFW_SET_II(WindowCloseCallback, window, wcloscb)
130
TEST_GLFW_SET_II(WindowRefreshCallback, window, wrfrscb)
131
TEST_GLFW_SET_II(WindowFocusCallback, window, wfocucb)
132
TEST_GLFW_SET_II(WindowIconifyCallback, window, wiconcb)
133
TEST_GLFW_SET_II(WindowMaximizeCallback, window, wmaxicb)
134
TEST_GLFW_SET_II(FramebufferSizeCallback, window, wfsizcb)
135
TEST_GLFW_SET_II(KeyCallback, window, wkeypcb)
136
TEST_GLFW_SET_II(CharCallback, window, wcharcb)
137
TEST_GLFW_SET_II(MouseButtonCallback, window, wmbutcb)
138
TEST_GLFW_SET_II(CursorPosCallback, window, wcurpcb)
139
TEST_GLFW_SET_II(CursorEnterCallback, window, wcurecb)
140
TEST_GLFW_SET_II(ScrollCallback, window, wscrocb)
141
TEST_GLFW_SET_II(DropCallback, window, wdropcb)
142
143
assert(glfwWindowShouldClose(window) == 0);
144
glfwSetWindowShouldClose(window, 1);
145
assert(glfwWindowShouldClose(window) == 1);
146
147
glfwSetWindowTitle(window, "test");
148
glfwSetWindowTitle(window, "glfw3.c");
149
150
// XXX: not implemented
151
// glfwSetWindowPos(window, 1, 1);
152
153
glfwGetWindowPos(window, &x, &y); // stub
154
glfwGetWindowSize(window, &w, &h);
155
assert(w == 640 && h == 480);
156
157
glfwSetWindowSize(window, 1, 1);
158
glfwGetWindowSize(window, &w, &h);
159
assert(w == 1 && h == 1);
160
161
glfwSetWindowSize(window, 640, 480);
162
glfwGetFramebufferSize(window, &w, &h);
163
164
float opacity = glfwGetWindowOpacity(window); // always returns 1.0 for now
165
glfwSetWindowOpacity(window, opacity); // ignored.
166
// how to check if x/yscale are correct? use emscripten_device_pixel_ratio from html5 header?
167
glfwGetWindowContentScale(window, &xscale, &yscale);
168
assert(opacity == 1.0);
169
// XXX: not implemented
170
// glfwIconifyWindow(window);
171
// glfwRestoreWindow(window);
172
// glfwShowWindow(window);
173
// glfwHideWindow(window);
174
175
assert(glfwGetWindowMonitor(window) == NULL);
176
glfwDestroyWindow(window);
177
178
window = glfwCreateWindow(640, 480, "glfw3.c", glfwGetPrimaryMonitor(), NULL);
179
assert(window != NULL);
180
assert(glfwGetWindowMonitor(window) == glfwGetPrimaryMonitor());
181
glfwDestroyWindow(window);
182
183
window = glfwCreateWindow(640, 480, "glfw3.c", NULL, NULL);
184
assert(window != NULL);
185
186
assert(glfwGetWindowAttrib(window, GLFW_CLIENT_API) == CLIENT_API);
187
glfwSetWindowAttrib(window, GLFW_TRANSPARENT_FRAMEBUFFER, GLFW_TRUE);
188
assert(glfwGetWindowAttrib(window, GLFW_TRANSPARENT_FRAMEBUFFER) == GLFW_TRUE);
189
assert(glfwGetWindowUserPointer(window) == NULL);
190
glfwSetWindowUserPointer(window, userptr);
191
assert(glfwGetWindowUserPointer(window) == userptr);
192
}
193
194
{
195
double x, y;
196
197
glfwSetKeyCallback(window, wkeypcb);
198
glfwSetCharCallback(window, wcharcb);
199
glfwSetMouseButtonCallback(window, wmbutcb);
200
glfwSetCursorPosCallback(window, wcurpcb);
201
glfwSetCursorEnterCallback(window, wcurecb);
202
glfwSetScrollCallback(window, wscrocb);
203
204
// XXX: stub, events come immediatly
205
// glfwPollEvents();
206
// glfwWaitEvents();
207
208
assert(glfwGetInputMode(window, GLFW_CURSOR) == GLFW_CURSOR_NORMAL);
209
210
// XXX: not implemented
211
// glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
212
213
assert(glfwRawMouseMotionSupported() == 0); // constant false
214
glfwGetKey(window, GLFW_KEY_A);
215
glfwGetMouseButton(window, 0);
216
glfwGetCursorPos(window, &x, &y);
217
218
// XXX: not implemented
219
// glfwSetCursorPos(window, 0, 0);
220
}
221
222
{
223
// XXX: not implemented
224
// glfwJoystickPresent(joy);
225
// glfwGetJoystickAxes(joy, &count);
226
// glfwGetJoystickButtons(joy, &count);
227
// glfwGetJoystickName(joy);
228
}
229
230
{
231
// XXX: not implemented
232
// glfwSetClipboardString(window, "string");
233
// glfwGetClipboardString(window);
234
}
235
236
{
237
glfwGetTime();
238
glfwSetTime(0);
239
}
240
241
{
242
GLFWcursor* cur = glfwCreateStandardCursor(GLFW_ARROW_CURSOR);
243
printf("glfwCreateStandardCursor => %p\n", cur);
244
}
245
246
#if CLIENT_API == GLFW_OPENGL_ES_API
247
{
248
glfwMakeContextCurrent(window); // stub
249
assert(glfwGetCurrentContext() == window);
250
glfwSwapBuffers(window); // stub
251
glfwSwapInterval(0); // stub
252
}
253
254
{
255
assert(glfwExtensionSupported("nonexistant") == 0);
256
assert(glfwGetProcAddress("nonexistant") == NULL);
257
}
258
#endif
259
260
glfwTerminate();
261
return 0;
262
}
263
264