Platformer in OpenGL
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

97 rindas
2.7 KiB

pirms 5 gadiem
  1. //========================================================================
  2. // Event wait timeout 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 waiting for events with timeout works
  27. //
  28. //========================================================================
  29. #include <glad/glad.h>
  30. #include <GLFW/glfw3.h>
  31. #include <time.h>
  32. #include <math.h>
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. static void error_callback(int error, const char* description)
  36. {
  37. fprintf(stderr, "Error: %s\n", description);
  38. }
  39. static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
  40. {
  41. if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  42. glfwSetWindowShouldClose(window, GLFW_TRUE);
  43. }
  44. static float nrand(void)
  45. {
  46. return (float) rand() / (float) RAND_MAX;
  47. }
  48. int main(void)
  49. {
  50. GLFWwindow* window;
  51. srand((unsigned int) time(NULL));
  52. glfwSetErrorCallback(error_callback);
  53. if (!glfwInit())
  54. exit(EXIT_FAILURE);
  55. window = glfwCreateWindow(640, 480, "Event Wait Timeout Test", NULL, NULL);
  56. if (!window)
  57. {
  58. glfwTerminate();
  59. exit(EXIT_FAILURE);
  60. }
  61. glfwMakeContextCurrent(window);
  62. gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
  63. glfwSetKeyCallback(window, key_callback);
  64. while (!glfwWindowShouldClose(window))
  65. {
  66. int width, height;
  67. float r = nrand(), g = nrand(), b = nrand();
  68. float l = (float) sqrt(r * r + g * g + b * b);
  69. glfwGetFramebufferSize(window, &width, &height);
  70. glViewport(0, 0, width, height);
  71. glClearColor(r / l, g / l, b / l, 1.f);
  72. glClear(GL_COLOR_BUFFER_BIT);
  73. glfwSwapBuffers(window);
  74. glfwWaitEventsTimeout(1.0);
  75. }
  76. glfwDestroyWindow(window);
  77. glfwTerminate();
  78. exit(EXIT_SUCCESS);
  79. }