-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
321 lines (277 loc) · 9.38 KB
/
Copy pathmain.cpp
File metadata and controls
321 lines (277 loc) · 9.38 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
#include <windows.h>
#include <wincodec.h>
#include <d2d1.h>
#include <gdiplus.h>
#include <vector>
#include <string>
#include <fstream>
#include "helpers.hpp"
#include "resource.h"
// Globals
bool g_bOffline = false;
HWND g_hWnd;
ID2D1Factory* g_pD2DFactory = nullptr;
IWICImagingFactory* g_pWICFactory = nullptr;
ID2D1HwndRenderTarget* g_pRenderTarget = nullptr;
ID2D1Bitmap* g_pD2DBitmap = nullptr;
Gdiplus::Image* g_pGdiImage = nullptr;
bool g_bUseDirect2D = true;
bool g_bAlwaysOnTop = false;
int g_nNumber = 1;
// Forward declarations
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
HRESULT InitDirect2D(HWND hwnd);
void LoadCatImage();
INT_PTR CALLBACK NumberDlgProc(HWND, UINT, WPARAM, LPARAM);
void PromptForNumber(HWND hwnd);
void Deinitialize(ULONG_PTR gdiplusToken)
{
if (g_pD2DBitmap) { g_pD2DBitmap->Release(); g_pD2DBitmap = nullptr; }
if (g_pRenderTarget) { g_pRenderTarget->Release(); g_pRenderTarget = nullptr; }
if (g_pD2DFactory) g_pD2DFactory->Release();
if (g_pWICFactory) g_pWICFactory->Release();
if (g_pGdiImage) delete g_pGdiImage;
Gdiplus::GdiplusShutdown(gdiplusToken);
CoUninitialize();
}
// Entry point
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR, int nCmdShow)
{
SYSTEMTIME time;
GetSystemTime(&time);
srand(time.wMilliseconds * time.wSecond * time.wHour);
CoInitializeEx(nullptr, COINIT_MULTITHREADED);
Gdiplus::GdiplusStartupInput gdiplusInput; ULONG_PTR gdiplusToken;
Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusInput, nullptr);
WNDCLASSW wc = {};
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.lpszClassName = L"Win32CatViewer";
wc.hCursor = LoadCursor(nullptr, IDC_ARROW);
wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1));
RegisterClassW(&wc);
g_hWnd = CreateWindowExW(0, wc.lpszClassName, L"Gary Viewer - #1",
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 1024, 768,
nullptr, nullptr, hInstance, nullptr);
if (!g_hWnd) return 0;
SetMenu(g_hWnd,LoadMenuW(hInstance, MAKEINTRESOURCE(IDR_MAINMENU)));
ShowWindow(g_hWnd, nCmdShow);
MSG msg;
while (GetMessage(&msg, nullptr, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
Deinitialize(gdiplusToken);
return 0;
}
void LoadCatImage()
{
// 1) Read image data (either offline from disk or online via HTTP)
std::vector<BYTE> data;
if (g_bOffline)
{
// Build local path: "<exeFolder>\Gary\Gary<num>.jpg"
std::wstring path = GetExeFolder() + L"\\Gary\\Gary" + std::to_wstring(g_nNumber) + L".jpg";
std::ifstream file(path, std::ios::binary);
if (file)
data.assign(std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>());
else if (!DownloadImage(data,g_nNumber))
return; // fallback for no file
}
else
{
if (!DownloadImage(data,g_nNumber))
return;
}
// 2) Clean up any previously loaded images/bitmaps
if (g_pGdiImage)
delete g_pGdiImage; g_pGdiImage = nullptr;
if (g_pD2DBitmap)
g_pD2DBitmap->Release(); g_pD2DBitmap = nullptr;
// 3) Create two separate IStream instances from the same data buffer
IStream* stmGdi = nullptr;
IStream* stmD2D = nullptr;
CreateStreamOnHGlobalFromData(data, &stmGdi);
CreateStreamOnHGlobalFromData(data, &stmD2D);
// 4) Load into GDI+
if (stmGdi)
g_pGdiImage = Gdiplus::Image::FromStream(stmGdi); stmGdi->Release();
// 5) Load into Direct2D (if initialized)
if (stmD2D && g_pRenderTarget && g_pWICFactory)
LoadBitmapFromStream(g_pWICFactory, g_pRenderTarget, stmD2D, &g_pD2DBitmap); stmD2D->Release();
// 6) Update the window title and force a repaint
wchar_t title[64];
swprintf_s(title, L"Gary Viewer - #%i", g_nNumber);
SetWindowTextW(g_hWnd, title);
InvalidateRect(g_hWnd, nullptr, TRUE);
}
// Initialize Direct2D & WIC
HRESULT InitDirect2D(HWND hwnd)
{
HRESULT hr;
if (!g_pD2DFactory && FAILED(hr = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &g_pD2DFactory)))
return hr;
if (!g_pWICFactory && FAILED(hr = CoCreateInstance(CLSID_WICImagingFactory, nullptr,
CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&g_pWICFactory))))
return hr;
if (!g_pRenderTarget)
{
RECT rc; GetClientRect(hwnd, &rc);
hr = g_pD2DFactory->CreateHwndRenderTarget(
D2D1::RenderTargetProperties(),
D2D1::HwndRenderTargetProperties(hwnd, D2D1::SizeU(rc.right, rc.bottom)),
&g_pRenderTarget);
}
return S_OK;
}
// Main Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_CREATE:
InitDirect2D(hwnd);
LoadCatImage();
return 0;
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDM_MODE_D2D:
{
g_bUseDirect2D = true;
CheckMenuRadioItem(GetMenu(hwnd), IDM_MODE_D2D, IDM_MODE_GDIP,
IDM_MODE_D2D, MF_BYCOMMAND);
InvalidateRect(hwnd, nullptr, TRUE);
break;
}
case IDM_MODE_GDIP:
{
g_bUseDirect2D = false;
CheckMenuRadioItem(GetMenu(hwnd), IDM_MODE_D2D, IDM_MODE_GDIP,
IDM_MODE_GDIP, MF_BYCOMMAND);
InvalidateRect(hwnd, nullptr, TRUE);
break;
}
case IDM_REFRESH:
{
g_nNumber = 1 + (rand() % 640);
LoadCatImage();
InvalidateRect(hwnd, nullptr, TRUE);
break;
}
case IDM_SET_NUMBER:
{
PromptForNumber(hwnd);
break;
}
case IDM_MODE_OFFLINE:
{
g_bOffline = !g_bOffline;
CheckMenuItem(GetMenu(hwnd), IDM_MODE_OFFLINE, g_bOffline ? MF_CHECKED : MF_UNCHECKED);
LoadCatImage();
InvalidateRect(hwnd, nullptr, TRUE);
break;
}
case IDM_MODE_ALWAYS_ON_TOP:
g_bAlwaysOnTop = !g_bAlwaysOnTop;
CheckMenuItem(GetMenu(hwnd), IDM_MODE_ALWAYS_ON_TOP, g_bAlwaysOnTop ? MF_CHECKED : MF_UNCHECKED);
SetWindowPos(hwnd, g_bAlwaysOnTop ? HWND_TOPMOST : HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
break;
case IDM_ABOUT:
{
DialogBoxW(GetModuleHandle(nullptr),
MAKEINTRESOURCE(IDD_ABOUTBOX),
hwnd,
[](HWND hDlg, UINT msg, WPARAM wParam, LPARAM) -> INT_PTR {
if (msg == WM_COMMAND && LOWORD(wParam) == IDOK) {
EndDialog(hDlg, IDOK);
return TRUE;
}
return FALSE;
});
}
}
return 0;
case WM_SIZE:
if (g_pRenderTarget)
{
RECT rc; GetClientRect(hwnd, &rc);
g_pRenderTarget->Resize(D2D1::SizeU(rc.right, rc.bottom));
}
InvalidateRect(hwnd, nullptr, TRUE);
return 0;
case WM_PAINT:
{
PAINTSTRUCT ps; HDC hdc = BeginPaint(hwnd, &ps);
RECT rc; GetClientRect(hwnd, &rc);
if (g_bUseDirect2D && g_pRenderTarget && g_pD2DBitmap)
{
g_pRenderTarget->BeginDraw();
g_pRenderTarget->Clear(D2D1::ColorF(D2D1::ColorF::White));
D2D1_SIZE_F sz = g_pRenderTarget->GetSize();
g_pRenderTarget->DrawBitmap(g_pD2DBitmap,
D2D1::RectF(0, 0, sz.width, sz.height));
g_pRenderTarget->EndDraw();
}
else if (!g_bUseDirect2D && g_pGdiImage)
{
Gdiplus::Graphics gr(hdc);
gr.Clear(Gdiplus::Color::White);
gr.DrawImage(g_pGdiImage, 0, 0, rc.right, rc.bottom);
}
EndPaint(hwnd, &ps);
}
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
// Dialog procedure for number input
INT_PTR CALLBACK NumberDlgProc(HWND dlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_INITDIALOG:
// lParam contains parent HWND
SetWindowLongPtr(dlg, GWLP_USERDATA, lParam);
// Initialize edit with current number
SetDlgItemInt(dlg, IDC_EDIT_NUMBER, g_nNumber, FALSE);
return TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK)
{
BOOL ok;
int val = GetDlgItemInt(dlg, IDC_EDIT_NUMBER, &ok, FALSE);
if (ok)
{
g_nNumber = val;
LoadCatImage();
InvalidateRect(g_hWnd, nullptr, TRUE);
}
EndDialog(dlg, IDOK);
return TRUE;
}
else if (LOWORD(wParam) == IDCANCEL)
{
EndDialog(dlg, IDCANCEL);
return TRUE;
}
break;
}
return FALSE;
}
// Prompt via modal dialog
void PromptForNumber(HWND hwnd)
{
// Launch number-entry dialog, passing hwnd to update the title
DialogBoxParam(
GetModuleHandle(nullptr),
MAKEINTRESOURCE(IDD_NUMBER_DIALOG),
hwnd,
NumberDlgProc,
(LPARAM)hwnd
);
}