-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMain.cpp
More file actions
250 lines (217 loc) · 7.76 KB
/
Main.cpp
File metadata and controls
250 lines (217 loc) · 7.76 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
#include "Resource.h"
#include <imgui.h>
#include <imgui_impl_dx11.h>
#include <imgui_impl_win32.h>
#include <d3d11.h>
//#define DIRECTINPUT_VERSION 0x0800
//#include <dinput.h>
#include <tchar.h>
#include <atomic>
#include <csignal>
#include <exception>
#include <format>
#include <stacktrace>
import GW2Viewer.Common.Time;
import GW2Viewer.Data.Game;
import GW2Viewer.System.Graphics;
import GW2Viewer.UI.Manager;
import GW2Viewer.User.Config;
void Render()
{
using namespace GW2Viewer;
ImVec4 clear_col = ImColor(0, 0, 0);
ImGui_ImplDX11_NewFrame();
ImGui_ImplWin32_NewFrame();
ImGui::NewFrame();
G::UI.Update();
// Rendering
ImGui::Render();
G::GraphicsDeviceContext->OMSetRenderTargets(1, &G::RenderTargetView, NULL);
G::GraphicsDeviceContext->ClearRenderTargetView(G::RenderTargetView, (float*)&clear_col);
ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData());
// Update and Render additional Platform Windows
if (ImGui::GetIO().ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
{
ImGui::UpdatePlatformWindows();
ImGui::RenderPlatformWindowsDefault();
}
G::SwapChain->Present(1, 0); // Present with vsync
//g_pSwapChain->Present(0, 0); // Present without vsync
}
extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
using namespace GW2Viewer;
if (ImGui_ImplWin32_WndProcHandler(hWnd, msg, wParam, lParam))
return true;
static bool windowResizing = false;
switch (msg)
{
case WM_SIZE:
if (G::GraphicsDevice && wParam != SIZE_MINIMIZED)
{
if (G::RenderTargetView) { G::RenderTargetView->Release(); G::RenderTargetView = nullptr; }
G::SwapChain->ResizeBuffers(0, (UINT)LOWORD(lParam), (UINT)HIWORD(lParam), DXGI_FORMAT_UNKNOWN, 0);
ID3D11Texture2D* pBackBuffer;
G::SwapChain->GetBuffer(0, (IID&)IID_PPV_ARGS(&pBackBuffer));
G::GraphicsDevice->CreateRenderTargetView(pBackBuffer, nullptr, &G::RenderTargetView);
pBackBuffer->Release();
}
return 0;
case WM_ENTERSIZEMOVE:
InvalidateRect(hWnd, nullptr, true);
windowResizing = true;
break;
case WM_EXITSIZEMOVE:
windowResizing = false;
break;
case WM_PAINT:
if (G::UI.IsLoaded())
{
Render();
if (windowResizing)
return 0;
}
break;
case WM_SYSCOMMAND:
if ((wParam & 0xfff0) == SC_KEYMENU) // Disable ALT application menu
return 0;
break;
case WM_CLOSE:
if (!G::Config.Save())
return 0;
break;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
using namespace GW2Viewer;
CoInitializeEx(nullptr, COINIT_MULTITHREADED);
// Create application window
WNDCLASSEX wc =
{
.cbSize = sizeof(WNDCLASSEX),
.style = CS_CLASSDC,
.lpfnWndProc = WndProc,
.cbClsExtra = 0,
.cbWndExtra = 0,
.hInstance = hInstance,
.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON)),
.hCursor = LoadCursor(nullptr, IDC_ARROW),
.hbrBackground = nullptr,
.lpszMenuName = nullptr,
.lpszClassName = _T("GW2Viewer Window"),
.hIconSm = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON)),
};
RegisterClassEx(&wc);
HWND hwnd = CreateWindow(_T("GW2Viewer Window"), _T("GW2Viewer"), WS_OVERLAPPEDWINDOW, 100, 100, 1280, 800, NULL, NULL, wc.hInstance, NULL);
// Setup swap chain
DXGI_SWAP_CHAIN_DESC sd;
ZeroMemory(&sd, sizeof(sd));
sd.BufferCount = 2;
sd.BufferDesc.Width = 0;
sd.BufferDesc.Height = 0;
sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
sd.BufferDesc.RefreshRate.Numerator = 60;
sd.BufferDesc.RefreshRate.Denominator = 1;
sd.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;
sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
sd.OutputWindow = hwnd;
sd.SampleDesc.Count = 1;
sd.SampleDesc.Quality = 0;
sd.Windowed = TRUE;
sd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
UINT createDeviceFlags = 0;
//createDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG;
D3D_FEATURE_LEVEL featureLevel;
const D3D_FEATURE_LEVEL featureLevelArray[2] = { D3D_FEATURE_LEVEL_11_0, D3D_FEATURE_LEVEL_10_0, };
if (D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, createDeviceFlags, featureLevelArray, 2, D3D11_SDK_VERSION, &sd, &G::SwapChain, &G::GraphicsDevice, &featureLevel, &G::GraphicsDeviceContext) != S_OK)
return false;
ID3D11Texture2D* pBackBuffer;
G::SwapChain->GetBuffer(0, (IID&)IID_PPV_ARGS(&pBackBuffer));
G::GraphicsDevice->CreateRenderTargetView(pBackBuffer, NULL, &G::RenderTargetView);
pBackBuffer->Release();
// Show the window
ShowWindow(hwnd, SW_SHOWDEFAULT);
UpdateWindow(hwnd);
// Setup Dear ImGui context
IMGUI_CHECKVERSION();
ImGui::CreateContext();
// Setup Dear ImGui style
//ImGui::StyleColorsDark();
ImGui::StyleColorsClassic();
// Setup Platform/Renderer backends
ImGui_ImplWin32_Init(hwnd);
ImGui_ImplDX11_Init(G::GraphicsDevice, G::GraphicsDeviceContext);
G::Config.Load();
G::UI.Load();
if (!IsDebuggerPresent())
{
static auto crashHandler = []
{
if (static std::atomic_flag crashed { }; !crashed.test_and_set())
{
try { G::Config.Save(true); }
catch (...) { }
try { std::ofstream(std::format("crash-{:%F_%H-%M-%S}.log", Time::Now())) << std::stacktrace::current(); }
catch (...) { }
MessageBox(nullptr, L"GW2Viewer has crashed. Config and stacktrace were saved.", L"GW2Viewer", MB_OK | MB_ICONERROR);
}
};
SetUnhandledExceptionFilter([](_EXCEPTION_POINTERS* pExceptionInfo) -> long __stdcall
{
crashHandler();
return EXCEPTION_CONTINUE_SEARCH;
});
std::set_terminate([]
{
crashHandler();
std::set_terminate(nullptr);
std::terminate();
});
auto signalHandler = [](int signal)
{
crashHandler();
std::signal(signal, SIG_DFL);
raise(signal);
};
std::signal(SIGABRT, signalHandler);
std::signal(SIGFPE, signalHandler);
std::signal(SIGILL, signalHandler);
std::signal(SIGSEGV, signalHandler);
std::signal(SIGTERM, signalHandler);
}
// Main loop
MSG msg;
ZeroMemory(&msg, sizeof(msg));
while (msg.message != WM_QUIT)
{
if (PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
continue;
}
Render();
if (GetForegroundWindow() != hwnd && !ImGui::GetIO().WantCaptureMouse)
Sleep(100);
}
G::UI.Unload();
//G::Config.Save();
G::Game.Texture.StopLoading();
ImGui_ImplDX11_Shutdown();
ImGui_ImplWin32_Shutdown();
ImGui::DestroyContext();
if (G::RenderTargetView) { G::RenderTargetView->Release(); G::RenderTargetView = nullptr; }
if (G::SwapChain) { G::SwapChain->Release(); G::SwapChain = nullptr; }
if (G::GraphicsDeviceContext) { G::GraphicsDeviceContext->Release(); G::GraphicsDeviceContext = nullptr; }
if (G::GraphicsDevice) try { G::GraphicsDevice->Release(); G::GraphicsDevice = nullptr; } catch (...) { }
DestroyWindow(hwnd);
UnregisterClass(_T("GW2Viewer Window"), wc.hInstance);
CoUninitialize();
return 0;
}