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