-
Notifications
You must be signed in to change notification settings - Fork 16
/
Win32Application.cpp
299 lines (258 loc) · 9.36 KB
/
Win32Application.cpp
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
//*********************************************************
//
// Copyright (c) Microsoft. All rights reserved.
// This code is licensed under the MIT License (MIT).
// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************
#include "stdafx.h"
#include "Win32Application.h"
#include "DXSampleHelper.h"
HWND Win32Application::m_hwnd = nullptr;
bool Win32Application::m_fullscreenMode = false;
RECT Win32Application::m_windowRect;
using Microsoft::WRL::ComPtr;
int Win32Application::Run(DXSample* pSample, HINSTANCE hInstance, int nCmdShow)
{
try
{
// Parse the command line parameters
int argc;
LPWSTR* argv = CommandLineToArgvW(GetCommandLineW(), &argc);
pSample->ParseCommandLineArgs(argv, argc);
LocalFree(argv);
// Initialize the window class.
WNDCLASSEX windowClass = { 0 };
windowClass.cbSize = sizeof(WNDCLASSEX);
windowClass.style = CS_HREDRAW | CS_VREDRAW;
windowClass.lpfnWndProc = WindowProc;
windowClass.hInstance = hInstance;
windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
windowClass.lpszClassName = L"DXSampleClass";
RegisterClassEx(&windowClass);
RECT windowRect = { 0, 0, static_cast<LONG>(pSample->GetWidth()), static_cast<LONG>(pSample->GetHeight()) };
AdjustWindowRect(&windowRect, WS_OVERLAPPEDWINDOW, FALSE);
// Create the window and store a handle to it.
m_hwnd = CreateWindow(
windowClass.lpszClassName,
pSample->GetTitle(),
m_windowStyle,
CW_USEDEFAULT,
CW_USEDEFAULT,
windowRect.right - windowRect.left,
windowRect.bottom - windowRect.top,
nullptr, // We have no parent window.
nullptr, // We aren't using menus.
hInstance,
pSample);
// Initialize the sample. OnInit is defined in each child-implementation of DXSample.
pSample->OnInit();
ShowWindow(m_hwnd, nCmdShow);
// Main sample loop.
MSG msg = {};
while (msg.message != WM_QUIT)
{
// Process any messages in the queue.
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
pSample->OnDestroy();
// Return this part of the WM_QUIT message to Windows.
return static_cast<char>(msg.wParam);
}
catch (std::exception& e)
{
OutputDebugString(L"Application hit a problem: ");
OutputDebugStringA(e.what());
OutputDebugString(L"\nTerminating.\n");
pSample->OnDestroy();
return EXIT_FAILURE;
}
}
// Convert a styled window into a fullscreen borderless window and back again.
void Win32Application::ToggleFullscreenWindow(IDXGISwapChain* pSwapChain)
{
if (m_fullscreenMode)
{
// Restore the window's attributes and size.
SetWindowLong(m_hwnd, GWL_STYLE, m_windowStyle);
SetWindowPos(
m_hwnd,
HWND_NOTOPMOST,
m_windowRect.left,
m_windowRect.top,
m_windowRect.right - m_windowRect.left,
m_windowRect.bottom - m_windowRect.top,
SWP_FRAMECHANGED | SWP_NOACTIVATE);
ShowWindow(m_hwnd, SW_NORMAL);
}
else
{
// Save the old window rect so we can restore it when exiting fullscreen mode.
GetWindowRect(m_hwnd, &m_windowRect);
// Make the window borderless so that the client area can fill the screen.
SetWindowLong(m_hwnd, GWL_STYLE, m_windowStyle & ~(WS_CAPTION | WS_MAXIMIZEBOX | WS_MINIMIZEBOX | WS_SYSMENU | WS_THICKFRAME));
RECT fullscreenWindowRect;
try
{
if (pSwapChain)
{
// Get the settings of the display on which the app's window is currently displayed
ComPtr<IDXGIOutput> pOutput;
ThrowIfFailed(pSwapChain->GetContainingOutput(&pOutput));
DXGI_OUTPUT_DESC Desc;
ThrowIfFailed(pOutput->GetDesc(&Desc));
fullscreenWindowRect = Desc.DesktopCoordinates;
}
else
{
// Fallback to EnumDisplaySettings implementation
throw HrException(S_FALSE);
}
}
catch (HrException& e)
{
UNREFERENCED_PARAMETER(e);
// Get the settings of the primary display
DEVMODE devMode = {};
devMode.dmSize = sizeof(DEVMODE);
EnumDisplaySettings(nullptr, ENUM_CURRENT_SETTINGS, &devMode);
fullscreenWindowRect = {
devMode.dmPosition.x,
devMode.dmPosition.y,
devMode.dmPosition.x + static_cast<LONG>(devMode.dmPelsWidth),
devMode.dmPosition.y + static_cast<LONG>(devMode.dmPelsHeight)
};
}
SetWindowPos(
m_hwnd,
HWND_TOPMOST,
fullscreenWindowRect.left,
fullscreenWindowRect.top,
fullscreenWindowRect.right,
fullscreenWindowRect.bottom,
SWP_FRAMECHANGED | SWP_NOACTIVATE);
ShowWindow(m_hwnd, SW_MAXIMIZE);
}
m_fullscreenMode = !m_fullscreenMode;
}
void Win32Application::SetWindowZorderToTopMost(bool setToTopMost)
{
RECT windowRect;
GetWindowRect(m_hwnd, &windowRect);
SetWindowPos(
m_hwnd,
(setToTopMost) ? HWND_TOPMOST : HWND_NOTOPMOST,
windowRect.left,
windowRect.top,
windowRect.right - windowRect.left,
windowRect.bottom - windowRect.top,
SWP_FRAMECHANGED | SWP_NOACTIVATE);
}
// Main message handler for the sample.
LRESULT CALLBACK Win32Application::WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
DXSample* pSample = reinterpret_cast<DXSample*>(GetWindowLongPtr(hWnd, GWLP_USERDATA));
switch (message)
{
case WM_CREATE:
{
// Save the DXSample* passed in to CreateWindow.
LPCREATESTRUCT pCreateStruct = reinterpret_cast<LPCREATESTRUCT>(lParam);
SetWindowLongPtr(hWnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(pCreateStruct->lpCreateParams));
}
return 0;
case WM_KEYDOWN:
if (pSample)
{
pSample->OnKeyDown(static_cast<UINT8>(wParam));
}
return 0;
case WM_KEYUP:
if (pSample)
{
pSample->OnKeyUp(static_cast<UINT8>(wParam));
}
return 0;
case WM_SYSKEYDOWN:
// Handle ALT+ENTER:
if ((wParam == VK_RETURN) && (lParam & (1 << 29)))
{
if (pSample && pSample->GetTearingSupport())
{
ToggleFullscreenWindow(pSample->GetSwapchain());
return 0;
}
}
// Send all other WM_SYSKEYDOWN messages to the default WndProc.
break;
case WM_PAINT:
if (pSample)
{
pSample->OnUpdate();
pSample->OnRender();
}
return 0;
case WM_SIZE:
if (pSample)
{
RECT windowRect = {};
GetWindowRect(hWnd, &windowRect);
pSample->SetWindowBounds(windowRect.left, windowRect.top, windowRect.right, windowRect.bottom);
RECT clientRect = {};
GetClientRect(hWnd, &clientRect);
pSample->OnSizeChanged(clientRect.right - clientRect.left, clientRect.bottom - clientRect.top, wParam == SIZE_MINIMIZED);
}
return 0;
case WM_MOVE:
if (pSample)
{
RECT windowRect = {};
GetWindowRect(hWnd, &windowRect);
pSample->SetWindowBounds(windowRect.left, windowRect.top, windowRect.right, windowRect.bottom);
int xPos = (int)(short) LOWORD(lParam);
int yPos = (int)(short) HIWORD(lParam);
pSample->OnWindowMoved(xPos, yPos);
}
return 0;
case WM_DISPLAYCHANGE:
if (pSample)
{
pSample->OnDisplayChanged();
}
return 0;
case WM_MOUSEMOVE:
if (pSample && static_cast<UINT8>(wParam)== MK_LBUTTON)
{
UINT x = LOWORD(lParam);
UINT y = HIWORD(lParam);
pSample->OnMouseMove(x, y);
}
return 0;
case WM_LBUTTONDOWN:
{
UINT x = LOWORD(lParam);
UINT y = HIWORD(lParam);
pSample->OnLeftButtonDown(x, y);
}
return 0;
case WM_LBUTTONUP:
{
UINT x = LOWORD(lParam);
UINT y = HIWORD(lParam);
pSample->OnLeftButtonUp(x, y);
}
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
// Handle any messages the switch statement didn't.
return DefWindowProc(hWnd, message, wParam, lParam);
}