-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMystifyFix.cpp
More file actions
405 lines (357 loc) · 15.1 KB
/
MystifyFix.cpp
File metadata and controls
405 lines (357 loc) · 15.1 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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
// MystifyFix.cpp — GDI-based Mystify screensaver replacement
// Fixes GPU TDR crashes caused by the original Mystify's legacy DirectX code.
//
// Build: compile.bat (from a VS Developer Command Prompt)
// Install: copy MystifyFix.exe %WINDIR%\System32\MystifyFix.scr
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <windowsx.h>
#include <commctrl.h>
#include <cmath>
#include <vector>
#include <random>
#include <algorithm>
#include <cstdio>
// ── settings ──────────────────────────────────────────────────────────────────
struct Settings {
int speed; // 1–20 px/frame per vertex
int trailLen; // 5–50 frames of history
int numShapes; // 1–8 bouncing polygons
int numSides; // 3–8 sides per polygon
};
static const Settings kDefaults = { 8, 20, 2, 4 };
static Settings g_cfg = kDefaults;
static const char* kRegKey = "Software\\MystifyFix";
static void LoadSettings() {
HKEY hk;
if (RegOpenKeyExA(HKEY_CURRENT_USER, kRegKey, 0, KEY_READ, &hk) != ERROR_SUCCESS) return;
auto rd = [&](const char* n, int& out, int lo, int hi) {
DWORD v = 0, sz = sizeof(DWORD);
if (RegQueryValueExA(hk, n, nullptr, nullptr, (BYTE*)&v, &sz) == ERROR_SUCCESS)
out = (int)std::clamp((int)v, lo, hi);
};
rd("Speed", g_cfg.speed, 1, 20);
rd("TrailLen", g_cfg.trailLen, 5, 50);
rd("NumShapes", g_cfg.numShapes, 1, 8);
rd("NumSides", g_cfg.numSides, 3, 8);
RegCloseKey(hk);
}
static void SaveSettings(const Settings& s) {
HKEY hk;
RegCreateKeyExA(HKEY_CURRENT_USER, kRegKey, 0, nullptr, 0, KEY_WRITE, nullptr, &hk, nullptr);
auto wr = [&](const char* n, int v) {
DWORD d = (DWORD)v;
RegSetValueExA(hk, n, 0, REG_DWORD, (BYTE*)&d, sizeof(d));
};
wr("Speed", s.speed); wr("TrailLen", s.trailLen);
wr("NumShapes", s.numShapes); wr("NumSides", s.numSides);
RegCloseKey(hk);
}
// ── colour ────────────────────────────────────────────────────────────────────
static COLORREF HsvToRgb(float h, float s, float v) {
h = fmodf(h + 360.f, 360.f);
float c = v * s, x = c * (1.f - fabsf(fmodf(h / 60.f, 2.f) - 1.f)), m = v - c, r, g, b;
if (h < 60) { r=c; g=x; b=0; } else if (h < 120) { r=x; g=c; b=0; }
else if (h < 180) { r=0; g=c; b=x; } else if (h < 240) { r=0; g=x; b=c; }
else if (h < 300) { r=x; g=0; b=c; } else { r=c; g=0; b=x; }
return RGB((int)((r+m)*255), (int)((g+m)*255), (int)((b+m)*255));
}
// ── simulation ────────────────────────────────────────────────────────────────
struct Vert { float x, y, vx, vy; };
struct Shape { std::vector<Vert> v; float hue, hueStep; };
struct State {
std::vector<Shape> shapes;
std::vector<std::vector<std::vector<POINT>>> trails; // [shape][frame][vertex]
int W, H;
Settings cfg;
};
static State* g_state = nullptr;
static bool g_preview = false;
static State* MakeState(int w, int h, const Settings& cfg) {
State* s = new State();
s->W = w; s->H = h; s->cfg = cfg;
s->shapes.resize(cfg.numShapes);
s->trails.resize(cfg.numShapes);
std::mt19937 rng(std::random_device{}());
std::uniform_real_distribution<float> rx(0.1f*w, 0.9f*w);
std::uniform_real_distribution<float> ry(0.1f*h, 0.9f*h);
std::uniform_real_distribution<float> rd(-1.f, 1.f);
std::uniform_real_distribution<float> rh(0.f, 360.f);
std::uniform_real_distribution<float> rhs(0.3f, 1.f);
for (auto& sh : s->shapes) {
sh.v.resize(cfg.numSides);
for (auto& v : sh.v) {
v.x = rx(rng); v.y = ry(rng);
float dx = rd(rng), dy = rd(rng);
if (dx == 0.f && dy == 0.f) dx = 1.f;
float len = sqrtf(dx*dx + dy*dy);
v.vx = (dx / len) * cfg.speed;
v.vy = (dy / len) * cfg.speed;
}
sh.hue = rh(rng); sh.hueStep = rhs(rng);
}
return s;
}
static void Tick(State* s) {
for (int p = 0; p < (int)s->shapes.size(); p++) {
auto& sh = s->shapes[p];
for (auto& v : sh.v) {
v.x += v.vx; v.y += v.vy;
if (v.x <= 0 || v.x >= s->W) { v.vx = -v.vx; v.x = (v.x <= 0) ? 0.f : (float)s->W; }
if (v.y <= 0 || v.y >= s->H) { v.vy = -v.vy; v.y = (v.y <= 0) ? 0.f : (float)s->H; }
}
sh.hue = fmodf(sh.hue + sh.hueStep, 360.f);
std::vector<POINT> pts(sh.v.size());
for (int i = 0; i < (int)sh.v.size(); i++) pts[i] = { (LONG)sh.v[i].x, (LONG)sh.v[i].y };
s->trails[p].push_back(std::move(pts));
if ((int)s->trails[p].size() > s->cfg.trailLen)
s->trails[p].erase(s->trails[p].begin());
}
}
static void Render(HDC hdc, State* s) {
RECT rc = { 0, 0, s->W, s->H };
FillRect(hdc, &rc, (HBRUSH)GetStockObject(BLACK_BRUSH));
for (int p = 0; p < (int)s->shapes.size(); p++) {
int n = (int)s->trails[p].size();
if (n == 0) continue;
float baseHue = s->shapes[p].hue, step = s->shapes[p].hueStep;
for (int t = 0; t < n; t++) {
float alpha = (float)(t + 1) / n;
float tHue = fmodf(baseHue - (n - t) * step + 720.f, 360.f);
HPEN pen = CreatePen(PS_SOLID, 1, HsvToRgb(tHue, 1.f, alpha));
HPEN old = (HPEN)SelectObject(hdc, pen);
SelectObject(hdc, GetStockObject(NULL_BRUSH));
Polygon(hdc, s->trails[p][t].data(), (int)s->trails[p][t].size());
SelectObject(hdc, old); DeleteObject(pen);
}
}
}
// ── config dialog (no .rc file needed — built programmatically) ───────────────
#define IDC_SLD_SPEED 100
#define IDC_SLD_TRAIL 101
#define IDC_SLD_SHAPES 102
#define IDC_SLD_SIDES 103
#define IDC_VAL_SPEED 110
#define IDC_VAL_TRAIL 111
#define IDC_VAL_SHAPES 112
#define IDC_VAL_SIDES 113
#define IDC_BTN_RESET 120
struct RowDef { int sldId, valId, lo, hi; const char* label; };
static const RowDef kRows[] = {
{ IDC_SLD_SPEED, IDC_VAL_SPEED, 1, 20, "Speed (1-20)" },
{ IDC_SLD_TRAIL, IDC_VAL_TRAIL, 5, 50, "Trail length (5-50)" },
{ IDC_SLD_SHAPES, IDC_VAL_SHAPES, 1, 8, "Shapes (1-8)" },
{ IDC_SLD_SIDES, IDC_VAL_SIDES, 3, 8, "Sides (3-8)" },
};
static HINSTANCE g_hInst = nullptr;
static bool g_cfgOk = false;
static Settings g_editCfg = kDefaults;
static void UpdateValLabel(HWND dlg, const RowDef& r) {
char buf[16];
sprintf_s(buf, "%d", (int)SendDlgItemMessage(dlg, r.sldId, TBM_GETPOS, 0, 0));
SetDlgItemTextA(dlg, r.valId, buf);
}
LRESULT CALLBACK CfgProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) {
switch (msg) {
case WM_CREATE: {
INITCOMMONCONTROLSEX ice = { sizeof(ice), ICC_BAR_CLASSES };
InitCommonControlsEx(&ice);
int vals[] = { g_editCfg.speed, g_editCfg.trailLen, g_editCfg.numShapes, g_editCfg.numSides };
for (int i = 0; i < 4; i++) {
int y = 14 + i * 42;
// label
CreateWindowEx(0, "STATIC", kRows[i].label,
WS_CHILD | WS_VISIBLE | SS_LEFT,
10, y + 5, 140, 18, hwnd, (HMENU)(UINT_PTR)(200 + i), g_hInst, nullptr);
// slider
HWND sld = CreateWindowEx(0, TRACKBAR_CLASS, "",
WS_CHILD | WS_VISIBLE | TBS_HORZ | TBS_AUTOTICKS | TBS_TOOLTIPS,
155, y, 175, 28, hwnd, (HMENU)(UINT_PTR)kRows[i].sldId, g_hInst, nullptr);
SendMessage(sld, TBM_SETRANGE, TRUE, MAKELONG(kRows[i].lo, kRows[i].hi));
SendMessage(sld, TBM_SETPOS, TRUE, vals[i]);
// value readout
char buf[8]; sprintf_s(buf, "%d", vals[i]);
CreateWindowEx(WS_EX_CLIENTEDGE, "STATIC", buf,
WS_CHILD | WS_VISIBLE | SS_CENTER,
335, y + 4, 36, 20, hwnd, (HMENU)(UINT_PTR)kRows[i].valId, g_hInst, nullptr);
}
int btnY = 14 + 4 * 42 + 8;
CreateWindowEx(0, "BUTTON", "Reset Defaults",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
10, btnY, 110, 26, hwnd, (HMENU)IDC_BTN_RESET, g_hInst, nullptr);
CreateWindowEx(0, "BUTTON", "OK",
WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON,
230, btnY, 70, 26, hwnd, (HMENU)IDOK, g_hInst, nullptr);
CreateWindowEx(0, "BUTTON", "Cancel",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
308, btnY, 70, 26, hwnd, (HMENU)IDCANCEL, g_hInst, nullptr);
return 0;
}
case WM_HSCROLL: {
HWND sldr = (HWND)lp;
for (const auto& r : kRows)
if (GetDlgItem(hwnd, r.sldId) == sldr) { UpdateValLabel(hwnd, r); break; }
return 0;
}
case WM_COMMAND:
switch (LOWORD(wp)) {
case IDOK:
g_editCfg.speed = (int)SendDlgItemMessage(hwnd, IDC_SLD_SPEED, TBM_GETPOS, 0, 0);
g_editCfg.trailLen = (int)SendDlgItemMessage(hwnd, IDC_SLD_TRAIL, TBM_GETPOS, 0, 0);
g_editCfg.numShapes = (int)SendDlgItemMessage(hwnd, IDC_SLD_SHAPES, TBM_GETPOS, 0, 0);
g_editCfg.numSides = (int)SendDlgItemMessage(hwnd, IDC_SLD_SIDES, TBM_GETPOS, 0, 0);
g_cfgOk = true;
DestroyWindow(hwnd);
return 0;
case IDCANCEL:
DestroyWindow(hwnd);
return 0;
case IDC_BTN_RESET: {
int defs[] = { kDefaults.speed, kDefaults.trailLen, kDefaults.numShapes, kDefaults.numSides };
for (int i = 0; i < 4; i++) {
SendDlgItemMessage(hwnd, kRows[i].sldId, TBM_SETPOS, TRUE, defs[i]);
UpdateValLabel(hwnd, kRows[i]);
}
return 0;
}
}
return 0;
case WM_CLOSE:
DestroyWindow(hwnd);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, msg, wp, lp);
}
static bool ShowConfig(HWND parent) {
g_editCfg = g_cfg;
g_cfgOk = false;
WNDCLASSEX wc = {};
wc.cbSize = sizeof(wc);
wc.lpfnWndProc = CfgProc;
wc.hInstance = g_hInst;
wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
wc.hCursor = LoadCursor(nullptr, IDC_ARROW);
wc.lpszClassName = "MystifyFixCfg";
RegisterClassEx(&wc);
// Calculate window size from desired client area
RECT wantedClient = { 0, 0, 390, 14 + 4*42 + 8 + 26 + 14 };
AdjustWindowRectEx(&wantedClient, WS_POPUP | WS_CAPTION | WS_SYSMENU, FALSE, WS_EX_DLGMODALFRAME);
int ww = wantedClient.right - wantedClient.left;
int wh = wantedClient.bottom - wantedClient.top;
int sw = GetSystemMetrics(SM_CXSCREEN), sh = GetSystemMetrics(SM_CYSCREEN);
HWND hwnd = CreateWindowEx(
WS_EX_DLGMODALFRAME,
"MystifyFixCfg", "MystifyFix Settings",
WS_POPUP | WS_CAPTION | WS_SYSMENU,
(sw - ww) / 2, (sh - wh) / 2, ww, wh,
parent, nullptr, g_hInst, nullptr);
if (!hwnd) return false;
if (parent) EnableWindow(parent, FALSE);
ShowWindow(hwnd, SW_SHOW);
UpdateWindow(hwnd);
MSG msg;
while (IsWindow(hwnd) && GetMessage(&msg, nullptr, 0, 0)) {
if (!IsDialogMessage(hwnd, &msg)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
if (parent) { EnableWindow(parent, TRUE); SetForegroundWindow(parent); }
return g_cfgOk;
}
// ── screensaver window ────────────────────────────────────────────────────────
static const int FRAME_MS = 16; // ~60 fps
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) {
static POINT lastPos = { -1, -1 };
switch (msg) {
case WM_CREATE:
SetTimer(hwnd, 1, FRAME_MS, nullptr);
return 0;
case WM_TIMER: {
if (!g_state) return 0;
Tick(g_state);
RECT rc; GetClientRect(hwnd, &rc);
HDC hdc = GetDC(hwnd);
HDC mdc = CreateCompatibleDC(hdc);
HBITMAP bm = CreateCompatibleBitmap(hdc, rc.right, rc.bottom);
HBITMAP oldBm = (HBITMAP)SelectObject(mdc, bm);
Render(mdc, g_state);
BitBlt(hdc, 0, 0, rc.right, rc.bottom, mdc, 0, 0, SRCCOPY);
SelectObject(mdc, oldBm); DeleteObject(bm); DeleteDC(mdc);
ReleaseDC(hwnd, hdc);
return 0;
}
case WM_MOUSEMOVE:
if (!g_preview) {
POINT pt = { GET_X_LPARAM(lp), GET_Y_LPARAM(lp) };
if (lastPos.x < 0) { lastPos = pt; }
else if (abs(pt.x - lastPos.x) > 5 || abs(pt.y - lastPos.y) > 5)
PostMessage(hwnd, WM_CLOSE, 0, 0);
}
return 0;
case WM_KEYDOWN:
case WM_LBUTTONDOWN:
case WM_RBUTTONDOWN:
case WM_MBUTTONDOWN:
if (!g_preview) PostMessage(hwnd, WM_CLOSE, 0, 0);
return 0;
case WM_DESTROY:
KillTimer(hwnd, 1);
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, msg, wp, lp);
}
// ── entry ─────────────────────────────────────────────────────────────────────
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR, int) {
g_hInst = hInst;
LoadSettings();
char* cmd = GetCommandLineA();
// /c — show settings dialog
if (strstr(cmd, "/c") || strstr(cmd, "/C") || strstr(cmd, "-c")) {
if (ShowConfig(nullptr) && g_cfgOk) SaveSettings(g_editCfg);
return 0;
}
// /p <hwnd> — preview inside screensaver control panel
HWND previewParent = nullptr;
const char* pp = strstr(cmd, "/p");
if (!pp) pp = strstr(cmd, "/P");
if (pp) { g_preview = true; previewParent = (HWND)(LONG_PTR)atoll(pp + 2); }
WNDCLASSEX wc = {};
wc.cbSize = sizeof(wc);
wc.lpfnWndProc = WndProc;
wc.hInstance = hInst;
wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wc.lpszClassName = "MystifyFixSCR";
RegisterClassEx(&wc);
HWND hwnd; int w, h;
if (g_preview && previewParent) {
RECT rc; GetClientRect(previewParent, &rc);
w = rc.right; h = rc.bottom;
hwnd = CreateWindowEx(0, "MystifyFixSCR", "",
WS_CHILD | WS_VISIBLE, 0, 0, w, h,
previewParent, nullptr, hInst, nullptr);
} else {
w = GetSystemMetrics(SM_CXVIRTUALSCREEN);
h = GetSystemMetrics(SM_CYVIRTUALSCREEN);
hwnd = CreateWindowEx(WS_EX_TOPMOST, "MystifyFixSCR", "",
WS_POPUP | WS_VISIBLE,
GetSystemMetrics(SM_XVIRTUALSCREEN),
GetSystemMetrics(SM_YVIRTUALSCREEN),
w, h, nullptr, nullptr, hInst, nullptr);
ShowCursor(FALSE);
}
if (!hwnd) return 1;
g_state = MakeState(w, h, g_cfg);
MSG msg;
while (GetMessage(&msg, nullptr, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
delete g_state;
if (!g_preview) ShowCursor(TRUE);
return (int)msg.wParam;
}