Platformer in OpenGL
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

188 行
4.7 KiB

5年前
  1. //========================================================================
  2. // Gamma correction 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 gamma correction 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. #define STEP_SIZE 0.1f
  36. static GLfloat gamma_value = 1.0f;
  37. static void usage(void)
  38. {
  39. printf("Usage: gamma [-h] [-f]\n");
  40. printf("Options:\n");
  41. printf(" -f create full screen window\n");
  42. printf(" -h show this help\n");
  43. }
  44. static void set_gamma(GLFWwindow* window, float value)
  45. {
  46. GLFWmonitor* monitor = glfwGetWindowMonitor(window);
  47. if (!monitor)
  48. monitor = glfwGetPrimaryMonitor();
  49. gamma_value = value;
  50. printf("Gamma: %f\n", gamma_value);
  51. glfwSetGamma(monitor, gamma_value);
  52. }
  53. static void error_callback(int error, const char* description)
  54. {
  55. fprintf(stderr, "Error: %s\n", description);
  56. }
  57. static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
  58. {
  59. if (action != GLFW_PRESS)
  60. return;
  61. switch (key)
  62. {
  63. case GLFW_KEY_ESCAPE:
  64. {
  65. glfwSetWindowShouldClose(window, GLFW_TRUE);
  66. break;
  67. }
  68. case GLFW_KEY_KP_ADD:
  69. case GLFW_KEY_UP:
  70. case GLFW_KEY_Q:
  71. {
  72. set_gamma(window, gamma_value + STEP_SIZE);
  73. break;
  74. }
  75. case GLFW_KEY_KP_SUBTRACT:
  76. case GLFW_KEY_DOWN:
  77. case GLFW_KEY_W:
  78. {
  79. if (gamma_value - STEP_SIZE > 0.f)
  80. set_gamma(window, gamma_value - STEP_SIZE);
  81. break;
  82. }
  83. }
  84. }
  85. static void framebuffer_size_callback(GLFWwindow* window, int width, int height)
  86. {
  87. glViewport(0, 0, width, height);
  88. }
  89. int main(int argc, char** argv)
  90. {
  91. int width, height, ch;
  92. GLFWmonitor* monitor = NULL;
  93. GLFWwindow* window;
  94. glfwSetErrorCallback(error_callback);
  95. if (!glfwInit())
  96. exit(EXIT_FAILURE);
  97. while ((ch = getopt(argc, argv, "fh")) != -1)
  98. {
  99. switch (ch)
  100. {
  101. case 'h':
  102. usage();
  103. exit(EXIT_SUCCESS);
  104. case 'f':
  105. monitor = glfwGetPrimaryMonitor();
  106. break;
  107. default:
  108. usage();
  109. exit(EXIT_FAILURE);
  110. }
  111. }
  112. if (monitor)
  113. {
  114. const GLFWvidmode* mode = glfwGetVideoMode(monitor);
  115. glfwWindowHint(GLFW_REFRESH_RATE, mode->refreshRate);
  116. glfwWindowHint(GLFW_RED_BITS, mode->redBits);
  117. glfwWindowHint(GLFW_GREEN_BITS, mode->greenBits);
  118. glfwWindowHint(GLFW_BLUE_BITS, mode->blueBits);
  119. width = mode->width;
  120. height = mode->height;
  121. }
  122. else
  123. {
  124. width = 200;
  125. height = 200;
  126. }
  127. window = glfwCreateWindow(width, height, "Gamma Test", monitor, NULL);
  128. if (!window)
  129. {
  130. glfwTerminate();
  131. exit(EXIT_FAILURE);
  132. }
  133. set_gamma(window, 1.f);
  134. glfwMakeContextCurrent(window);
  135. gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
  136. glfwSwapInterval(1);
  137. glfwSetKeyCallback(window, key_callback);
  138. glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
  139. glMatrixMode(GL_PROJECTION);
  140. glOrtho(-1.f, 1.f, -1.f, 1.f, -1.f, 1.f);
  141. glMatrixMode(GL_MODELVIEW);
  142. glClearColor(0.5f, 0.5f, 0.5f, 0);
  143. while (!glfwWindowShouldClose(window))
  144. {
  145. glClear(GL_COLOR_BUFFER_BIT);
  146. glColor3f(0.8f, 0.2f, 0.4f);
  147. glRectf(-0.5f, -0.5f, 0.5f, 0.5f);
  148. glfwSwapBuffers(window);
  149. glfwWaitEvents();
  150. }
  151. glfwTerminate();
  152. exit(EXIT_SUCCESS);
  153. }