]> git.jsancho.org Git - lugaru.git/blob - Source/GameInitDispose.cpp
Switch to using «The Lean Mean C++ Option Parser»
[lugaru.git] / Source / GameInitDispose.cpp
1 /*
2 Copyright (C) 2003, 2010 - Wolfire Games
3 Copyright (C) 2010-2016 - Lugaru contributors (see AUTHORS file)
4
5 This file is part of Lugaru.
6
7 Lugaru is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 Lugaru is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Lugaru.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "Game.h"
22 #include "openal_wrapper.h"
23 #include "Animation.h"
24 #include "Texture.h"
25
26 extern float screenwidth, screenheight;
27 extern float viewdistance;
28 extern XYZ viewer;
29 extern XYZ lightlocation;
30 extern float fadestart;
31 extern float texscale;
32 extern float gravity;
33 extern Light light;
34 extern Skeleton testskeleton;
35 extern int numsounds;
36 extern Terrain terrain;
37 extern int kTextureSize;
38 extern float texdetail;
39 extern float realtexdetail;
40 extern float volume;
41 extern Objects objects;
42 extern int detail;
43 extern bool cellophane;
44 extern GLubyte bloodText[512 * 512 * 3];
45 extern GLubyte wolfbloodText[512 * 512 * 3];
46 extern bool ismotionblur;
47 extern bool trilinear;
48 extern bool musictoggle;
49 extern int environment;
50 extern bool ambientsound;
51 extern float multiplier;
52 extern int netdatanew;
53 extern float mapinfo;
54 extern bool stillloading;
55 extern short vRefNum;
56 extern long dirID;
57 extern int mainmenu;
58 extern bool visibleloading;
59 extern float flashamount, flashr, flashg, flashb;
60 extern int flashdelay;
61 extern int whichjointstartarray[26];
62 extern int whichjointendarray[26];
63 extern int difficulty;
64 extern float slomospeed;
65 extern bool gamestarted;
66
67 extern int numdialogues;
68 extern int numdialogueboxes[20];
69 extern int dialoguetype[20];
70 extern int dialogueboxlocation[20][20];
71 extern float dialogueboxcolor[20][20][3];
72 extern int dialogueboxsound[20][20];
73 extern char dialoguetext[20][20][128];
74 extern char dialoguename[20][20][64];
75 extern XYZ dialoguecamera[20][20];
76 extern float dialoguecamerayaw[20][20];
77 extern float dialoguecamerapitch[20][20];
78 extern int indialogue;
79 extern int whichdialogue;
80 extern float dialoguetime;
81
82 extern float accountcampaignhighscore[10];
83 extern float accountcampaignfasttime[10];
84 extern float accountcampaignscore[10];
85 extern float accountcampaigntime[10];
86
87 extern int accountcampaignchoicesmade[10];
88 extern int accountcampaignchoices[10][5000];
89
90 void LOG(const std::string &fmt, ...)
91 {
92     // !!! FIXME: write me.
93 }
94
95 void Dispose()
96 {
97     LOGFUNC;
98
99     if (Game::endgame == 2) {
100         Game::accountactive->endGame();
101         Game::endgame = 0;
102     }
103
104     Account::saveFile(":Data:Users", Game::accountactive);
105
106     //textures.clear();
107
108     LOG("Shutting down sound system...");
109
110     OPENAL_StopSound(OPENAL_ALL);
111
112 // this is causing problems on Linux, but we'll force an _exit() a little
113 //  later in the shutdown process.  --ryan.
114 #if !PLATFORM_LINUX
115
116     for (int i = 0; i < sounds_count; ++i) {
117         OPENAL_Sample_Free(samp[i]);
118     }
119
120     OPENAL_Close();
121 #endif
122 }
123
124 void Game::newGame()
125 {
126     text = new Text();
127     skybox = new SkyBox();
128 }
129
130 void Game::deleteGame()
131 {
132     if (skybox)
133         delete skybox;
134     if (text)
135         delete text;
136     terraintexture.destroy();
137     terraintexture2.destroy();
138     cursortexture.destroy();
139     Maparrowtexture.destroy();
140     Mapboxtexture.destroy();
141     Mapcircletexture.destroy();
142     hawktexture.destroy();
143     loadscreentexture.destroy();
144
145     for (int i = 0; i < 10; i++)
146         Mainmenuitems[i].destroy();
147
148     glDeleteTextures(1, &screentexture);
149     glDeleteTextures(1, &screentexture2);
150
151     Dispose();
152 }
153
154
155
156 void LoadSave(const char *fileName, GLuint *textureid, bool mipmap, GLubyte *array, int *skinsize)
157 {
158     LOGFUNC;
159
160     LOG(std::string("Loading (S)...") + fileName);
161
162     //Load Image
163     float temptexdetail = texdetail;
164     texdetail = 1;
165
166     //Load Image
167     ImageRec texture;
168     load_image(ConvertFileName(fileName), texture);
169     texdetail = temptexdetail;
170
171     int bytesPerPixel = texture.bpp / 8;
172
173     int tempnum = 0;
174     for (int i = 0; i < (int)(texture.sizeY * texture.sizeX * bytesPerPixel); i++) {
175         if ((i + 1) % 4 || bytesPerPixel == 3) {
176             array[tempnum] = texture.data[i];
177             tempnum++;
178         }
179     }
180 }
181
182
183
184 //***************> ResizeGLScene() <******/
185 GLvoid Game::ReSizeGLScene(float fov, float pnear)
186 {
187     if (screenheight == 0) {
188         screenheight = 1;
189     }
190
191     glViewport(0, 0, screenwidth, screenheight);
192
193     glMatrixMode(GL_PROJECTION);
194     glLoadIdentity();
195
196     gluPerspective(fov, (GLfloat)screenwidth / (GLfloat)screenheight, pnear, viewdistance);
197
198     glMatrixMode(GL_MODELVIEW);
199     glLoadIdentity();
200 }
201
202 void Game::LoadingScreen()
203 {
204     static float loadprogress;
205     static AbsoluteTime frametime = {0, 0};
206     AbsoluteTime currTime = UpTime ();
207     double deltaTime = (float) AbsoluteDeltaToDuration (currTime, frametime);
208
209     if (0 > deltaTime) // if negative microseconds
210         deltaTime /= -1000000.0;
211     else // else milliseconds
212         deltaTime /= 1000.0;
213
214     multiplier = deltaTime;
215     if (multiplier < .001)
216         multiplier = .001;
217     if (multiplier > 10)
218         multiplier = 10;
219     if (multiplier > .05) {
220         frametime = currTime; // reset for next time interval
221
222         glLoadIdentity();
223         //Clear to black
224         glClearColor(0, 0, 0, 1);
225         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
226
227
228         loadtime += multiplier * 4;
229
230         loadprogress = loadtime;
231         if (loadprogress > 100)
232             loadprogress = 100;
233
234         //Background
235
236         glEnable(GL_TEXTURE_2D);
237         loadscreentexture.bind();
238         glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
239         glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
240         glDisable(GL_DEPTH_TEST);
241         glDisable(GL_CULL_FACE);
242         glDisable(GL_LIGHTING);
243         glDepthMask(0);
244         glMatrixMode(GL_PROJECTION);
245         glPushMatrix();
246         glLoadIdentity();
247         glOrtho(0, screenwidth, 0, screenheight, -100, 100);
248         glMatrixMode(GL_MODELVIEW);
249         glPushMatrix();
250         glLoadIdentity();
251         glTranslatef(screenwidth / 2, screenheight / 2, 0);
252         glScalef((float)screenwidth / 2, (float)screenheight / 2, 1);
253         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
254         glDisable(GL_BLEND);
255         glColor4f(loadprogress / 100, loadprogress / 100, loadprogress / 100, 1);
256         glPushMatrix();
257         glBegin(GL_QUADS);
258         glTexCoord2f(.1 - loadprogress / 100, 0 + loadprogress / 100 + .3);
259         glVertex3f(-1, -1, 0.0f);
260         glTexCoord2f(.1 - loadprogress / 100, 0 + loadprogress / 100 + .3);
261         glVertex3f(1, -1, 0.0f);
262         glTexCoord2f(.1 - loadprogress / 100, 1 + loadprogress / 100 + .3);
263         glVertex3f(1, 1, 0.0f);
264         glTexCoord2f(.1 - loadprogress / 100, 1 + loadprogress / 100 + .3);
265         glVertex3f(-1, 1, 0.0f);
266         glEnd();
267         glPopMatrix();
268         glEnable(GL_BLEND);
269         glPushMatrix();
270         glBegin(GL_QUADS);
271         glTexCoord2f(.4 + loadprogress / 100, 0 + loadprogress / 100);
272         glVertex3f(-1, -1, 0.0f);
273         glTexCoord2f(.4 + loadprogress / 100, 0 + loadprogress / 100);
274         glVertex3f(1, -1, 0.0f);
275         glTexCoord2f(.4 + loadprogress / 100, 1 + loadprogress / 100);
276         glVertex3f(1, 1, 0.0f);
277         glTexCoord2f(.4 + loadprogress / 100, 1 + loadprogress / 100);
278         glVertex3f(-1, 1, 0.0f);
279         glEnd();
280         glPopMatrix();
281         glDisable(GL_TEXTURE_2D);
282         glMatrixMode(GL_PROJECTION);
283         glPopMatrix();
284         glMatrixMode(GL_MODELVIEW);
285         glPopMatrix();
286         glDisable(GL_BLEND);
287         glDepthMask(1);
288
289         glEnable(GL_TEXTURE_2D);
290         loadscreentexture.bind();
291         glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
292         glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
293         glDisable(GL_DEPTH_TEST);
294         glDisable(GL_CULL_FACE);
295         glDisable(GL_LIGHTING);
296         glDepthMask(0);
297         glMatrixMode(GL_PROJECTION);
298         glPushMatrix();
299         glLoadIdentity();
300         glOrtho(0, screenwidth, 0, screenheight, -100, 100);
301         glMatrixMode(GL_MODELVIEW);
302         glPushMatrix();
303         glLoadIdentity();
304         glTranslatef(screenwidth / 2, screenheight / 2, 0);
305         glScalef((float)screenwidth / 2 * (1.5 - (loadprogress) / 200), (float)screenheight / 2 * (1.5 - (loadprogress) / 200), 1);
306         glBlendFunc(GL_SRC_ALPHA, GL_ONE);
307         glEnable(GL_BLEND);
308         glColor4f(loadprogress / 100, loadprogress / 100, loadprogress / 100, 1);
309         glPushMatrix();
310         glBegin(GL_QUADS);
311         glTexCoord2f(0 + .5, 0 + .5);
312         glVertex3f(-1, -1, 0.0f);
313         glTexCoord2f(1 + .5, 0 + .5);
314         glVertex3f(1, -1, 0.0f);
315         glTexCoord2f(1 + .5, 1 + .5);
316         glVertex3f(1, 1, 0.0f);
317         glTexCoord2f(0 + .5, 1 + .5);
318         glVertex3f(-1, 1, 0.0f);
319         glEnd();
320         glPopMatrix();
321         glDisable(GL_TEXTURE_2D);
322         glMatrixMode(GL_PROJECTION);
323         glPopMatrix();
324         glMatrixMode(GL_MODELVIEW);
325         glPopMatrix();
326         glDisable(GL_BLEND);
327         glDepthMask(1);
328
329         glEnable(GL_TEXTURE_2D);
330         loadscreentexture.bind();
331         glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
332         glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
333         glDisable(GL_DEPTH_TEST);
334         glDisable(GL_CULL_FACE);
335         glDisable(GL_LIGHTING);
336         glDepthMask(0);
337         glMatrixMode(GL_PROJECTION);
338         glPushMatrix();
339         glLoadIdentity();
340         glOrtho(0, screenwidth, 0, screenheight, -100, 100);
341         glMatrixMode(GL_MODELVIEW);
342         glPushMatrix();
343         glLoadIdentity();
344         glTranslatef(screenwidth / 2, screenheight / 2, 0);
345         glScalef((float)screenwidth / 2 * (100 + loadprogress) / 100, (float)screenheight / 2 * (100 + loadprogress) / 100, 1);
346         glBlendFunc(GL_SRC_ALPHA, GL_ONE);
347         glEnable(GL_BLEND);
348         glColor4f(loadprogress / 100, loadprogress / 100, loadprogress / 100, .4);
349         glPushMatrix();
350         glBegin(GL_QUADS);
351         glTexCoord2f(0 + .2, 0 + .8);
352         glVertex3f(-1, -1, 0.0f);
353         glTexCoord2f(1 + .2, 0 + .8);
354         glVertex3f(1, -1, 0.0f);
355         glTexCoord2f(1 + .2, 1 + .8);
356         glVertex3f(1, 1, 0.0f);
357         glTexCoord2f(0 + .2, 1 + .8);
358         glVertex3f(-1, 1, 0.0f);
359         glEnd();
360         glPopMatrix();
361         glDisable(GL_TEXTURE_2D);
362         glMatrixMode(GL_PROJECTION);
363         glPopMatrix();
364         glMatrixMode(GL_MODELVIEW);
365         glPopMatrix();
366         glDisable(GL_BLEND);
367         glDepthMask(1);
368
369         //Text
370
371         if (flashamount > 0) {
372             if (flashamount > 1)
373                 flashamount = 1;
374             if (flashdelay <= 0)
375                 flashamount -= multiplier;
376             flashdelay--;
377             if (flashamount < 0)
378                 flashamount = 0;
379             glDisable(GL_DEPTH_TEST);
380             glDisable(GL_CULL_FACE);
381             glDisable(GL_LIGHTING);
382             glDisable(GL_TEXTURE_2D);
383             glDepthMask(0);
384             glMatrixMode(GL_PROJECTION);
385             glPushMatrix();
386             glLoadIdentity();
387             glOrtho(0, screenwidth, 0, screenheight, -100, 100);
388             glMatrixMode(GL_MODELVIEW);
389             glPushMatrix();
390             glLoadIdentity();
391             glScalef(screenwidth, screenheight, 1);
392             glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
393             glEnable(GL_BLEND);
394             glColor4f(flashr, flashg, flashb, flashamount);
395             glBegin(GL_QUADS);
396             glVertex3f(0, 0, 0.0f);
397             glVertex3f(256, 0, 0.0f);
398             glVertex3f(256, 256, 0.0f);
399             glVertex3f(0, 256, 0.0f);
400             glEnd();
401             glMatrixMode(GL_PROJECTION);
402             glPopMatrix();
403             glMatrixMode(GL_MODELVIEW);
404             glPopMatrix();
405             glEnable(GL_DEPTH_TEST);
406             glEnable(GL_CULL_FACE);
407             glDisable(GL_BLEND);
408             glDepthMask(1);
409         }
410
411         swap_gl_buffers();
412     }
413 }
414
415 void FadeLoadingScreen(float howmuch)
416 {
417     static float loadprogress;
418
419     glLoadIdentity();
420     //Clear to black
421     glClearColor(0, 0, 0, 1);
422     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
423
424     loadprogress = howmuch;
425
426     //Background
427
428     glDisable(GL_TEXTURE_2D);
429     glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
430     glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
431     glDisable(GL_DEPTH_TEST);
432     glDisable(GL_CULL_FACE);
433     glDisable(GL_LIGHTING);
434     glDepthMask(0);
435     glMatrixMode(GL_PROJECTION);
436     glPushMatrix();
437     glLoadIdentity();
438     glOrtho(0, screenwidth, 0, screenheight, -100, 100);
439     glMatrixMode(GL_MODELVIEW);
440     glPushMatrix();
441     glLoadIdentity();
442     glTranslatef(screenwidth / 2, screenheight / 2, 0);
443     glScalef((float)screenwidth / 2, (float)screenheight / 2, 1);
444     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
445     glDisable(GL_BLEND);
446     glColor4f(loadprogress / 100, 0, 0, 1);
447     glPushMatrix();
448     glBegin(GL_QUADS);
449     glTexCoord2f(0, 0);
450     glVertex3f(-1, -1, 0.0f);
451     glTexCoord2f(1, 0);
452     glVertex3f(1, -1, 0.0f);
453     glTexCoord2f(1, 1);
454     glVertex3f(1, 1, 0.0f);
455     glTexCoord2f(0, 1);
456     glVertex3f(-1, 1, 0.0f);
457     glEnd();
458     glPopMatrix();
459     glDisable(GL_TEXTURE_2D);
460     glMatrixMode(GL_PROJECTION);
461     glPopMatrix();
462     glMatrixMode(GL_MODELVIEW);
463     glPopMatrix();
464     glDisable(GL_BLEND);
465     glDepthMask(1);
466     //Text
467     swap_gl_buffers();
468 }
469
470 void Game::InitGame()
471 {
472     LOGFUNC;
473
474     numchallengelevels = 14;
475
476     accountactive = Account::loadFile(":Data:Users");
477
478     whichjointstartarray[0] = righthip;
479     whichjointendarray[0] = rightfoot;
480
481     whichjointstartarray[1] = righthip;
482     whichjointendarray[1] = rightankle;
483
484     whichjointstartarray[2] = righthip;
485     whichjointendarray[2] = rightknee;
486
487     whichjointstartarray[3] = rightknee;
488     whichjointendarray[3] = rightankle;
489
490     whichjointstartarray[4] = rightankle;
491     whichjointendarray[4] = rightfoot;
492
493     whichjointstartarray[5] = lefthip;
494     whichjointendarray[5] = leftfoot;
495
496     whichjointstartarray[6] = lefthip;
497     whichjointendarray[6] = leftankle;
498
499     whichjointstartarray[7] = lefthip;
500     whichjointendarray[7] = leftknee;
501
502     whichjointstartarray[8] = leftknee;
503     whichjointendarray[8] = leftankle;
504
505     whichjointstartarray[9] = leftankle;
506     whichjointendarray[9] = leftfoot;
507
508     whichjointstartarray[10] = abdomen;
509     whichjointendarray[10] = rightshoulder;
510
511     whichjointstartarray[11] = abdomen;
512     whichjointendarray[11] = rightelbow;
513
514     whichjointstartarray[12] = abdomen;
515     whichjointendarray[12] = rightwrist;
516
517     whichjointstartarray[13] = abdomen;
518     whichjointendarray[13] = righthand;
519
520     whichjointstartarray[14] = rightshoulder;
521     whichjointendarray[14] = rightelbow;
522
523     whichjointstartarray[15] = rightelbow;
524     whichjointendarray[15] = rightwrist;
525
526     whichjointstartarray[16] = rightwrist;
527     whichjointendarray[16] = righthand;
528
529     whichjointstartarray[17] = abdomen;
530     whichjointendarray[17] = leftshoulder;
531
532     whichjointstartarray[18] = abdomen;
533     whichjointendarray[18] = leftelbow;
534
535     whichjointstartarray[19] = abdomen;
536     whichjointendarray[19] = leftwrist;
537
538     whichjointstartarray[20] = abdomen;
539     whichjointendarray[20] = lefthand;
540
541     whichjointstartarray[21] = leftshoulder;
542     whichjointendarray[21] = leftelbow;
543
544     whichjointstartarray[22] = leftelbow;
545     whichjointendarray[22] = leftwrist;
546
547     whichjointstartarray[23] = leftwrist;
548     whichjointendarray[23] = lefthand;
549
550     whichjointstartarray[24] = abdomen;
551     whichjointendarray[24] = neck;
552
553     whichjointstartarray[25] = neck;
554     whichjointendarray[25] = head;
555
556     FadeLoadingScreen(0);
557
558     stillloading = 1;
559
560     int temptexdetail = texdetail;
561     texdetail = 1;
562     text->LoadFontTexture(":Data:Textures:Font.png");
563     text->BuildFont();
564     texdetail = temptexdetail;
565
566     FadeLoadingScreen(10);
567
568     if (detail == 2) {
569         texdetail = 1;
570     }
571     if (detail == 1) {
572         texdetail = 2;
573     }
574     if (detail == 0) {
575         texdetail = 4;
576     }
577
578     LOG("Initializing sound system...");
579
580 #if PLATFORM_LINUX
581     unsigned char rc = 0;
582     int output = OPENAL_OUTPUT_ALSA;  // Try alsa first...
583     if (commandLineOptions[SOUND]) {
584         output = commandLineOptions[SOUND].last()->type(); //  ...but let user override that.
585     }
586
587     OPENAL_SetOutput(output);
588     if ((rc = OPENAL_Init(44100, 32, 0)) == false) {
589         // if we tried ALSA and failed, fall back to OSS.
590         if ( (output == OPENAL_OUTPUT_ALSA) && (commandLineOptions[SOUND].last()->type() != OPENAL_OUTPUT_ALSA) ) {
591             OPENAL_Close();
592             output = OPENAL_OUTPUT_OSS;
593             OPENAL_SetOutput(output);
594             rc = OPENAL_Init(44100, 32, 0);
595         }
596     }
597
598     if (rc == false) {
599         OPENAL_Close();
600         output = OPENAL_OUTPUT_NOSOUND;  // we tried! just do silence.
601         OPENAL_SetOutput(output);
602         rc = OPENAL_Init(44100, 32, 0);
603     }
604 #else
605     OPENAL_Init(44100, 32, 0);
606 #endif
607
608     OPENAL_SetSFXMasterVolume((int)(volume * 255));
609     loadAllSounds();
610
611     if (musictoggle)
612         emit_stream_np(stream_menutheme);
613
614     cursortexture.load(":Data:Textures:Cursor.png", 0, 1);
615
616     Mapcircletexture.load(":Data:Textures:MapCircle.png", 0, 1);
617     Mapboxtexture.load(":Data:Textures:MapBox.png", 0, 1);
618     Maparrowtexture.load(":Data:Textures:MapArrow.png", 0, 1);
619
620     temptexdetail = texdetail;
621     if (texdetail > 2)
622         texdetail = 2;
623     Mainmenuitems[0].load(":Data:Textures:Lugaru.png", 0, 0);
624     Mainmenuitems[1].load(":Data:Textures:Newgame.png", 0, 0);
625     Mainmenuitems[2].load(":Data:Textures:Options.png", 0, 0);
626     Mainmenuitems[3].load(":Data:Textures:Quit.png", 0, 0);
627     Mainmenuitems[4].load(":Data:Textures:Eyelid.png", 0, 1);
628     Mainmenuitems[5].load(":Data:Textures:Resume.png", 0, 0);
629     Mainmenuitems[6].load(":Data:Textures:Endgame.png", 0, 0);
630
631     //LoadTexture(":Data:Textures:Eye.jpg",&Mainmenuitems[5],0,1);
632     //~ LoadTexture(":Data:Textures:World.png",&Mainmenuitems[7],0,0); // LoadCampaign will take care of that
633     texdetail = temptexdetail;
634
635     FadeLoadingScreen(95);
636
637
638     gameon = 0;
639     mainmenu = 1;
640
641     stillloading = 0;
642     firstload = 0;
643
644     newdetail = detail;
645     newscreenwidth = screenwidth;
646     newscreenheight = screenheight;
647
648     LoadMenu();
649 }
650
651
652 void Game::LoadScreenTexture()
653 {
654     glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
655
656     if (!Game::screentexture)
657         glGenTextures( 1, &Game::screentexture );
658     glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
659
660
661     glEnable(GL_TEXTURE_2D);
662     glBindTexture( GL_TEXTURE_2D, Game::screentexture);
663     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
664     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
665
666     glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 0, 0, kTextureSize, kTextureSize, 0);
667 }
668
669 //TODO: move LoadStuff() closer to GameTick.cpp to get rid of various vars shared in Game.h
670 void Game::LoadStuff()
671 {
672     static float temptexdetail;
673     static float viewdistdetail;
674     static int i, j;
675     float megascale = 1;
676
677     LOGFUNC;
678
679     loadtime = 0;
680
681     stillloading = 1;
682
683     for (auto p:Person::players) {
684         p->skeleton.drawmodel.textureptr.destroy();
685     }
686
687     i = abs(Random() % 4);
688     visibleloading = 0; //don't use loadscreentexture yet
689     loadscreentexture.load(":Data:Textures:fire.jpg", 1, 0);
690     visibleloading = 1;
691
692     temptexdetail = texdetail;
693     texdetail = 1;
694     text->LoadFontTexture(":Data:Textures:Font.png");
695     text->BuildFont();
696     texdetail = temptexdetail;
697
698     numsounds = 71;
699
700     viewdistdetail = 2;
701     viewdistance = 50 * megascale * viewdistdetail;
702
703     if (detail == 2) {
704         texdetail = 1;
705     }
706     if (detail == 1) {
707         texdetail = 2;
708     }
709     if (detail == 0) {
710         texdetail = 4;
711     }
712
713     realtexdetail = texdetail;
714
715     LOG("Loading weapon data...");
716
717     Weapon::knifetextureptr.load(":Data:Textures:knife.png", 0, 1);
718     Weapon::bloodknifetextureptr.load(":Data:Textures:bloodknife.png", 0, 1);
719     Weapon::lightbloodknifetextureptr.load(":Data:Textures:lightbloodknife.png", 0, 1);
720     Weapon::swordtextureptr.load(":Data:Textures:sword.jpg", 1, 0);
721     Weapon::bloodswordtextureptr.load(":Data:Textures:Swordblood.jpg", 1, 0);
722     Weapon::lightbloodswordtextureptr.load(":Data:Textures:Swordbloodlight.jpg", 1, 0);
723     Weapon::stafftextureptr.load(":Data:Textures:Staff.jpg", 1, 0);
724
725     Weapon::throwingknifemodel.load((char *)":Data:Models:throwingknife.solid", 1);
726     Weapon::throwingknifemodel.Scale(.001, .001, .001);
727     Weapon::throwingknifemodel.Rotate(90, 0, 0);
728     Weapon::throwingknifemodel.Rotate(0, 90, 0);
729     Weapon::throwingknifemodel.flat = 0;
730     Weapon::throwingknifemodel.CalculateNormals(1);
731
732     Weapon::swordmodel.load((char *)":Data:Models:sword.solid", 1);
733     Weapon::swordmodel.Scale(.001, .001, .001);
734     Weapon::swordmodel.Rotate(90, 0, 0);
735     Weapon::swordmodel.Rotate(0, 90, 0);
736     Weapon::swordmodel.Rotate(0, 0, 90);
737     Weapon::swordmodel.flat = 1;
738     Weapon::swordmodel.CalculateNormals(1);
739
740     Weapon::staffmodel.load((char *)":Data:Models:staff.solid", 1);
741     Weapon::staffmodel.Scale(.005, .005, .005);
742     Weapon::staffmodel.Rotate(90, 0, 0);
743     Weapon::staffmodel.Rotate(0, 90, 0);
744     Weapon::staffmodel.Rotate(0, 0, 90);
745     Weapon::staffmodel.flat = 1;
746     Weapon::staffmodel.CalculateNormals(1);
747
748     terrain.shadowtexture.load(":Data:Textures:shadow.png", 0, 1);
749     terrain.bloodtexture.load(":Data:Textures:blood.png", 0, 1);
750     terrain.breaktexture.load(":Data:Textures:break.png", 0, 1);
751     terrain.bloodtexture2.load(":Data:Textures:blood.png", 0, 1);
752
753
754     terrain.footprinttexture.load(":Data:Textures:footprint.png", 0, 1);
755     terrain.bodyprinttexture.load(":Data:Textures:bodyprint.png", 0, 1);
756     hawktexture.load(":Data:Textures:hawk.png", 0, 1);
757
758
759     Sprite::cloudtexture.load(":Data:Textures:cloud.png", 1, 1);
760     Sprite::cloudimpacttexture.load(":Data:Textures:cloudimpact.png", 1, 1);
761     Sprite::bloodtexture.load(":Data:Textures:bloodparticle.png", 1, 1);
762     Sprite::snowflaketexture.load(":Data:Textures:snowflake.png", 1, 1);
763     Sprite::flametexture.load(":Data:Textures:flame.png", 1, 1);
764     Sprite::bloodflametexture.load(":Data:Textures:bloodflame.png", 1, 1);
765     Sprite::smoketexture.load(":Data:Textures:smoke.png", 1, 1);
766     Sprite::shinetexture.load(":Data:Textures:shine.png", 1, 0);
767     Sprite::splintertexture.load(":Data:Textures:splinter.png", 1, 1);
768     Sprite::leaftexture.load(":Data:Textures:leaf.png", 1, 1);
769     Sprite::toothtexture.load(":Data:Textures:tooth.png", 1, 1);
770
771     yaw = 0;
772     pitch = 0;
773     ReSizeGLScene(90, .01);
774
775     viewer = 0;
776
777
778     if (detail)
779         kTextureSize = 1024;
780     if (detail == 1)
781         kTextureSize = 512;
782     if (detail == 0)
783         kTextureSize = 256;
784
785     //Set up distant light
786     light.color[0] = .95;
787     light.color[1] = .95;
788     light.color[2] = 1;
789     light.ambient[0] = .2;
790     light.ambient[1] = .2;
791     light.ambient[2] = .24;
792     light.location.x = 1;
793     light.location.y = 1;
794     light.location.z = -.2;
795     Normalise(&light.location);
796
797     LoadingScreen();
798
799     SetUpLighting();
800
801
802     fadestart = .6;
803     gravity = -10;
804
805     texscale = .2 / megascale / viewdistdetail;
806     terrain.scale = 3 * megascale * viewdistdetail;
807
808     viewer.x = terrain.size / 2 * terrain.scale;
809     viewer.z = terrain.size / 2 * terrain.scale;
810
811     hawk.load((char *)":Data:Models:hawk.solid", 1);
812     hawk.Scale(.03, .03, .03);
813     hawk.Rotate(90, 1, 1);
814     hawk.CalculateNormals(0);
815     hawk.ScaleNormals(-1, -1, -1);
816     hawkcoords.x = terrain.size / 2 * terrain.scale - 5 - 7;
817     hawkcoords.z = terrain.size / 2 * terrain.scale - 5 - 7;
818     hawkcoords.y = terrain.getHeight(hawkcoords.x, hawkcoords.z) + 25;
819
820     eye.load((char *)":Data:Models:eye.solid", 1);
821     eye.Scale(.03, .03, .03);
822     eye.CalculateNormals(0);
823
824     cornea.load((char *)":Data:Models:cornea.solid", 1);
825     cornea.Scale(.03, .03, .03);
826     cornea.CalculateNormals(0);
827
828     iris.load((char *)":Data:Models:iris.solid", 1);
829     iris.Scale(.03, .03, .03);
830     iris.CalculateNormals(0);
831
832     LoadSave(":Data:Textures:Bloodfur.png", 0, 1, &bloodText[0], 0);
833     LoadSave(":Data:Textures:Wolfbloodfur.png", 0, 1, &wolfbloodText[0], 0);
834
835     oldenvironment = -4;
836
837     gameon = 1;
838     mainmenu = 0;
839
840     firstload = 0;
841
842     loadAllAnimations();
843     //Fix knife stab, too lazy to do it manually
844     XYZ moveamount;
845     moveamount = 0;
846     moveamount.z = 2;
847     for (i = 0; i < Person::players[0]->skeleton.num_joints; i++) {
848         for (j = 0; j < animation[knifesneakattackanim].numframes; j++) {
849             animation[knifesneakattackanim].position[i][j] += moveamount;
850         }
851     }
852
853     LoadingScreen();
854
855     for (i = 0; i < Person::players[0]->skeleton.num_joints; i++) {
856         for (j = 0; j < animation[knifesneakattackedanim].numframes; j++) {
857             animation[knifesneakattackedanim].position[i][j] += moveamount;
858         }
859     }
860
861     LoadingScreen();
862
863     for (i = 0; i < Person::players[0]->skeleton.num_joints; i++) {
864         animation[dead1anim].position[i][1] = animation[dead1anim].position[i][0];
865         animation[dead2anim].position[i][1] = animation[dead2anim].position[i][0];
866         animation[dead3anim].position[i][1] = animation[dead3anim].position[i][0];
867         animation[dead4anim].position[i][1] = animation[dead4anim].position[i][0];
868     }
869     animation[dead1anim].speed[0] = 0.001;
870     animation[dead2anim].speed[0] = 0.001;
871     animation[dead3anim].speed[0] = 0.001;
872     animation[dead4anim].speed[0] = 0.001;
873
874     animation[dead1anim].speed[1] = 0.001;
875     animation[dead2anim].speed[1] = 0.001;
876     animation[dead3anim].speed[1] = 0.001;
877     animation[dead4anim].speed[1] = 0.001;
878
879     for (i = 0; i < Person::players[0]->skeleton.num_joints; i++) {
880         for (j = 0; j < animation[swordsneakattackanim].numframes; j++) {
881             animation[swordsneakattackanim].position[i][j] += moveamount;
882         }
883     }
884     LoadingScreen();
885     for (j = 0; j < animation[swordsneakattackanim].numframes; j++) {
886         animation[swordsneakattackanim].weapontarget[j] += moveamount;
887     }
888
889     LoadingScreen();
890
891     for (i = 0; i < Person::players[0]->skeleton.num_joints; i++) {
892         for (j = 0; j < animation[swordsneakattackedanim].numframes; j++) {
893             animation[swordsneakattackedanim].position[i][j] += moveamount;
894         }
895     }
896
897     LoadingScreen();
898     temptexdetail = texdetail;
899     texdetail = 1;
900     texdetail = temptexdetail;
901
902     LoadingScreen();
903
904     if (!screentexture) {
905         LoadScreenTexture();
906     }
907
908     if (targetlevel != 7) {
909         emit_sound_at(fireendsound);
910     }
911
912     stillloading = 0;
913     loading = 0;
914     changedelay = 1;
915
916     visibleloading = 0;
917 }
918