This repository has been archived by the owner on Nov 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
TestIcon.cs
135 lines (118 loc) · 3.84 KB
/
TestIcon.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
namespace Glfw3.Tests
{
using OpenGL;
using System;
/// <summary>
/// This program is used to test the icon feature.
/// </summary>
/// <remarks>
/// Ported from <c>icon.c</c>.
/// </remarks>
class TestIcon : TestBase
{
// a simple glfw logo
static string[] m_Logo =
{
"................",
"................",
"...0000..0......",
"...0.....0......",
"...0.00..0......",
"...0..0..0......",
"...0000..0000...",
"................",
"................",
"...000..0...0...",
"...0....0...0...",
"...000..0.0.0...",
"...0....0.0.0...",
"...0....00000...",
"................",
"................"
};
static byte[][] m_IconColors =
{
new byte[] { 0, 0, 0, 255 }, // black
new byte[] { 255, 0, 0, 255 }, // red
new byte[] { 0, 255, 0, 255 }, // green
new byte[] { 0, 0, 255, 255 }, // blue
new byte[] { 255, 255, 255, 255 } // white
};
static int m_CurIconColor = 0;
static void SetIcon(Glfw.Window window, int iconColor)
{
var pixels = new byte[16 * 16 * 4];
var img = new Glfw.Image
{
Width = 16,
Height = 16,
Pixels = pixels
};
int index = 0;
for (int y = 0; y < img.Width; y++)
{
for (int x = 0; x < img.Height; x++)
{
if (m_Logo[y][x] == '0')
{
pixels[index] = m_IconColors[iconColor][0];
pixels[index + 1] = m_IconColors[iconColor][1];
pixels[index + 2] = m_IconColors[iconColor][2];
pixels[index + 3] = m_IconColors[iconColor][3];
}
else
{
pixels[index] = 0;
pixels[index + 1] = 0;
pixels[index + 2] = 0;
pixels[index + 3] = 0;
}
index += 4;
}
}
Glfw.SetWindowIcon(window, img);
}
static void KeyCallback(Glfw.Window window, Glfw.KeyCode key, int scancode, Glfw.InputState state, Glfw.KeyMods mods)
{
if (state != Glfw.InputState.Press)
return;
switch (key)
{
case Glfw.KeyCode.Escape:
Glfw.SetWindowShouldClose(window, true);
break;
case Glfw.KeyCode.Space:
m_CurIconColor = (m_CurIconColor + 1) % 5;
SetIcon(window, m_CurIconColor);
break;
case Glfw.KeyCode.X:
Glfw.SetWindowIcon(window, null);
break;
}
}
static void Main(string[] args)
{
Init();
Glfw.Window window;
if (!Glfw.Init())
Environment.Exit(1);
window = Glfw.CreateWindow(200, 200, "Window Icon");
if (!window)
{
Glfw.Terminate();
Environment.Exit(1);
}
Glfw.MakeContextCurrent(window);
Glfw.SetKeyCallback(window, KeyCallback);
SetIcon(window, m_CurIconColor);
while (!Glfw.WindowShouldClose(window))
{
Gl.Clear(ClearBufferMask.ColorBufferBit);
Glfw.SwapBuffers(window);
Glfw.WaitEvents();
}
Glfw.DestroyWindow(window);
Glfw.Terminate();
}
}
}