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