-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication.cpp
176 lines (135 loc) · 4.38 KB
/
application.cpp
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
#include "framework.h"
#include "screensaver.h"
using namespace std;
Application::Application(HWND hWnd, Configuration* config)
: hWnd(hWnd), config(config)
{
#ifdef _DEBUG
running = true;
#endif
// Initialize stars and timer
Initialize();
}
void Application::Deinitialize()
{
KillTimer(hWnd, TIMER_ID);
}
void Application::Initialize()
{
SetTimer(hWnd, TIMER_ID, config->fontSize, nullptr);
// Use font from configuration
hFont = CreateFont(
config->fontSize, // Height of font
0, // Width of font
0, // Escapement
0, // Orientation
FW_BOLD, // Font weight
FALSE, // Italic
FALSE, // Underline
FALSE, // Strikeout
ANSI_CHARSET, // Character set
OUT_DEFAULT_PRECIS, // Output precision
CLIP_DEFAULT_PRECIS, // Clipping precision
DEFAULT_QUALITY, // Output quality
DEFAULT_PITCH | FF_DONTCARE, // Pitch and family
config->fontName.c_str()); // Font name
// Clear last rendered screen (in debug mode)
InvalidateRect(hWnd, nullptr, TRUE);
}
void Application::SetCenterPosition(int x, int y)
{
centerX = x / 2;
centerY = y / 2;
}
COLORREF MixColors(COLORREF color1, COLORREF color2, double ratio) {
BYTE r = GetRValue(color1) * (1 - ratio) + GetRValue(color2) * ratio;
BYTE g = GetGValue(color1) * (1 - ratio) + GetGValue(color2) * ratio;
BYTE b = GetBValue(color1) * (1 - ratio) + GetBValue(color2) * ratio;
return RGB(r, g, b);
}
void Application::DrawBackground(HDC hDC)
{
RECT rect;
GetClientRect(hWnd, &rect);
int width = rect.right - rect.left;
int height = rect.bottom - rect.top;
// Define main colors for the gradient
COLORREF color1 = config->gradientStartColor;
COLORREF color2 = config->gradientEndColor;
// Calculate the center and maximum radius for the gradient circles
int centerX = width / 2;
int centerY = height / 2;
int maxRadius = (int)sqrt(centerX * centerX + centerY * centerY);
// Draw concentric circles with varying colors
for (int radius = maxRadius; radius > 0; radius -= 5) { // Adjust step for performance vs. quality
double ratio = (double)radius / maxRadius;
COLORREF mixedColor = MixColors(color1, color2, ratio);
HBRUSH hBrush = CreateSolidBrush(mixedColor);
HPEN hPen = CreatePen(PS_SOLID, 1, mixedColor);
SelectObject(hDC, hBrush);
SelectObject(hDC, hPen);
// Draw the circle
Ellipse(hDC, centerX - radius, centerY - radius, centerX + radius, centerY + radius);
DeleteObject(hBrush);
DeleteObject(hPen);
}
// Draw the clock at the center of the screen
// Set the background color to black
//SetBkColor(hDC, RGB(0, 0, 0));
SetBkMode(hDC, TRANSPARENT);
SetTextColor(hDC, config->fontColor);
}
void Application::Draw(HDC hDC)
{
// Get current time
auto now = std::chrono::system_clock::now();
auto in_time_t = std::chrono::system_clock::to_time_t(now);
// Format time as HH:MM:SS using localtime_s for safer conversion
char timeStr[9]; // HH:MM:SS\0
std::tm tmResult;
localtime_s(&tmResult, &in_time_t);
std::strftime(timeStr, sizeof(timeStr), /* use clock format saved in config */
config->clockFormat.c_str() //"%H:%M:%S"
, &tmResult);
SelectObject(hDC, hFont);
SIZE textSize;
GetTextExtentPoint32(hDC, timeStr, strlen(timeStr), &textSize);
// Calculate the starting position to center the text
int startX = centerX - (textSize.cx / 2);
int startY = centerY - (textSize.cy / 2);
TextOut(hDC, startX, startY, timeStr, strlen(timeStr));
}
void Application::Update()
{
}
void Application::Loop()
{
#ifdef _DEBUG
if (running) {
#endif
Update();
// Create an off-screen DC
HDC hdcScreen = GetDC(hWnd);
HDC hdcBuffer = CreateCompatibleDC(hdcScreen);
RECT rect;
GetClientRect(hWnd, &rect);
int width = rect.right - rect.left;
int height = rect.bottom - rect.top;
HBITMAP hbmBuffer = CreateCompatibleBitmap(hdcScreen, width, height);
HBITMAP hbmOld = (HBITMAP)SelectObject(hdcBuffer, hbmBuffer);
// Fill the buffer with black (or any other background color)
FillRect(hdcBuffer, &rect, (HBRUSH)GetStockObject(BLACK_BRUSH));
// Perform all drawing operations on the off-screen DC, including background
DrawBackground(hdcBuffer); // Draw the gradient background first
Draw(hdcBuffer); // Then draw the rest of the scene
// Copy the off-screen buffer to the screen
BitBlt(hdcScreen, 0, 0, width, height, hdcBuffer, 0, 0, SRCCOPY);
// Clean up
SelectObject(hdcBuffer, hbmOld);
DeleteObject(hbmBuffer);
DeleteDC(hdcBuffer);
ReleaseDC(hWnd, hdcScreen);
#ifdef _DEBUG
}
#endif
}