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