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