1 //========================================================================
2 // Empty event test
3 // Copyright (c) Camilla Berglund <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 posting of empty events works
27 //
28 //========================================================================
29
30 #include "tinycthread.h"
31
32 #include <glad/glad.h>
33 #include <GLFW/glfw3.h>
34
35 #include <math.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38
39 static volatile int running = GLFW_TRUE;
40
error_callback(int error,const char * description)41 static void error_callback(int error, const char* description)
42 {
43 fprintf(stderr, "Error: %s\n", description);
44 }
45
thread_main(void * data)46 static int thread_main(void* data)
47 {
48 struct timespec time;
49
50 while (running)
51 {
52 clock_gettime(CLOCK_REALTIME, &time);
53 time.tv_sec += 1;
54 thrd_sleep(&time, NULL);
55
56 glfwPostEmptyEvent();
57 }
58
59 return 0;
60 }
61
key_callback(GLFWwindow * window,int key,int scancode,int action,int mods)62 static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
63 {
64 if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
65 glfwSetWindowShouldClose(window, GLFW_TRUE);
66 }
67
nrand(void)68 static float nrand(void)
69 {
70 return (float) rand() / (float) RAND_MAX;
71 }
72
main(void)73 int main(void)
74 {
75 int result;
76 thrd_t thread;
77 GLFWwindow* window;
78
79 srand((unsigned int) time(NULL));
80
81 glfwSetErrorCallback(error_callback);
82
83 if (!glfwInit())
84 exit(EXIT_FAILURE);
85
86 window = glfwCreateWindow(640, 480, "Empty Event Test", NULL, NULL);
87 if (!window)
88 {
89 glfwTerminate();
90 exit(EXIT_FAILURE);
91 }
92
93 glfwMakeContextCurrent(window);
94 gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
95 glfwSetKeyCallback(window, key_callback);
96
97 if (thrd_create(&thread, thread_main, NULL) != thrd_success)
98 {
99 fprintf(stderr, "Failed to create secondary thread\n");
100
101 glfwTerminate();
102 exit(EXIT_FAILURE);
103 }
104
105 while (running)
106 {
107 int width, height;
108 float r = nrand(), g = nrand(), b = nrand();
109 float l = (float) sqrt(r * r + g * g + b * b);
110
111 glfwGetFramebufferSize(window, &width, &height);
112
113 glViewport(0, 0, width, height);
114 glClearColor(r / l, g / l, b / l, 1.f);
115 glClear(GL_COLOR_BUFFER_BIT);
116 glfwSwapBuffers(window);
117
118 glfwWaitEvents();
119
120 if (glfwWindowShouldClose(window))
121 running = GLFW_FALSE;
122 }
123
124 glfwHideWindow(window);
125 thrd_join(thread, &result);
126 glfwDestroyWindow(window);
127
128 glfwTerminate();
129 exit(EXIT_SUCCESS);
130 }
131
132