Platformer in OpenGL
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

166 строки
4.2 KiB

5 лет назад
  1. //========================================================================
  2. // Simple multi-window 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 creates four windows and clears each in a different color
  27. //
  28. //========================================================================
  29. #include <glad/glad.h>
  30. #include <GLFW/glfw3.h>
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include "getopt.h"
  34. static const char* titles[] =
  35. {
  36. "Red",
  37. "Green",
  38. "Blue",
  39. "Yellow"
  40. };
  41. static const struct
  42. {
  43. float r, g, b;
  44. } colors[] =
  45. {
  46. { 0.95f, 0.32f, 0.11f },
  47. { 0.50f, 0.80f, 0.16f },
  48. { 0.f, 0.68f, 0.94f },
  49. { 0.98f, 0.74f, 0.04f }
  50. };
  51. static void usage(void)
  52. {
  53. printf("Usage: windows [-h] [-b]\n");
  54. printf("Options:\n");
  55. printf(" -b create decorated windows\n");
  56. printf(" -h show this help\n");
  57. }
  58. static void error_callback(int error, const char* description)
  59. {
  60. fprintf(stderr, "Error: %s\n", description);
  61. }
  62. static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
  63. {
  64. if (action != GLFW_PRESS)
  65. return;
  66. switch (key)
  67. {
  68. case GLFW_KEY_SPACE:
  69. {
  70. int xpos, ypos;
  71. glfwGetWindowPos(window, &xpos, &ypos);
  72. glfwSetWindowPos(window, xpos, ypos);
  73. break;
  74. }
  75. case GLFW_KEY_ESCAPE:
  76. glfwSetWindowShouldClose(window, GLFW_TRUE);
  77. break;
  78. }
  79. }
  80. int main(int argc, char** argv)
  81. {
  82. int i, ch;
  83. int decorated = GLFW_FALSE;
  84. int running = GLFW_TRUE;
  85. GLFWwindow* windows[4];
  86. while ((ch = getopt(argc, argv, "bh")) != -1)
  87. {
  88. switch (ch)
  89. {
  90. case 'b':
  91. decorated = GLFW_TRUE;
  92. break;
  93. case 'h':
  94. usage();
  95. exit(EXIT_SUCCESS);
  96. default:
  97. usage();
  98. exit(EXIT_FAILURE);
  99. }
  100. }
  101. glfwSetErrorCallback(error_callback);
  102. if (!glfwInit())
  103. exit(EXIT_FAILURE);
  104. glfwWindowHint(GLFW_DECORATED, decorated);
  105. glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
  106. for (i = 0; i < 4; i++)
  107. {
  108. int left, top, right, bottom;
  109. windows[i] = glfwCreateWindow(200, 200, titles[i], NULL, NULL);
  110. if (!windows[i])
  111. {
  112. glfwTerminate();
  113. exit(EXIT_FAILURE);
  114. }
  115. glfwSetKeyCallback(windows[i], key_callback);
  116. glfwMakeContextCurrent(windows[i]);
  117. gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
  118. glClearColor(colors[i].r, colors[i].g, colors[i].b, 1.f);
  119. glfwGetWindowFrameSize(windows[i], &left, &top, &right, &bottom);
  120. glfwSetWindowPos(windows[i],
  121. 100 + (i & 1) * (200 + left + right),
  122. 100 + (i >> 1) * (200 + top + bottom));
  123. }
  124. for (i = 0; i < 4; i++)
  125. glfwShowWindow(windows[i]);
  126. while (running)
  127. {
  128. for (i = 0; i < 4; i++)
  129. {
  130. glfwMakeContextCurrent(windows[i]);
  131. glClear(GL_COLOR_BUFFER_BIT);
  132. glfwSwapBuffers(windows[i]);
  133. if (glfwWindowShouldClose(windows[i]))
  134. running = GLFW_FALSE;
  135. }
  136. glfwWaitEvents();
  137. }
  138. glfwTerminate();
  139. exit(EXIT_SUCCESS);
  140. }