icon.c
Go to the documentation of this file.
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/glad.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 
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 
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:
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 
112 int main(int argc, char** argv)
113 {
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);
133 
135  set_icon(window, cur_icon_color);
136 
137  while (!glfwWindowShouldClose(window))
138  {
140  glfwSwapBuffers(window);
141  glfwWaitEvents();
142  }
143 
144  glfwDestroyWindow(window);
145  glfwTerminate();
146  exit(EXIT_SUCCESS);
147 }
148 
GLint y
static int cur_icon_color
Definition: icon.c:67
const char *const logo[]
Definition: icon.c:38
The header of the GLFW 3 API.
GLFWAPI GLFWglproc glfwGetProcAddress(const char *procname)
Returns the address of the specified function for the current context.
Definition: context.c:741
void *(* GLADloadproc)(const char *name)
static GLFWwindow * window
Definition: joysticks.c:55
#define GLFW_KEY_X
Definition: glfw3.h:401
int main(int argc, char **argv)
Definition: icon.c:112
#define GLFW_KEY_SPACE
Definition: glfw3.h:360
int height
Definition: glfw3.h:1595
static void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods)
Definition: icon.c:92
GLuint64 key
Definition: glext.h:8966
GLFWAPI int glfwInit(void)
Initializes the GLFW library.
Definition: init.c:198
#define GL_COLOR_BUFFER_BIT
GLdouble x
#define GLFW_KEY_ESCAPE
Definition: glfw3.h:412
#define glClear
GLFWAPI void glfwSwapBuffers(GLFWwindow *window)
Swaps the front and back buffers of the specified window.
Definition: context.c:641
GLFWAPI void glfwMakeContextCurrent(GLFWwindow *window)
Makes the context of the specified window current for the calling thread.
Definition: context.c:611
GLenum target
Definition: glext.h:1565
const unsigned char icon_colors[5][4]
Definition: icon.c:58
GLint void * img
GLint GLint GLsizei GLint GLenum GLenum const void * pixels
action
Definition: enums.py:62
GLAPI int gladLoadGLLoader(GLADloadproc)
Definition: glad/glad.c:1697
static const textual_icon exit
Definition: model-views.h:254
GLFWAPI void glfwSetWindowShouldClose(GLFWwindow *window, int value)
Sets the close flag of the specified window.
Definition: window.c:486
static void set_icon(GLFWwindow *window, int icon_color)
Definition: icon.c:69
GLFWAPI void glfwWaitEvents(void)
Waits until events are queued and processes them.
Definition: window.c:1078
int width
Definition: glfw3.h:1592
GLFWAPI GLFWwindow * glfwCreateWindow(int width, int height, const char *title, GLFWmonitor *monitor, GLFWwindow *share)
Creates a window and its associated context.
Definition: window.c:151
#define GLFW_TRUE
One.
Definition: glfw3.h:279
GLFWAPI void glfwDestroyWindow(GLFWwindow *window)
Destroys the specified window and its context.
Definition: window.c:444
GLFWAPI void glfwTerminate(void)
Terminates the GLFW library.
Definition: init.c:243
GLFWAPI GLFWkeyfun glfwSetKeyCallback(GLFWwindow *window, GLFWkeyfun cbfun)
Sets the key callback.
Definition: input.c:791
#define NULL
Definition: tinycthread.c:47
Image data.
Definition: glfw3.h:1588
#define GLFW_PRESS
The key or mouse button was pressed.
Definition: glfw3.h:304
struct GLFWwindow GLFWwindow
GLFWAPI void glfwSetWindowIcon(GLFWwindow *window, int count, const GLFWimage *images)
Sets the icon for the specified window.
Definition: window.c:505
GLFWAPI int glfwWindowShouldClose(GLFWwindow *window)
Checks the close flag of the specified window.
Definition: window.c:477


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:47:16