-
Notifications
You must be signed in to change notification settings - Fork 7
/
MainDialog.cpp
370 lines (292 loc) · 9.4 KB
/
MainDialog.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
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
#include "StdAfx.h"
#include "MainDialog.h"
#include "PrefDialog.h"
#include "DebugDialog.h"
#include "CreditsDialog.h"
#include "SDDialog.h"
#include "Preferences.h"
#include "Render.h"
#include "TestRoutines.h"
void SaveBitmapFile(const char *szFile, UINT Width, UINT Height, UINT Bpp, LPVOID lpData);
HWND MainDialog::m_hWnd = 0;
HBRUSH MainDialog::m_hBGBrush = 0;
HFONT MainDialog::m_hFont = 0;
HACCEL MainDialog::m_hAccelTable = 0;
void MainDialog::Init()
{
m_hBGBrush = CreateSolidBrush(DEF_BACKGROUND_COLOR);
LOGFONT lf;
lf.lfHeight = DEFAULT_DIALOG_FONT_HEIGHT;
lf.lfWidth = 0;
lf.lfEscapement = 0;
lf.lfOrientation = 0;
lf.lfWeight = FW_NORMAL;
lf.lfItalic = FALSE;
lf.lfUnderline = FALSE;
lf.lfStrikeOut = FALSE;
lf.lfCharSet = DEFAULT_CHARSET;
lf.lfOutPrecision = OUT_DEFAULT_PRECIS;
lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
lf.lfQuality = ANTIALIASED_QUALITY;
lf.lfPitchAndFamily = VARIABLE_PITCH;
strcpy(lf.lfFaceName, DEFAULT_DIALOG_FONT_NAME);
m_hFont = CreateFontIndirect(&lf);
}
void MainDialog::Cleanup()
{
DeleteObject(m_hFont);
DeleteObject(m_hBGBrush);
}
HFONT MainDialog::GetWindowFont()
{
return m_hFont;
}
HWND MainDialog::GetWindowHandle()
{
return m_hWnd;
}
void MainDialog::RegisterClass()
{
WNDCLASSEX wcex;
HINSTANCE hInstance = Globals::GetAppInstance();
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_MAINICON);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = m_hBGBrush;
wcex.lpszMenuName = (LPCTSTR)IDM_MAINMENU;
wcex.lpszClassName = DEF_CLASS_NAME;
wcex.hIconSm = LoadIcon(hInstance, (LPCTSTR)IDI_MAINICON);
::RegisterClassEx(&wcex);
}
void MainDialog::UnregisterClass()
{
::UnregisterClass(DEF_CLASS_NAME, Globals::GetAppInstance());
}
BOOL MainDialog::Create()
{
// Find desktop center.
long lScreenWidth;
long lScreenHeight;
Globals::GetScreenDimensions(&lScreenWidth, &lScreenHeight);
int X = (lScreenWidth - DEF_WINDOW_WIDTH) / 2;
int Y = (lScreenHeight - DEF_WINDOW_HEIGHT) / 2;
if (X < 0)
X = 0;
if (Y < 0)
Y = 0;
// Create the window.
m_hWnd = CreateWindow(
DEF_CLASS_NAME, DEF_WINDOW_NAME,
WS_BORDER | WS_MINIMIZEBOX | WS_SYSMENU,
X, Y, DEF_WINDOW_WIDTH, DEF_WINDOW_HEIGHT,
NULL, NULL, Globals::GetAppInstance(), NULL);
return(m_hWnd ? TRUE : FALSE);
}
BOOL MainDialog::LoadHotkeys()
{
m_hAccelTable = LoadAccelerators(Globals::GetAppInstance(), (LPCTSTR)IDA_HOTKEYS);
return(m_hAccelTable ? TRUE : FALSE);
}
long MainDialog::Modal()
{
ShowWindow(m_hWnd, SW_SHOWNORMAL);
UpdateWindow(m_hWnd);
MSG Msg;
for (;;)
{
if (::PeekMessage(&Msg, NULL, 0U, 0U, PM_REMOVE))
{
if (Msg.message == WM_QUIT)
return (long)Msg.wParam;
if (!TranslateAccelerator(Msg.hwnd, m_hAccelTable, &Msg))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
}
else
{
//if (::GetActiveWindow() == m_hWnd)
Render::DrawFrame();
if (!RenderPreferences::NoSleep)
Sleep(1);
}
}
}
LRESULT MainDialog::WndProc(HWND hWnd, UINT MsgID, WPARAM wParam, LPARAM lParam)
{
switch(MsgID)
{
case WM_PAINT:
{
PAINTSTRUCT ps;
RECT rect;
HDC hDC;
hDC = BeginPaint(hWnd, &ps);
::GetClientRect(hWnd, &rect);
// Draw(hDC);
EndPaint(hWnd, &ps);
break;
}
case WM_CLOSE:
{
PostQuitMessage(0);
break;
}
case WM_DESTROY:
{
m_hWnd = 0;
break;
}
case WM_COMMAND:
{
switch(LOWORD(wParam))
{
case ID_FILE_EXIT:
// Exit
PostQuitMessage(0);
break;
case ID_FILE_PREFERENCES:
// Preferences
PrefDialog::Create(hWnd);
break;
case ID_FILE_VIEWDEBUG:
case ID_VIEWDEBUG:
// Debug
DebugDialog::Create(hWnd);
break;
case ID_VIEW_DUNGEON:
// View Dungeon
SDDialog::Create(hWnd);
break;
case ID_HELP_CREDITS:
// Show Credits
CreditsDialog::Create(hWnd);
break;
case ID_TAKESCREENSHOT:
// Screenshot
SaveScreenshot();
break;
}
break;
}
}
if (RenderScene::WndProc(MsgID, wParam, lParam))
return 0;
return DefWindowProc(hWnd, MsgID, wParam, lParam);
}
void MainDialog::SaveScreenshot()
{
D3DDISPLAYMODE DisplayMode;
if (FAILED(g_pD3DDevice->GetDisplayMode(0, &DisplayMode)))
return;
LPDIRECT3DSURFACE9 pSurface = NULL;
if (FAILED(g_pD3DDevice->
CreateOffscreenPlainSurface(DisplayMode.Width, DisplayMode.Height, D3DFMT_A8R8G8B8, D3DPOOL_SYSTEMMEM, &pSurface, NULL)))
return;
// FrontBuffer should be received as D3DFMT_A8R8G8B8
if (SUCCEEDED(g_pD3DDevice->GetFrontBufferData(0, pSurface)))
{
// D3DSURFACE_DESC SurfaceDesc;
// if (FAILED(pSurface->GetDesc(&SurfaceDesc)))
// return;
UINT Width = DisplayMode.Width;
UINT Height = DisplayMode.Height;
D3DLOCKED_RECT Locked;
if (SUCCEEDED(pSurface->LockRect(&Locked, NULL, D3DLOCK_READONLY)))
{
RGBTRIPLE *pOutput = new RGBTRIPLE[ Width * Height ];
LPVOID pBitStream = Locked.pBits;
RGBTRIPLE *pBitDest = pOutput;
for (UINT y = 0; y < Height; y++)
{
LPVOID pNextLine = (LPVOID) (((BYTE *)pBitStream) + Locked.Pitch);
for (UINT x = 0; x < Width; x++)
{
*pBitDest = *((RGBTRIPLE *)pBitStream);
pBitDest += 1;
pBitStream = (LPVOID) (((BYTE *)pBitStream) + 4); // D3DFMT_A8R8G8B8
}
pBitStream = pNextLine;
}
pSurface->UnlockRect();
// Ask where the file is to be saved to.
char szFile[ MAX_PATH ];
szFile[0] = '\0';
OPENFILENAME ofn;
memset(&ofn, 0, sizeof(OPENFILENAME));
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = m_hWnd;
ofn.lpstrFilter = "Bitmap File\0*.bmp\0\0";
ofn.lpstrFile = szFile;
ofn.nMaxFile = sizeof(szFile);
ofn.lpstrInitialDir = Preferences::GetApplicationPath();
ofn.lpstrTitle = "Save Screenshot As..";
ofn.lpstrDefExt = "bmp";
ofn.Flags = OFN_OVERWRITEPROMPT;
if (GetSaveFileName(&ofn))
{
// Create the output bitmap.
SaveBitmapFile(szFile, Width, Height, 24, pOutput);
}
delete [] pOutput;
}
}
pSurface->Release();
}
void SaveBitmapFile(const char *szFile, UINT Width, UINT Height, UINT Bpp, LPVOID lpData)
{
// Bpp = Bits per Pixel
UINT BytesPerPixel = Bpp >> 3;
// Create the output file
HANDLE hFile = CreateFile(szFile, GENERIC_WRITE,
0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
DEBUGOUT("Couldn't create output bitmap file: \"%s\"!\r\n", szFile);
return;
}
// Create the Bitmap Header
BITMAPFILEHEADER FileHeader;
BITMAPINFOHEADER InfoHeader;
// Scan lines must be DWORD-aligned.
UINT LineSize = Width * BytesPerPixel;
UINT LinePadding = 0;
if (LineSize & 3)
LinePadding += 4 - (LineSize & 3);
FileHeader.bfType = 'MB';
FileHeader.bfSize = sizeof(FileHeader) + sizeof(InfoHeader) + (LineSize + LinePadding) * Height;
FileHeader.bfReserved1 = 0;
FileHeader.bfReserved2 = 0;
FileHeader.bfOffBits = sizeof(FileHeader) + sizeof(InfoHeader);
InfoHeader.biSize = sizeof(InfoHeader);
InfoHeader.biWidth = Width;
InfoHeader.biHeight = Height;
InfoHeader.biPlanes = 1;
InfoHeader.biBitCount = Bpp;
InfoHeader.biCompression = BI_RGB;
InfoHeader.biSizeImage = FileHeader.bfSize;
InfoHeader.biXPelsPerMeter = 0;
InfoHeader.biYPelsPerMeter = 0;
InfoHeader.biClrUsed = 0;
InfoHeader.biClrImportant = 0;
// Write the Bitmap File Header.
DWORD Dummy;
if (!WriteFile(hFile, &FileHeader, sizeof(FileHeader), &Dummy, NULL))
DEBUGOUT("Failed writing bitmap file header!\r\n");
if (!WriteFile(hFile, &InfoHeader, sizeof(InfoHeader), &Dummy, NULL))
DEBUGOUT("Failed writing bitmap info header!\r\n");
// Bitmap's prefer bottom-up format, so we're doing it that way.
for (long y = (long)Height - 1; y >= 0; y--)
{
WriteFile(hFile, (BYTE *)lpData + y*LineSize, LineSize, &Dummy, NULL);
DWORD Padding = 0;
WriteFile(hFile, &Padding, LinePadding, &Dummy, NULL);
}
CloseHandle(hFile);
}