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