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