-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
214 lines (185 loc) · 5.52 KB
/
main.cpp
File metadata and controls
214 lines (185 loc) · 5.52 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include <SDL.h>
#include <iostream>
#include <random>
#include <vector>
#include <cmath>
// Glow configuration
const int BORDER_THICKNESS = 20;
const Uint32 FADE_DURATION = 1500;
struct Glow
{
Uint8 r, g, b;
Uint32 startTime;
float alpha;
};
std::vector<Glow> activeGlows;
// Random number generator
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> hueDist(0, 360);
std::uniform_real_distribution<> satDist(0.8, 1.0);
std::uniform_real_distribution<> valDist(0.8, 1.0);
void logSDLError(const std::string &msg)
{
std::cerr << msg << " Error: " << SDL_GetError() << std::endl;
}
// Convert HSV to RGB for vibrant colors
void hsvToRgb(float h, float s, float v, Uint8 &r, Uint8 &g, Uint8 &b)
{
float c = v * s;
float x = c * (1.0f - std::abs(std::fmod(h / 60.0f, 2.0f) - 1.0f));
float m = v - c;
float r_prime, g_prime, b_prime;
if (h < 60)
{
r_prime = c;
g_prime = x;
b_prime = 0;
}
else if (h < 120)
{
r_prime = x;
g_prime = c;
b_prime = 0;
}
else if (h < 180)
{
r_prime = 0;
g_prime = c;
b_prime = x;
}
else if (h < 240)
{
r_prime = 0;
g_prime = x;
b_prime = c;
}
else if (h < 300)
{
r_prime = x;
g_prime = 0;
b_prime = c;
}
else
{
r_prime = c;
g_prime = 0;
b_prime = x;
}
r = static_cast<Uint8>((r_prime + m) * 255);
g = static_cast<Uint8>((g_prime + m) * 255);
b = static_cast<Uint8>((b_prime + m) * 255);
}
int main(int argc, char *argv[])
{
if (SDL_Init(SDL_INIT_VIDEO) != 0)
{
logSDLError("SDL_Init Error");
return 1;
}
// Get usable display bounds (excludes menu bar and dock)
SDL_Rect usableBounds;
if (SDL_GetDisplayUsableBounds(0, &usableBounds) != 0)
{
logSDLError("SDL_GetDisplayUsableBounds Error");
SDL_Quit();
return 1;
}
const int SCREEN_WIDTH = usableBounds.w;
const int SCREEN_HEIGHT = usableBounds.h;
// Fullscreen borderless window (positioned below menu bar)
SDL_Window *window = SDL_CreateWindow(
"Keyboard Glow",
usableBounds.x, usableBounds.y,
SCREEN_WIDTH, SCREEN_HEIGHT,
SDL_WINDOW_BORDERLESS);
if (window == nullptr)
{
logSDLError("SDL_CreateWindow Error");
SDL_Quit();
return 1;
}
// Make window semi-transparent so you can see through the center
SDL_SetWindowOpacity(window, 0.3f);
SDL_Renderer *renderer = SDL_CreateRenderer(
window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (renderer == nullptr)
{
logSDLError("SDL_CreateRenderer Error");
SDL_DestroyWindow(window);
SDL_Quit();
return 1;
}
std::cout << "Keyboard Glow Visualizer running!\n";
std::cout << "Type while this window is focused to see colors.\n";
std::cout << "Press ESC to quit.\n";
SDL_Event e;
bool quit = false;
while (!quit)
{
while (SDL_PollEvent(&e))
{
if (e.type == SDL_QUIT)
{
quit = true;
}
else if (e.type == SDL_KEYDOWN)
{
if (e.key.keysym.sym == SDLK_ESCAPE)
{
quit = true;
}
else
{
// Generate random vibrant color
float hue = hueDist(gen);
float sat = satDist(gen);
float val = valDist(gen);
Glow newGlow;
hsvToRgb(hue, sat, val, newGlow.r, newGlow.g, newGlow.b);
newGlow.startTime = SDL_GetTicks();
newGlow.alpha = 1.0f;
activeGlows.push_back(newGlow);
}
}
}
// Update glows
Uint32 currentTime = SDL_GetTicks();
for (auto it = activeGlows.begin(); it != activeGlows.end();)
{
Uint32 elapsed = currentTime - it->startTime;
if (elapsed < FADE_DURATION)
{
it->alpha = 1.0f - (float)elapsed / FADE_DURATION;
++it;
}
else
{
it = activeGlows.erase(it);
}
}
// Render
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
// Border rectangles (connected at corners)
SDL_Rect topBorder = {0, 0, SCREEN_WIDTH, BORDER_THICKNESS};
SDL_Rect bottomBorder = {0, SCREEN_HEIGHT - BORDER_THICKNESS, SCREEN_WIDTH, BORDER_THICKNESS};
SDL_Rect leftBorder = {0, BORDER_THICKNESS, BORDER_THICKNESS, SCREEN_HEIGHT - 2 * BORDER_THICKNESS};
SDL_Rect rightBorder = {SCREEN_WIDTH - BORDER_THICKNESS, BORDER_THICKNESS, BORDER_THICKNESS, SCREEN_HEIGHT - 2 * BORDER_THICKNESS};
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_ADD);
for (const auto &glow : activeGlows)
{
Uint8 alphaValue = static_cast<Uint8>(glow.alpha * 255);
SDL_SetRenderDrawColor(renderer, glow.r, glow.g, glow.b, alphaValue);
SDL_RenderFillRect(renderer, &topBorder);
SDL_RenderFillRect(renderer, &bottomBorder);
SDL_RenderFillRect(renderer, &leftBorder);
SDL_RenderFillRect(renderer, &rightBorder);
}
SDL_RenderPresent(renderer);
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}