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.

148 line
3.8 KiB

5 年之前
  1. //========================================================================
  2. // Window icon 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 icon feature.
  27. //
  28. //========================================================================
  29. #include <glad/glad.h>
  30. #include <GLFW/glfw3.h>
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. // a simple glfw logo
  35. const char* const logo[] =
  36. {
  37. "................",
  38. "................",
  39. "...0000..0......",
  40. "...0.....0......",
  41. "...0.00..0......",
  42. "...0..0..0......",
  43. "...0000..0000...",
  44. "................",
  45. "................",
  46. "...000..0...0...",
  47. "...0....0...0...",
  48. "...000..0.0.0...",
  49. "...0....0.0.0...",
  50. "...0....00000...",
  51. "................",
  52. "................"
  53. };
  54. const unsigned char icon_colors[5][4] =
  55. {
  56. { 0, 0, 0, 255 }, // black
  57. { 255, 0, 0, 255 }, // red
  58. { 0, 255, 0, 255 }, // green
  59. { 0, 0, 255, 255 }, // blue
  60. { 255, 255, 255, 255 } // white
  61. };
  62. static int cur_icon_color = 0;
  63. static void set_icon(GLFWwindow* window, int icon_color)
  64. {
  65. int x, y;
  66. unsigned char pixels[16 * 16 * 4];
  67. unsigned char* target = pixels;
  68. GLFWimage img = { 16, 16, pixels };
  69. for (y = 0; y < img.width; y++)
  70. {
  71. for (x = 0; x < img.height; x++)
  72. {
  73. if (logo[y][x] == '0')
  74. memcpy(target, icon_colors[icon_color], 4);
  75. else
  76. memset(target, 0, 4);
  77. target += 4;
  78. }
  79. }
  80. glfwSetWindowIcon(window, 1, &img);
  81. }
  82. static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
  83. {
  84. if (action != GLFW_PRESS)
  85. return;
  86. switch (key)
  87. {
  88. case GLFW_KEY_ESCAPE:
  89. glfwSetWindowShouldClose(window, GLFW_TRUE);
  90. break;
  91. case GLFW_KEY_SPACE:
  92. cur_icon_color = (cur_icon_color + 1) % 5;
  93. set_icon(window, cur_icon_color);
  94. break;
  95. case GLFW_KEY_X:
  96. glfwSetWindowIcon(window, 0, NULL);
  97. break;
  98. }
  99. }
  100. int main(int argc, char** argv)
  101. {
  102. GLFWwindow* window;
  103. if (!glfwInit())
  104. {
  105. fprintf(stderr, "Failed to initialize GLFW\n");
  106. exit(EXIT_FAILURE);
  107. }
  108. window = glfwCreateWindow(200, 200, "Window Icon", NULL, NULL);
  109. if (!window)
  110. {
  111. glfwTerminate();
  112. fprintf(stderr, "Failed to open GLFW window\n");
  113. exit(EXIT_FAILURE);
  114. }
  115. glfwMakeContextCurrent(window);
  116. gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
  117. glfwSetKeyCallback(window, key_callback);
  118. set_icon(window, cur_icon_color);
  119. while (!glfwWindowShouldClose(window))
  120. {
  121. glClear(GL_COLOR_BUFFER_BIT);
  122. glfwSwapBuffers(window);
  123. glfwWaitEvents();
  124. }
  125. glfwDestroyWindow(window);
  126. glfwTerminate();
  127. exit(EXIT_SUCCESS);
  128. }