-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWinMain.cpp
More file actions
276 lines (217 loc) · 7.42 KB
/
WinMain.cpp
File metadata and controls
276 lines (217 loc) · 7.42 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#include <Windows.h>
#include <gl/GL.h>
#include <chrono>
#include <fstream>
#include <nlohmann/json.hpp>
#include <thread>
#include "BackGround.h"
#include "Projection.h"
#include "Rendering.h"
#include "Rotation.h"
#include "DesktopUtils.h"
#include "trayUtils.h"
#include "settings.h"
constexpr float PI_F = 3.14159265f;
constinit bool running = true;
static LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
if (msg == WM_TRAYICON) {
if (lParam == WM_RBUTTONUP) {
// Create a popup menu
HMENU menu = CreatePopupMenu();
AppendMenuW(menu, MF_STRING, 1, L"Quit");
// Get the cursor position
POINT cursorPos;
GetCursorPos(&cursorPos);
// Show the menu
SetForegroundWindow(hwnd);
// Example with TPM_NONOTIFY to avoid blocking
int selection = TrackPopupMenu(menu, TPM_RETURNCMD | TPM_NONOTIFY, cursorPos.x - 120, cursorPos.y - 22, 0, hwnd, NULL);
DestroyMenu(menu);
// Handle the menu selection
if (selection == 1) {
OnQuit(hwnd, running);
}
}
}
return DefWindowProcW(hwnd, msg, wParam, lParam);
}
inline static void gameTick(float& frameTime, const float& stepInterval, float& fractionalTime) {
if (frameTime < stepInterval) {
// Calculate total sleep time including any leftover fractional time
float totalSleepTime = (stepInterval - frameTime) + fractionalTime;
// Truncate to whole milliseconds
int sleepMilliseconds = static_cast<int>(totalSleepTime * 1e+3f);
// Calculate remaining fractional time and ensure it s within 0.0f to 1.0f
fractionalTime = (totalSleepTime - sleepMilliseconds * 1e-3f);
// Sleep for the calculated milliseconds
std::this_thread::sleep_for(std::chrono::milliseconds(sleepMilliseconds));
}
}
int CALLBACK WinMain(
_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPSTR lpCmdLine,
_In_ int nCmdShow)
{
const wchar_t* className = L"HypercubeClass";
WNDCLASSEX wc = { 0 };
wc.cbSize = sizeof(wc);
wc.style = CS_OWNDC;
wc.lpfnWndProc = WindowProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = nullptr;
wc.hCursor = nullptr;
wc.hbrBackground = nullptr;
wc.lpszMenuName = nullptr;
wc.lpszClassName = className;
wc.hIconSm = nullptr;
RegisterClassEx(&wc);
const int Width = GetSystemMetrics(SM_CXVIRTUALSCREEN);
const int Height = GetSystemMetrics(SM_CYVIRTUALSCREEN);
const int HalfWidth = Width / 2;
const int HalfHeight = Height / 2;
HWND hwnd = CreateWindowEx(
WS_EX_TOOLWINDOW,
className,
L"Hypercube Harmony",
WS_POPUP,
0, 0,
Width, Height,
nullptr,
nullptr,
hInstance,
nullptr
);
if (!hwnd) return -1;
Settings settings = loadSettings("settings.json");
const float stepInterval = 1.0f / settings.targetFPS;
// Load the icon from resources
HICON hIcon = LoadIconFromResource();
// Add the tray icon
AddTrayIcon(hwnd, hIcon, L"Just a Simple Icon");
wchar_t* originalWallpaper = GetCurrentWallpaper();
SetAsDesktop(hwnd);
ShowWindow(hwnd, SW_SHOW);
HDC hdc = GetDC(hwnd); // Get device context
// Step 1: Set up the pixel format
PIXELFORMATDESCRIPTOR pfd = {};
pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 32;
pfd.cDepthBits = 24;
pfd.cStencilBits = 8;
pfd.iLayerType = PFD_MAIN_PLANE;
int pixelFormat = ChoosePixelFormat(hdc, &pfd);
SetPixelFormat(hdc, pixelFormat, &pfd);
// Step 2: Create and set the rendering context
HGLRC hglrc = wglCreateContext(hdc);
wglMakeCurrent(hdc, hglrc);
// OpenGL is now set up for this window!
constexpr float initialTesseractVertices[64] = {
-330.0f, -330.0f, -330.0f, -330.0f,
330.0f, -330.0f, -330.0f, -330.0f,
330.0f, 330.0f, -330.0f, -330.0f,
-330.0f, 330.0f, -330.0f, -330.0f,
-330.0f, -330.0f, 330.0f, -330.0f,
330.0f, -330.0f, 330.0f, -330.0f,
330.0f, 330.0f, 330.0f, -330.0f,
-330.0f, 330.0f, 330.0f, -330.0f,
-330.0f, -330.0f, -330.0f, 330.0f,
330.0f, -330.0f, -330.0f, 330.0f,
330.0f, 330.0f, -330.0f, 330.0f,
-330.0f, 330.0f, -330.0f, 330.0f,
-330.0f, -330.0f, 330.0f, 330.0f,
330.0f, -330.0f, 330.0f, 330.0f,
330.0f, 330.0f, 330.0f, 330.0f,
-330.0f, 330.0f, 330.0f, 330.0f
};
float tesseractVertices[64];
std::copy(std::begin(initialTesseractVertices), std::end(initialTesseractVertices), tesseractVertices);
float cameraPos[4] = { 0.0f, 0.0f, 0.0f, -1000.0f };
float FOV = 90.0f;
float fovScale = 1.0f / std::tanf((FOV * PI_F / 180) / 2);
float scale[2] = { 300.0f, 300.0f };
int offset[2] = { HalfWidth, HalfHeight };
unsigned int tesseractIndices[] = {
0, 1, 1, 2, 2, 3, 3, 0, // First square
4, 5, 5, 6, 6, 7, 7, 4, // Second square
8, 9, 9, 10, 10, 11, 11, 8, // Third square
12, 13, 13, 14, 14, 15, 15, 12, // Fourth square
0, 3, 3, 7, 7, 4, 4, 0, // Fifth square
1, 2, 2, 6, 6, 5, 5, 1, // Sixth square
8, 11, 11, 15, 15, 12, 12, 8, // Seventh square
9, 10, 10, 14, 14, 13, 13, 9, // Eighth square
0, 1, 1, 9, 9, 8, 8, 0, // Ninth square
3, 2, 2, 10, 10, 11, 11, 3, // Tenth square
7, 6, 6, 14, 14, 15, 15, 7, // Eleventh square
4, 5, 5, 13, 13, 12, 12, 4 // Twelfth square
};
int indicesSize = sizeof(tesseractIndices) / sizeof(tesseractIndices[0]);
float* tesseract2D = new float[32];
float dt;
float rotate1 = 0.0f;
float rotate2 = 0.0f;
float rotate3 = 0.0f;
float rotate4 = 0.0f;
float rotate1Speed = 0.42f;
float rotate2Speed = 0.55f;
float rotate3Speed = 0.49f;
float rotate4Speed = 0.69f;
float angles[4]{};
float frameTime{ 0 };
float fractionalTime{ 0 };
auto newF = std::chrono::high_resolution_clock::now();
auto oldF = std::chrono::high_resolution_clock::now();
auto endF = std::chrono::high_resolution_clock::now();
// Message loop
MSG msg = {};
while (running) {
oldF = newF;
newF = std::chrono::high_resolution_clock::now();
// Check for messages
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
if (msg.message == WM_QUIT) {
running = false;
break;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
dt = std::chrono::duration<float>(newF - oldF).count();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
BG::fillGradient(settings.backgroundColors, Width, Height);
glLoadIdentity();
glOrtho(0, Width, Height, 0, -1, 1);
rotate1 += rotate1Speed * dt;
rotate2 += rotate2Speed * dt;
rotate3 += rotate3Speed * dt;
rotate4 += rotate4Speed * dt;
angles[0] = std::sinf(rotate2) * 2.4f;
angles[1] = std::sinf(rotate3) * 1.7f;
angles[2] = std::sinf(rotate4) * 2.9f;
angles[3] = std::powf(std::sinf(rotate1) * 1.47f, 3.0f);
rotateTesseractVertices(initialTesseractVertices, 16, 4, angles, tesseractVertices);
tesseract2D = PerspectiveProject(tesseractVertices, cameraPos, fovScale, scale, offset);
renderTesseract(tesseract2D, tesseractIndices, indicesSize, settings.lineWidth, settings.tesseractColor);
SwapBuffers(hdc);
endF = std::chrono::high_resolution_clock::now();
frameTime = std::chrono::duration<float>(endF - newF).count();
gameTick(frameTime, stepInterval, fractionalTime);
}
SetParent(hwnd, nullptr);
SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, (void*)originalWallpaper, SPIF_UPDATEINIFILE | SPIF_SENDCHANGE);
RemoveTrayIcon(hwnd);
wglMakeCurrent(NULL, NULL);
wglDeleteContext(hglrc);
ReleaseDC(hwnd, hdc);
DestroyWindow(hwnd);
UnregisterClassW(wc.lpszClassName, wc.hInstance);
delete[] tesseract2D;
delete[] originalWallpaper;
DestroyIcon(hIcon);
return 0;
}