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