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