-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.cpp
438 lines (284 loc) · 11.2 KB
/
game.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/*
Àâòîð(ñ): Ñàííèêîâ, Êóëåáÿêèí, Ñòóïàê è Ïàðõîìåíêî
Íàçâàíèå êîìàíäû: AGRAGE
e-mail: [email protected]
*/
#include "header.h"
#include <string>
xGame::xGame(HINSTANCE hInstance) : xDlgScene(hInstance) {
terrain = NULL;
user = NULL;
tex_life = 0u;
tex_digits = 0u;
tex_alpha = 0u;
tex_cursor = 0u;
state_game = GAME_MENU;
}
xGame::~xGame() {
}
void xGame::OnCreate(HDC hDC, HWND hwnd) {
user = new xUser(Sound());
terrain = new xTerrain(); //êàðòà âûñîò
terrain->LoadTextura(_T("image\\terrain\\floor.jpg")); //çàãðóæàåì ïîë
terrain->Create(Sound(), 1024, 16);
terrain->LoadTerrain(_T("image\\terrain\\map.raw"), 0.3f); // 0.3f
//çàãðóæàåì êàðòó âûñîò èç map.raw
skybox.Create(500.0f, 180.0f, 500.0f, -1.0f); //ñîçäàåì íåáî
skybox.Load(0, _T("image\\skybox\\back.jpg")); //çàãðóæàåì íåáî ñî âñåõ ñòîðîí
skybox.Load(1, _T("image\\skybox\\front.jpg"));
skybox.Load(2, _T("image\\skybox\\left.jpg"));
skybox.Load(3, _T("image\\skybox\\right.jpg"));
skybox.Load(4, _T("image\\skybox\\top.jpg"));
tex_digits = xTextura::OpenImageTGA(_T("image\\digits.tga")); //öèôðû
tex_alpha = xTextura::OpenImageTGA(_T("image\\alpha.tga")); //áóêâû
tex_life = xTextura::OpenImageTGA(_T("image\\life.tga")); //çíà÷îê æèçíåé
tex_cursor = xTextura::OpenImageTGA(_T("image\\cursor.tga")); //êóðñîð
light_pos = GLVECTOR4(140.0f, 100.0f, -60.0f, 0.0f);
//ñîçäàåì èñòî÷íèêè ñâåòà
xEffect::SetLight(&GLCOLOR(1.0f, 1.0f, 1.0f, 1.0f),
&GLCOLOR(1.0f, 1.0f, 1.0f, 1.0f),
&GLCOLOR(0.4f, 0.4f, 0.4f, 1.0f));
//çàäàåì ïîçèöèþ èñòî÷íèêîâ ñâåòà
terrain->SetLightPos(&light_pos);
//èíèöèàëèçèðóåì êàìåðó
camera.Initialize(GLVECTOR3(160.0f, 3.9f, 0.0f), -45.0f);
// êíîïêè òîëüêî äëÿ ìåíþ è êîíöà èãðû
buttons[0] = sButton(1, _T("ÍÀ×ÀÒÜ ÈÃÐÀÒÜ"), Screen().cx / 2 - 140, Screen().cy / 2 + 100, 280, 60, RGB(30, 100, 255), RGB(230, 100, 155));
buttons[1] = sButton(2, _T("ÂÛÉÒÈ ÈÇ ÈÃÐÛ"), Screen().cx / 2 - 140, Screen().cy / 2 + 190, 280, 60, RGB(30, 100, 255), RGB(230, 100, 155));
state_game = GAME_MENU;
//êàìåðà è ìåíþ
camera.CinematicMenu(TRUE);
}
void xGame::Initialize(void) {
//àâòîìàòè÷åñêèé êàìåðî-îáçîð
camera.CinematicMenu(FALSE);
//çàäàåì íà÷àëüíóþ ïîçèöèþ
camera.Initialize(GLVECTOR3(160.0f, 3.9f, 0.0f), -45.0f);
//íà÷àëüíûå íàñòðîéêè èãðîêà
user->Initialize();
terrain->SetLightPos(&light_pos);
terrain->OffsetCameraY(&camera.GetPos());
terrain->Initialize(&GLVECTOR3(camera.GetPos().x, camera.GetPos().y, -camera.GetPos().z));
}
// Âîò çäåñü, ñàìî âåëè÷åñòâî ìàãèÿ ïðîöåññà ðèñîâàíèÿ
void xGame::OnRender(void) {
if(state_game & GAME_START) {
// âêëþ÷èòü òåêñòóèðîâàíèå
xTextura::Enabled(GL_TRUE);
// âêëþ÷èòü îñâåùåíèå
xEffect::BeginLight();
xEffect::LightPos(&light_pos);
user->Display(fElapsed(), fTime());
xEffect::EndLight();
camera.UpdateMove();
// âêëþ÷èòü îòñå÷åíèå ïðîòèâ ÷àñîâîé ñòðåëêè
xEffect::ModeCull(GL_CCW);
////////////////////// âûâîä ëàíäøàôòà è åãî îáúåêòîâ
terrain->BeginMatrix(&GLVECTOR3(0.0f, 0.0f, 0.0f));
//ïîêàçàòü íåáî
skybox.Display();
// âêëþ÷èòü îñâåùåíèå
xEffect::BeginLight();
xEffect::LightPos(&light_pos);
terrain->DisplayObjects();
terrain->Display();
terrain->EndMatrix();
///////////////////////////////////////////////
GLVECTOR3 puser = GLVECTOR3(camera.GetPos().x, camera.GetPos().y, -camera.GetPos().z);
// âûâîä ðîáîòîâ
terrain->DisplayRobots(&puser, fElapsed(), fTime());
// êîíåö âûâîäà
// îòêëþ÷èòü îñâåùåíèå ñöåíû
xEffect::EndLight();
// âûâîä äåðåâüåâ
xEffect::ModeClip(0.4f);
terrain->DisplayVegetations(&puser, -camera.GetYaw(), fTime(), fElapsed());
xEffect::EndClip();
// îòêëþ÷èòü òåêñòóèðîâàíèå
xTextura::Enabled(GL_FALSE);
////////âûâîä òåíåé//////////
xEffect::ModeStencil();
terrain->DisplayShadow();
terrain->DisplayShadowRobots(&light_pos);
xEffect::EndStencil(&GLCOLOR(0.0f, 0.0f, 0.0f, 0.3f));
//////////////////////////////
// âêëþ÷èòü òåêñòóèðîâàíèå
xTextura::Enabled(GL_TRUE);
// âêëþ÷èòü àëüôà ñìåøèâàíèå(blend)
xEffect::ModeAlpha(&GLCOLOR(1.0f, 1.0f, 1.0f, 1.0f));
terrain->DisplayEffectRobots(&puser, -camera.GetYaw(), fElapsed(), fTime());
user->DisplayLazer(fElapsed(), fTime(), -camera.GetYaw());
xEffect::EndAlpha();
// âêëþ÷èòü 2D
this->Begin2D();
user->DisplayCross(&Screen());
xEffect::ModeClip(0.6f);
//ðèñóåì æèçíè
xRect2D::DrawRectTex(tex_life, 5, 5, 32, 32);
//âûâîä öèôð
xFont::DisplayFontInt(tex_digits, user->GetLife(), 38, 7, 15, 24, &GLVECTOR2(4.0f, 3.0f));
xEffect::EndClip();
this->End2D();
// îòêëþ÷èòü 2D
// îòêëþ÷èòü îòñå÷åíèå back-faces
xEffect::EndCull();
// ñòîëêíîâåíèÿ
//îáíîâëåíèå ñòîëêíîâåíèé
camera.UpdateMove(fElapsed(), fTime());
terrain->CollisionBullet(&puser, user->Bullets());
if(terrain->CollisionUser(&camera, &user->Size())) {
// sound
user->DecLife();
//âñ¸ ïîëüçîâàòåëü ïðîèãðàë GAME OVER
if(user->GetLife() < 0)
state_game = GAME_OVER;
}
// ïåðåìåùåíèå êàìåðîé
//ïîâîðîò êàìåðû
camera.MouseMove(NULL, fElapsed());
//ïåðåìåùåíèå âïåðåä - íàçàä
if(camera.KeyMove(fElapsed(), terrain->CubeSize())) {
terrain->OffsetCameraY(&camera.GetPos());
user->SetMove(TRUE);
} else
user->SetMove(FALSE);
// âûõîä èç èãðû â ëþáîå âðåìÿ
if(GetAsyncKeyState(VK_ESCAPE) & 0x8000) {
state_game = GAME_QUIT;
SendMessage(Handle(), WM_DESTROY, 0, 0);
return;
}
} else if(state_game & GAME_MENU)
this->DrawMenu();
else if(state_game & GAME_OVER)
this->DrawGameOver();
/*
if(glGetError() != GL_NO_ERROR)
logPrint("îøèáêà");*/
}
// Ñîáûòèå âîçíèêàåò ïðè ùåë÷êå êíîïêè-ìûøè
void xGame::OnMouseDown(WPARAM wParam, int x, int y) {
if (state_game & GAME_START)
//ìåòîä âûïóñêàåò ïëàçìåííûé ïó÷îê èç áëàñòåðà
user->OnFire(wParam, &camera.GetPos(), fElapsed(), camera.GetYaw(), camera.GetPitch());
else if(state_game & GAME_MENU) {
POINT pt = { x, y };
int cmd = 0;
for(int i = 0; i < MAX_BUTTONS; i++) {
if((cmd = buttons[i].MouseDown(&pt)) != 0) {
if(cmd & 1) {
state_game = GAME_START;
this->Initialize();
//âêëþ÷àåì ìóçûêó â íà÷àëå èãðû
sndPlaySound(_T("sound\\music.wav"), SND_FILENAME | SND_ASYNC | SND_LOOP);
//ñèíõðîíèçàöèÿ ñ ìóçûêîé
Sleep(500uL);
break;
} else {
//çàâåðøåíèå èãðû
state_game = GAME_QUIT;
//îòïðàâëÿåì ñîîáùåíèå î çàêðûòèè îêíà
SendMessage(Handle(), WM_DESTROY, 0, 0);
break;
}
}
}
}
}
//î÷èòñêà ïàìÿòè íà êîíåö èãðû
void xGame::OnDestroy(void) {
xTextura::Free(tex_digits);
xTextura::Free(tex_alpha);
xTextura::Free(tex_life);
xTextura::Free(tex_cursor);
DELETE_HEAP(terrain);
DELETE_HEAP(user);
skybox.Destroy();
}
// ìåòîä ðèñóåò äèàëîã ìåíþ
void xGame::DrawMenu(void) {
const TCHAR* descgame = _T("ÓÏÐÀÂËÅÍÈÅ Â ÈÃÐÅ ÊÀÊ Â ÎÁÛ×ÍÛÕ ØÓÒÅÐÀÕ");
POINT pt;
GetCursorPos(&pt);
ScreenToClient(Handle(), &pt);
// âêëþ÷èòü òåêñòóèðîâàíèå
xTextura::Enabled(GL_TRUE);
glColor4f(0.4f, 0.4f, 0.9f, 1.0f);
camera.UpdateMove();
xEffect::ModeCull(GL_CCW);
terrain->BeginMatrix(&GLVECTOR3(0.0f, 0.0f, 0.0f));
skybox.Display();
xEffect::BeginLight();
xEffect::LightPos(&light_pos);
terrain->DisplayObjects();
terrain->Display();
terrain->EndMatrix();
xEffect::EndLight();
// âûâîä äåðåâüåâ
xEffect::ModeClip(0.4f);
GLVECTOR3 puser = GLVECTOR3(camera.GetPos().x, camera.GetPos().y, -camera.GetPos().z);
terrain->DisplayVegetations(&puser, -camera.GetYaw(), fTime(), fElapsed());
xEffect::EndClip();
camera.UpdateMove(fElapsed(), fTime());
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
this->Begin2D();
xEffect::ModeAlpha(&GLCOLOR(0.0f, 0.0f, 0.0f, 0.4f));
xTextura::Enabled(GL_FALSE);
for(int i = 0; i < MAX_BUTTONS; i++) { // âûâîä êíîïîê
xRect2D::DrawRect(buttons[i].color, buttons[i].x, buttons[i].y, buttons[i].width, buttons[i].height);
buttons[i].MouseMove(&pt);
}
xTextura::Enabled(GL_TRUE);
xEffect::EndAlpha();
xRect2D::DrawRectTex(terrain->tHouse(0), Screen().cx / 2 - 220, 40, 440, 142, 2.0f);
xEffect::ModeClip(0.6f);
xFont::DisplayFontText(tex_alpha, descgame, Screen().cx / 2 - _tcslen(descgame) * 14 / 2, 200, 14, 17, &GLVECTOR2(7.0f, 5.0f));
GLfloat alpha = 0.2f + 0.8f * powf(cosf(fTime() / 300.0f), 2.0f);
glColor4f(alpha, 1.0f, alpha, 1.0f);
const TCHAR* str = _T("");
xFont::DisplayFontText(tex_alpha, str, Screen().cx / 2 - _tcslen(str) * 38 / 2, 70, 38, 80, &GLVECTOR2(7.0f, 5.0f));
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
const TCHAR* author = _T("ÑÀÍÍÈÊΠÊÓËÅÁßÊÈÍ ÑÒÓÏÀÊ È ÏÀÐÕÎÌÅÍÊÎ");
xFont::DisplayFontText(tex_alpha, author, Screen().cx / 2 - _tcslen(author) * 15 / 2, Screen().cy - 30, 15, 20, &GLVECTOR2(7.0f, 5.0f));
glColor4f(1.0f, 0.5f, 0.0f, 1.0f);
for(int c = 0; c < MAX_BUTTONS; c++) // âûâîä íàäïèñè íà êíîïêè
xFont::DisplayFontText(tex_alpha, buttons[c].caption, buttons[c].x + buttons[c].width / 2 - _tcslen(buttons[c].caption) * 15 / 2, buttons[c].y + buttons[c].height / 2 - 20/2, 15, 20, &GLVECTOR2(7.0f, 5.0f));
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
xRect2D::DrawRectTex(tex_cursor, pt.x - 16, pt.y - 16, 32, 32);
xEffect::EndClip();
this->End2D();
xTextura::Enabled(GL_FALSE);
}
// ìåòîä ðèñóåò äèàëîã ïðîèãðûøà
void xGame::DrawGameOver(void) {
const TCHAR* cmds[2] = { _T("ÍÀ×ÀÒÜ ÇÀÍÎÂÎ ÅÍÒÅÐ"), _T("ÂÛÕÎÄ ÅÑÊÝÉÏÅ") };
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
this->Begin2D();
xRect2D::DrawRectTex(terrain->tSkull(), 0, 0, Screen().cx, Screen().cy, 5.0f);
xEffect::ModeClip(0.6f);
xTextura::Enabled(GL_FALSE);
xEffect::ModeAlpha(&GLCOLOR(0.0f, 0.0f, 0.0f, 0.1f), GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
xRect2D::DrawRect(&GLCOLOR(0.4f, 0.2f, 0.1f, 0.7f), Screen().cx / 2 - 300, Screen().cy / 2 - 100, 600, 200);
xRect2D::DrawRect(&GLCOLOR(0.0f, 0.4f, 0.3f, 0.8f), Screen().cx / 2 - 200, Screen().cy / 2 + 120, 400, 120);
xEffect::EndAlpha();
xTextura::Enabled(GL_TRUE);
GLfloat alpha = 0.4f + 0.5f * powf(cosf(fTime() / 400.0f), 2.0f);
glColor4f(alpha, 0.0f, 0.0f, 1.0f);
const TCHAR* str = _T("ÂÛ ÏÐÎÈÃÐÀËÈ ÁÈÒÂÓ");
xFont::DisplayFontText(tex_alpha, str, Screen().cx / 2 - _tcslen(str) * 30 / 2, Screen().cy / 2 - 40 / 2, 30, 40, &GLVECTOR2(7.0f, 5.0f));
// âûâîä íàçâàíèå êîììàíä êëàâèø
glColor4f(alpha, alpha, 0.5f, 1.0f);
xFont::DisplayFontText(tex_alpha, cmds[0], Screen().cx / 2 - _tcslen(cmds[0]) * 20 / 2, Screen().cy / 2 + 140, 20, 30, &GLVECTOR2(7.0f, 5.0f));
xFont::DisplayFontText(tex_alpha, cmds[1], Screen().cx / 2 - _tcslen(cmds[1]) * 20 / 2, Screen().cy / 2 + 190, 20, 30, &GLVECTOR2(7.0f, 5.0f));
xEffect::EndClip();
this->End2D();
// âûõîä èç èãðû
if(GetAsyncKeyState(VK_ESCAPE) & 0x8000) {
state_game = GAME_QUIT;
SendMessage(Handle(), WM_DESTROY, 0, 0);
} else if(GetAsyncKeyState(VK_RETURN) & 0x8000) { // íà÷àòü çàíîâî èãðàòü
state_game = GAME_START;
this->Initialize();
Sleep(500uL);
}
}