icon.c 3.8 KB

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