Platformer in OpenGL
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

298 Zeilen
8.1 KiB

vor 5 Jahren
  1. //========================================================================
  2. // Iconify/restore test program
  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 program is used to test the iconify/restore functionality for
  27. // both full screen and windowed mode windows
  28. //
  29. //========================================================================
  30. #include <glad/glad.h>
  31. #include <GLFW/glfw3.h>
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include "getopt.h"
  35. static int windowed_xpos, windowed_ypos, windowed_width, windowed_height;
  36. static void usage(void)
  37. {
  38. printf("Usage: iconify [-h] [-f [-a] [-n]]\n");
  39. printf("Options:\n");
  40. printf(" -a create windows for all monitors\n");
  41. printf(" -f create full screen window(s)\n");
  42. printf(" -h show this help\n");
  43. printf(" -n no automatic iconification of full screen windows\n");
  44. }
  45. static void error_callback(int error, const char* description)
  46. {
  47. fprintf(stderr, "Error: %s\n", description);
  48. }
  49. static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
  50. {
  51. printf("%0.2f Key %s\n",
  52. glfwGetTime(),
  53. action == GLFW_PRESS ? "pressed" : "released");
  54. if (action != GLFW_PRESS)
  55. return;
  56. switch (key)
  57. {
  58. case GLFW_KEY_I:
  59. glfwIconifyWindow(window);
  60. break;
  61. case GLFW_KEY_M:
  62. glfwMaximizeWindow(window);
  63. break;
  64. case GLFW_KEY_R:
  65. glfwRestoreWindow(window);
  66. break;
  67. case GLFW_KEY_ESCAPE:
  68. glfwSetWindowShouldClose(window, GLFW_TRUE);
  69. break;
  70. case GLFW_KEY_F11:
  71. case GLFW_KEY_ENTER:
  72. {
  73. if (mods != GLFW_MOD_ALT)
  74. return;
  75. if (glfwGetWindowMonitor(window))
  76. {
  77. glfwSetWindowMonitor(window, NULL,
  78. windowed_xpos, windowed_ypos,
  79. windowed_width, windowed_height,
  80. 0);
  81. }
  82. else
  83. {
  84. GLFWmonitor* monitor = glfwGetPrimaryMonitor();
  85. if (monitor)
  86. {
  87. const GLFWvidmode* mode = glfwGetVideoMode(monitor);
  88. glfwGetWindowPos(window, &windowed_xpos, &windowed_ypos);
  89. glfwGetWindowSize(window, &windowed_width, &windowed_height);
  90. glfwSetWindowMonitor(window, monitor,
  91. 0, 0, mode->width, mode->height,
  92. mode->refreshRate);
  93. }
  94. }
  95. break;
  96. }
  97. }
  98. }
  99. static void window_size_callback(GLFWwindow* window, int width, int height)
  100. {
  101. printf("%0.2f Window resized to %ix%i\n", glfwGetTime(), width, height);
  102. }
  103. static void framebuffer_size_callback(GLFWwindow* window, int width, int height)
  104. {
  105. printf("%0.2f Framebuffer resized to %ix%i\n", glfwGetTime(), width, height);
  106. glViewport(0, 0, width, height);
  107. }
  108. static void window_focus_callback(GLFWwindow* window, int focused)
  109. {
  110. printf("%0.2f Window %s\n",
  111. glfwGetTime(),
  112. focused ? "focused" : "defocused");
  113. }
  114. static void window_iconify_callback(GLFWwindow* window, int iconified)
  115. {
  116. printf("%0.2f Window %s\n",
  117. glfwGetTime(),
  118. iconified ? "iconified" : "restored");
  119. }
  120. static void window_refresh_callback(GLFWwindow* window)
  121. {
  122. int width, height;
  123. printf("%0.2f Window refresh\n", glfwGetTime());
  124. glfwGetFramebufferSize(window, &width, &height);
  125. glfwMakeContextCurrent(window);
  126. glEnable(GL_SCISSOR_TEST);
  127. glScissor(0, 0, width, height);
  128. glClearColor(0, 0, 0, 0);
  129. glClear(GL_COLOR_BUFFER_BIT);
  130. glScissor(0, 0, 640, 480);
  131. glClearColor(1, 1, 1, 0);
  132. glClear(GL_COLOR_BUFFER_BIT);
  133. glfwSwapBuffers(window);
  134. }
  135. static GLFWwindow* create_window(GLFWmonitor* monitor)
  136. {
  137. int width, height;
  138. GLFWwindow* window;
  139. if (monitor)
  140. {
  141. const GLFWvidmode* mode = glfwGetVideoMode(monitor);
  142. glfwWindowHint(GLFW_REFRESH_RATE, mode->refreshRate);
  143. glfwWindowHint(GLFW_RED_BITS, mode->redBits);
  144. glfwWindowHint(GLFW_GREEN_BITS, mode->greenBits);
  145. glfwWindowHint(GLFW_BLUE_BITS, mode->blueBits);
  146. width = mode->width;
  147. height = mode->height;
  148. }
  149. else
  150. {
  151. width = 640;
  152. height = 480;
  153. }
  154. window = glfwCreateWindow(width, height, "Iconify", monitor, NULL);
  155. if (!window)
  156. {
  157. glfwTerminate();
  158. exit(EXIT_FAILURE);
  159. }
  160. glfwMakeContextCurrent(window);
  161. gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
  162. return window;
  163. }
  164. int main(int argc, char** argv)
  165. {
  166. int ch, i, window_count;
  167. int auto_iconify = GLFW_TRUE, fullscreen = GLFW_FALSE, all_monitors = GLFW_FALSE;
  168. GLFWwindow** windows;
  169. while ((ch = getopt(argc, argv, "afhn")) != -1)
  170. {
  171. switch (ch)
  172. {
  173. case 'a':
  174. all_monitors = GLFW_TRUE;
  175. break;
  176. case 'h':
  177. usage();
  178. exit(EXIT_SUCCESS);
  179. case 'f':
  180. fullscreen = GLFW_TRUE;
  181. break;
  182. case 'n':
  183. auto_iconify = GLFW_FALSE;
  184. break;
  185. default:
  186. usage();
  187. exit(EXIT_FAILURE);
  188. }
  189. }
  190. glfwSetErrorCallback(error_callback);
  191. if (!glfwInit())
  192. exit(EXIT_FAILURE);
  193. glfwWindowHint(GLFW_AUTO_ICONIFY, auto_iconify);
  194. if (fullscreen && all_monitors)
  195. {
  196. int monitor_count;
  197. GLFWmonitor** monitors = glfwGetMonitors(&monitor_count);
  198. window_count = monitor_count;
  199. windows = calloc(window_count, sizeof(GLFWwindow*));
  200. for (i = 0; i < monitor_count; i++)
  201. {
  202. windows[i] = create_window(monitors[i]);
  203. if (!windows[i])
  204. break;
  205. }
  206. }
  207. else
  208. {
  209. GLFWmonitor* monitor = NULL;
  210. if (fullscreen)
  211. monitor = glfwGetPrimaryMonitor();
  212. window_count = 1;
  213. windows = calloc(window_count, sizeof(GLFWwindow*));
  214. windows[0] = create_window(monitor);
  215. }
  216. for (i = 0; i < window_count; i++)
  217. {
  218. glfwSetKeyCallback(windows[i], key_callback);
  219. glfwSetFramebufferSizeCallback(windows[i], framebuffer_size_callback);
  220. glfwSetWindowSizeCallback(windows[i], window_size_callback);
  221. glfwSetWindowFocusCallback(windows[i], window_focus_callback);
  222. glfwSetWindowIconifyCallback(windows[i], window_iconify_callback);
  223. glfwSetWindowRefreshCallback(windows[i], window_refresh_callback);
  224. window_refresh_callback(windows[i]);
  225. printf("Window is %s and %s\n",
  226. glfwGetWindowAttrib(windows[i], GLFW_ICONIFIED) ? "iconified" : "restored",
  227. glfwGetWindowAttrib(windows[i], GLFW_FOCUSED) ? "focused" : "defocused");
  228. }
  229. for (;;)
  230. {
  231. glfwWaitEvents();
  232. for (i = 0; i < window_count; i++)
  233. {
  234. if (glfwWindowShouldClose(windows[i]))
  235. break;
  236. }
  237. if (i < window_count)
  238. break;
  239. // Workaround for an issue with msvcrt and mintty
  240. fflush(stdout);
  241. }
  242. glfwTerminate();
  243. exit(EXIT_SUCCESS);
  244. }