-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameEngine.cpp
293 lines (263 loc) · 10.1 KB
/
GameEngine.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
//-----------------------------------------------------------------
// Game Engine Class
// C++ Source - GameEngine.cpp
//-----------------------------------------------------------------
//-----------------------------------------------------------------
// Include Files
//-----------------------------------------------------------------
#include "GameEngine.h"
//-----------------------------------------------------------------
// Global parameters (some system setting variables will later be
//replaced by reading from e.g., ini files)
//-----------------------------------------------------------------
BOOL ISWINDOWED = TRUE;
BOOL USED3D11 = TRUE;
int OPENGLMAJORVERSION = 4;
int OPENGLMINORVERSION = 0;
using namespace Emerald;
GameEngine* pGameEngine = nullptr;
//-----------------------------------------------------------------
// Windows Functions
//-----------------------------------------------------------------
LRESULT CALLBACK WndProc(HWND hWindow, UINT msg, WPARAM wParam, LPARAM lParam)
{
// Route all Windows messages to the game engine
return pGameEngine->HandleEvent(hWindow, msg, wParam, lParam);
}
//-----------------------------------------------------------------
// GameEngine Constructor(s)/Destructor
//-----------------------------------------------------------------
GameEngine::GameEngine(HINSTANCE hInstance, GameApp* pGameApp, LPTSTR szWindowClass,
LPTSTR szTitle, WORD icon, WORD smallIcon, int width, int height)
{
// Set the member variables for the game engine
m_hInstance = hInstance;
m_hWindow = NULL;
m_pWindowClass = new WCHAR[32];
m_pTitle = new WCHAR[32];
unsigned int stringSize;
if (StringCbLength(szWindowClass, 32 * sizeof(WCHAR), &stringSize) == S_OK)//test incoming string length
{
if (StringCbCopy(m_pWindowClass, 32 * sizeof(WCHAR), szWindowClass) != S_OK)
{
m_pWindowClass = L"default class name";
}
}
else
{
m_pWindowClass = L"default class name";
}
if (StringCbLength(szTitle, 32 * sizeof(WCHAR), &stringSize) == S_OK)
{
if (StringCbCopy(m_pTitle, 32 * sizeof(WCHAR), szTitle) != S_OK)
{
m_pTitle = L"default title";
}
}
else
{
m_pTitle = L"default title";
}
m_icon = icon;
m_smallIcon = smallIcon;
m_clientWidth = width;
m_clientHeight = height;
m_frameDelay = 50; // 20 FPS default
m_sleep = TRUE;
m_windowed = ISWINDOWED;
m_pGameApp = pGameApp;
m_appPaused = false;
m_minimised = false;
m_maxmised = false;
m_resizing = false;
m_pD3D11Renderer = nullptr;
m_pOpenGLRenderer = nullptr;
m_pResourceManager = nullptr;
}
GameEngine::~GameEngine()
{
SAFE_DELETEARRAY(m_pWindowClass);
SAFE_DELETEARRAY(m_pTitle);
SAFE_DELETE(m_pD3D11Renderer);
if (!USED3D11 && !ISWINDOWED)//opengl full screen
{
ChangeDisplaySettings(NULL, 0);//set back to the desktop
}
SAFE_DELETE(m_pOpenGLRenderer);
SAFE_DELETE(m_pResourceManager);
}
//-----------------------------------------------------------------
// Game Engine General Methods
//-----------------------------------------------------------------
BOOL GameEngine::Initialise(int iCmdShow)
{
WNDCLASSEX wndclass;
// Create the window class for the main window
wndclass.cbSize = sizeof(wndclass);
wndclass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;//owndc is because of opengl (private dc)
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = m_hInstance;
wndclass.hIcon = LoadIcon(m_hInstance, MAKEINTRESOURCE(GetIcon()));
wndclass.hIconSm = LoadIcon(m_hInstance, MAKEINTRESOURCE(GetSmallIcon()));
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = NULL;//(HBRUSH)(COLOR_WINDOW + 1);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = m_pWindowClass;
// Register the window class
if (!RegisterClassEx(&wndclass))
{
return FALSE;
}
//opengl full screen
DWORD windowStyle;
if (!USED3D11 && !ISWINDOWED)
{
DEVMODE dmScreenSettings; // Device Mode
memset(&dmScreenSettings, 0, sizeof(dmScreenSettings)); // Makes Sure Memory's Cleared
dmScreenSettings.dmSize = sizeof(dmScreenSettings); // Size Of The Devmode Structure
dmScreenSettings.dmPelsWidth = m_clientWidth; // Selected Screen Width
dmScreenSettings.dmPelsHeight = m_clientHeight; // Selected Screen Height
dmScreenSettings.dmBitsPerPel = 32; // Selected Bits Per Pixel (Must be the same as set in the pixel format attrib list
dmScreenSettings.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
// Try To Set Selected Mode And Get Results. NOTE: CDS_FULLSCREEN Gets Rid Of Start Bar.
if (ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
{
MessageBox(NULL, TEXT("Sorry, can\'t play in full screen. Continue with window mode."), TEXT("ERROR"), MB_OK | MB_ICONSTOP);
windowStyle = WS_POPUPWINDOW | WS_CAPTION | WS_MINIMIZEBOX;
}
else
{
windowStyle = WS_POPUP;
}
}
else//D3D9 fullscreen also comes here but these styles take no effects
{
windowStyle = WS_POPUPWINDOW | WS_CAPTION | WS_MINIMIZEBOX;
}
// Calculate the window size and position based upon the game size
int windowWidth = m_clientWidth + GetSystemMetrics(SM_CXFIXEDFRAME) * 2,
windowHeight = m_clientHeight + GetSystemMetrics(SM_CYFIXEDFRAME) * 2 +
GetSystemMetrics(SM_CYCAPTION);
if (wndclass.lpszMenuName != NULL)
{
windowHeight += GetSystemMetrics(SM_CYMENU);
}
int windowPosX = (GetSystemMetrics(SM_CXSCREEN) - windowWidth) / 2;
int windowPosY = (GetSystemMetrics(SM_CYSCREEN) - windowHeight) / 2;
// Create the window
m_hWindow = CreateWindow(m_pWindowClass, m_pTitle, windowStyle, windowPosX, windowPosY,
windowWidth, windowHeight, NULL, NULL, m_hInstance, NULL);
if (!m_hWindow)
{
return FALSE;
}
//Initiate the renderer
if (USED3D11)
{
m_pD3D11Renderer = new D3D11Renderer;
if (m_pD3D11Renderer == NULL)
{
MessageBox(NULL, TEXT("error in initiating D3D9Renderer"), TEXT("ERROR"), MB_OK | MB_ICONEXCLAMATION);
return FALSE;
}
if (!(m_pD3D11Renderer->Initialise(m_hWindow, m_clientWidth, m_clientHeight)))
{
return FALSE;
}
}
else
{
m_pOpenGLRenderer = new OpenGLRenderer;
m_pOpenGLRenderer->SetHDC(GetDC(m_hWindow));
if (!(m_pOpenGLRenderer->Initialise(m_hWindow, m_pOpenGLRenderer->GetHDC(),
m_hInstance, OPENGLMAJORVERSION, OPENGLMINORVERSION, WGL_CONTEXT_CORE_PROFILE_BIT_ARB)))
{
return FALSE;
}
}
//Initialise the resource manager
m_pResourceManager = new ResourceManager;
// Set the game window and start the game
SetWindow(m_hWindow);
m_pGameApp->GameStart();
// Show and update the window
ShowWindow(m_hWindow, iCmdShow);
UpdateWindow(m_hWindow);
return TRUE;
}
LRESULT GameEngine::HandleEvent(HWND hWindow, UINT msg, WPARAM wParam, LPARAM lParam)
{
// Route Windows messages to game engine member functions
switch (msg)
{
case WM_ACTIVATE:
// Activate/deactivate the game and update the Sleep status
if (wParam != WA_INACTIVE)
{
m_pGameApp->GameActivate();
SetSleep(FALSE);
}
else
{
m_pGameApp->GameDeactivate();
SetSleep(TRUE);
}
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_MBUTTONUP://simulate the situation where player select the quit game menu item in game.
PostMessage(hWindow, WM_CLOSE, NULL, NULL);
return 0;
case WM_KEYUP:
if (wParam == VK_ESCAPE)
PostMessage(hWindow, WM_CLOSE, NULL, NULL);
return 0;
case WM_SIZE:
m_clientWidth = LOWORD(lParam);
m_clientHeight = HIWORD(lParam);
if (wParam == SIZE_MINIMIZED)
{
m_appPaused = true;
m_minimised = true;
m_maxmised = false;
}
else if (wParam == SIZE_MAXIMIZED)
{
m_appPaused = false;
m_minimised = false;
m_maxmised = true;
m_pD3D11Renderer->OnWindowResize(m_clientWidth, m_clientHeight);
}
else if (wParam == SIZE_RESTORED)
{
if (m_minimised)// Restoring from minimised state?
{
m_appPaused = false;
m_minimised = false;
m_pD3D11Renderer->OnWindowResize(m_clientWidth, m_clientHeight);
}
else if (m_maxmised)// Restoring from maximised state?
{
m_appPaused = false;
m_maxmised = false;
m_pD3D11Renderer->OnWindowResize(m_clientWidth, m_clientHeight);
}
else if (m_resizing)
{
//wait for user done dragging the resize handler
}
else // API call such as SetWindowPos or mSwapChain->SetFullscreenState.
{
if (m_pD3D11Renderer != nullptr)
{
m_pD3D11Renderer->OnWindowResize(m_clientWidth, m_clientHeight);
}
}
}
return 0;
}
return DefWindowProc(hWindow, msg, wParam, lParam);
}