Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/test/browser/test_glfw.c
4150 views
1
/*
2
* Copyright 2013 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 <stdlib.h>
9
#include <GL/glfw.h>
10
#include <stdio.h>
11
#include <assert.h>
12
#include <emscripten/emscripten.h>
13
14
int params[] = {GLFW_OPENED,
15
GLFW_ACTIVE,
16
GLFW_ICONIFIED,
17
GLFW_ACCELERATED,
18
GLFW_RED_BITS,
19
GLFW_GREEN_BITS,
20
GLFW_BLUE_BITS,
21
GLFW_ALPHA_BITS,
22
GLFW_DEPTH_BITS,
23
GLFW_STENCIL_BITS,
24
GLFW_REFRESH_RATE,
25
GLFW_ACCUM_RED_BITS,
26
GLFW_ACCUM_GREEN_BITS,
27
GLFW_ACCUM_BLUE_BITS,
28
GLFW_ACCUM_ALPHA_BITS,
29
GLFW_AUX_BUFFERS,
30
GLFW_STEREO,
31
GLFW_WINDOW_NO_RESIZE,
32
GLFW_FSAA_SAMPLES,
33
GLFW_OPENGL_VERSION_MAJOR,
34
GLFW_OPENGL_VERSION_MINOR,
35
GLFW_OPENGL_FORWARD_COMPAT,
36
GLFW_OPENGL_DEBUG_CONTEXT,
37
GLFW_OPENGL_PROFILE};
38
unsigned int nb_params = sizeof(params) / sizeof(int);
39
40
int features[] = {GLFW_MOUSE_CURSOR,
41
GLFW_STICKY_KEYS,
42
GLFW_STICKY_MOUSE_BUTTONS,
43
GLFW_SYSTEM_KEYS,
44
GLFW_KEY_REPEAT,
45
GLFW_AUTO_POLL_EVENTS};
46
unsigned int nb_features = sizeof(features) / sizeof(int);
47
48
// Will be set to 1 as soon as OnResize callback is called
49
int on_resize_called = 0;
50
51
float rotate_y = 0,
52
rotate_z = 0;
53
const float rotations_per_tick = .2;
54
55
void Shut_Down(int return_code) {
56
glfwTerminate();
57
exit(return_code);
58
}
59
60
char* GetParamName(int param){
61
switch (param){
62
case GLFW_WINDOW : return "GLFW_WINDOW";
63
case GLFW_FULLSCREEN : return "GLFW_FULLSCREEN";
64
case GLFW_OPENED : return "GLFW_OPENED";
65
case GLFW_ACTIVE : return "GLFW_ACTIVE";
66
case GLFW_ICONIFIED : return "GLFW_ICONIFIED";
67
case GLFW_ACCELERATED : return "GLFW_ACCELERATED";
68
case GLFW_RED_BITS : return "GLFW_RED_BITS";
69
case GLFW_GREEN_BITS : return "GLFW_GREEN_BITS";
70
case GLFW_BLUE_BITS : return "GLFW_BLUE_BITS";
71
case GLFW_ALPHA_BITS : return "GLFW_ALPHA_BITS";
72
case GLFW_DEPTH_BITS : return "GLFW_DEPTH_BITS";
73
case GLFW_STENCIL_BITS : return "GLFW_STENCIL_BITS";
74
case GLFW_REFRESH_RATE : return "GLFW_REFRESH_RATE";
75
case GLFW_ACCUM_RED_BITS : return "GLFW_ACCUM_RED_BITS";
76
case GLFW_ACCUM_GREEN_BITS : return "GLFW_ACCUM_GREEN_BITS";
77
case GLFW_ACCUM_BLUE_BITS : return "GLFW_BLUE_BITS";
78
case GLFW_ACCUM_ALPHA_BITS : return "GLFW_ALPHA_BITS";
79
case GLFW_AUX_BUFFERS : return "GLFW_AUX_BUFFERS";
80
case GLFW_STEREO : return "GLFW_STEREO";
81
case GLFW_WINDOW_NO_RESIZE : return "GLFW_WINDOW_NO_RESIZE";
82
case GLFW_FSAA_SAMPLES : return "GLFW_FSAA_SAMPLES";
83
case GLFW_OPENGL_VERSION_MAJOR : return "GLFW_OPENGL_VERSION_MAJOR";
84
case GLFW_OPENGL_VERSION_MINOR : return "GLFW_OPENGL_VERSION_MINOR";
85
case GLFW_OPENGL_FORWARD_COMPAT : return "GLFW_OPENGL_FORWARD_COMPAT";
86
case GLFW_OPENGL_DEBUG_CONTEXT : return "GLFW_OPENGL_DEBUG_CONTEXT";
87
case GLFW_OPENGL_PROFILE : return "GLFW_OPENGL_PROFILE";
88
case GLFW_OPENGL_CORE_PROFILE : return "GLFW_OPENGL_CORE_PROFILE | GLFW_PRESENT";
89
case GLFW_OPENGL_COMPAT_PROFILE : return "GLFW_OPENGL_COMPAT_PROFILE | GLFW_AXES";
90
case GLFW_MOUSE_CURSOR : return "GLFW_MOUSE_CURSOR";
91
case GLFW_STICKY_KEYS : return "GLFW_STICKY_KEYS";
92
case GLFW_STICKY_MOUSE_BUTTONS : return "GLFW_STICKY_MOUSE_BUTTONS";
93
case GLFW_SYSTEM_KEYS : return "GLFW_SYSTEM_KEYS";
94
case GLFW_KEY_REPEAT : return "GLFW_KEY_REPEAT";
95
case GLFW_AUTO_POLL_EVENTS : return "GLFW_AUTO_POLL_EVENTS";
96
case GLFW_WAIT : return "GLFW_WAIT";
97
case GLFW_NOWAIT : return "GLFW_NOWAIT";
98
case GLFW_BUTTONS : return "GLFW_BUTTONS";
99
case GLFW_NO_RESCALE_BIT : return "GLFW_NO_RESCALE_BIT";
100
case GLFW_ORIGIN_UL_BIT : return "GLFW_ORIGIN_UL_BIT";
101
case GLFW_BUILD_MIPMAPS_BIT : return "GLFW_BUILD_MIPMAPS_BIT";
102
case GLFW_ALPHA_MAP_BIT : return "GLFW_ALPHA_MAP_BIT";
103
default : return "Invalid param";
104
}
105
}
106
107
void PullInfo() {
108
printf("================================================================================\n");
109
110
int major, minor, rev;
111
glfwGetVersion(&major, &minor, &rev);
112
printf("GLFW version is %i.%i.%i\n", major, minor, rev);
113
114
int width, height;
115
glfwGetWindowSize(&width, &height);
116
printf("Window size is %i %i\n", width, height);
117
118
int status = glfwGetKey(GLFW_KEY_LCTRL);
119
if (status == GLFW_PRESS) {
120
printf("Left control is pressed\n");
121
} else {
122
printf("Left control is released\n");
123
}
124
125
status = glfwGetMouseButton(GLFW_MOUSE_BUTTON_1);
126
if (status == GLFW_PRESS) {
127
printf("Mouse button 1 is pressed\n");
128
} else {
129
printf("Mouse button 1 is released\n");
130
}
131
132
int x, y;
133
glfwGetMousePos(&x, &y);
134
printf("Mouse position is %i %i\n", x, y);
135
136
int wheel = glfwGetMouseWheel();
137
printf("Mouse wheel pos is %i\n", wheel);
138
139
double time = glfwGetTime();
140
printf("Time is %f\n", time);
141
142
glfwGetGLVersion(&major, &minor, &rev);
143
printf("GL version is %i.%i.%i\n", major, minor, rev);
144
145
int proc = glfwGetNumberOfProcessors();
146
printf("%i processors are available\n", proc);
147
148
unsigned int i;
149
for (i = 0; i<nb_params; i++) {
150
printf(" - %-27s : %i\n", GetParamName(params[i]), glfwGetWindowParam(params[i]));
151
}
152
153
const char* extension = "WEBGL_compressed_texture_s3tc";
154
printf("'%s' extension is %s.\n", extension, glfwExtensionSupported(extension) ? "supported" : "not supported");
155
156
extension = "GL_EXT_framebuffer_object";
157
printf("'%s' extension is %s.\n", extension, glfwExtensionSupported(extension) ? "supported" : "not supported");
158
159
extension = "glBindBuffer";
160
void* proc_addr = glfwGetProcAddress(extension);
161
printf("'%s' extension proc address is %p.\n", extension, proc_addr);
162
163
printf("Sleeping 1 sec...\n");
164
glfwSleep(1);
165
printf("...Done.\n");
166
167
printf("================================================================================\n");
168
169
exit(0);
170
}
171
172
char* GetKeyName(int key){
173
switch (key){
174
case GLFW_KEY_UNKNOWN: return "unknown";
175
case GLFW_KEY_SPACE: return "space";
176
case GLFW_KEY_SPECIAL: return "special";
177
case GLFW_KEY_ESC: return "escape";
178
case GLFW_KEY_F1 : return "F1";
179
case GLFW_KEY_F2 : return "F2";
180
case GLFW_KEY_F3 : return "F3";
181
case GLFW_KEY_F4 : return "F4";
182
case GLFW_KEY_F5 : return "F5";
183
case GLFW_KEY_F6 : return "F6";
184
case GLFW_KEY_F7 : return "F7";
185
case GLFW_KEY_F8 : return "F8";
186
case GLFW_KEY_F9 : return "F9";
187
case GLFW_KEY_F10: return "F10";
188
case GLFW_KEY_F11: return "F11";
189
case GLFW_KEY_F12: return "F12";
190
case GLFW_KEY_F13: return "F13";
191
case GLFW_KEY_F14: return "F14";
192
case GLFW_KEY_F15: return "F15";
193
case GLFW_KEY_F16: return "F16";
194
case GLFW_KEY_F17: return "F17";
195
case GLFW_KEY_F18: return "F18";
196
case GLFW_KEY_F19: return "F19";
197
case GLFW_KEY_F20: return "F20";
198
case GLFW_KEY_F21: return "F21";
199
case GLFW_KEY_F22: return "F22";
200
case GLFW_KEY_F23: return "F23";
201
case GLFW_KEY_F24: return "F24";
202
case GLFW_KEY_F25: return "F25";
203
case GLFW_KEY_UP : return "up";
204
case GLFW_KEY_DOWN: return "down";
205
case GLFW_KEY_LEFT: return "left";
206
case GLFW_KEY_RIGHT: return "right";
207
case GLFW_KEY_LSHIFT: return "left shift";
208
case GLFW_KEY_RSHIFT: return "right shift";
209
case GLFW_KEY_LCTRL: return "left ctrl";
210
case GLFW_KEY_RCTRL: return "right ctrl";
211
case GLFW_KEY_LALT: return "left alt";
212
case GLFW_KEY_RALT: return "right alt";
213
case GLFW_KEY_TAB: return "tab";
214
case GLFW_KEY_ENTER: return "enter";
215
case GLFW_KEY_BACKSPACE: return "backspace";
216
case GLFW_KEY_INSERT: return "insertr";
217
case GLFW_KEY_DEL: return "del";
218
case GLFW_KEY_PAGEUP: return "page up";
219
case GLFW_KEY_PAGEDOWN: return "page down";
220
case GLFW_KEY_HOME: return "home";
221
case GLFW_KEY_END: return "end";
222
case GLFW_KEY_KP_0: return "0";
223
case GLFW_KEY_KP_1: return "1";
224
case GLFW_KEY_KP_2: return "2";
225
case GLFW_KEY_KP_3: return "3";
226
case GLFW_KEY_KP_4: return "4";
227
case GLFW_KEY_KP_5: return "5";
228
case GLFW_KEY_KP_6: return "6";
229
case GLFW_KEY_KP_7: return "7";
230
case GLFW_KEY_KP_8: return "8";
231
case GLFW_KEY_KP_9: return "9";
232
case GLFW_KEY_KP_DIVIDE: return "/";
233
case GLFW_KEY_KP_MULTIPLY: return "*";
234
case GLFW_KEY_KP_SUBTRACT: return "-";
235
case GLFW_KEY_KP_ADD: return "+";
236
case GLFW_KEY_KP_DECIMAL: return ".";
237
case GLFW_KEY_KP_EQUAL: return "=";
238
case GLFW_KEY_KP_ENTER: return "enter";
239
case GLFW_KEY_KP_NUM_LOCK: return "num lock";
240
case GLFW_KEY_CAPS_LOCK: return "caps lock";
241
case GLFW_KEY_SCROLL_LOCK: return "scroll lock";
242
case GLFW_KEY_PAUSE: return "pause";
243
case GLFW_KEY_LSUPER: return "left super";
244
case GLFW_KEY_RSUPER: return "right super";
245
case GLFW_KEY_MENU: return "menu";
246
}
247
char* chr = malloc(2*sizeof(char));
248
chr[0] = key;
249
chr[1] = '\0';
250
return chr;
251
}
252
253
void OnKeyPressed( int key, int action ){
254
const char* key_name = GetKeyName(key);
255
if (key_name == 0) {
256
return;
257
}
258
if (action == GLFW_PRESS) {
259
printf("'%s' (%i) key is pressed\n", key_name, key);
260
}
261
if (action == GLFW_RELEASE) {
262
printf("'%s' (%i) key is released\n", key_name, key);
263
}
264
if (action == GLFW_RELEASE && key == GLFW_KEY_ENTER) {
265
PullInfo();
266
}
267
}
268
269
int OnClose(){
270
printf("Closed\n");
271
return 0;
272
}
273
274
void OnRefresh(){
275
printf("Refresh\n");
276
}
277
278
void OnResize( int width, int height ){
279
on_resize_called = 1;
280
printf("Resizing to %i %i\n", width, height);
281
}
282
283
void OnMouseClick( int button, int action ){
284
if (action == GLFW_PRESS) {
285
printf("Mouse button %i has been pressed\n", button);
286
}
287
if (action == GLFW_RELEASE) {
288
printf("Mouse button %i has been released\n", button);
289
}
290
}
291
292
void OnMouseMove(int x, int y) {
293
int lState = glfwGetMouseButton(GLFW_MOUSE_BUTTON_LEFT);
294
295
if (lState == GLFW_PRESS) {
296
printf("Dragged %i to %i %i\n", GLFW_MOUSE_BUTTON_LEFT, x, y);
297
}
298
if (lState == GLFW_RELEASE) {
299
printf("Moved %i to %i %i\n", GLFW_MOUSE_BUTTON_LEFT, x, y);
300
}
301
}
302
303
void OnMouseWheel(int pos) {
304
printf("Mouse wheel has been moved to %i\n", pos);
305
}
306
307
void OnCharPressed( int character, int action ){
308
if (action == GLFW_PRESS) {
309
printf("'%c' (%i) char is pressed\n", character, character);
310
}
311
if (action == GLFW_RELEASE) {
312
printf("'%c' (%i) char is released\n", character, character);
313
}
314
}
315
316
void Init() {
317
const int window_width = 800,
318
window_height = 600;
319
320
if (glfwInit() != GL_TRUE) {
321
Shut_Down(1);
322
}
323
324
glfwEnable(GLFW_KEY_REPEAT); // test for issue #3059
325
326
int red_bits = glfwGetWindowParam(GLFW_RED_BITS);
327
glfwOpenWindowHint(GLFW_RED_BITS, 8);
328
assert(glfwGetWindowParam(GLFW_RED_BITS) == 8);
329
glfwOpenWindowHint(GLFW_RED_BITS, red_bits);
330
331
// 800 x 600, 16 bit color, no depth, alpha or stencil buffers, windowed
332
if (glfwOpenWindow(window_width, window_height, 5, 6, 5,
333
0, 0, 0, GLFW_WINDOW) != GL_TRUE) {
334
Shut_Down(1);
335
}
336
glfwSetWindowTitle("The GLFW Window");
337
338
glfwSetKeyCallback(OnKeyPressed);
339
glfwSetCharCallback(OnCharPressed);
340
glfwSetWindowCloseCallback(OnClose);
341
glfwSetWindowSizeCallback(OnResize);
342
if (!on_resize_called) {
343
Shut_Down(1);
344
}
345
346
glfwSetWindowRefreshCallback(OnRefresh);
347
glfwSetMouseWheelCallback(OnMouseWheel);
348
glfwSetMousePosCallback(OnMouseMove);
349
glfwSetMouseButtonCallback(OnMouseClick);
350
351
// set the projection matrix to a normal frustum with a max depth of 50
352
glMatrixMode(GL_PROJECTION);
353
glLoadIdentity();
354
float aspect_ratio = ((float)window_height) / window_width;
355
glFrustum(.5, -.5, -.5 * aspect_ratio, .5 * aspect_ratio, 1, 50);
356
glMatrixMode(GL_MODELVIEW);
357
358
PullInfo();
359
}
360
361
void Draw(void) {
362
int width, height, x;
363
double t;
364
365
t = glfwGetTime ();
366
glfwGetMousePos (&x, NULL);
367
368
// Get window size (may be different than the requested size)
369
glfwGetWindowSize (&width, &height);
370
371
// Special case: avoid division by zero below
372
height = height > 0 ? height : 1;
373
374
glViewport (0, 0, width, height);
375
// Clear color buffer to black
376
glClearColor (0.1f, 0.2f, 0.3f, 0.0f);
377
glClear (GL_COLOR_BUFFER_BIT);
378
379
// Select and setup the projection matrix
380
glMatrixMode (GL_PROJECTION);
381
glLoadIdentity ();
382
gluPerspective (65.0f, (GLfloat) width / (GLfloat) height, 1.0f, 100.0f);
383
384
// Select and setup the modelview matrix
385
glMatrixMode (GL_MODELVIEW);
386
glLoadIdentity ();
387
gluLookAt (0.0f, 1.0f, 0.0f, // Eye-position
388
0.0f, 20.0f, 0.0f, // View-point
389
0.0f, 0.0f, 1.0f); // Up-vector
390
391
// Draw a rotating colorful triangle
392
//glTranslatef (0.0f, 14.0f, 0.0f);
393
glTranslatef (0.0f, 1.0f, 0.0f);
394
glRotatef (0.3f * (GLfloat) x + (GLfloat) t * 100.0f, 0.0f, 0.0f, 1.0f);
395
glBegin (GL_TRIANGLES);
396
glColor3f (1.0f, 0.0f, 0.0f);
397
glVertex3f (-5.0f, 0.0f, -4.0f);
398
glColor3f (0.0f, 1.0f, 0.0f);
399
glVertex3f (5.0f, 0.0f, -4.0f);
400
glColor3f (0.0f, 0.0f, 1.0f);
401
glVertex3f (0.0f, 0.0f, 6.0f);
402
glEnd ();
403
404
// Swap buffers
405
glfwSwapBuffers ();
406
}
407
408
// the time of the previous frame
409
double old_time;
410
411
void Iteration() {
412
// calculate time elapsed, and the amount by which stuff rotates
413
double current_time = glfwGetTime(),
414
delta_rotate = (current_time - old_time) * rotations_per_tick * 360;
415
old_time = current_time;
416
if (glfwGetKey(GLFW_KEY_LEFT) == GLFW_PRESS)
417
rotate_y += delta_rotate;
418
if (glfwGetKey(GLFW_KEY_RIGHT) == GLFW_PRESS)
419
rotate_y -= delta_rotate;
420
// z axis always rotates
421
rotate_z += delta_rotate;
422
423
// clear the buffer
424
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
425
// draw the figure
426
Draw();
427
428
// Test that glfwSwapInterval doesn't crash (although we don't test actual timings)
429
static int i = 0;
430
glfwSwapInterval(i);
431
if (i < 2) ++i;
432
433
// swap back and front buffers
434
glfwSwapBuffers();
435
}
436
437
int main() {
438
Init();
439
old_time = glfwGetTime();
440
emscripten_set_main_loop(Iteration, 0, 1);
441
Shut_Down(0);
442
}
443
444