reopen.c
Go to the documentation of this file.
1 //========================================================================
2 // Window re-opener (open/close stress test)
3 // Copyright (c) Camilla Löwy <elmindreda@glfw.org>
4 //
5 // This software is provided 'as-is', without any express or implied
6 // warranty. In no event will the authors be held liable for any damages
7 // arising from the use of this software.
8 //
9 // Permission is granted to anyone to use this software for any purpose,
10 // including commercial applications, and to alter it and redistribute it
11 // freely, subject to the following restrictions:
12 //
13 // 1. The origin of this software must not be misrepresented; you must not
14 // claim that you wrote the original software. If you use this software
15 // in a product, an acknowledgment in the product documentation would
16 // be appreciated but is not required.
17 //
18 // 2. Altered source versions must be plainly marked as such, and must not
19 // be misrepresented as being the original software.
20 //
21 // 3. This notice may not be removed or altered from any source
22 // distribution.
23 //
24 //========================================================================
25 //
26 // This test came about as the result of bug #1262773
27 //
28 // It closes and re-opens the GLFW window every five seconds, alternating
29 // between windowed and full screen mode
30 //
31 // It also times and logs opening and closing actions and attempts to separate
32 // user initiated window closing from its own
33 //
34 //========================================================================
35 
36 #include <glad/glad.h>
37 #include <GLFW/glfw3.h>
38 
39 #include <time.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 
43 #include "linmath.h"
44 
45 static const char* vertex_shader_text =
46 "#version 110\n"
47 "uniform mat4 MVP;\n"
48 "attribute vec2 vPos;\n"
49 "void main()\n"
50 "{\n"
51 " gl_Position = MVP * vec4(vPos, 0.0, 1.0);\n"
52 "}\n";
53 
54 static const char* fragment_shader_text =
55 "#version 110\n"
56 "void main()\n"
57 "{\n"
58 " gl_FragColor = vec4(1.0);\n"
59 "}\n";
60 
61 static const vec2 vertices[4] =
62 {
63  { -0.5f, -0.5f },
64  { 0.5f, -0.5f },
65  { 0.5f, 0.5f },
66  { -0.5f, 0.5f }
67 };
68 
69 static void error_callback(int error, const char* description)
70 {
71  fprintf(stderr, "Error: %s\n", description);
72 }
73 
75 {
76  printf("Close callback triggered\n");
77 }
78 
79 static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
80 {
81  if (action != GLFW_PRESS)
82  return;
83 
84  switch (key)
85  {
86  case GLFW_KEY_Q:
87  case GLFW_KEY_ESCAPE:
89  break;
90  }
91 }
92 
94 {
95  double base = glfwGetTime();
96  glfwDestroyWindow(window);
97  printf("Closing window took %0.3f seconds\n", glfwGetTime() - base);
98 }
99 
100 int main(int argc, char** argv)
101 {
102  int count = 0;
103  double base;
105 
106  srand((unsigned int) time(NULL));
107 
109 
110  if (!glfwInit())
111  exit(EXIT_FAILURE);
112 
115 
116  for (;;)
117  {
118  int width, height;
119  GLFWmonitor* monitor = NULL;
120  GLuint vertex_shader, fragment_shader, program, vertex_buffer;
121  GLint mvp_location, vpos_location;
122 
123  if (count & 1)
124  {
125  int monitorCount;
126  GLFWmonitor** monitors = glfwGetMonitors(&monitorCount);
127  monitor = monitors[rand() % monitorCount];
128  }
129 
130  if (monitor)
131  {
132  const GLFWvidmode* mode = glfwGetVideoMode(monitor);
133  width = mode->width;
134  height = mode->height;
135  }
136  else
137  {
138  width = 640;
139  height = 480;
140  }
141 
142  base = glfwGetTime();
143 
144  window = glfwCreateWindow(width, height, "Window Re-opener", monitor, NULL);
145  if (!window)
146  {
147  glfwTerminate();
148  exit(EXIT_FAILURE);
149  }
150 
151  if (monitor)
152  {
153  printf("Opening full screen window on monitor %s took %0.3f seconds\n",
154  glfwGetMonitorName(monitor),
155  glfwGetTime() - base);
156  }
157  else
158  {
159  printf("Opening regular window took %0.3f seconds\n",
160  glfwGetTime() - base);
161  }
162 
165 
166  glfwMakeContextCurrent(window);
168  glfwSwapInterval(1);
169 
170  vertex_shader = glCreateShader(GL_VERTEX_SHADER);
171  glShaderSource(vertex_shader, 1, &vertex_shader_text, NULL);
172  glCompileShader(vertex_shader);
173 
174  fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
175  glShaderSource(fragment_shader, 1, &fragment_shader_text, NULL);
176  glCompileShader(fragment_shader);
177 
178  program = glCreateProgram();
179  glAttachShader(program, vertex_shader);
180  glAttachShader(program, fragment_shader);
181  glLinkProgram(program);
182 
183  mvp_location = glGetUniformLocation(program, "MVP");
184  vpos_location = glGetAttribLocation(program, "vPos");
185 
186  glGenBuffers(1, &vertex_buffer);
187  glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
189 
190  glEnableVertexAttribArray(vpos_location);
191  glVertexAttribPointer(vpos_location, 2, GL_FLOAT, GL_FALSE,
192  sizeof(vertices[0]), (void*) 0);
193 
194  glfwSetTime(0.0);
195 
196  while (glfwGetTime() < 5.0)
197  {
198  float ratio;
199  int width, height;
200  mat4x4 m, p, mvp;
201 
202  glfwGetFramebufferSize(window, &width, &height);
203  ratio = width / (float) height;
204 
205  glViewport(0, 0, width, height);
207 
208  mat4x4_ortho(p, -ratio, ratio, -1.f, 1.f, 0.f, 1.f);
209 
210  mat4x4_identity(m);
211  mat4x4_rotate_Z(m, m, (float) glfwGetTime());
212  mat4x4_mul(mvp, p, m);
213 
214  glUseProgram(program);
215  glUniformMatrix4fv(mvp_location, 1, GL_FALSE, (const GLfloat*) mvp);
217 
218  glfwSwapBuffers(window);
219  glfwPollEvents();
220 
221  if (glfwWindowShouldClose(window))
222  {
223  close_window(window);
224  printf("User closed window\n");
225 
226  glfwTerminate();
227  exit(EXIT_SUCCESS);
228  }
229  }
230 
231  printf("Closing window\n");
232  close_window(window);
233 
234  count++;
235  }
236 
237  glfwTerminate();
238 }
239 
static const char * vertex_shader_text
Definition: reopen.c:45
int height
Definition: glfw3.h:1532
#define glBufferData
#define glCreateProgram
#define GL_VERTEX_SHADER
vec4 mat4x4[4]
Definition: linmath.h:83
#define GL_STATIC_DRAW
khronos_float_t GLfloat
The header of the GLFW 3 API.
GLFWAPI GLFWglproc glfwGetProcAddress(const char *procname)
Returns the address of the specified function for the current context.
Definition: context.c:741
static void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods)
Definition: reopen.c:79
static void mat4x4_ortho(mat4x4 M, float l, float r, float b, float t, float n, float f)
Definition: linmath.h:341
#define glGetAttribLocation
static void mat4x4_identity(mat4x4 M)
Definition: linmath.h:84
GLfloat GLfloat p
Definition: glext.h:12687
const GLfloat * m
Definition: glext.h:6814
void *(* GLADloadproc)(const char *name)
static GLFWwindow * window
Definition: joysticks.c:55
int width
Definition: glfw3.h:1529
GLFWAPI const GLFWvidmode * glfwGetVideoMode(GLFWmonitor *monitor)
Returns the current mode of the specified monitor.
Definition: monitor.c:417
#define GLFW_CONTEXT_VERSION_MAJOR
Context client API major version hint and attribute.
Definition: glfw3.h:921
#define glDrawArrays
struct GLFWmonitor GLFWmonitor
GLFWAPI GLFWerrorfun glfwSetErrorCallback(GLFWerrorfun cbfun)
Sets the error callback.
Definition: init.c:309
static void close_window(GLFWwindow *window)
Definition: reopen.c:93
#define glUniformMatrix4fv
static void mat4x4_rotate_Z(mat4x4 Q, mat4x4 M, float angle)
Definition: linmath.h:247
#define glUseProgram
static const char * fragment_shader_text
Definition: reopen.c:54
#define glCreateShader
GLuint64 key
Definition: glext.h:8966
GLFWAPI int glfwInit(void)
Initializes the GLFW library.
Definition: init.c:198
GLdouble f
#define glVertexAttribPointer
GLenum mode
GLFWAPI GLFWmonitor ** glfwGetMonitors(int *count)
Returns the currently connected monitors.
Definition: monitor.c:296
int main(int argc, char **argv)
Definition: reopen.c:100
#define GL_FLOAT
GLFWAPI const char * glfwGetMonitorName(GLFWmonitor *monitor)
Returns the name of the specified monitor.
Definition: monitor.c:366
#define glEnableVertexAttribArray
#define GL_COLOR_BUFFER_BIT
GLbitfield GLuint program
Definition: glext.h:1889
int GLint
GLFWAPI void glfwSwapInterval(int interval)
Sets the swap interval for the current context.
Definition: context.c:658
#define glBindBuffer
#define GLFW_KEY_ESCAPE
Definition: glfw3.h:412
#define glClear
GLint GLsizei GLsizei height
GLFWAPI void glfwSwapBuffers(GLFWwindow *window)
Swaps the front and back buffers of the specified window.
Definition: context.c:641
GLFWAPI void glfwMakeContextCurrent(GLFWwindow *window)
Makes the context of the specified window current for the calling thread.
Definition: context.c:611
#define glGetUniformLocation
#define GL_FRAGMENT_SHADER
#define glShaderSource
GLuint * monitors
Definition: glext.h:5686
action
Definition: enums.py:62
#define glViewport
GLAPI int gladLoadGLLoader(GLADloadproc)
Definition: glad/glad.c:1697
static const textual_icon exit
Definition: model-views.h:254
#define glAttachShader
static void window_close_callback(GLFWwindow *window)
Definition: reopen.c:74
static void error_callback(int error, const char *description)
Definition: reopen.c:69
GLFWAPI void glfwSetWindowShouldClose(GLFWwindow *window, int value)
Sets the close flag of the specified window.
Definition: window.c:486
#define glCompileShader
GLFWAPI void glfwSetTime(double time)
Sets the GLFW timer.
Definition: input.c:1282
GLFWAPI GLFWwindow * glfwCreateWindow(int width, int height, const char *title, GLFWmonitor *monitor, GLFWwindow *share)
Creates a window and its associated context.
Definition: window.c:151
#define GLFW_TRUE
One.
Definition: glfw3.h:279
#define GL_FALSE
GLFWAPI double glfwGetTime(void)
Returns the value of the GLFW timer.
Definition: input.c:1275
Video mode type.
Definition: glfw3.h:1525
unsigned int GLuint
#define glGenBuffers
GLFWAPI void glfwDestroyWindow(GLFWwindow *window)
Destroys the specified window and its context.
Definition: window.c:444
GLFWAPI void glfwGetFramebufferSize(GLFWwindow *window, int *width, int *height)
Retrieves the size of the framebuffer of the specified window.
Definition: window.c:647
#define glLinkProgram
GLint GLsizei count
GLFWAPI GLFWwindowclosefun glfwSetWindowCloseCallback(GLFWwindow *window, GLFWwindowclosefun cbfun)
Sets the close callback for the specified window.
Definition: window.c:995
#define GL_TRIANGLE_FAN
GLFWAPI void glfwTerminate(void)
Terminates the GLFW library.
Definition: init.c:243
#define GLFW_KEY_Q
Definition: glfw3.h:394
GLFWAPI GLFWkeyfun glfwSetKeyCallback(GLFWwindow *window, GLFWkeyfun cbfun)
Sets the key callback.
Definition: input.c:791
GLFWAPI void glfwPollEvents(void)
Processes all pending events.
Definition: window.c:1072
#define NULL
Definition: tinycthread.c:47
static void mat4x4_mul(mat4x4 M, mat4x4 a, mat4x4 b)
Definition: linmath.h:145
static const vec2 vertices[4]
Definition: reopen.c:61
#define GLFW_CONTEXT_VERSION_MINOR
Context client API minor version hint and attribute.
Definition: glfw3.h:927
#define GLFW_PRESS
The key or mouse button was pressed.
Definition: glfw3.h:304
#define GL_ARRAY_BUFFER
struct GLFWwindow GLFWwindow
GLFWAPI void glfwWindowHint(int hint, int value)
Sets the specified window hint to the desired value.
Definition: window.c:291
GLint GLsizei width
GLFWAPI int glfwWindowShouldClose(GLFWwindow *window)
Checks the close flag of the specified window.
Definition: window.c:477


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:47:40