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