1 //========================================================================
2 // Event wait timeout 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 is intended to verify that waiting for events with timeout works
27 //
28 //========================================================================
29 
30 #include <glad/gl.h>
31 #include <GLFW/glfw3.h>
32 
33 #include <time.h>
34 #include <math.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 
error_callback(int error,const char * description)38 static void error_callback(int error, const char* description)
39 {
40     fprintf(stderr, "Error: %s\n", description);
41 }
42 
key_callback(GLFWwindow * window,int key,int scancode,int action,int mods)43 static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
44 {
45     if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
46         glfwSetWindowShouldClose(window, GLFW_TRUE);
47 }
48 
nrand(void)49 static float nrand(void)
50 {
51     return (float) rand() / (float) RAND_MAX;
52 }
53 
main(void)54 int main(void)
55 {
56     GLFWwindow* window;
57 
58     srand((unsigned int) time(NULL));
59 
60     glfwSetErrorCallback(error_callback);
61 
62     if (!glfwInit())
63         exit(EXIT_FAILURE);
64 
65     window = glfwCreateWindow(640, 480, "Event Wait Timeout Test", NULL, NULL);
66     if (!window)
67     {
68         glfwTerminate();
69         exit(EXIT_FAILURE);
70     }
71 
72     glfwMakeContextCurrent(window);
73     gladLoadGL(glfwGetProcAddress);
74     glfwSetKeyCallback(window, key_callback);
75 
76     while (!glfwWindowShouldClose(window))
77     {
78         int width, height;
79         float r = nrand(), g = nrand(), b = nrand();
80         float l = (float) sqrt(r * r + g * g + b * b);
81 
82         glfwGetFramebufferSize(window, &width, &height);
83 
84         glViewport(0, 0, width, height);
85         glClearColor(r / l, g / l, b / l, 1.f);
86         glClear(GL_COLOR_BUFFER_BIT);
87         glfwSwapBuffers(window);
88 
89         glfwWaitEventsTimeout(1.0);
90     }
91 
92     glfwDestroyWindow(window);
93 
94     glfwTerminate();
95     exit(EXIT_SUCCESS);
96 }
97 
98