Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/test/browser/glut_fullscreen.c
7085 views
1
/*
2
* Copyright 2018 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
#ifdef __EMSCRIPTEN__
9
#define GL_GLEXT_PROTOTYPES
10
#include <GL/gl.h>
11
#else
12
#include <GL/glew.h>
13
#endif
14
#include <GL/glut.h>
15
#ifdef __EMSCRIPTEN__
16
#include <emscripten.h>
17
#include <emscripten/html5.h>
18
#endif
19
#include <stdio.h>
20
#include <stdlib.h>
21
#include <string.h>
22
#include <time.h>
23
24
#ifndef __EMSCRIPTEN__
25
int fullscreen;
26
#endif
27
28
void trace(char* tag) {
29
static char* prev_tag = NULL;
30
static int prev_screen_width;
31
static int prev_screen_height;
32
static int prev_window_width;
33
static int prev_window_height;
34
static int prev_viewport_width;
35
static int prev_viewport_height;
36
static int prev_is_fullscreen;
37
static int prev_is_resized;
38
static int prev_coalesced;
39
int screen_width;
40
int screen_height;
41
int window_width;
42
int window_height;
43
int viewport_width;
44
int viewport_height;
45
int is_fullscreen;
46
int is_resized;
47
int coalesced;
48
49
GLint viewport[4];
50
screen_width = glutGet(GLUT_SCREEN_WIDTH);
51
screen_height = glutGet(GLUT_SCREEN_HEIGHT);
52
window_width = glutGet(GLUT_WINDOW_WIDTH);
53
window_height = glutGet(GLUT_WINDOW_HEIGHT);
54
glGetIntegerv(GL_VIEWPORT, viewport);
55
viewport_width = viewport[2];
56
viewport_height = viewport[3];
57
#ifdef __EMSCRIPTEN__
58
EmscriptenFullscreenChangeEvent fullscreen_status;
59
emscripten_get_fullscreen_status(&fullscreen_status);
60
is_fullscreen = fullscreen_status.isFullscreen;
61
is_fullscreen = is_fullscreen ? 1 : -1;
62
is_resized = EM_ASM_INT({
63
return document.getElementById('resize').checked;
64
});
65
is_resized = is_resized ? 1 : -1;
66
#else
67
is_fullscreen = 0;
68
is_resized = 1;
69
#endif
70
coalesced = prev_tag &&
71
!strcmp(tag, prev_tag) &&
72
screen_width == prev_screen_width &&
73
screen_height == prev_screen_height &&
74
window_width == prev_window_width &&
75
window_height == prev_window_height &&
76
viewport_width == prev_viewport_width &&
77
viewport_height == prev_viewport_height &&
78
is_fullscreen == prev_is_fullscreen &&
79
is_resized == prev_is_resized;
80
81
if (coalesced) {
82
if (!prev_coalesced) {
83
printf("...\n");
84
}
85
} else {
86
time_t t = time(NULL);
87
char* local_time = ctime(&t);
88
char* message = malloc(strlen(local_time) + strlen(tag) + 500);
89
sprintf(message, "[");
90
sprintf(message + strlen(message), "%s", local_time);
91
sprintf(message + strlen(message) - 1, " ");
92
sprintf(message + strlen(message), "%s", tag);
93
sprintf(message + strlen(message), "]");
94
sprintf(message + strlen(message), " ");
95
sprintf(message + strlen(message),
96
"screen width: %d"
97
", "
98
"screen height: %d"
99
", "
100
"window width: %d"
101
", "
102
"window height: %d"
103
", "
104
"viewport width: %d"
105
", "
106
"viewport height: %d"
107
", "
108
"fullscreen: %s"
109
", "
110
"resize: %s",
111
screen_width,
112
screen_height,
113
window_width,
114
window_height,
115
viewport_width,
116
viewport_height,
117
is_fullscreen > 0 ? "yes" : (is_fullscreen < 0 ? "no" : "unknown"),
118
is_resized > 0 ? "yes" : (is_resized < 0 ? "no" : "unknown"));
119
printf("%s\n", message);
120
free(message);
121
}
122
123
prev_tag = tag;
124
prev_screen_width = screen_width;
125
prev_screen_height = screen_height;
126
prev_window_width = window_width;
127
prev_window_height = window_height;
128
prev_viewport_width = viewport_width;
129
prev_viewport_height = viewport_height;
130
prev_is_fullscreen = is_fullscreen;
131
prev_is_resized = is_resized;
132
prev_coalesced = coalesced;
133
}
134
135
void onDisplay()
136
{
137
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
138
glDrawArrays(GL_TRIANGLES, 0, 6);
139
glutSwapBuffers();
140
trace("onDisplay");
141
}
142
143
void onReshape(int width, int height)
144
{
145
glViewport(0, 0, width, height);
146
trace("onReshape");
147
}
148
149
void onKeyboard(unsigned char key, int x, int y)
150
{
151
if (key == 'f') {
152
#ifdef __EMSCRIPTEN__
153
EmscriptenFullscreenChangeEvent fullscreen_status;
154
int fullscreen;
155
emscripten_get_fullscreen_status(&fullscreen_status);
156
fullscreen = fullscreen_status.isFullscreen;
157
#endif
158
if (fullscreen) {
159
glutReshapeWindow(glutGet(GLUT_INIT_WINDOW_WIDTH), glutGet(GLUT_INIT_WINDOW_HEIGHT));
160
} else {
161
glutFullScreen();
162
}
163
#ifndef __EMSCRIPTEN__
164
fullscreen = !fullscreen;
165
#endif
166
trace("onKeyboard");
167
}
168
}
169
170
void onIdle()
171
{
172
glutPostRedisplay();
173
}
174
175
int main(int argc, char* argv[])
176
{
177
int win;
178
GLuint vertex_shader;
179
GLuint fragment_shader;
180
GLuint program;
181
GLuint vbo;
182
GLuint vao;
183
GLfloat vertices[] = {
184
/*
185
x, y,
186
*/
187
188
-1.f, -1.f,
189
1.f, -1.f,
190
-1.f, 1.f,
191
192
1.f, -1.f,
193
1.f, 1.f,
194
-1.f, 1.f,
195
};
196
197
glutInit(&argc, argv);
198
glutInitWindowSize(600, 450);
199
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
200
win = glutCreateWindow(__FILE__);
201
202
#ifndef __EMSCRIPTEN__
203
GLenum err = glewInit();
204
if (err != GLEW_OK) {
205
printf("error: %s\n", glewGetErrorString(err));
206
glutDestroyWindow(win);
207
return EXIT_FAILURE;
208
}
209
#endif
210
211
glEnable(GL_DEPTH_TEST);
212
{
213
const GLchar* str = "precision mediump float;\n"
214
"attribute vec2 aPosition;\n"
215
"varying vec2 vPosition;\n"
216
"void main()\n"
217
"{\n"
218
" gl_Position = vec4(aPosition, 0.0, 1.0);\n"
219
" vPosition = aPosition;\n"
220
"}\n";
221
vertex_shader = glCreateShader(GL_VERTEX_SHADER);
222
glShaderSource(vertex_shader, 1, &str, NULL);
223
glCompileShader(vertex_shader);
224
{
225
GLint params;
226
GLchar* log;
227
glGetShaderiv(vertex_shader, GL_COMPILE_STATUS, &params);
228
if (params == GL_FALSE) {
229
glGetShaderiv(vertex_shader, GL_INFO_LOG_LENGTH, &params);
230
log = malloc(params);
231
glGetShaderInfoLog(vertex_shader, params, NULL, log);
232
printf("%s", log);
233
free(log);
234
glutDestroyWindow(win);
235
return EXIT_FAILURE;
236
}
237
}
238
}
239
{
240
const GLchar* str = "precision mediump float;\n"
241
"varying vec2 vPosition;\n"
242
"void main()\n"
243
"{\n"
244
" if (abs(vPosition.x) > 0.9 || abs(vPosition.y) > 0.9) {\n"
245
" gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\n"
246
" } else {\n"
247
" gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);\n"
248
" }\n"
249
"}\n";
250
fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
251
glShaderSource(fragment_shader, 1, &str, NULL);
252
glCompileShader(fragment_shader);
253
{
254
GLint params;
255
GLchar* log;
256
glGetShaderiv(fragment_shader, GL_COMPILE_STATUS, &params);
257
if (params == GL_FALSE) {
258
glGetShaderiv(fragment_shader, GL_INFO_LOG_LENGTH, &params);
259
log = malloc(params);
260
glGetShaderInfoLog(fragment_shader, params, NULL, log);
261
printf("%s", log);
262
free(log);
263
glutDestroyWindow(win);
264
return EXIT_FAILURE;
265
}
266
}
267
}
268
program = glCreateProgram();
269
glAttachShader(program, vertex_shader);
270
glAttachShader(program, fragment_shader);
271
glBindAttribLocation(program, 0, "aPosition");
272
glLinkProgram(program);
273
{
274
GLint params;
275
GLchar* log;
276
glGetProgramiv(program, GL_LINK_STATUS, &params);
277
if (params == GL_FALSE) {
278
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &params);
279
log = malloc(params);
280
glGetProgramInfoLog(program, params, NULL, log);
281
printf("%s", log);
282
free(log);
283
glutDestroyWindow(win);
284
return EXIT_FAILURE;
285
}
286
}
287
glUseProgram(program);
288
glGenBuffers(1, &vbo);
289
glBindBuffer(GL_ARRAY_BUFFER, vbo);
290
glBufferData(GL_ARRAY_BUFFER, sizeof vertices, vertices, GL_STATIC_DRAW);
291
glGenVertexArrays(1, &vao);
292
glBindVertexArray(vao);
293
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, (const GLvoid*)0);
294
glEnableVertexAttribArray(0);
295
296
#ifndef __EMSCRIPTEN__
297
fullscreen = 0;
298
#endif
299
300
printf("You should see a green rectangle with red borders.\n");
301
printf("Press 'f' or click the 'Fullscreen' button on the upper right corner to enter full screen, and press 'f' or ESC to exit.\n");
302
printf("No matter 'Resize canvas' is checked or not, you should see the whole screen filled by the rectangle when in full screen, and after exiting, the rectangle should be restored in the window.\n");
303
304
glutDisplayFunc(onDisplay);
305
glutReshapeFunc(onReshape);
306
glutKeyboardFunc(onKeyboard);
307
glutIdleFunc(onIdle);
308
glutMainLoop();
309
310
glutDestroyWindow(win);
311
return EXIT_SUCCESS;
312
}
313
314