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