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