Platformer in OpenGL
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

216 rader
5.6 KiB

5 år sedan
  1. //========================================================================
  2. // Joystick input 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 displays the state of every button and axis of every connected
  27. // joystick and/or gamepad
  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. #ifdef _MSC_VER
  36. #define strdup(x) _strdup(x)
  37. #endif
  38. static int joysticks[GLFW_JOYSTICK_LAST + 1];
  39. static int joystick_count = 0;
  40. static void error_callback(int error, const char* description)
  41. {
  42. fprintf(stderr, "Error: %s\n", description);
  43. }
  44. static void framebuffer_size_callback(GLFWwindow* window, int width, int height)
  45. {
  46. glViewport(0, 0, width, height);
  47. }
  48. static void draw_joystick(int index, int x, int y, int width, int height)
  49. {
  50. int i;
  51. int axis_count, button_count;
  52. const float* axes;
  53. const unsigned char* buttons;
  54. const int axis_height = 3 * height / 4;
  55. const int button_height = height / 4;
  56. axes = glfwGetJoystickAxes(joysticks[index], &axis_count);
  57. if (axis_count)
  58. {
  59. const int axis_width = width / axis_count;
  60. for (i = 0; i < axis_count; i++)
  61. {
  62. float value = axes[i] / 2.f + 0.5f;
  63. glColor3f(0.3f, 0.3f, 0.3f);
  64. glRecti(x + i * axis_width,
  65. y,
  66. x + (i + 1) * axis_width,
  67. y + axis_height);
  68. glColor3f(1.f, 1.f, 1.f);
  69. glRecti(x + i * axis_width,
  70. y + (int) (value * (axis_height - 5)),
  71. x + (i + 1) * axis_width,
  72. y + 5 + (int) (value * (axis_height - 5)));
  73. }
  74. }
  75. buttons = glfwGetJoystickButtons(joysticks[index], &button_count);
  76. if (button_count)
  77. {
  78. const int button_width = width / button_count;
  79. for (i = 0; i < button_count; i++)
  80. {
  81. if (buttons[i])
  82. glColor3f(1.f, 1.f, 1.f);
  83. else
  84. glColor3f(0.3f, 0.3f, 0.3f);
  85. glRecti(x + i * button_width,
  86. y + axis_height,
  87. x + (i + 1) * button_width,
  88. y + axis_height + button_height);
  89. }
  90. }
  91. }
  92. static void draw_joysticks(GLFWwindow* window)
  93. {
  94. int i, width, height, offset = 0;
  95. glfwGetFramebufferSize(window, &width, &height);
  96. glMatrixMode(GL_PROJECTION);
  97. glLoadIdentity();
  98. glOrtho(0.f, width, height, 0.f, 1.f, -1.f);
  99. glMatrixMode(GL_MODELVIEW);
  100. for (i = 0; i < joystick_count; i++)
  101. {
  102. draw_joystick(i,
  103. 0, offset * height / joystick_count,
  104. width, height / joystick_count);
  105. offset++;
  106. }
  107. }
  108. static void joystick_callback(int joy, int event)
  109. {
  110. if (event == GLFW_CONNECTED)
  111. {
  112. int axis_count, button_count;
  113. glfwGetJoystickAxes(joy, &axis_count);
  114. glfwGetJoystickButtons(joy, &button_count);
  115. printf("Found joystick %i named \'%s\' with %i axes, %i buttons\n",
  116. joy + 1,
  117. glfwGetJoystickName(joy),
  118. axis_count,
  119. button_count);
  120. joysticks[joystick_count++] = joy;
  121. }
  122. else if (event == GLFW_DISCONNECTED)
  123. {
  124. int i;
  125. for (i = 0; i < joystick_count; i++)
  126. {
  127. if (joysticks[i] == joy)
  128. break;
  129. }
  130. for (i = i + 1; i < joystick_count; i++)
  131. joysticks[i - 1] = joysticks[i];
  132. printf("Lost joystick %i\n", joy + 1);
  133. joystick_count--;
  134. }
  135. }
  136. static void find_joysticks(void)
  137. {
  138. int joy;
  139. for (joy = GLFW_JOYSTICK_1; joy <= GLFW_JOYSTICK_LAST; joy++)
  140. {
  141. if (glfwJoystickPresent(joy))
  142. joystick_callback(joy, GLFW_CONNECTED);
  143. }
  144. }
  145. int main(void)
  146. {
  147. GLFWwindow* window;
  148. memset(joysticks, 0, sizeof(joysticks));
  149. glfwSetErrorCallback(error_callback);
  150. if (!glfwInit())
  151. exit(EXIT_FAILURE);
  152. find_joysticks();
  153. glfwSetJoystickCallback(joystick_callback);
  154. window = glfwCreateWindow(640, 480, "Joystick Test", NULL, NULL);
  155. if (!window)
  156. {
  157. glfwTerminate();
  158. exit(EXIT_FAILURE);
  159. }
  160. glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
  161. glfwMakeContextCurrent(window);
  162. gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
  163. glfwSwapInterval(1);
  164. while (!glfwWindowShouldClose(window))
  165. {
  166. glClear(GL_COLOR_BUFFER_BIT);
  167. draw_joysticks(window);
  168. glfwSwapBuffers(window);
  169. glfwPollEvents();
  170. // Workaround for an issue with msvcrt and mintty
  171. fflush(stdout);
  172. }
  173. glfwTerminate();
  174. exit(EXIT_SUCCESS);
  175. }