timeout.c 3.5 KB

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