-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCengaGame.cpp
331 lines (293 loc) · 7.45 KB
/
CengaGame.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
#pragma once
#include <Windows.h>
#include "GameObjects/Game.h"
#include "UserInterface/Menu.h"
#include "Utilities/Macro.h"
#include "Utilities/Globals.h"
#include "Data/GameLoader.h"
#include "Data/ParamLoader.h"
#include "UserInterface/SplashScreen.h"
#include <time.h>
#include <stdlib.h>
// Attributes:
CGame *g;
Menu *m;
SplashScreen *s;
bool running;
long splashTime = 40*2;
BYTE keysPressed[256];
RECT scrnSize;
HDC bufDC, hDC;
HBITMAP bufBMP;
HANDLE hImage;
HWND hWnd,hWndSplash;
// Methods:
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
void HandleMenuSelection();
// PROGRAM ENTRY POINT:
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
// Main window programming using Win32 API:
MSG msg;
WNDCLASSEX wcex; // Win32 GUI
HACCEL m_hAccelTable;
// Load Params from ini File
ParamLoader::Instance()->LoadParamsFromFile("Resources/Params.ini");
// Main Window Properties:
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = 0;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = "Window";
wcex.hIconSm = 0;
if (!RegisterClassEx(&wcex))
{
return FALSE;
}
// Create the main window:
hWnd = CreateWindow("Window", "Cengaver The Prism Operation", WS_SYSMENU | WS_BORDER | WS_MINIMIZEBOX,
GetSystemMetrics(SM_CXSCREEN)/2-WINDOWWIDTH/2, GetSystemMetrics(SM_CYSCREEN)/2-WINDOWHEIGHT/2, WINDOWWIDTH, WINDOWHEIGHT, NULL, NULL, hInstance, NULL);
// Create the splash window:
hWndSplash = CreateWindow("Window", "Cengaver Splash", WS_POPUPWINDOW | WS_EX_TOPMOST,
GetSystemMetrics(SM_CXSCREEN)/2-576/2,GetSystemMetrics(SM_CYSCREEN)/2-320/2, 576, 320, hWnd, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
if (!hWndSplash)
{
return FALSE;
}
hDC = ::GetDC(hWnd);
::GetClientRect(hWnd, &scrnSize);
bufDC = CreateCompatibleDC(NULL);
bufBMP = CreateCompatibleBitmap(hDC, scrnSize.right, scrnSize.bottom);
SelectObject(bufDC, bufBMP);
DeleteObject(bufBMP);
// Create menu and splash screen:
m = new Menu(&bufDC);
s = new SplashScreen(&bufDC);
// Set highscore and lowtime loaded:
m->SetHighscore(GameLoader().LoadGame()[3]);
m->SetLowtime(GameLoader().LoadGame()[4]);
// Input Handling:
GetKeyboardState(keysPressed);
ShowWindow(hWndSplash, nCmdShow);
UpdateWindow(hWndSplash);
// Main game loop:
unsigned long time_count = 40; // Target FPS
unsigned long long cur_time;
unsigned long long perf_counter;
unsigned long long next_time = 0;
QueryPerformanceFrequency((LARGE_INTEGER*) &perf_counter);
time_count = perf_counter/40;
QueryPerformanceCounter((LARGE_INTEGER*) & next_time);
// Blit picture on the splash screen and delete the resources used:
HANDLE hh = LoadImage(NULL, "Resources/SplashScreen.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
HDC hcd = CreateCompatibleDC(NULL);
HDC hc = GetDC(hWndSplash);
SelectObject(hcd,hh);
BitBlt(hc, 0, 0, 576, 320, hcd, 0, 0, SRCCOPY);
DeleteDC(hc);
DeleteDC(hcd);
DeleteObject(hh);
srand((unsigned) time(0));
// Gameloop and message handling system:
running = true;
while(running)
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
// Translate and dispatch the received message:
if (!TranslateAccelerator(msg.hwnd, m_hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
else
{
// Increase the query performance timer:
QueryPerformanceCounter((LARGE_INTEGER*) &cur_time);
// If timer goes off and in time frame:
if(cur_time >= next_time)
{
// Set new time for query performance timer:
next_time += time_count;
if(next_time < cur_time)
next_time = cur_time + time_count;
// Splash screen:
if(splashTime > 0)
{
splashTime--;
}
else if(splashTime == 0)
{
// Delete splash screen:
DestroyWindow(hWndSplash);
splashTime = -1;
hWndSplash = 0;
// Show the main window:
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
}
else
{
// Update splash screen:
if(s != 0 && s->running == true)
{
SplashScreens result = s->Update();
if(result == SplashScreens::Credits)
{
m->running = true;
}
else if(result == SplashScreens::Dead)
{
m->running = true;
}
else if(result == SplashScreens::EndLevel)
{
g->running = true;
}
else if(result == SplashScreens::Splash)
{
m->running = true;
}
s->Draw();
}
// Update the game:
if(g != 0 && g->running == true)
{
char result = g->Update();
if(result == GAME_END_END)
{
g->running = false;
s->SetNewScreen(SplashScreens::EndGame);
s->running = true;
m->SetHighscore(g->GetScore());
m->SetLowtime(g->currentTime);
m->SetLeftMenu(false);
GameLoader().SaveGame(0,0,HERO_MAX_HEALTH,m->GetHighscore(),m->GetLowtime());
}
else if(result == GAME_END_DEAD)
{
g->running = false;
s->SetNewScreen(SplashScreens::Dead);
s->running = true;
m->SetLeftMenu(false);
}
else if(result == GAME_END_LEVEL)
{
g->running = false;
GameLoader().SaveGame(g->currentLevel,g->currentTime,g->GetHero()->GetHealth(),m->GetHighscore(),m->GetLowtime());
s->SetNewScreen(SplashScreens::EndLevel);
s->running = true;
}
else
{
g->Draw();
}
}
else
{
if(s->running == false)
m->running = true;
}
// Update the menu:
if(m != 0 && m->running == true)
{
m->Update();
m->Draw();
HandleMenuSelection();
if(running == false)
break;
}
// Present the buffer:
BitBlt(hDC, 0, 0, 576, 320, bufDC, 0, 0, SRCCOPY);
}
}
}
}
// Delete the resources used:
CLEAN(m)
CLEAN(g)
CLEAN(s)
DeleteDC(bufDC);
DeleteDC(hDC);
::PostQuitMessage(0);
return (int) msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_DESTROY:
if(hWndSplash)
{
DestroyWindow(hWndSplash);
}
else
{
running = false;
}
break;
}
return ::DefWindowProc(hWnd, message, wParam, lParam);
}
void HandleMenuSelection()
{
unsigned long* loadResult;
switch(m->GetMenuSelected())
{
// Start new game:
case MENU_NEW:
CLEAN(g)
m->SetLeftMenu(true);
m->running = false;
g = new CGame(&bufDC);
GameLoader().SaveGame(1,0,HERO_MAX_HEALTH, -1, -1);
break;
// Load game:
case MENU_LOAD:
if (g != 0) {
if (g->currentLevel != 0 && g->currentLevel != 1) {
CLEAN(g)
m->SetLeftMenu(true);
m->running = false;
g = new CGame(&bufDC);
loadResult = GameLoader().LoadGame();
g->currentLevel = (unsigned char)loadResult[0];
g->currentTime = (long)loadResult[2];
g->GetHero()->SetHealth((char)loadResult[1]);
g->NextLevel();
}
}
break;
// Show credits:
case MENU_CREDITS:
s->SetNewScreen(Credits);
s->running = true;
m->running = false;
break;
// Quit:
case MENU_EXIT:
running = false;
break;
// Resume:
case MENU_RESUME:
if(g != 0 && m->isResumable)
g->running = true;
break;
default:
break;
}
}