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.

157 lines
4.0 KiB

5 years ago
  1. //========================================================================
  2. // Clipboard 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 clipboard functionality.
  27. //
  28. //========================================================================
  29. #include <glad/glad.h>
  30. #include <GLFW/glfw3.h>
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include "getopt.h"
  34. #if defined(__APPLE__)
  35. #define MODIFIER GLFW_MOD_SUPER
  36. #else
  37. #define MODIFIER GLFW_MOD_CONTROL
  38. #endif
  39. static void usage(void)
  40. {
  41. printf("Usage: clipboard [-h]\n");
  42. }
  43. static void error_callback(int error, const char* description)
  44. {
  45. fprintf(stderr, "Error: %s\n", description);
  46. }
  47. static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
  48. {
  49. if (action != GLFW_PRESS)
  50. return;
  51. switch (key)
  52. {
  53. case GLFW_KEY_ESCAPE:
  54. glfwSetWindowShouldClose(window, GLFW_TRUE);
  55. break;
  56. case GLFW_KEY_V:
  57. if (mods == MODIFIER)
  58. {
  59. const char* string;
  60. string = glfwGetClipboardString(window);
  61. if (string)
  62. printf("Clipboard contains \"%s\"\n", string);
  63. else
  64. printf("Clipboard does not contain a string\n");
  65. }
  66. break;
  67. case GLFW_KEY_C:
  68. if (mods == MODIFIER)
  69. {
  70. const char* string = "Hello GLFW World!";
  71. glfwSetClipboardString(window, string);
  72. printf("Setting clipboard to \"%s\"\n", string);
  73. }
  74. break;
  75. }
  76. }
  77. static void framebuffer_size_callback(GLFWwindow* window, int width, int height)
  78. {
  79. glViewport(0, 0, width, height);
  80. }
  81. int main(int argc, char** argv)
  82. {
  83. int ch;
  84. GLFWwindow* window;
  85. while ((ch = getopt(argc, argv, "h")) != -1)
  86. {
  87. switch (ch)
  88. {
  89. case 'h':
  90. usage();
  91. exit(EXIT_SUCCESS);
  92. default:
  93. usage();
  94. exit(EXIT_FAILURE);
  95. }
  96. }
  97. glfwSetErrorCallback(error_callback);
  98. if (!glfwInit())
  99. {
  100. fprintf(stderr, "Failed to initialize GLFW\n");
  101. exit(EXIT_FAILURE);
  102. }
  103. window = glfwCreateWindow(200, 200, "Clipboard Test", NULL, NULL);
  104. if (!window)
  105. {
  106. glfwTerminate();
  107. fprintf(stderr, "Failed to open GLFW window\n");
  108. exit(EXIT_FAILURE);
  109. }
  110. glfwMakeContextCurrent(window);
  111. gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
  112. glfwSwapInterval(1);
  113. glfwSetKeyCallback(window, key_callback);
  114. glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
  115. glMatrixMode(GL_PROJECTION);
  116. glOrtho(-1.f, 1.f, -1.f, 1.f, -1.f, 1.f);
  117. glMatrixMode(GL_MODELVIEW);
  118. glClearColor(0.5f, 0.5f, 0.5f, 0);
  119. while (!glfwWindowShouldClose(window))
  120. {
  121. glClear(GL_COLOR_BUFFER_BIT);
  122. glColor3f(0.8f, 0.2f, 0.4f);
  123. glRectf(-0.5f, -0.5f, 0.5f, 0.5f);
  124. glfwSwapBuffers(window);
  125. glfwWaitEvents();
  126. }
  127. glfwTerminate();
  128. exit(EXIT_SUCCESS);
  129. }