-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
497 lines (426 loc) · 15.8 KB
/
main.cpp
File metadata and controls
497 lines (426 loc) · 15.8 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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
#include "DX12Renderer.h"
#include "ImgViewerUI.h"
#include "Logger.h"
#include "backends/imgui_impl_dx12.h"
#include "backends/imgui_impl_win32.h"
#include "framework.h"
#include "imgui.h"
#include "pch.h"
#include <boost/program_options.hpp>
#include <dwmapi.h>
#include <iostream>
#include <shellapi.h>
#include <string>
namespace po = boost::program_options;
#pragma comment(lib, "dwmapi.lib")
#define MAX_LOADSTRING 100
#define WINDOW_NAME L"BorderlessWindowClass"
// Global Variables:
HINSTANCE hInst; // Current Instance
// DX12 Renderer
DX12Renderer *g_pRenderer = nullptr;
ImgViewerUI *g_pViewerUI = nullptr;
// Borderless Window Config
const int g_ResizeBorderWidth = 8;
const int g_TitleBarHeight = 32;
// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
/**
* @brief Main entry point of the application.
*/
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine,
_In_ int nCmdShow) {
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// Parse command line arguments
int argc;
LPWSTR *argv = CommandLineToArgvW(GetCommandLineW(), &argc);
bool verbose = false;
std::wstring inputFilePath;
try {
po::options_description desc("Allowed options");
desc.add_options()("help,h", "produce help message")(
"verbose,v", "enable verbose logging")(
"input-file", po::wvalue<std::wstring>(&inputFilePath),
"input file to open");
po::positional_options_description p;
p.add("input-file", -1);
po::variables_map vm;
po::store(
po::wcommand_line_parser(argc, argv).options(desc).positional(p).run(),
vm);
po::notify(vm);
if (vm.count("help")) {
if (AttachConsole(ATTACH_PARENT_PROCESS)) {
freopen("CONOUT$", "w", stdout);
freopen("CONOUT$", "w", stderr);
}
std::cout << desc << "\n";
return 0;
}
if (vm.count("verbose")) {
verbose = true;
}
} catch (const std::exception &e) {
std::string what = e.what();
std::wstring whatW(what.begin(), what.end());
MessageBoxW(nullptr, whatW.c_str(), L"Error parsing command line arguments",
MB_OK | MB_ICONERROR);
return 1;
}
LocalFree(argv);
// Initialize logger
if (verbose) {
Logger::Get().Init("log.txt");
}
LOG("=== ImgViewer Starting ===");
// Enable DPI awareness for proper mouse coordinates with Windows scaling
SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
LOG("DPI awareness set to PER_MONITOR_AWARE_V2");
MyRegisterClass(hInstance);
if (!InitInstance(hInstance, nCmdShow)) {
LOG_ERROR("InitInstance failed!");
return FALSE;
}
// Load input file if present
if (!inputFilePath.empty()) {
int size_needed = WideCharToMultiByte(CP_UTF8, 0, inputFilePath.c_str(), -1,
NULL, 0, NULL, NULL);
std::string utf8Filename(size_needed, 0);
WideCharToMultiByte(CP_UTF8, 0, inputFilePath.c_str(), -1, &utf8Filename[0],
size_needed, NULL, NULL);
utf8Filename.resize(size_needed - 1);
g_pViewerUI->HandleDragDrop(utf8Filename);
}
MSG msg = {};
LOG("Entering main loop...");
while (msg.message != WM_QUIT) {
// Process all pending messages
while (PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
if (msg.message == WM_QUIT)
break;
}
if (msg.message == WM_QUIT)
break;
// Render continuously
if (g_pRenderer) {
// Start ImGui frame
ImGui_ImplDX12_NewFrame();
ImGui_ImplWin32_NewFrame();
ImGui::NewFrame();
// Render UI (this collects ImGui draw commands and saves image render
// info)
if (g_pViewerUI) {
g_pViewerUI->Render();
}
// Finalize ImGui
ImGui::Render();
// Start recording commands
g_pRenderer->BeginRender();
// Render image TO TEXTURE first
// This renders the image content into an off-screen texture that ImGui
// will display
if (g_pViewerUI) {
g_pViewerUI->RenderImageToTexture(g_pRenderer->GetCommandList());
}
// RESTORE Main Render Target (Back Buffer) and Viewport
// We must switch back to the screen buffer before letting ImGui render
// the UI
D3D12_CPU_DESCRIPTOR_HANDLE backBufferRTV = g_pRenderer->GetCurrentRTV();
g_pRenderer->GetCommandList()->OMSetRenderTargets(1, &backBufferRTV,
FALSE, nullptr);
D3D12_VIEWPORT viewport = {0.0f,
0.0f,
(float)g_pRenderer->GetWidth(),
(float)g_pRenderer->GetHeight(),
0.0f,
1.0f};
D3D12_RECT scissor = {0, 0, (LONG)g_pRenderer->GetWidth(),
(LONG)g_pRenderer->GetHeight()};
g_pRenderer->GetCommandList()->RSSetViewports(1, &viewport);
g_pRenderer->GetCommandList()->RSSetScissorRects(1, &scissor);
// Render ImGui NEXT (foreground)
// This draws the UI, including the image widget (which samples the
// texture we just rendered)
ImGui_ImplDX12_RenderDrawData(ImGui::GetDrawData(),
g_pRenderer->GetCommandList());
// End recording and present
g_pRenderer->EndRender();
}
}
// Cleanup
LOG("Shutting down...");
if (g_pViewerUI) {
delete g_pViewerUI;
g_pViewerUI = nullptr;
}
if (g_pRenderer) {
ImGui_ImplDX12_Shutdown();
ImGui_ImplWin32_Shutdown();
ImGui::DestroyContext();
delete g_pRenderer;
g_pRenderer = nullptr;
}
LOG("=== ImgViewer Shutdown Complete ===");
Logger::Get().Close();
return (int)msg.wParam;
}
/**
* @brief Registers the window class.
* @param hInstance Application instance handle.
* @return ATOM identifying the registered class.
*/
ATOM MyRegisterClass(HINSTANCE hInstance) {
// Register the windows class
WNDCLASSEXW wcx{};
wcx.cbSize = sizeof(wcx);
wcx.style = CS_HREDRAW | CS_VREDRAW;
wcx.hInstance = nullptr;
wcx.lpfnWndProc = WndProc;
wcx.lpszClassName = WINDOW_NAME;
wcx.hbrBackground = reinterpret_cast<HBRUSH>(COLOR_WINDOW + 1);
wcx.hCursor = ::LoadCursorW(nullptr, IDC_ARROW);
return RegisterClassExW(&wcx);
}
/**
* @brief Saves instance handle and creates main window.
*
* In this function, we save the instance handle in a global variable and
* create and display the main program window.
*
* @param hInstance Instance handle.
* @param nCmdShow Show window command.
* @return BOOL True on success, False on failure.
*/
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) {
hInst = hInstance; // Store instance handle in our global variable
// This example uses a non-resizable 640 by 480 viewport for simplicity.
int nDefaultWidth = 1920;
int nDefaultHeight = 1080;
// Use WS_POPUP to remove all standard decorations, but we need some frames
// for standard behavior logic internally often standard WS_OVERLAPPEDWINDOW
// is fine if we handle WM_NCCALCSIZE to remove it visually Let's use
// WS_OVERLAPPEDWINDOW but we will strip the client area via WM_NCCALCSIZE
HWND hWnd = CreateWindowExW(0, WINDOW_NAME, L"ImgViewer",
WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT,
CW_USEDEFAULT, nDefaultWidth, nDefaultHeight,
nullptr, nullptr, hInstance, nullptr);
// show and update window
if (!hWnd) {
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
// DWM Rounded Corners (Windows 11+)
// DWMWCP_ROUND = 2
DWM_WINDOW_CORNER_PREFERENCE preference = DWMWCP_ROUND;
DwmSetWindowAttribute(hWnd, DWMWA_WINDOW_CORNER_PREFERENCE, &preference,
sizeof(preference));
// Initialize DX12 Renderer
LOG("Initializing DX12 Renderer...");
// Get actual client area size (may differ from window size due to borders,
// DPI, etc.)
RECT clientRect;
GetClientRect(hWnd, &clientRect);
UINT actualWidth = clientRect.right - clientRect.left;
UINT actualHeight = clientRect.bottom - clientRect.top;
LOG("Window size: %dx%d, Client area: %ux%u", nDefaultWidth, nDefaultHeight,
actualWidth, actualHeight);
g_pRenderer = new DX12Renderer();
if (!g_pRenderer->Initialize(hWnd, actualWidth, actualHeight)) {
LOG_ERROR("Failed to initialize DX12 Renderer!");
delete g_pRenderer;
g_pRenderer = nullptr;
return FALSE;
}
LOG("DX12 Renderer initialized successfully");
// Setup ImGui
LOG("Initializing ImGui...");
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO &io = ImGui::GetIO();
(void)io;
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
ImGui::StyleColorsDark();
ImGui_ImplWin32_Init(hWnd);
// Load Fonts
// Default Font (Consolas 16px)
io.Fonts->AddFontFromFileTTF("C:\\Windows\\Fonts\\consola.ttf", 16.0f);
// Title Font (Consolas 24px) - We will access this as io.Fonts->Fonts[1]
io.Fonts->AddFontFromFileTTF("C:\\Windows\\Fonts\\consola.ttf", 24.0f);
// Initialize ImGui DX12 backend with new struct-based API
ImGui_ImplDX12_InitInfo initInfo = {};
initInfo.Device = g_pRenderer->GetDevice();
initInfo.CommandQueue = g_pRenderer->GetCommandQueue();
initInfo.NumFramesInFlight = 2;
initInfo.RTVFormat = DXGI_FORMAT_R8G8B8A8_UNORM;
initInfo.SrvDescriptorHeap = g_pRenderer->GetSrvHeap();
initInfo.LegacySingleSrvCpuDescriptor =
g_pRenderer->GetSrvHeap()->GetCPUDescriptorHandleForHeapStart();
initInfo.LegacySingleSrvGpuDescriptor =
g_pRenderer->GetSrvHeap()->GetGPUDescriptorHandleForHeapStart();
LOG("ImGui DX12 init - SrvHeap=%p, CpuDesc=%llu, GpuDesc=%llu",
initInfo.SrvDescriptorHeap, initInfo.LegacySingleSrvCpuDescriptor.ptr,
initInfo.LegacySingleSrvGpuDescriptor.ptr);
ImGui_ImplDX12_Init(&initInfo);
LOG("ImGui initialized successfully");
// Initialize ImgViewerUI
g_pViewerUI = new ImgViewerUI();
g_pViewerUI->Initialize(g_pRenderer);
LOG("ImgViewerUI initialized successfully");
// Enable drag and drop
DragAcceptFiles(hWnd, TRUE);
LOG("Drag and drop enabled");
return TRUE;
}
/**
* @brief Processes messages for the main window.
*
* WM_COMMAND - Process the application menu
* WM_PAINT - Paint the main window
* WM_DESTROY - Post a quit message and return
*
* @param hWnd Window handle.
* @param message Message ID.
* @param wParam Word parameter.
* @param lParam Long parameter.
* @return LRESULT Message processing result.
*/
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam,
LPARAM lParam) {
extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(
HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
if (ImGui_ImplWin32_WndProcHandler(hWnd, message, wParam, lParam))
return true;
switch (message) {
case WM_NCCALCSIZE: {
// Intercept this to remove the standard window frame while keeping the
// window logic
if (wParam == TRUE && g_pViewerUI) {
// If we return 0, the Client Rect will be the same as the Window Rect
// (removing borders/caption) However, we need to consider if the window
// is maximized. When maximized, a standard window extends slightly
// off-screen (shadow/borders). We should adjust for that to prevent
// content being cut off.
// Standard approach for borderless window:
// Return 0 (handled) to indicate we are handling the client area
// calculation. But we might need to adjust rgrc[0] if maximized? Let's
// check IsZoomed(hWnd)
if (IsZoomed(hWnd)) {
// Adjust for the border that flows offscreen when maximized
// Logic omitted for brevity, usually you get monitor info and clamp to
// work area But usually simply returning 0 is "good enough" for basic
// borderless, just edges might be hidden. Let's try just returning 0
// first.
}
return 0;
}
break;
}
case WM_NCHITTEST: {
// Custom Hit Testing for Resizing and Dragging
// 1. Let Windows calculate the default result first? No, we need to
// override client area behavior.
POINT pt = {LOWORD(lParam), HIWORD(lParam)}; // Screen coordinates
ScreenToClient(hWnd, &pt);
RECT rcClient;
GetClientRect(hWnd, &rcClient);
// Check resize borders
if (pt.y >= rcClient.bottom - g_ResizeBorderWidth) {
if (pt.x <= g_ResizeBorderWidth)
return HTBOTTOMLEFT;
if (pt.x >= rcClient.right - g_ResizeBorderWidth)
return HTBOTTOMRIGHT;
return HTBOTTOM;
}
if (pt.y <= g_ResizeBorderWidth) {
if (pt.x <= g_ResizeBorderWidth)
return HTTOPLEFT;
if (pt.x >= rcClient.right - g_ResizeBorderWidth)
return HTTOPRIGHT;
return HTTOP;
}
if (pt.x <= g_ResizeBorderWidth)
return HTLEFT;
if (pt.x >= rcClient.right - g_ResizeBorderWidth)
return HTRIGHT;
// Title Bar Dragging Logic
if (pt.y < g_TitleBarHeight) {
// Explicit Hit Testing for interactive areas to allow dragging in empty
// space
// 1. Menu Area (Left side): Icon + "File" menu.
float interactWidth =
g_pViewerUI ? g_pViewerUI->GetTitleBarInteractWidth() : 400.0f;
if ((pt.x < interactWidth) && (pt.x > (interactWidth - 55)))
return HTCLIENT;
// 2. Window Controls (Right side): 3 Buttons * 46px = 138px.
// Let's use 150px to be safe.
if (pt.x > rcClient.right - 150)
return HTCLIENT; // Let ImGui handle buttons
// Otherwise, we are in the "Title Bar Background".
if (!IsZoomed(hWnd)) {
return HTCAPTION;
}
// Allow dragging maximized window to restore/unsnap
return HTCAPTION;
}
return HTCLIENT;
}
case WM_COMMAND:
break;
case WM_SIZE:
if (g_pRenderer && wParam != SIZE_MINIMIZED) {
UINT width = LOWORD(lParam);
UINT height = HIWORD(lParam);
g_pRenderer->OnResize(width, height);
}
break;
case WM_DROPFILES: {
HDROP hDrop = (HDROP)wParam;
UINT fileCount = DragQueryFileW(hDrop, 0xFFFFFFFF, nullptr, 0);
if (fileCount > 0 && g_pViewerUI) {
WCHAR filename[MAX_PATH];
if (DragQueryFileW(hDrop, 0, filename, MAX_PATH)) {
// Convert wide string to UTF-8 string
int size_needed =
WideCharToMultiByte(CP_UTF8, 0, filename, -1, NULL, 0, NULL, NULL);
std::string utf8Filename(size_needed, 0);
WideCharToMultiByte(CP_UTF8, 0, filename, -1, &utf8Filename[0],
size_needed, NULL, NULL);
utf8Filename.resize(size_needed - 1);
g_pViewerUI->HandleDragDrop(utf8Filename);
}
}
DragFinish(hDrop);
} break;
case WM_PAINT: {
ValidateRect(hWnd, NULL);
}
return 0;
case WM_DESTROY:
PostQuitMessage(0);
break;
// keyboard events
case WM_KEYDOWN: {
switch (wParam) {
case VK_ESCAPE:
exit(0);
break;
default:
// wasd control position, up down left right control direction
break;
}
} break;
case WM_KEYUP: {
} break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}