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