Platformer in OpenGL
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

620 wiersze
20 KiB

5 lat temu
  1. //========================================================================
  2. // Event linter (event spewer)
  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 hooks every available callback and outputs their arguments
  27. //
  28. // Log messages go to stdout, error messages to stderr
  29. //
  30. // Every event also gets a (sequential) number to aid discussion of logs
  31. //
  32. //========================================================================
  33. #include <glad/glad.h>
  34. #include <GLFW/glfw3.h>
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <ctype.h>
  38. #include <string.h>
  39. #include <locale.h>
  40. #include "getopt.h"
  41. // Event index
  42. static unsigned int counter = 0;
  43. typedef struct
  44. {
  45. GLFWwindow* window;
  46. int number;
  47. int closeable;
  48. } Slot;
  49. static void usage(void)
  50. {
  51. printf("Usage: events [-f] [-h] [-n WINDOWS]\n");
  52. printf("Options:\n");
  53. printf(" -f use full screen\n");
  54. printf(" -h show this help\n");
  55. printf(" -n the number of windows to create\n");
  56. }
  57. static const char* get_key_name(int key)
  58. {
  59. switch (key)
  60. {
  61. // Printable keys
  62. case GLFW_KEY_A: return "A";
  63. case GLFW_KEY_B: return "B";
  64. case GLFW_KEY_C: return "C";
  65. case GLFW_KEY_D: return "D";
  66. case GLFW_KEY_E: return "E";
  67. case GLFW_KEY_F: return "F";
  68. case GLFW_KEY_G: return "G";
  69. case GLFW_KEY_H: return "H";
  70. case GLFW_KEY_I: return "I";
  71. case GLFW_KEY_J: return "J";
  72. case GLFW_KEY_K: return "K";
  73. case GLFW_KEY_L: return "L";
  74. case GLFW_KEY_M: return "M";
  75. case GLFW_KEY_N: return "N";
  76. case GLFW_KEY_O: return "O";
  77. case GLFW_KEY_P: return "P";
  78. case GLFW_KEY_Q: return "Q";
  79. case GLFW_KEY_R: return "R";
  80. case GLFW_KEY_S: return "S";
  81. case GLFW_KEY_T: return "T";
  82. case GLFW_KEY_U: return "U";
  83. case GLFW_KEY_V: return "V";
  84. case GLFW_KEY_W: return "W";
  85. case GLFW_KEY_X: return "X";
  86. case GLFW_KEY_Y: return "Y";
  87. case GLFW_KEY_Z: return "Z";
  88. case GLFW_KEY_1: return "1";
  89. case GLFW_KEY_2: return "2";
  90. case GLFW_KEY_3: return "3";
  91. case GLFW_KEY_4: return "4";
  92. case GLFW_KEY_5: return "5";
  93. case GLFW_KEY_6: return "6";
  94. case GLFW_KEY_7: return "7";
  95. case GLFW_KEY_8: return "8";
  96. case GLFW_KEY_9: return "9";
  97. case GLFW_KEY_0: return "0";
  98. case GLFW_KEY_SPACE: return "SPACE";
  99. case GLFW_KEY_MINUS: return "MINUS";
  100. case GLFW_KEY_EQUAL: return "EQUAL";
  101. case GLFW_KEY_LEFT_BRACKET: return "LEFT BRACKET";
  102. case GLFW_KEY_RIGHT_BRACKET: return "RIGHT BRACKET";
  103. case GLFW_KEY_BACKSLASH: return "BACKSLASH";
  104. case GLFW_KEY_SEMICOLON: return "SEMICOLON";
  105. case GLFW_KEY_APOSTROPHE: return "APOSTROPHE";
  106. case GLFW_KEY_GRAVE_ACCENT: return "GRAVE ACCENT";
  107. case GLFW_KEY_COMMA: return "COMMA";
  108. case GLFW_KEY_PERIOD: return "PERIOD";
  109. case GLFW_KEY_SLASH: return "SLASH";
  110. case GLFW_KEY_WORLD_1: return "WORLD 1";
  111. case GLFW_KEY_WORLD_2: return "WORLD 2";
  112. // Function keys
  113. case GLFW_KEY_ESCAPE: return "ESCAPE";
  114. case GLFW_KEY_F1: return "F1";
  115. case GLFW_KEY_F2: return "F2";
  116. case GLFW_KEY_F3: return "F3";
  117. case GLFW_KEY_F4: return "F4";
  118. case GLFW_KEY_F5: return "F5";
  119. case GLFW_KEY_F6: return "F6";
  120. case GLFW_KEY_F7: return "F7";
  121. case GLFW_KEY_F8: return "F8";
  122. case GLFW_KEY_F9: return "F9";
  123. case GLFW_KEY_F10: return "F10";
  124. case GLFW_KEY_F11: return "F11";
  125. case GLFW_KEY_F12: return "F12";
  126. case GLFW_KEY_F13: return "F13";
  127. case GLFW_KEY_F14: return "F14";
  128. case GLFW_KEY_F15: return "F15";
  129. case GLFW_KEY_F16: return "F16";
  130. case GLFW_KEY_F17: return "F17";
  131. case GLFW_KEY_F18: return "F18";
  132. case GLFW_KEY_F19: return "F19";
  133. case GLFW_KEY_F20: return "F20";
  134. case GLFW_KEY_F21: return "F21";
  135. case GLFW_KEY_F22: return "F22";
  136. case GLFW_KEY_F23: return "F23";
  137. case GLFW_KEY_F24: return "F24";
  138. case GLFW_KEY_F25: return "F25";
  139. case GLFW_KEY_UP: return "UP";
  140. case GLFW_KEY_DOWN: return "DOWN";
  141. case GLFW_KEY_LEFT: return "LEFT";
  142. case GLFW_KEY_RIGHT: return "RIGHT";
  143. case GLFW_KEY_LEFT_SHIFT: return "LEFT SHIFT";
  144. case GLFW_KEY_RIGHT_SHIFT: return "RIGHT SHIFT";
  145. case GLFW_KEY_LEFT_CONTROL: return "LEFT CONTROL";
  146. case GLFW_KEY_RIGHT_CONTROL: return "RIGHT CONTROL";
  147. case GLFW_KEY_LEFT_ALT: return "LEFT ALT";
  148. case GLFW_KEY_RIGHT_ALT: return "RIGHT ALT";
  149. case GLFW_KEY_TAB: return "TAB";
  150. case GLFW_KEY_ENTER: return "ENTER";
  151. case GLFW_KEY_BACKSPACE: return "BACKSPACE";
  152. case GLFW_KEY_INSERT: return "INSERT";
  153. case GLFW_KEY_DELETE: return "DELETE";
  154. case GLFW_KEY_PAGE_UP: return "PAGE UP";
  155. case GLFW_KEY_PAGE_DOWN: return "PAGE DOWN";
  156. case GLFW_KEY_HOME: return "HOME";
  157. case GLFW_KEY_END: return "END";
  158. case GLFW_KEY_KP_0: return "KEYPAD 0";
  159. case GLFW_KEY_KP_1: return "KEYPAD 1";
  160. case GLFW_KEY_KP_2: return "KEYPAD 2";
  161. case GLFW_KEY_KP_3: return "KEYPAD 3";
  162. case GLFW_KEY_KP_4: return "KEYPAD 4";
  163. case GLFW_KEY_KP_5: return "KEYPAD 5";
  164. case GLFW_KEY_KP_6: return "KEYPAD 6";
  165. case GLFW_KEY_KP_7: return "KEYPAD 7";
  166. case GLFW_KEY_KP_8: return "KEYPAD 8";
  167. case GLFW_KEY_KP_9: return "KEYPAD 9";
  168. case GLFW_KEY_KP_DIVIDE: return "KEYPAD DIVIDE";
  169. case GLFW_KEY_KP_MULTIPLY: return "KEYPAD MULTPLY";
  170. case GLFW_KEY_KP_SUBTRACT: return "KEYPAD SUBTRACT";
  171. case GLFW_KEY_KP_ADD: return "KEYPAD ADD";
  172. case GLFW_KEY_KP_DECIMAL: return "KEYPAD DECIMAL";
  173. case GLFW_KEY_KP_EQUAL: return "KEYPAD EQUAL";
  174. case GLFW_KEY_KP_ENTER: return "KEYPAD ENTER";
  175. case GLFW_KEY_PRINT_SCREEN: return "PRINT SCREEN";
  176. case GLFW_KEY_NUM_LOCK: return "NUM LOCK";
  177. case GLFW_KEY_CAPS_LOCK: return "CAPS LOCK";
  178. case GLFW_KEY_SCROLL_LOCK: return "SCROLL LOCK";
  179. case GLFW_KEY_PAUSE: return "PAUSE";
  180. case GLFW_KEY_LEFT_SUPER: return "LEFT SUPER";
  181. case GLFW_KEY_RIGHT_SUPER: return "RIGHT SUPER";
  182. case GLFW_KEY_MENU: return "MENU";
  183. default: return "UNKNOWN";
  184. }
  185. }
  186. static const char* get_action_name(int action)
  187. {
  188. switch (action)
  189. {
  190. case GLFW_PRESS:
  191. return "pressed";
  192. case GLFW_RELEASE:
  193. return "released";
  194. case GLFW_REPEAT:
  195. return "repeated";
  196. }
  197. return "caused unknown action";
  198. }
  199. static const char* get_button_name(int button)
  200. {
  201. switch (button)
  202. {
  203. case GLFW_MOUSE_BUTTON_LEFT:
  204. return "left";
  205. case GLFW_MOUSE_BUTTON_RIGHT:
  206. return "right";
  207. case GLFW_MOUSE_BUTTON_MIDDLE:
  208. return "middle";
  209. default:
  210. {
  211. static char name[16];
  212. sprintf(name, "%i", button);
  213. return name;
  214. }
  215. }
  216. }
  217. static const char* get_mods_name(int mods)
  218. {
  219. static char name[512];
  220. if (mods == 0)
  221. return " no mods";
  222. name[0] = '\0';
  223. if (mods & GLFW_MOD_SHIFT)
  224. strcat(name, " shift");
  225. if (mods & GLFW_MOD_CONTROL)
  226. strcat(name, " control");
  227. if (mods & GLFW_MOD_ALT)
  228. strcat(name, " alt");
  229. if (mods & GLFW_MOD_SUPER)
  230. strcat(name, " super");
  231. return name;
  232. }
  233. static const char* get_character_string(int codepoint)
  234. {
  235. // This assumes UTF-8, which is stupid
  236. static char result[6 + 1];
  237. int length = wctomb(result, codepoint);
  238. if (length == -1)
  239. length = 0;
  240. result[length] = '\0';
  241. return result;
  242. }
  243. static void error_callback(int error, const char* description)
  244. {
  245. fprintf(stderr, "Error: %s\n", description);
  246. }
  247. static void window_pos_callback(GLFWwindow* window, int x, int y)
  248. {
  249. Slot* slot = glfwGetWindowUserPointer(window);
  250. printf("%08x to %i at %0.3f: Window position: %i %i\n",
  251. counter++, slot->number, glfwGetTime(), x, y);
  252. }
  253. static void window_size_callback(GLFWwindow* window, int width, int height)
  254. {
  255. Slot* slot = glfwGetWindowUserPointer(window);
  256. printf("%08x to %i at %0.3f: Window size: %i %i\n",
  257. counter++, slot->number, glfwGetTime(), width, height);
  258. }
  259. static void framebuffer_size_callback(GLFWwindow* window, int width, int height)
  260. {
  261. Slot* slot = glfwGetWindowUserPointer(window);
  262. printf("%08x to %i at %0.3f: Framebuffer size: %i %i\n",
  263. counter++, slot->number, glfwGetTime(), width, height);
  264. glViewport(0, 0, width, height);
  265. }
  266. static void window_close_callback(GLFWwindow* window)
  267. {
  268. Slot* slot = glfwGetWindowUserPointer(window);
  269. printf("%08x to %i at %0.3f: Window close\n",
  270. counter++, slot->number, glfwGetTime());
  271. glfwSetWindowShouldClose(window, slot->closeable);
  272. }
  273. static void window_refresh_callback(GLFWwindow* window)
  274. {
  275. Slot* slot = glfwGetWindowUserPointer(window);
  276. printf("%08x to %i at %0.3f: Window refresh\n",
  277. counter++, slot->number, glfwGetTime());
  278. glfwMakeContextCurrent(window);
  279. glClear(GL_COLOR_BUFFER_BIT);
  280. glfwSwapBuffers(window);
  281. }
  282. static void window_focus_callback(GLFWwindow* window, int focused)
  283. {
  284. Slot* slot = glfwGetWindowUserPointer(window);
  285. printf("%08x to %i at %0.3f: Window %s\n",
  286. counter++, slot->number, glfwGetTime(),
  287. focused ? "focused" : "defocused");
  288. }
  289. static void window_iconify_callback(GLFWwindow* window, int iconified)
  290. {
  291. Slot* slot = glfwGetWindowUserPointer(window);
  292. printf("%08x to %i at %0.3f: Window was %s\n",
  293. counter++, slot->number, glfwGetTime(),
  294. iconified ? "iconified" : "restored");
  295. }
  296. static void mouse_button_callback(GLFWwindow* window, int button, int action, int mods)
  297. {
  298. Slot* slot = glfwGetWindowUserPointer(window);
  299. printf("%08x to %i at %0.3f: Mouse button %i (%s) (with%s) was %s\n",
  300. counter++, slot->number, glfwGetTime(), button,
  301. get_button_name(button),
  302. get_mods_name(mods),
  303. get_action_name(action));
  304. }
  305. static void cursor_position_callback(GLFWwindow* window, double x, double y)
  306. {
  307. Slot* slot = glfwGetWindowUserPointer(window);
  308. printf("%08x to %i at %0.3f: Cursor position: %f %f\n",
  309. counter++, slot->number, glfwGetTime(), x, y);
  310. }
  311. static void cursor_enter_callback(GLFWwindow* window, int entered)
  312. {
  313. Slot* slot = glfwGetWindowUserPointer(window);
  314. printf("%08x to %i at %0.3f: Cursor %s window\n",
  315. counter++, slot->number, glfwGetTime(),
  316. entered ? "entered" : "left");
  317. }
  318. static void scroll_callback(GLFWwindow* window, double x, double y)
  319. {
  320. Slot* slot = glfwGetWindowUserPointer(window);
  321. printf("%08x to %i at %0.3f: Scroll: %0.3f %0.3f\n",
  322. counter++, slot->number, glfwGetTime(), x, y);
  323. }
  324. static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
  325. {
  326. Slot* slot = glfwGetWindowUserPointer(window);
  327. const char* name = glfwGetKeyName(key, scancode);
  328. if (name)
  329. {
  330. printf("%08x to %i at %0.3f: Key 0x%04x Scancode 0x%04x (%s) (%s) (with%s) was %s\n",
  331. counter++, slot->number, glfwGetTime(), key, scancode,
  332. get_key_name(key),
  333. name,
  334. get_mods_name(mods),
  335. get_action_name(action));
  336. }
  337. else
  338. {
  339. printf("%08x to %i at %0.3f: Key 0x%04x Scancode 0x%04x (%s) (with%s) was %s\n",
  340. counter++, slot->number, glfwGetTime(), key, scancode,
  341. get_key_name(key),
  342. get_mods_name(mods),
  343. get_action_name(action));
  344. }
  345. if (action != GLFW_PRESS)
  346. return;
  347. switch (key)
  348. {
  349. case GLFW_KEY_C:
  350. {
  351. slot->closeable = !slot->closeable;
  352. printf("(( closing %s ))\n", slot->closeable ? "enabled" : "disabled");
  353. break;
  354. }
  355. }
  356. }
  357. static void char_callback(GLFWwindow* window, unsigned int codepoint)
  358. {
  359. Slot* slot = glfwGetWindowUserPointer(window);
  360. printf("%08x to %i at %0.3f: Character 0x%08x (%s) input\n",
  361. counter++, slot->number, glfwGetTime(), codepoint,
  362. get_character_string(codepoint));
  363. }
  364. static void char_mods_callback(GLFWwindow* window, unsigned int codepoint, int mods)
  365. {
  366. Slot* slot = glfwGetWindowUserPointer(window);
  367. printf("%08x to %i at %0.3f: Character 0x%08x (%s) with modifiers (with%s) input\n",
  368. counter++, slot->number, glfwGetTime(), codepoint,
  369. get_character_string(codepoint),
  370. get_mods_name(mods));
  371. }
  372. static void drop_callback(GLFWwindow* window, int count, const char** paths)
  373. {
  374. int i;
  375. Slot* slot = glfwGetWindowUserPointer(window);
  376. printf("%08x to %i at %0.3f: Drop input\n",
  377. counter++, slot->number, glfwGetTime());
  378. for (i = 0; i < count; i++)
  379. printf(" %i: \"%s\"\n", i, paths[i]);
  380. }
  381. static void monitor_callback(GLFWmonitor* monitor, int event)
  382. {
  383. if (event == GLFW_CONNECTED)
  384. {
  385. int x, y, widthMM, heightMM;
  386. const GLFWvidmode* mode = glfwGetVideoMode(monitor);
  387. glfwGetMonitorPos(monitor, &x, &y);
  388. glfwGetMonitorPhysicalSize(monitor, &widthMM, &heightMM);
  389. printf("%08x at %0.3f: Monitor %s (%ix%i at %ix%i, %ix%i mm) was connected\n",
  390. counter++,
  391. glfwGetTime(),
  392. glfwGetMonitorName(monitor),
  393. mode->width, mode->height,
  394. x, y,
  395. widthMM, heightMM);
  396. }
  397. else if (event == GLFW_DISCONNECTED)
  398. {
  399. printf("%08x at %0.3f: Monitor %s was disconnected\n",
  400. counter++,
  401. glfwGetTime(),
  402. glfwGetMonitorName(monitor));
  403. }
  404. }
  405. static void joystick_callback(int joy, int event)
  406. {
  407. if (event == GLFW_CONNECTED)
  408. {
  409. int axisCount, buttonCount;
  410. glfwGetJoystickAxes(joy, &axisCount);
  411. glfwGetJoystickButtons(joy, &buttonCount);
  412. printf("%08x at %0.3f: Joystick %i (%s) was connected with %i axes and %i buttons\n",
  413. counter++, glfwGetTime(),
  414. joy,
  415. glfwGetJoystickName(joy),
  416. axisCount,
  417. buttonCount);
  418. }
  419. else
  420. {
  421. printf("%08x at %0.3f: Joystick %i was disconnected\n",
  422. counter++, glfwGetTime(), joy);
  423. }
  424. }
  425. int main(int argc, char** argv)
  426. {
  427. Slot* slots;
  428. GLFWmonitor* monitor = NULL;
  429. int ch, i, width, height, count = 1;
  430. setlocale(LC_ALL, "");
  431. glfwSetErrorCallback(error_callback);
  432. if (!glfwInit())
  433. exit(EXIT_FAILURE);
  434. printf("Library initialized\n");
  435. glfwSetMonitorCallback(monitor_callback);
  436. glfwSetJoystickCallback(joystick_callback);
  437. while ((ch = getopt(argc, argv, "hfn:")) != -1)
  438. {
  439. switch (ch)
  440. {
  441. case 'h':
  442. usage();
  443. exit(EXIT_SUCCESS);
  444. case 'f':
  445. monitor = glfwGetPrimaryMonitor();
  446. break;
  447. case 'n':
  448. count = (int) strtol(optarg, NULL, 10);
  449. break;
  450. default:
  451. usage();
  452. exit(EXIT_FAILURE);
  453. }
  454. }
  455. if (monitor)
  456. {
  457. const GLFWvidmode* mode = glfwGetVideoMode(monitor);
  458. glfwWindowHint(GLFW_REFRESH_RATE, mode->refreshRate);
  459. glfwWindowHint(GLFW_RED_BITS, mode->redBits);
  460. glfwWindowHint(GLFW_GREEN_BITS, mode->greenBits);
  461. glfwWindowHint(GLFW_BLUE_BITS, mode->blueBits);
  462. width = mode->width;
  463. height = mode->height;
  464. }
  465. else
  466. {
  467. width = 640;
  468. height = 480;
  469. }
  470. if (!count)
  471. {
  472. fprintf(stderr, "Invalid user\n");
  473. exit(EXIT_FAILURE);
  474. }
  475. slots = calloc(count, sizeof(Slot));
  476. for (i = 0; i < count; i++)
  477. {
  478. char title[128];
  479. slots[i].closeable = GLFW_TRUE;
  480. slots[i].number = i + 1;
  481. sprintf(title, "Event Linter (Window %i)", slots[i].number);
  482. if (monitor)
  483. {
  484. printf("Creating full screen window %i (%ix%i on %s)\n",
  485. slots[i].number,
  486. width, height,
  487. glfwGetMonitorName(monitor));
  488. }
  489. else
  490. {
  491. printf("Creating windowed mode window %i (%ix%i)\n",
  492. slots[i].number,
  493. width, height);
  494. }
  495. slots[i].window = glfwCreateWindow(width, height, title, monitor, NULL);
  496. if (!slots[i].window)
  497. {
  498. free(slots);
  499. glfwTerminate();
  500. exit(EXIT_FAILURE);
  501. }
  502. glfwSetWindowUserPointer(slots[i].window, slots + i);
  503. glfwSetWindowPosCallback(slots[i].window, window_pos_callback);
  504. glfwSetWindowSizeCallback(slots[i].window, window_size_callback);
  505. glfwSetFramebufferSizeCallback(slots[i].window, framebuffer_size_callback);
  506. glfwSetWindowCloseCallback(slots[i].window, window_close_callback);
  507. glfwSetWindowRefreshCallback(slots[i].window, window_refresh_callback);
  508. glfwSetWindowFocusCallback(slots[i].window, window_focus_callback);
  509. glfwSetWindowIconifyCallback(slots[i].window, window_iconify_callback);
  510. glfwSetMouseButtonCallback(slots[i].window, mouse_button_callback);
  511. glfwSetCursorPosCallback(slots[i].window, cursor_position_callback);
  512. glfwSetCursorEnterCallback(slots[i].window, cursor_enter_callback);
  513. glfwSetScrollCallback(slots[i].window, scroll_callback);
  514. glfwSetKeyCallback(slots[i].window, key_callback);
  515. glfwSetCharCallback(slots[i].window, char_callback);
  516. glfwSetCharModsCallback(slots[i].window, char_mods_callback);
  517. glfwSetDropCallback(slots[i].window, drop_callback);
  518. glfwMakeContextCurrent(slots[i].window);
  519. gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
  520. glfwSwapInterval(1);
  521. }
  522. printf("Main loop starting\n");
  523. for (;;)
  524. {
  525. for (i = 0; i < count; i++)
  526. {
  527. if (glfwWindowShouldClose(slots[i].window))
  528. break;
  529. }
  530. if (i < count)
  531. break;
  532. glfwWaitEvents();
  533. // Workaround for an issue with msvcrt and mintty
  534. fflush(stdout);
  535. }
  536. free(slots);
  537. glfwTerminate();
  538. exit(EXIT_SUCCESS);
  539. }