1 /*
2 * GameDraw.cpp
3 * Copyright (C) 2007 by Bryan Duff <duff0097@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18 * USA
19 */
20
21 #include "Weapon.h"
22 #include "TGALoader.h"
23 #include "Game.h"
24
25 extern double multiplier;
26
27 extern unsigned int gSourceID[100];
28 extern unsigned int gSampleSet[100];
29
30 extern Camera camera;
31 extern Sprites sprites;
32 extern Fog fog;
33 extern Decals decals;
34 extern Config config;
35 extern Environment environment;
36 extern Font font;
37
38 extern float sinefluct;
39 extern float sinefluctprog;
40
SaveScreenshot(const char * path)41 void Game::SaveScreenshot(const char *path)
42 {
43 int width = config.screenwidth;
44 int height = config.screenheight;
45 int bpp = config.bpp;
46
47 unsigned int size = width*height*3; //3 = bbp 24
48 unsigned char *pixels = (unsigned char *)malloc(size);
49 if(!pixels) {
50 printf("SaveScreenshot(): malloc failed");
51 }
52
53 glFinish(); //finish any drawing
54
55 glReadPixels(0, 0, width, height, GL_BGR, GL_UNSIGNED_BYTE, (GLvoid*)pixels);
56
57 TGAImageRec image;
58 image.data = pixels;
59 image.bpp = bpp;
60 image.sizeX = width;
61 image.sizeY = height;
62
63 WriteTGA(path, &image, size);
64
65 free(pixels);
66 }
67
LoadingScreen(float percent)68 void Game::LoadingScreen(float percent)
69 {
70 glLoadIdentity();
71
72 //Clear to black
73 glClearColor(0, 0, 0, 1);
74 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
75
76 //Background
77 glDisable(GL_TEXTURE_2D);
78 glDisable(GL_DEPTH_TEST); // Disables Depth Testing
79 glDisable(GL_CULL_FACE);
80 glDisable(GL_LIGHTING);
81
82 glDepthMask(0);
83
84 glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
85
86 glPushMatrix(); // Store The Projection Matrix
87
88 glLoadIdentity(); // Reset The Projection Matrix
89
90 glOrtho(0, screenwidth, 0, screenheight, -100, 100); // Set Up An Ortho Screen
91
92 glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
93
94 glPushMatrix(); // Store The Modelview Matrix
95
96 for(int i = 19; i >= 0; i--) {
97 glLoadIdentity(); // Reset The Modelview Matrix
98 glTranslatef((int)(screenwidth * 0.1875) - i * 1,
99 (int)(screenheight * 0.396) - i * 1, 0);
100 glScalef((int)(screenwidth * 0.625) + i * 2, 30 + i * 2, 1);
101
102 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
103
104 glEnable(GL_BLEND);
105
106 if(i) {
107 glColor4f(1 - (float)i / 20 - percent / 100,
108 1 - (float)i / 20 - percent / 100,
109 1 - (float)i / 20 - percent / 100, 1);
110 }
111
112 if(!i)
113 glColor4f(0, 0, 0, 1);
114
115 glBegin(GL_QUADS);
116 glVertex3f(0, 0, 0.0f);
117 glVertex3f(1, 0, 0.0f);
118 glVertex3f(1, 1, 0.0f);
119 glVertex3f(0, 1, 0.0f);
120 glEnd();
121 }
122
123 glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
124
125 glPopMatrix(); // Restore The Old Projection Matrix
126
127 glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
128
129 glPopMatrix(); // Restore The Old Projection Matrix
130
131 glDisable(GL_BLEND);
132
133 glDepthMask(1);
134
135 //Progress
136 glDisable(GL_DEPTH_TEST); // Disables Depth Testing
137 glDisable(GL_CULL_FACE);
138 glDisable(GL_LIGHTING);
139 glDisable(GL_TEXTURE_2D);
140
141 glDepthMask(0);
142
143 glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
144
145 glPushMatrix(); // Store The Projection Matrix
146
147 glLoadIdentity(); // Reset The Projection Matrix
148
149 glOrtho(0, screenwidth, 0, screenheight, -100, 100); // Set Up An Ortho Screen
150
151 glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
152
153 glPushMatrix(); // Store The Modelview Matrix
154
155 for(int i = 19; i >= 0; i--) {
156 glLoadIdentity(); // Reset The Modelview Matrix
157
158 glTranslatef((int)(screenwidth * 0.1875), (int)(screenheight * 0.396), 0);
159
160 if(4 * percent + i * 2 < 400)
161 glScalef((int)(screenwidth * 0.006) * percent + i * 2, 30, 1);
162
163 if(4 * percent + i * 2 >= 400)
164 glScalef(400, 30, 1);
165
166 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
167
168 glEnable(GL_BLEND);
169
170 glColor4f(1, 0, 0, .1);
171
172 glBegin(GL_QUADS);
173 glVertex3f(0, 0, 0.0f);
174 glVertex3f(1, 0, 0.0f);
175 glVertex3f(1, 1, 0.0f);
176 glVertex3f(0, 1, 0.0f);
177 glEnd();
178 }
179
180 glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
181
182 glPopMatrix(); // Restore The Old Projection Matrix
183
184 glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
185
186 glPopMatrix(); // Restore The Old Projection Matrix
187
188 glDisable(GL_BLEND);
189
190 glDepthMask(1);
191
192 //Text
193 glEnable(GL_TEXTURE_2D);
194 glColor4f(.6 - .6 * percent / 100, 0, 0, 1);
195
196 text.glPrint((int)(screenwidth * 0.4375), (int)(screenheight * 0.406),
197 "LOADING...", 1, 1, screenwidth, screenheight);
198
199 SDL_GL_SwapBuffers();
200 }
201
DrawMouse(void)202 void Game::DrawMouse(void)
203 {
204 glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
205
206 glPushMatrix(); // Store The Projection Matrix
207
208 glLoadIdentity(); // Reset The Projection Matrix
209
210 glOrtho(0, screenwidth, 0, screenheight, -100, 100); // Set Up An Ortho Screen
211
212 glMatrixMode(GL_MODELVIEW);
213
214 glDisable(GL_TEXTURE_2D);
215
216 Point mouseloc;
217
218 GetMouse(&mouseloc);
219
220 mouseloc.v = screenheight - mouseloc.v;
221
222 glColor4f(.1, 0, 0, 1);
223
224 float size = 5;
225
226 glBegin(GL_TRIANGLES);
227 glVertex3f(mouseloc.h, mouseloc.v, 0);
228 glVertex3f(mouseloc.h + 2 * size, mouseloc.v - 2 * size, 0);
229 glVertex3f(mouseloc.h + .5 * size, mouseloc.v - 2 * size, 0);
230 glEnd();
231
232 glColor4f(1, 0, 0, 1);
233
234 glBegin(GL_QUADS);
235 glVertex3f(olddrawmouse.h, olddrawmouse.v, 0);
236 glVertex3f(mouseloc.h, mouseloc.v, 0);
237 glVertex3f(mouseloc.h + 2 * size, mouseloc.v - 2 * size, 0);
238 glVertex3f(olddrawmouse.h + 2 * size, olddrawmouse.v - 2 * size, 0);
239
240 glVertex3f(olddrawmouse.h, olddrawmouse.v, 0);
241 glVertex3f(mouseloc.h, mouseloc.v, 0);
242 glVertex3f(mouseloc.h + .5 * size, mouseloc.v - 2 * size, 0);
243 glVertex3f(olddrawmouse.h + .5 * size, olddrawmouse.v - 2 * size, 0);
244
245 glVertex3f(olddrawmouse.h + 2 * size, olddrawmouse.v - 2 * size, 0);
246 glVertex3f(mouseloc.h + 2 * size, mouseloc.v - 2 * size, 0);
247 glVertex3f(mouseloc.h + .5 * size, mouseloc.v - 2 * size, 0);
248 glVertex3f(olddrawmouse.h + .5 * size, olddrawmouse.v - 2 * size, 0);
249 glEnd();
250
251 glPopMatrix();
252
253 olddrawmouse = mouseloc;
254 }
255
DrawFlash()256 void Game::DrawFlash()
257 {
258 if(flashamount > 0) {
259
260 if(flashamount > 1)
261 flashamount = 1;
262
263 flashamount -= multiplier;
264
265 if(flashamount < 0)
266 flashamount = 0;
267
268 glDisable(GL_DEPTH_TEST); // Disables Depth Testing
269 glDisable(GL_CULL_FACE);
270 glDisable(GL_LIGHTING);
271
272 glDepthMask(0);
273
274 glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
275
276 glPushMatrix(); // Store The Projection Matrix
277
278 glLoadIdentity(); // Reset The Projection Matrix
279
280 glOrtho(0, screenwidth, 0, screenheight, -100, 100); // Set Up An Ortho Screen
281
282 glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
283
284 glPushMatrix(); // Store The Modelview Matrix
285
286 glLoadIdentity(); // Reset The Modelview Matrix
287
288 glScalef(screenwidth, screenheight, 1);
289
290 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
291
292 glEnable(GL_BLEND);
293
294 glColor4f(flashr, flashg, flashb, flashamount);
295
296 glBegin(GL_QUADS);
297 glVertex3f(0, 0, 0.0f);
298 glVertex3f(256, 0, 0.0f);
299 glVertex3f(256, 256, 0.0f);
300 glVertex3f(0, 256, 0.0f);
301 glEnd();
302
303 glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
304
305 glPopMatrix(); // Restore The Old Projection Matrix
306
307 glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
308
309 glPopMatrix(); // Restore The Old Projection Matrix
310
311 glEnable(GL_DEPTH_TEST); // Enables Depth Testing
312 glEnable(GL_CULL_FACE);
313
314 glDisable(GL_BLEND);
315
316 glDepthMask(1);
317
318 }
319 }
320
DrawMainMenu()321 void Game::DrawMainMenu()
322 {
323 //Setup fast sine fluctuation
324 sinefluct = sin(sinefluctprog);
325 sinefluctprog += multiplier * 1.5;
326
327 glLoadIdentity();
328 glClearColor(0, 0, 0, 1);
329 glDisable(GL_CLIP_PLANE0);
330 glDisable(GL_FOG);
331 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
332
333 //Title
334 //"Black Shades"
335 Image *image =
336 (Image *) WindowMgr::getInstance().getRoot()->getWindow("mainmenu")->
337 getWindow("title")->getWindow("title_image1");
338 image->update(sinefluct, sinefluctprog); //FIXME: hack
339
340 image =
341 (Image *) WindowMgr::getInstance().getRoot()->getWindow("mainmenu")->
342 getWindow("title")->getWindow("title_image2");
343 image->update(sinefluct, sinefluctprog); //FIXME: hack
344
345 //"New Game"
346 image =
347 (Image *) WindowMgr::getInstance().getRoot()->getWindow("mainmenu")->
348 getWindow("game")->getWindow("game_image1");
349 image->update(sinefluct, sinefluctprog);
350
351 Image *image2 =
352 (Image *) WindowMgr::getInstance().getRoot()->getWindow("mainmenu")->
353 getWindow("game")->getWindow("game_image2");
354 image2->update(sinefluct, sinefluctprog);
355
356 if(mouseoverbutton != 1) {
357 image->setActive(0);
358 image2->setActive(0);
359 }
360
361 if(mouseoverbutton == 1) {
362 image->setActive(1);
363 image2->setActive(1);
364 }
365
366 Label *label =
367 (Label *) WindowMgr::getInstance().getRoot()->getWindow("mainmenu")->
368 getWindow("game");
369
370 if(!gameinprogress)
371 label->setText("New Game");
372
373 if(gameinprogress)
374 label->setText("Resume Game");
375
376 image =
377 (Image *) WindowMgr::getInstance().getRoot()->getWindow("mainmenu")->
378 getWindow("quit")->getWindow("quit_image1");
379 image->update(sinefluct, sinefluctprog);
380
381 image2 =
382 (Image *) WindowMgr::getInstance().getRoot()->getWindow("mainmenu")->
383 getWindow("quit")->getWindow("quit_image2");
384 image2->update(sinefluct, sinefluctprog);
385
386 if(mouseoverbutton != 2) {
387 image->setActive(0);
388 image2->setActive(0);
389 }
390
391 if(mouseoverbutton == 2) {
392 image->setActive(1);
393 image2->setActive(1);
394 }
395
396 label =
397 (Label *) WindowMgr::getInstance().getRoot()->getWindow("mainmenu")->
398 getWindow("quit");
399
400 if(!gameinprogress)
401 label->setText("Quit");
402
403 if(gameinprogress)
404 label->setText("End Game");
405
406 //WindowMgr::getInstance().draw();
407 WindowMgr::getInstance().getRoot()->getWindow("mainmenu")->draw();
408
409 static char string[256] = "";
410 //text.glPrint((int)(screenwidth*0.308) - gameinprogress * 15, (int)(screenheight*0.181), string, 1, 1.5, screenwidth, screenheight);
411
412 //High score
413 glColor4f(.5 + sinefluct / 5, 0, 0, 1);
414
415 if(!config.beatgame)
416 sprintf(string, "High Score: %d", config.highscore);
417
418 if(config.beatgame)
419 sprintf(string,
420 "High Score: %d *COMPLETED* Please vote for Black Shades at iDevGames.com!",
421 config.highscore);
422
423 text.glPrint(0, 0, string, 1, .8, screenwidth, screenheight);
424
425 //Mandatory udg text
426 glColor4f(.3 - sinefluct / 20, .3 - sinefluct / 20, .3 - sinefluct / 20, 1);
427 sprintf(string, "uDevGame 2002 Entry - Visit iDevGames.com for more games!");
428 text.glPrint((int)(screenwidth * 0.961), (int)(screenheight * 1.563), string,
429 1, .6, screenwidth, screenheight);
430
431 DrawMouse();
432
433 DrawFlash();
434 }
435
DrawGame(void)436 void Game::DrawGame(void)
437 {
438 #if 0
439 //FIXME: is this still an isssue?
440 //If flashing to fix menu bug, go back to menu after a frame
441 if(mainmenu == 2)
442 mainmenu = 1;
443 #endif
444
445 glLoadIdentity();
446
447 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
448
449 glEnable(GL_DEPTH_TEST);
450
451 glDisable(GL_CLIP_PLANE0);
452
453 //Visions
454 sinefluct = sin(sinefluctprog);
455 sinefluctprog += multiplier * 3;
456
457 int visions = config.visions;
458 viewdistance = environment.viewdistance;
459
460 if(visions == 0) {
461
462 fog.fogcolor.setColor(environment.fogcolor);
463 fog.SetFog(environment.fogcolor, 0, viewdistance * .8, .2);
464
465 glClearColor(fog.fogcolor.r, fog.fogcolor.g, fog.fogcolor.b, 1);
466
467 if(environment.type == sunny_environment) {
468
469 GLfloat LightAmbient[] =
470 { fog.fogcolor.r / 4, fog.fogcolor.g / 4, fog.fogcolor.b / 4, 1.0f };
471 GLfloat LightDiffuse[] =
472 { fog.fogcolor.r * 1.6, fog.fogcolor.g * 1.6, fog.fogcolor.r * 1.6,
473 1.0f };
474
475 glLightfv(GL_LIGHT0, GL_AMBIENT, LightAmbient);
476 glLightfv(GL_LIGHT0, GL_DIFFUSE, LightDiffuse);
477 } else {
478
479 GLfloat LightAmbient[] =
480 { fog.fogcolor.r * .8, fog.fogcolor.g * .8, fog.fogcolor.b * .8,
481 1.0f };
482 GLfloat LightDiffuse[] =
483 { fog.fogcolor.r * .8, fog.fogcolor.g * .8, fog.fogcolor.r * .8,
484 1.0f };
485
486 glLightfv(GL_LIGHT0, GL_AMBIENT, LightAmbient);
487 glLightfv(GL_LIGHT0, GL_DIFFUSE, LightDiffuse);
488 }
489
490 glEnable(GL_LIGHT0);
491
492 //Change fov if zooming with scope
493 if(!zoom)
494 ReSizeGLScene(90, .1, viewdistance);
495 else
496 ReSizeGLScene(10, .6, viewdistance);
497
498 config.nocolors = 0;
499 }
500
501 if(visions == 1) {
502
503 //light
504 GLfloat LightAmbient[] = { 0, 0, 0, 1.0f };
505 GLfloat LightDiffuse[] = { .1 + sinefluct / 5, 0, 0, 1.0f };
506
507 glLightfv(GL_LIGHT0, GL_AMBIENT, LightAmbient);
508 glLightfv(GL_LIGHT0, GL_DIFFUSE, LightDiffuse);
509
510 glEnable(GL_LIGHT0);
511
512 fog.fogcolor.r = (sinefluct / 4 + .5);
513 fog.fogcolor.g = 0;
514 fog.fogcolor.b = 0;
515
516 fog.SetFog(fog.fogcolor.r, fog.fogcolor.g, fog.fogcolor.b, 0,
517 viewdistance * .8 * .5 * (sinefluct / 4 + .3),
518 sinefluct / 3 + .7);
519
520 glClearColor(fog.fogcolor.r, fog.fogcolor.g, fog.fogcolor.b, 1);
521
522 ReSizeGLScene(120 - sinefluct * 20, .3, viewdistance);
523
524 glRotatef(sinefluct * 10, 0, 0, .1);
525
526 config.nocolors = 1;
527
528 //Pitch higher if moving for effect
529
530 if(person[0].currentanimation == idleanim)
531 alSourcef(gSourceID[visionsound], AL_PITCH, 1);
532
533 if(person[0].currentanimation != idleanim)
534 alSourcef(gSourceID[visionsound], AL_PITCH, 2);
535 }
536 //Camera
537 float bluramount = .1 * config.blurness;
538 blur = 1 - blur;
539
540 //Set rotation/position
541 int thirdperson = config.thirdperson;
542
543 if(thirdperson)
544 glTranslatef(camera.targetoffset.x, camera.targetoffset.y,
545 camera.targetoffset.z);
546
547 if(thirdperson != 2 && (person[0].skeleton.free != 1 || thirdperson)) {
548
549 glRotatef(camera.visrotation2 + -bluramount / 2 +
550 (float)blur * bluramount, 1, 0, 0);
551 glRotatef(camera.visrotation + -bluramount / 2 +
552 (float)blur * bluramount, 0, 1, 0);
553 }
554
555 if(thirdperson == 0 && person[0].skeleton.free == 1) {
556
557 glRotatef(person[0].skeleton.
558 joints[(person[0].skeleton.jointlabels[head])].rotate3, 0, 1, 0);
559 glRotatef(180 -
560 (person[0].skeleton.
561 joints[(person[0].skeleton.jointlabels[head])].rotate2 +
562 90), 0, 0, 1);
563 glRotatef(person[0].skeleton.
564 joints[(person[0].skeleton.jointlabels[head])].rotate1 + 90,
565 0, 1, 0);
566 }
567
568 if(thirdperson == 2) {
569 glRotatef(oldrot2 + -bluramount / 2 + (float)blur * bluramount, 1, 0, 0);
570 glRotatef(oldrot + -bluramount / 2 + (float)blur * bluramount, 0, 1, 0);
571 }
572 //Shake camera if grenade explosion
573 if(camera.camerashake > 0) {
574 if(!(person[0].aiming < 1 || person[0].weapon.type == grenade || thirdperson)) {
575 camera.camerashake = 0;
576 }
577
578 glTranslatef((float)(Random() % 100) / 100 * camera.camerashake,
579 (float)(Random() % 100) / 100 * camera.camerashake,
580 (float)(Random() % 100) / 100 * camera.camerashake);
581 }
582
583 camera.Apply();
584
585 glPushMatrix();
586 glClipPlane(GL_CLIP_PLANE0, eqn);
587 glDisable(GL_CLIP_PLANE0);
588 glPopMatrix();
589
590 frustum.GetFrustum();
591
592 GLfloat LightPosition[] = { -.5, 1, -.8, 0.0f };
593
594 glLightfv(GL_LIGHT0, GL_POSITION, LightPosition);
595
596 glDisable(GL_TEXTURE_2D);
597
598 glEnable(GL_FOG);
599 glEnable(GL_COLOR_MATERIAL);
600 glEnable(GL_CULL_FACE);
601
602 glDepthMask(1);
603
604 //Draw street
605 glPushMatrix();
606
607 glDepthMask(0);
608
609 glDisable(GL_DEPTH_TEST);
610
611 glEnable(GL_LIGHTING);
612
613 glTranslatef(camera.position.x, 0, camera.position.z);
614
615 glScalef(viewdistance * 0.05, 1, viewdistance * 0.05);
616
617 if(visions == 0)
618 street.draw(.22, .22, .22);
619
620 if(visions == 1)
621 street.draw(0, 0, 0);
622
623 glEnable(GL_DEPTH_TEST);
624
625 glDepthMask(1);
626
627 glPopMatrix();
628
629 if(visions == 0)
630 glEnable(GL_LIGHTING);
631
632 if(visions == 1)
633 glDisable(GL_LIGHTING);
634
635 //Draw blocks
636 glEnable(GL_BLEND);
637
638 XYZ move;
639
640 int beginx, endx;
641 int beginz, endz;
642
643 int distsquared;
644
645 //Only nearby blocks
646 beginx =
647 (int)(camera.position.x - viewdistance +
648 block_spacing / 2) / block_spacing - 2;
649
650 if(beginx < 0)
651 beginx = 0;
652
653 beginz =
654 (int)(camera.position.z - viewdistance +
655 block_spacing / 2) / block_spacing - 2;
656
657 if(beginz < 0)
658 beginz = 0;
659
660 endx =
661 (int)(camera.position.x + viewdistance +
662 block_spacing / 2) / block_spacing + 2;
663
664 if(endx > num_blocks - 1)
665 endx = num_blocks - 1;
666
667 endz =
668 (int)(camera.position.z + viewdistance +
669 block_spacing / 2) / block_spacing + 2;
670
671 if(endz > num_blocks - 1)
672 endz = num_blocks - 1;
673
674 bool draw;
675 int whichtri;
676 XYZ collpoint;
677
678 for(int i = beginx; i <= endx; i++) {
679 for(int j = beginz; j <= endz; j++) {
680 drawn[i][j] = 1;
681 }
682 }
683
684 if(beginx < endx && beginz < endz)
685
686 for(int i = beginx; i <= endx; i++) {
687 for(int j = beginz; j <= endz; j++) {
688 draw = 1;
689
690 //Only draw if visible
691 distsquared =
692 (int)(((i) * block_spacing -
693 camera.position.x) * ((i) * block_spacing -
694 camera.position.x) +
695 ((j) * block_spacing -
696 camera.position.z) * ((j) * block_spacing -
697 camera.position.z));
698
699 if(distsquared >
700 (viewdistance * viewdistance + block_spacing * block_spacing))
701 draw = 0;
702
703 if(draw && citytype[i][j] != 3
704 && !frustum.CubeInFrustum((i) * block_spacing, 0,
705 (j) * block_spacing, block_spacing))
706 draw = 0;
707
708 if(draw && citytype[i][j] != 3
709 && !frustum.SphereInFrustum(blocks[citytype[i][j]].
710 boundingspherecenter.x +
711 (i) * block_spacing,
712 blocks[citytype[i][j]].
713 boundingspherecenter.y,
714 blocks[citytype[i][j]].
715 boundingspherecenter.z +
716 (j) * block_spacing,
717 blocks[citytype[i][j]].
718 boundingsphereradius))
719 draw = 0;
720
721 if(draw) {
722 glPushMatrix();
723 glTranslatef(i * block_spacing, 0, j * block_spacing);
724 glRotatef(cityrotation[i][j] * 90, 0, 1, 0);
725 blocks[citytype[i][j]].draw();
726 glPopMatrix();
727 }
728
729 if(!draw) {
730 move.y = 0;
731 move.x = i * block_spacing;
732 move.z = j * block_spacing;
733
734 if(findDistancefast(move, camera.position) < 300000)
735 drawn[i][j] = 0;
736 }
737 }
738 }
739 //Decals
740 decals.draw();
741
742 //Occluding blocks
743 beginx = (int)(camera.position.x + block_spacing / 2) / block_spacing - 2;
744
745 if(beginx < 0)
746 beginx = 0;
747
748 beginz = (int)(camera.position.z + block_spacing / 2) / block_spacing - 2;
749
750 if(beginz < 0)
751 beginz = 0;
752
753 endx = (int)(camera.position.x + block_spacing / 2) / block_spacing + 2;
754
755 if(endx > num_blocks - 1)
756 endx = num_blocks - 1;
757
758 endz = (int)(camera.position.z + block_spacing / 2) / block_spacing + 2;
759
760 if(endz > num_blocks - 1)
761 endz = num_blocks - 1;
762
763 float M[16];
764 float size = 20;
765 XYZ drawpoint;
766
767 //Draw people
768 if(visions == 1)
769 fog.SetFog(fog.fogcolor.r, fog.fogcolor.g, fog.fogcolor.b, 0,
770 viewdistance * .8 * .5 * (-sinefluct / 4 + .3),
771 -sinefluct / 3 + .7);
772
773 glColor4f(1, 1, 1, 1);
774
775 glEnable(GL_COLOR_MATERIAL);
776 glEnable(GL_BLEND);
777
778 for(int i = 0; i < numpeople; i++) {
779 draw = 1;
780
781 if(person[i].skeleton.free < 1) {
782
783 if(person[i].whichblockx >= 0
784 && person[i].whichblockx < num_blocks
785 && person[i].whichblocky >= 0 && person[i].whichblocky < num_blocks) {
786
787 if(!drawn[person[i].whichblockx][person[i].whichblocky])
788 draw = 0;
789
790 } else
791 draw = 0;
792
793 if(draw)
794
795 if(!frustum.
796 CubeInFrustum(person[i].playercoords.x,
797 person[i].playercoords.y, person[i].playercoords.z, 5))
798 draw = 0;
799
800 if(draw)
801
802 if(findDistancefast(person[i].playercoords, camera.position) > 1000000)
803 draw = 0;
804
805 if(draw)
806
807 for(int j = beginx; j <= endx; j++) {
808
809 for(int k = beginz; k <= endz; k++) {
810
811 if(draw) {
812 move.y = 0;
813 move.x = j * block_spacing;
814 move.z = k * block_spacing;
815
816 if(findDistancefast(move, camera.position) < 100000) {
817
818 whichtri =
819 blockocclude.LineCheck2(camera.position,
820 person[i].
821 playercoords, &collpoint, move, 0);
822
823 if(whichtri != -1)
824 draw = 0;
825
826 }
827 }
828 }
829 }
830
831 if(draw) {
832 move.y = 0;
833 move.x = person[i].whichblockx * block_spacing;
834 move.z = person[i].whichblocky * block_spacing;
835
836 whichtri =
837 blockocclude.LineCheck2(camera.position,
838 person[i].playercoords,
839 &collpoint, move, 0);
840
841 if(whichtri != -1)
842 draw = 0;
843 }
844
845 if(i == 0)
846 draw = 1;
847 }
848
849 if(person[i].skeleton.free == 1) {
850
851 if(draw)
852 if(!person[i].skeleton.broken
853 && !frustum.CubeInFrustum(person[i].averageloc.x,
854 person[i].averageloc.y,
855 person[i].averageloc.z, 5))
856 draw = 0;
857
858 if(draw)
859 if(findDistancefast(person[i].averageloc, camera.position) > 1000000)
860 draw = 0;
861
862 if(draw)
863 if(person[i].skeleton.joints[0].position.y < -2)
864 draw = 0;
865
866 for(int j = beginx; j <= endx; j++) {
867
868 for(int k = beginz; k <= endz; k++) {
869
870 if(draw) {
871 move.y = 0;
872 move.x = j * block_spacing;
873 move.z = k * block_spacing;
874
875 if(findDistancefast(move, camera.position) < 100000) {
876
877 whichtri =
878 blockocclude.LineCheck2(camera.position,
879 person[i].averageloc,
880 &collpoint, move, 0);
881
882 if(whichtri != -1)
883 draw = 0;
884
885 }
886 }
887 }
888 }
889
890 if(draw) {
891 move.y = 0;
892 move.x = person[i].whichblockx * block_spacing;
893 move.z = person[i].whichblocky * block_spacing;
894
895 whichtri =
896 blockocclude.LineCheck2(camera.position,
897 person[i].averageloc, &collpoint, move, 0);
898
899 if(whichtri != -1)
900 draw = 0;
901
902 }
903
904 if(i == 0)
905 draw = 1;
906 }
907
908 if(draw && person[i].existing == 1) {
909
910 if((findDistancefast(person[i].playercoords, camera.position) <
911 100000 + zoom * 3000000 && person[i].skeleton.free < 1)
912 || (findDistancefast(person[i].averageloc, camera.position) <
913 100000 + zoom * 3000000 && person[i].skeleton.free >= 1)) {
914
915 glPushMatrix();
916
917 if(person[i].skeleton.free == 0) {
918
919 glTranslatef(person[i].playercoords.x,
920 person[i].playercoords.y, person[i].playercoords.z);
921
922 glRotatef(person[i].playerrotation, 0, 1, 0);
923
924 if(i != 0 || visions == 0)
925 person[i].DoAnimations(i);
926
927 if(i == 0 && visions == 1)
928 person[i].DoAnimationslite(i);
929
930 }
931
932 if(visions == 1) {
933 config.nocolors = 1;
934
935 if(person[i].type == eviltype)
936 config.nocolors = 2;
937
938 if(person[i].type == viptype)
939 config.nocolors = 3;
940 }
941
942 //where the people are drawn...
943 if(!(visions==1 && i == 0) && !(zoom == 1 && i == 0))
944 person[i].DrawSkeleton(i);
945
946 glPopMatrix();
947 } else {
948
949 glPushMatrix();
950
951 if(person[i].skeleton.free < 1)
952 person[i].DoAnimationslite(i);
953
954 glColor4f(1, 1, 1, 1);
955
956 glEnable(GL_BLEND);
957
958 glDisable(GL_CULL_FACE);
959
960 glEnable(GL_TEXTURE_2D);
961
962 glDisable(GL_LIGHTING);
963
964 glDepthMask(0);
965
966 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
967
968 if(person[i].skeleton.free < 1) {
969
970 glBindTexture(GL_TEXTURE_2D, personspritetextureptr);
971
972 glTranslatef(person[i].playercoords.x,
973 person[i].playercoords.y + size / 2 * .3,
974 person[i].playercoords.z);
975 }
976
977 if(person[i].skeleton.free == 1) {
978
979 glBindTexture(GL_TEXTURE_2D, deadpersonspritetextureptr);
980
981 glTranslatef(person[i].averageloc.x,
982 person[i].averageloc.y + size / 2 * .3,
983 person[i].averageloc.z);
984 }
985
986 glGetFloatv(GL_MODELVIEW_MATRIX, M);
987
988 drawpoint.x = M[12];
989 drawpoint.y = M[13];
990 drawpoint.z = M[14];
991
992 glLoadIdentity();
993
994 glTranslatef(drawpoint.x, drawpoint.y, drawpoint.z);
995
996 glBegin(GL_TRIANGLES);
997 glTexCoord2f(1.0f, 1.0f);
998 glVertex3f(.3f * size, .3f * size, 0.0f);
999 glTexCoord2f(0.0f, 1.0f);
1000 glVertex3f(-.3f * size, .3f * size, 0.0f);
1001 glTexCoord2f(1.0f, 0.0f);
1002 glVertex3f(.3f * size, -.3f * size, 0.0f);
1003 glTexCoord2f(0.0f, 0.0f);
1004 glVertex3f(-.3f * size, -.3f * size, 0.0f);
1005 glTexCoord2f(1.0f, 0.0f);
1006 glVertex3f(.3f * size, -.3f * size, 0.0f);
1007 glTexCoord2f(0.0f, 1.0f);
1008 glVertex3f(-.3f * size, .3f * size, 0.0f);
1009 glEnd();
1010
1011 glPopMatrix();
1012
1013 glDepthMask(1);
1014
1015 glDisable(GL_TEXTURE_2D);
1016
1017 glEnable(GL_CULL_FACE);
1018
1019 if(visions != 1)
1020 glEnable(GL_LIGHTING);
1021
1022 }
1023
1024 }
1025
1026 if(person[i].skeleton.free < 1 && !draw)
1027 person[i].DoAnimationslite(i);
1028
1029 if(!person[i].existing)
1030
1031 if(!draw
1032 || findDistancefast(person[i].playercoords, camera.position) > 10000) {
1033 person[i].existing = 1;
1034 }
1035
1036 }
1037
1038 glDisable(GL_COLOR_MATERIAL);
1039 glDisable(GL_BLEND);
1040
1041 //Attacker psychicness
1042 for(int i = 0; i < numpeople; i++) {
1043
1044 if(person[i].killtarget > -1 && person[i].killtargetvisible
1045 && person[i].skeleton.free == 0
1046 && person[person[i].killtarget].skeleton.free == 0) {
1047
1048 sprites.MakeSprite(bulletinstant,
1049 (shotdelayamount / difficulty -
1050 person[i].weapon.shotdelay) / shotdelayamount /
1051 difficulty / 2, 1,
1052 person[i].weapon.shotdelay / shotdelayamount /
1053 difficulty,
1054 person[i].weapon.shotdelay / shotdelayamount /
1055 difficulty,
1056 DoRotation(person[i].skeleton.
1057 joints[person[i].skeleton.
1058 jointlabels[lefthand]].
1059 position, 0,
1060 person[i].playerrotation,
1061 0) + person[i].playercoords,
1062 person[person[i].killtarget].skeleton.
1063 joints[person[person[i].killtarget].skeleton.
1064 jointlabels[abdomen]].position +
1065 person[person[i].killtarget].playercoords,
1066 person[i].weapon.shotdelay * 2);
1067
1068 }
1069
1070 if(person[i].killtarget > -1 && person[i].killtargetvisible
1071 && person[i].skeleton.free == 0
1072 && person[person[i].killtarget].skeleton.free != 0) {
1073
1074 sprites.MakeSprite(bulletinstant,
1075 (shotdelayamount / difficulty -
1076 person[i].weapon.shotdelay) / shotdelayamount /
1077 difficulty / 2, 1,
1078 person[i].weapon.shotdelay / shotdelayamount /
1079 difficulty,
1080 person[i].weapon.shotdelay / shotdelayamount /
1081 difficulty,
1082 DoRotation(person[i].skeleton.
1083 joints[person[i].skeleton.
1084 jointlabels[lefthand]].
1085 position, 0,
1086 person[i].playerrotation,
1087 0) + person[i].playercoords,
1088 person[person[i].killtarget].skeleton.
1089 joints[person[person[i].killtarget].skeleton.
1090 jointlabels[abdomen]].position,
1091 person[i].weapon.shotdelay * 2);
1092
1093 }
1094
1095 }
1096
1097 //Sprites
1098 glEnable(GL_CLIP_PLANE0);
1099 sprites.draw();
1100
1101 glDisable(GL_CLIP_PLANE0);
1102 glDisable(GL_FOG);
1103
1104 //Zoom
1105 glAlphaFunc(GL_GREATER, 0.01);
1106
1107 if(zoom) {
1108 glDisable(GL_DEPTH_TEST); // Disables Depth Testing
1109 glDisable(GL_CULL_FACE);
1110 glDisable(GL_LIGHTING);
1111
1112 glDepthMask(0);
1113
1114 glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
1115
1116 glPushMatrix(); // Store The Projection Matrix
1117
1118 glLoadIdentity(); // Reset The Projection Matrix
1119
1120 glOrtho(0, screenwidth, 0, screenheight, -100, 100); // Set Up An Ortho Screen
1121
1122 glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
1123
1124 glPushMatrix(); // Store The Modelview Matrix
1125
1126 glLoadIdentity(); // Reset The Modelview Matrix
1127
1128 glScalef(screenwidth, screenheight, 1);
1129
1130 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1131
1132 glEnable(GL_BLEND);
1133 glEnable(GL_TEXTURE_2D);
1134
1135 glColor4f(.5, .5, .5, 1);
1136
1137 glBindTexture(GL_TEXTURE_2D, scopetextureptr);
1138
1139 glBegin(GL_QUADS);
1140 glTexCoord2f(0, 0);
1141 glVertex3f(0, 0, 0.0f);
1142
1143 glTexCoord2f(1, 0);
1144 glVertex3f(1, 0, 0.0f);
1145
1146 glTexCoord2f(1, 1);
1147 glVertex3f(1, 1, 0.0f);
1148
1149 glTexCoord2f(0, 1);
1150 glVertex3f(0, 1, 0.0f);
1151 glEnd();
1152
1153 glDisable(GL_TEXTURE_2D);
1154
1155 glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
1156
1157 glPopMatrix(); // Restore The Old Projection Matrix
1158
1159 glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
1160
1161 glPopMatrix(); // Restore The Old Projection Matrix
1162
1163 glEnable(GL_DEPTH_TEST); // Enables Depth Testing
1164 glEnable(GL_CULL_FACE);
1165
1166 glDisable(GL_BLEND);
1167
1168 glDepthMask(1);
1169
1170 }
1171
1172 //Flash
1173 if(flashamount > 0) {
1174
1175 if(flashamount > 1)
1176 flashamount = 1;
1177
1178 flashamount -= multiplier;
1179
1180 if(flashamount < 0)
1181 flashamount = 0;
1182
1183 glDisable(GL_DEPTH_TEST); // Disables Depth Testing
1184 glDisable(GL_CULL_FACE);
1185 glDisable(GL_LIGHTING);
1186
1187 glDepthMask(0);
1188
1189 glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
1190
1191 glPushMatrix(); // Store The Projection Matrix
1192
1193 glLoadIdentity(); // Reset The Projection Matrix
1194
1195 glOrtho(0, screenwidth, 0, screenheight, -100, 100); // Set Up An Ortho Screen
1196
1197 glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
1198
1199 glPushMatrix(); // Store The Modelview Matrix
1200
1201 glLoadIdentity(); // Reset The Modelview Matrix
1202
1203 glScalef(screenwidth, screenheight, 1);
1204
1205 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1206
1207 glEnable(GL_BLEND);
1208
1209 glColor4f(flashr, flashg, flashb, flashamount);
1210
1211 glBegin(GL_QUADS);
1212 glVertex3f(0, 0, 0.0f);
1213 glVertex3f(256, 0, 0.0f);
1214 glVertex3f(256, 256, 0.0f);
1215 glVertex3f(0, 256, 0.0f);
1216 glEnd();
1217
1218 glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
1219
1220 glPopMatrix(); // Restore The Old Projection Matrix
1221
1222 glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
1223
1224 glPopMatrix(); // Restore The Old Projection Matrix
1225
1226 glEnable(GL_DEPTH_TEST); // Enables Depth Testing
1227 glEnable(GL_CULL_FACE);
1228
1229 glDisable(GL_BLEND);
1230
1231 glDepthMask(1);
1232
1233 }
1234
1235 if(person[0].skeleton.free > 0 && thirdperson != 2) {
1236 glDisable(GL_DEPTH_TEST); // Disables Depth Testing
1237 glDisable(GL_CULL_FACE);
1238 glDisable(GL_LIGHTING);
1239
1240 glDepthMask(0);
1241
1242 glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
1243
1244 glPushMatrix(); // Store The Projection Matrix
1245
1246 glLoadIdentity(); // Reset The Projection Matrix
1247
1248 glOrtho(0, screenwidth, 0, screenheight, -100, 100); // Set Up An Ortho Screen
1249
1250 glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
1251
1252 glPushMatrix(); // Store The Modelview Matrix
1253
1254 glLoadIdentity(); // Reset The Modelview Matrix
1255
1256 glScalef(screenwidth, screenheight, 1);
1257
1258 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1259
1260 glEnable(GL_BLEND);
1261
1262 glColor4f(0, 0, 0, 1 - person[0].longdead);
1263
1264 glBegin(GL_QUADS);
1265 glVertex3f(0, 0, 0.0f);
1266 glVertex3f(256, 0, 0.0f);
1267 glVertex3f(256, 256, 0.0f);
1268 glVertex3f(0, 256, 0.0f);
1269 glEnd();
1270
1271 glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
1272
1273 glPopMatrix(); // Restore The Old Projection Matrix
1274
1275 glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
1276
1277 glPopMatrix(); // Restore The Old Projection Matrix
1278
1279 glEnable(GL_DEPTH_TEST); // Enables Depth Testing
1280 glEnable(GL_CULL_FACE);
1281
1282 glDisable(GL_BLEND);
1283
1284 glDepthMask(1);
1285
1286 }
1287
1288 //Text
1289 glEnable(GL_TEXTURE_2D);
1290
1291 char string[256] = "";
1292 Window *ingame = WindowMgr::getInstance().getRoot()->getWindow("ingame");
1293
1294 //In Game text
1295 if(!config.debug) {
1296 // This is the player playing
1297 Person *player = person;
1298 int weapon_type = player->weapon.type;
1299
1300 switch (weapon_type) {
1301 case nogun:
1302 sprintf(string, "UNARMED");
1303 break;
1304 case knife:
1305 sprintf(string, "KNIFE");
1306 player->weapon.ammo = 0;
1307 break;
1308 case assaultrifle:
1309 sprintf(string, "ASSAULT RIFLE");
1310 break;
1311 case shotgun:
1312 sprintf(string, "SHOTGUN");
1313 break;
1314 case sniperrifle:
1315 sprintf(string, "SNIPER RIFLE");
1316 break;
1317 case grenade:
1318 sprintf(string, "HAND GRENADE");
1319 break;
1320 case handgun1:
1321 sprintf(string, "MAGNUM");
1322 break;
1323 case handgun2:
1324 sprintf(string, "HANDGUN");
1325 break;
1326 default:
1327 sprintf(string, "N\\A");
1328 break;
1329 }
1330
1331 Label *label = (Label *)ingame->getWindow("weapon");
1332 label->setText(string);
1333
1334 if(weapon_type != nogun && weapon_type != knife
1335 && weapon_type != grenade) {
1336
1337 sprintf(string, "Magazines");
1338 Label *label = (Label *)ingame->getWindow("magazines");
1339 label->setText(string);
1340
1341 sprintf(string, "0%d", (int)player->reloads[weapon_type]);
1342 label = (Label *)ingame->getWindow("magazines_num");
1343 label->setText(string);
1344
1345 sprintf(string, "Ammo");
1346 label = (Label *)ingame->getWindow("ammo");
1347 label->setText(string);
1348
1349 if(player->weapon.ammo > 9)
1350 sprintf(string, "%d", (int)player->weapon.ammo);
1351 else
1352 sprintf(string, "0%d", (int)player->weapon.ammo);
1353
1354 label = (Label *)ingame->getWindow("ammo_num");
1355 label->setText(string);
1356 } else if(weapon_type == grenade) {
1357 sprintf(string, "Grenades Left: %d",
1358 (int)player->reloads[weapon_type] + 1);
1359 label = (Label *)ingame->getWindow("ammo");
1360 label->setText(string);
1361 } //otherwise nogun/knife
1362
1363 sprintf(string, "%d", score);
1364 label = (Label *)ingame->getWindow("score_val");
1365 label->setText(string);
1366
1367 int time_remain = (int)timeremaining % 60;
1368 // padding the time correctly
1369 if(time_remain > 9) {
1370 sprintf(string, "%d:%d", (int)(timeremaining / 60), time_remain);
1371 } else {
1372 sprintf(string, "%d:0%d", (int)(timeremaining / 60),
1373 (int)timeremaining % 60);
1374 }
1375
1376 label = (Label *)ingame->getWindow("time_val");
1377 label->setText(string);
1378
1379 label = (Label *)ingame->getWindow("mission");
1380 sprintf(string, "Mission %d", mission);
1381 label->setText(string);
1382 }
1383
1384 if(config.debug) {
1385 sprintf(string, "The framespersecond is %d out of maximum %d.",
1386 (int)framespersecond + 1, maxfps);
1387 text.glPrint(10, 30, string, 0, .8, screenwidth, screenheight);
1388
1389 switch (enemystate) {
1390 case 0:
1391 sprintf(string, "Enemies are in random assassination mode.");
1392 break;
1393 case 1:
1394 sprintf(string, "Enemies are in passive mode.");
1395 break;
1396 case 2:
1397 sprintf(string, "Enemies are in DIE!!!! mode.");
1398 break;
1399 }
1400
1401 text.glPrint(10, 20, string, 0, .8, screenwidth, screenheight);
1402
1403 sprintf(string,
1404 "You have pointlessly shot or beaten %d unarmed civilians.",
1405 civkills);
1406 text.glPrint(10, 60, string, 0, .8, screenwidth, screenheight);
1407
1408 sprintf(string, "You have incapacitated %d assassins.", goodkills);
1409 text.glPrint(10, 70, string, 0, .8, screenwidth, screenheight);
1410
1411 sprintf(string, "You have allowed %d successful murders.", badkills);
1412 text.glPrint(10, 80, string, 0, .8, screenwidth, screenheight);
1413 }
1414
1415 WindowMgr::getInstance().getRoot()->getWindow("ingame")->draw();
1416 }
1417
DrawHelp()1418 void DrawHelp()
1419 {
1420 glLoadIdentity();
1421 glClearColor(0, 0, 0, 1.0);
1422 glDisable(GL_CLIP_PLANE0);
1423 glDisable(GL_FOG);
1424 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1425
1426 WindowMgr::getInstance().getRoot()->getWindow("helpscreen")->draw();
1427 }
1428
DrawGLScene(void)1429 int Game::DrawGLScene(void)
1430 {
1431 switch (state) {
1432 case INIT:
1433 //TODO: loading screen moved here?
1434 break;
1435 case MAINMENU:
1436 DrawMainMenu();
1437 break;
1438 case GAME:
1439 DrawGame();
1440 break;
1441 case HELP:
1442 DrawHelp();
1443 break;
1444 default:
1445 return 0;
1446 break;
1447 }
1448
1449 return 1;
1450 }
1451