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.

143 rivejä
3.7 KiB

5 vuotta sitten
  1. //========================================================================
  2. // Multi-threading 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 whether the OpenGL context part of
  27. // the GLFW API is able to be used from multiple threads
  28. //
  29. //========================================================================
  30. #include "tinycthread.h"
  31. #include <glad/glad.h>
  32. #include <GLFW/glfw3.h>
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <math.h>
  36. typedef struct
  37. {
  38. GLFWwindow* window;
  39. const char* title;
  40. float r, g, b;
  41. thrd_t id;
  42. } Thread;
  43. static volatile int running = GLFW_TRUE;
  44. static void error_callback(int error, const char* description)
  45. {
  46. fprintf(stderr, "Error: %s\n", description);
  47. }
  48. static int thread_main(void* data)
  49. {
  50. const Thread* thread = data;
  51. glfwMakeContextCurrent(thread->window);
  52. glfwSwapInterval(1);
  53. while (running)
  54. {
  55. const float v = (float) fabs(sin(glfwGetTime() * 2.f));
  56. glClearColor(thread->r * v, thread->g * v, thread->b * v, 0.f);
  57. glClear(GL_COLOR_BUFFER_BIT);
  58. glfwSwapBuffers(thread->window);
  59. }
  60. glfwMakeContextCurrent(NULL);
  61. return 0;
  62. }
  63. int main(void)
  64. {
  65. int i, result;
  66. Thread threads[] =
  67. {
  68. { NULL, "Red", 1.f, 0.f, 0.f, 0 },
  69. { NULL, "Green", 0.f, 1.f, 0.f, 0 },
  70. { NULL, "Blue", 0.f, 0.f, 1.f, 0 }
  71. };
  72. const int count = sizeof(threads) / sizeof(Thread);
  73. glfwSetErrorCallback(error_callback);
  74. if (!glfwInit())
  75. exit(EXIT_FAILURE);
  76. glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
  77. for (i = 0; i < count; i++)
  78. {
  79. threads[i].window = glfwCreateWindow(200, 200,
  80. threads[i].title,
  81. NULL, NULL);
  82. if (!threads[i].window)
  83. {
  84. glfwTerminate();
  85. exit(EXIT_FAILURE);
  86. }
  87. glfwSetWindowPos(threads[i].window, 200 + 250 * i, 200);
  88. glfwShowWindow(threads[i].window);
  89. }
  90. glfwMakeContextCurrent(threads[0].window);
  91. gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
  92. glfwMakeContextCurrent(NULL);
  93. for (i = 0; i < count; i++)
  94. {
  95. if (thrd_create(&threads[i].id, thread_main, threads + i) !=
  96. thrd_success)
  97. {
  98. fprintf(stderr, "Failed to create secondary thread\n");
  99. glfwTerminate();
  100. exit(EXIT_FAILURE);
  101. }
  102. }
  103. while (running)
  104. {
  105. glfwWaitEvents();
  106. for (i = 0; i < count; i++)
  107. {
  108. if (glfwWindowShouldClose(threads[i].window))
  109. running = GLFW_FALSE;
  110. }
  111. }
  112. for (i = 0; i < count; i++)
  113. glfwHideWindow(threads[i].window);
  114. for (i = 0; i < count; i++)
  115. thrd_join(threads[i].id, &result);
  116. exit(EXIT_SUCCESS);
  117. }