Platformer in OpenGL
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
3.3 KiB

преди 5 години
  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. #include "tinycthread.h"
  30. #include <glad/glad.h>
  31. #include <GLFW/glfw3.h>
  32. #include <math.h>
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. static volatile int running = GLFW_TRUE;
  36. static void error_callback(int error, const char* description)
  37. {
  38. fprintf(stderr, "Error: %s\n", description);
  39. }
  40. static int thread_main(void* data)
  41. {
  42. struct timespec time;
  43. while (running)
  44. {
  45. clock_gettime(CLOCK_REALTIME, &time);
  46. time.tv_sec += 1;
  47. thrd_sleep(&time, NULL);
  48. glfwPostEmptyEvent();
  49. }
  50. return 0;
  51. }
  52. static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
  53. {
  54. if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  55. glfwSetWindowShouldClose(window, GLFW_TRUE);
  56. }
  57. static float nrand(void)
  58. {
  59. return (float) rand() / (float) RAND_MAX;
  60. }
  61. int main(void)
  62. {
  63. int result;
  64. thrd_t thread;
  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, "Empty Event Test", NULL, NULL);
  71. if (!window)
  72. {
  73. glfwTerminate();
  74. exit(EXIT_FAILURE);
  75. }
  76. glfwMakeContextCurrent(window);
  77. gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
  78. glfwSetKeyCallback(window, key_callback);
  79. if (thrd_create(&thread, thread_main, NULL) != thrd_success)
  80. {
  81. fprintf(stderr, "Failed to create secondary thread\n");
  82. glfwTerminate();
  83. exit(EXIT_FAILURE);
  84. }
  85. while (running)
  86. {
  87. int width, height;
  88. float r = nrand(), g = nrand(), b = nrand();
  89. float l = (float) sqrt(r * r + g * g + b * b);
  90. glfwGetFramebufferSize(window, &width, &height);
  91. glViewport(0, 0, width, height);
  92. glClearColor(r / l, g / l, b / l, 1.f);
  93. glClear(GL_COLOR_BUFFER_BIT);
  94. glfwSwapBuffers(window);
  95. glfwWaitEvents();
  96. if (glfwWindowShouldClose(window))
  97. running = GLFW_FALSE;
  98. }
  99. glfwHideWindow(window);
  100. thrd_join(thread, &result);
  101. glfwDestroyWindow(window);
  102. glfwTerminate();
  103. exit(EXIT_SUCCESS);
  104. }