Atlas - timeout.c

Home / ext / glfw / tests Lines: 2 | Size: 3558 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
1//======================================================================== 2// Event wait timeout test 3// Copyright (c) Camilla Löwy <[email protected]> 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#define GLAD_GL_IMPLEMENTATION 31#include <glad/gl.h> 32#define GLFW_INCLUDE_NONE 33#include <GLFW/glfw3.h> 34 35#include <time.h> 36#include <math.h> 37#include <stdio.h> 38#include <stdlib.h> 39#include <stdbool.h> 40 41static bool needs_update; 42 43static void window_close_callback(GLFWwindow* window) 44{ 45 needs_update = true; 46} 47 48static void error_callback(int error, const char* description) 49{ 50 fprintf(stderr, "Error: %s\n", description); 51} 52 53static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) 54{ 55 if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) 56 { 57 glfwSetWindowShouldClose(window, GLFW_TRUE); 58 needs_update = true; 59 } 60} 61 62static void framebuffer_size_callback(GLFWwindow* window, int width, int height) 63{ 64 needs_update = true; 65} 66 67static float nrand(void) 68{ 69 return (float) rand() / (float) RAND_MAX; 70} 71 72int main(void) 73{ 74 GLFWwindow* window; 75 76 srand((unsigned int) time(NULL)); 77 78 glfwSetErrorCallback(error_callback); 79 80 if (!glfwInit()) 81 exit(EXIT_FAILURE); 82 83 window = glfwCreateWindow(640, 480, "Event Wait Timeout Test", NULL, NULL); 84 if (!window) 85 { 86 glfwTerminate(); 87 exit(EXIT_FAILURE); 88 } 89 90 glfwMakeContextCurrent(window); 91 glfwSwapInterval(0); 92 gladLoadGL(glfwGetProcAddress); 93 glfwSetWindowCloseCallback(window, window_close_callback); 94 glfwSetKeyCallback(window, key_callback); 95 glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); 96 97 while (!glfwWindowShouldClose(window)) 98 { 99 int width, height; 100 float r = nrand(), g = nrand(), b = nrand(); 101 float l = (float) sqrt(r * r + g * g + b * b); 102 103 glfwGetFramebufferSize(window, &width, &height); 104 105 glViewport(0, 0, width, height); 106 glClearColor(r / l, g / l, b / l, 1.f); 107 glClear(GL_COLOR_BUFFER_BIT); 108 glfwSwapBuffers(window); 109 needs_update = false; 110 111 const double start = glfwGetTime(); 112 113 while (!needs_update) 114 { 115 const double elapsed = glfwGetTime() - start; 116 if (elapsed >= 1.0) 117 needs_update = true; 118 else 119 glfwWaitEventsTimeout(1.0 - elapsed); 120 } 121 } 122 123 glfwDestroyWindow(window); 124 125 glfwTerminate(); 126 exit(EXIT_SUCCESS); 127} 128 129
[FILE END]
(C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.