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.

243 lines
6.8 KiB

5 years ago
  1. //========================================================================
  2. // Monitor information tool
  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 prints monitor and video mode information or verifies video
  27. // modes
  28. //
  29. //========================================================================
  30. #include <glad/glad.h>
  31. #include <GLFW/glfw3.h>
  32. #include <stdio.h>
  33. #include <string.h>
  34. #include <stdlib.h>
  35. #include "getopt.h"
  36. enum Mode
  37. {
  38. LIST_MODE,
  39. TEST_MODE
  40. };
  41. static void usage(void)
  42. {
  43. printf("Usage: monitors [-t]\n");
  44. printf(" monitors -h\n");
  45. }
  46. static const char* format_mode(const GLFWvidmode* mode)
  47. {
  48. static char buffer[512];
  49. sprintf(buffer,
  50. "%i x %i x %i (%i %i %i) %i Hz",
  51. mode->width, mode->height,
  52. mode->redBits + mode->greenBits + mode->blueBits,
  53. mode->redBits, mode->greenBits, mode->blueBits,
  54. mode->refreshRate);
  55. buffer[sizeof(buffer) - 1] = '\0';
  56. return buffer;
  57. }
  58. static void error_callback(int error, const char* description)
  59. {
  60. fprintf(stderr, "Error: %s\n", description);
  61. }
  62. static void framebuffer_size_callback(GLFWwindow* window, int width, int height)
  63. {
  64. printf("Framebuffer resized to %ix%i\n", width, height);
  65. glViewport(0, 0, width, height);
  66. }
  67. static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
  68. {
  69. if (key == GLFW_KEY_ESCAPE)
  70. glfwSetWindowShouldClose(window, GLFW_TRUE);
  71. }
  72. static void list_modes(GLFWmonitor* monitor)
  73. {
  74. int count, x, y, widthMM, heightMM, i;
  75. const GLFWvidmode* mode = glfwGetVideoMode(monitor);
  76. const GLFWvidmode* modes = glfwGetVideoModes(monitor, &count);
  77. glfwGetMonitorPos(monitor, &x, &y);
  78. glfwGetMonitorPhysicalSize(monitor, &widthMM, &heightMM);
  79. printf("Name: %s (%s)\n",
  80. glfwGetMonitorName(monitor),
  81. glfwGetPrimaryMonitor() == monitor ? "primary" : "secondary");
  82. printf("Current mode: %s\n", format_mode(mode));
  83. printf("Virtual position: %i %i\n", x, y);
  84. printf("Physical size: %i x %i mm (%0.2f dpi)\n",
  85. widthMM, heightMM, mode->width * 25.4f / widthMM);
  86. printf("Modes:\n");
  87. for (i = 0; i < count; i++)
  88. {
  89. printf("%3u: %s", (unsigned int) i, format_mode(modes + i));
  90. if (memcmp(mode, modes + i, sizeof(GLFWvidmode)) == 0)
  91. printf(" (current mode)");
  92. putchar('\n');
  93. }
  94. }
  95. static void test_modes(GLFWmonitor* monitor)
  96. {
  97. int i, count;
  98. GLFWwindow* window;
  99. const GLFWvidmode* modes = glfwGetVideoModes(monitor, &count);
  100. for (i = 0; i < count; i++)
  101. {
  102. const GLFWvidmode* mode = modes + i;
  103. GLFWvidmode current;
  104. glfwWindowHint(GLFW_RED_BITS, mode->redBits);
  105. glfwWindowHint(GLFW_GREEN_BITS, mode->greenBits);
  106. glfwWindowHint(GLFW_BLUE_BITS, mode->blueBits);
  107. glfwWindowHint(GLFW_REFRESH_RATE, mode->refreshRate);
  108. printf("Testing mode %u on monitor %s: %s\n",
  109. (unsigned int) i,
  110. glfwGetMonitorName(monitor),
  111. format_mode(mode));
  112. window = glfwCreateWindow(mode->width, mode->height,
  113. "Video Mode Test",
  114. glfwGetPrimaryMonitor(),
  115. NULL);
  116. if (!window)
  117. {
  118. printf("Failed to enter mode %u: %s\n",
  119. (unsigned int) i,
  120. format_mode(mode));
  121. continue;
  122. }
  123. glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
  124. glfwSetKeyCallback(window, key_callback);
  125. glfwMakeContextCurrent(window);
  126. gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
  127. glfwSwapInterval(1);
  128. glfwSetTime(0.0);
  129. while (glfwGetTime() < 5.0)
  130. {
  131. glClear(GL_COLOR_BUFFER_BIT);
  132. glfwSwapBuffers(window);
  133. glfwPollEvents();
  134. if (glfwWindowShouldClose(window))
  135. {
  136. printf("User terminated program\n");
  137. glfwTerminate();
  138. exit(EXIT_SUCCESS);
  139. }
  140. }
  141. glGetIntegerv(GL_RED_BITS, &current.redBits);
  142. glGetIntegerv(GL_GREEN_BITS, &current.greenBits);
  143. glGetIntegerv(GL_BLUE_BITS, &current.blueBits);
  144. glfwGetWindowSize(window, &current.width, &current.height);
  145. if (current.redBits != mode->redBits ||
  146. current.greenBits != mode->greenBits ||
  147. current.blueBits != mode->blueBits)
  148. {
  149. printf("*** Color bit mismatch: (%i %i %i) instead of (%i %i %i)\n",
  150. current.redBits, current.greenBits, current.blueBits,
  151. mode->redBits, mode->greenBits, mode->blueBits);
  152. }
  153. if (current.width != mode->width || current.height != mode->height)
  154. {
  155. printf("*** Size mismatch: %ix%i instead of %ix%i\n",
  156. current.width, current.height,
  157. mode->width, mode->height);
  158. }
  159. printf("Closing window\n");
  160. glfwDestroyWindow(window);
  161. window = NULL;
  162. glfwPollEvents();
  163. }
  164. }
  165. int main(int argc, char** argv)
  166. {
  167. int ch, i, count, mode = LIST_MODE;
  168. GLFWmonitor** monitors;
  169. while ((ch = getopt(argc, argv, "th")) != -1)
  170. {
  171. switch (ch)
  172. {
  173. case 'h':
  174. usage();
  175. exit(EXIT_SUCCESS);
  176. case 't':
  177. mode = TEST_MODE;
  178. break;
  179. default:
  180. usage();
  181. exit(EXIT_FAILURE);
  182. }
  183. }
  184. glfwSetErrorCallback(error_callback);
  185. if (!glfwInit())
  186. exit(EXIT_FAILURE);
  187. monitors = glfwGetMonitors(&count);
  188. for (i = 0; i < count; i++)
  189. {
  190. if (mode == LIST_MODE)
  191. list_modes(monitors[i]);
  192. else if (mode == TEST_MODE)
  193. test_modes(monitors[i]);
  194. }
  195. glfwTerminate();
  196. exit(EXIT_SUCCESS);
  197. }