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