icon.c 3.8 KB

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