Path: blob/main/test/browser/glut_fullscreen.c
7085 views
/*1* Copyright 2018 The Emscripten Authors. All rights reserved.2* Emscripten is available under two separate licenses, the MIT license and the3* University of Illinois/NCSA Open Source License. Both these licenses can be4* found in the LICENSE file.5*/67#ifdef __EMSCRIPTEN__8#define GL_GLEXT_PROTOTYPES9#include <GL/gl.h>10#else11#include <GL/glew.h>12#endif13#include <GL/glut.h>14#ifdef __EMSCRIPTEN__15#include <emscripten.h>16#include <emscripten/html5.h>17#endif18#include <stdio.h>19#include <stdlib.h>20#include <string.h>21#include <time.h>2223#ifndef __EMSCRIPTEN__24int fullscreen;25#endif2627void trace(char* tag) {28static char* prev_tag = NULL;29static int prev_screen_width;30static int prev_screen_height;31static int prev_window_width;32static int prev_window_height;33static int prev_viewport_width;34static int prev_viewport_height;35static int prev_is_fullscreen;36static int prev_is_resized;37static int prev_coalesced;38int screen_width;39int screen_height;40int window_width;41int window_height;42int viewport_width;43int viewport_height;44int is_fullscreen;45int is_resized;46int coalesced;4748GLint viewport[4];49screen_width = glutGet(GLUT_SCREEN_WIDTH);50screen_height = glutGet(GLUT_SCREEN_HEIGHT);51window_width = glutGet(GLUT_WINDOW_WIDTH);52window_height = glutGet(GLUT_WINDOW_HEIGHT);53glGetIntegerv(GL_VIEWPORT, viewport);54viewport_width = viewport[2];55viewport_height = viewport[3];56#ifdef __EMSCRIPTEN__57EmscriptenFullscreenChangeEvent fullscreen_status;58emscripten_get_fullscreen_status(&fullscreen_status);59is_fullscreen = fullscreen_status.isFullscreen;60is_fullscreen = is_fullscreen ? 1 : -1;61is_resized = EM_ASM_INT({62return document.getElementById('resize').checked;63});64is_resized = is_resized ? 1 : -1;65#else66is_fullscreen = 0;67is_resized = 1;68#endif69coalesced = prev_tag &&70!strcmp(tag, prev_tag) &&71screen_width == prev_screen_width &&72screen_height == prev_screen_height &&73window_width == prev_window_width &&74window_height == prev_window_height &&75viewport_width == prev_viewport_width &&76viewport_height == prev_viewport_height &&77is_fullscreen == prev_is_fullscreen &&78is_resized == prev_is_resized;7980if (coalesced) {81if (!prev_coalesced) {82printf("...\n");83}84} else {85time_t t = time(NULL);86char* local_time = ctime(&t);87char* message = malloc(strlen(local_time) + strlen(tag) + 500);88sprintf(message, "[");89sprintf(message + strlen(message), "%s", local_time);90sprintf(message + strlen(message) - 1, " ");91sprintf(message + strlen(message), "%s", tag);92sprintf(message + strlen(message), "]");93sprintf(message + strlen(message), " ");94sprintf(message + strlen(message),95"screen width: %d"96", "97"screen height: %d"98", "99"window width: %d"100", "101"window height: %d"102", "103"viewport width: %d"104", "105"viewport height: %d"106", "107"fullscreen: %s"108", "109"resize: %s",110screen_width,111screen_height,112window_width,113window_height,114viewport_width,115viewport_height,116is_fullscreen > 0 ? "yes" : (is_fullscreen < 0 ? "no" : "unknown"),117is_resized > 0 ? "yes" : (is_resized < 0 ? "no" : "unknown"));118printf("%s\n", message);119free(message);120}121122prev_tag = tag;123prev_screen_width = screen_width;124prev_screen_height = screen_height;125prev_window_width = window_width;126prev_window_height = window_height;127prev_viewport_width = viewport_width;128prev_viewport_height = viewport_height;129prev_is_fullscreen = is_fullscreen;130prev_is_resized = is_resized;131prev_coalesced = coalesced;132}133134void onDisplay()135{136glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);137glDrawArrays(GL_TRIANGLES, 0, 6);138glutSwapBuffers();139trace("onDisplay");140}141142void onReshape(int width, int height)143{144glViewport(0, 0, width, height);145trace("onReshape");146}147148void onKeyboard(unsigned char key, int x, int y)149{150if (key == 'f') {151#ifdef __EMSCRIPTEN__152EmscriptenFullscreenChangeEvent fullscreen_status;153int fullscreen;154emscripten_get_fullscreen_status(&fullscreen_status);155fullscreen = fullscreen_status.isFullscreen;156#endif157if (fullscreen) {158glutReshapeWindow(glutGet(GLUT_INIT_WINDOW_WIDTH), glutGet(GLUT_INIT_WINDOW_HEIGHT));159} else {160glutFullScreen();161}162#ifndef __EMSCRIPTEN__163fullscreen = !fullscreen;164#endif165trace("onKeyboard");166}167}168169void onIdle()170{171glutPostRedisplay();172}173174int main(int argc, char* argv[])175{176int win;177GLuint vertex_shader;178GLuint fragment_shader;179GLuint program;180GLuint vbo;181GLuint vao;182GLfloat vertices[] = {183/*184x, y,185*/186187-1.f, -1.f,1881.f, -1.f,189-1.f, 1.f,1901911.f, -1.f,1921.f, 1.f,193-1.f, 1.f,194};195196glutInit(&argc, argv);197glutInitWindowSize(600, 450);198glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);199win = glutCreateWindow(__FILE__);200201#ifndef __EMSCRIPTEN__202GLenum err = glewInit();203if (err != GLEW_OK) {204printf("error: %s\n", glewGetErrorString(err));205glutDestroyWindow(win);206return EXIT_FAILURE;207}208#endif209210glEnable(GL_DEPTH_TEST);211{212const GLchar* str = "precision mediump float;\n"213"attribute vec2 aPosition;\n"214"varying vec2 vPosition;\n"215"void main()\n"216"{\n"217" gl_Position = vec4(aPosition, 0.0, 1.0);\n"218" vPosition = aPosition;\n"219"}\n";220vertex_shader = glCreateShader(GL_VERTEX_SHADER);221glShaderSource(vertex_shader, 1, &str, NULL);222glCompileShader(vertex_shader);223{224GLint params;225GLchar* log;226glGetShaderiv(vertex_shader, GL_COMPILE_STATUS, ¶ms);227if (params == GL_FALSE) {228glGetShaderiv(vertex_shader, GL_INFO_LOG_LENGTH, ¶ms);229log = malloc(params);230glGetShaderInfoLog(vertex_shader, params, NULL, log);231printf("%s", log);232free(log);233glutDestroyWindow(win);234return EXIT_FAILURE;235}236}237}238{239const GLchar* str = "precision mediump float;\n"240"varying vec2 vPosition;\n"241"void main()\n"242"{\n"243" if (abs(vPosition.x) > 0.9 || abs(vPosition.y) > 0.9) {\n"244" gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\n"245" } else {\n"246" gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);\n"247" }\n"248"}\n";249fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);250glShaderSource(fragment_shader, 1, &str, NULL);251glCompileShader(fragment_shader);252{253GLint params;254GLchar* log;255glGetShaderiv(fragment_shader, GL_COMPILE_STATUS, ¶ms);256if (params == GL_FALSE) {257glGetShaderiv(fragment_shader, GL_INFO_LOG_LENGTH, ¶ms);258log = malloc(params);259glGetShaderInfoLog(fragment_shader, params, NULL, log);260printf("%s", log);261free(log);262glutDestroyWindow(win);263return EXIT_FAILURE;264}265}266}267program = glCreateProgram();268glAttachShader(program, vertex_shader);269glAttachShader(program, fragment_shader);270glBindAttribLocation(program, 0, "aPosition");271glLinkProgram(program);272{273GLint params;274GLchar* log;275glGetProgramiv(program, GL_LINK_STATUS, ¶ms);276if (params == GL_FALSE) {277glGetProgramiv(program, GL_INFO_LOG_LENGTH, ¶ms);278log = malloc(params);279glGetProgramInfoLog(program, params, NULL, log);280printf("%s", log);281free(log);282glutDestroyWindow(win);283return EXIT_FAILURE;284}285}286glUseProgram(program);287glGenBuffers(1, &vbo);288glBindBuffer(GL_ARRAY_BUFFER, vbo);289glBufferData(GL_ARRAY_BUFFER, sizeof vertices, vertices, GL_STATIC_DRAW);290glGenVertexArrays(1, &vao);291glBindVertexArray(vao);292glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, (const GLvoid*)0);293glEnableVertexAttribArray(0);294295#ifndef __EMSCRIPTEN__296fullscreen = 0;297#endif298299printf("You should see a green rectangle with red borders.\n");300printf("Press 'f' or click the 'Fullscreen' button on the upper right corner to enter full screen, and press 'f' or ESC to exit.\n");301printf("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");302303glutDisplayFunc(onDisplay);304glutReshapeFunc(onReshape);305glutKeyboardFunc(onKeyboard);306glutIdleFunc(onIdle);307glutMainLoop();308309glutDestroyWindow(win);310return EXIT_SUCCESS;311}312313314