]> git.jsancho.org Git - lugaru.git/blob - Source/GameInitDispose.cpp
merge
[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 int oldmainmenu;
64 extern bool visibleloading;
65 extern float flashamount,flashr,flashg,flashb;
66 extern int flashdelay;
67 extern int whichjointstartarray[26];
68 extern int whichjointendarray[26];
69 extern int difficulty;
70 extern float tintr,tintg,tintb;
71 extern float slomospeed;
72 extern bool gamestarted;
73
74 extern int numdialogues;
75 extern int numdialogueboxes[20];
76 extern int dialoguetype[20];
77 extern int dialogueboxlocation[20][20];
78 extern float dialogueboxcolor[20][20][3];
79 extern int dialogueboxsound[20][20];
80 extern char dialoguetext[20][20][128];
81 extern char dialoguename[20][20][64];
82 extern XYZ dialoguecamera[20][20];
83 extern float dialoguecamerarotation[20][20];
84 extern float dialoguecamerarotation2[20][20];
85 extern int indialogue;
86 extern int whichdialogue;
87 extern float dialoguetime;
88
89 extern float accountcampaignhighscore[10];
90 extern float accountcampaignfasttime[10];
91 extern float accountcampaignscore[10];
92 extern float accountcampaigntime[10];
93
94 extern int accountcampaignchoicesmade[10];
95 extern int accountcampaignchoices[10][5000];
96
97 void LOG(const std::string &fmt, ...)
98 {
99     // !!! FIXME: write me.
100 }
101
102 void Game::Dispose()
103 {
104         LOGFUNC;
105
106         if(endgame==2){
107                 accountactive->endGame();
108                 endgame=0;
109         }
110
111         Account::saveFile(":Data:Users", accountactive);
112
113         //textures.clear();
114
115         LOG("Shutting down sound system...");
116
117         OPENAL_StopSound(OPENAL_ALL);
118
119 // this is causing problems on Linux, but we'll force an _exit() a little
120 //  later in the shutdown process.  --ryan.
121 #if !PLATFORM_LINUX
122
123         for (int i=0; i < sounds_count; ++i)
124         {
125                 OPENAL_Sample_Free(samp[i]);
126         }
127
128         OPENAL_Close();
129         if (texture.data)
130         {
131                 free(texture.data);
132         }
133         texture.data = 0;
134 #endif
135 }
136
137
138 void Game::LoadTexture(const string fileName, GLuint *textureid,int mipmap, bool hasalpha) {
139         *textureid = Texture::Load(fileName,mipmap,hasalpha);
140 }
141
142 void Game::LoadTextureSave(const string fileName, GLuint *textureid,int mipmap,GLubyte *array, int *skinsize) {
143         *textureid = Texture::Load(fileName,mipmap,false,array,skinsize);
144 }
145
146 void Game::LoadSave(const char *fileName, GLuint *textureid,bool mipmap,GLubyte *array, int *skinsize)
147 {
148         int i;
149         int bytesPerPixel;
150
151         LOGFUNC;
152
153         LOG(std::string("Loading (S)...") + fileName);
154
155         //Load Image
156         float temptexdetail=texdetail;
157         texdetail=1;
158         //upload_image( fileName );
159         //LoadTGA( fileName );
160
161         // Converting file to something os specific
162         char * fixedFN = ConvertFileName(fileName);
163
164         //Load Image
165         unsigned char fileNamep[256];
166         CopyCStringToPascal(fixedFN, fileNamep);
167         //Load Image
168         upload_image( fileNamep ,0);
169         texdetail=temptexdetail;
170
171         //Is it valid?
172         if(1==1){
173                 bytesPerPixel=texture.bpp/8;
174
175                 int tempnum=0;
176                 for(i=0;i<(int)(texture.sizeY*texture.sizeX*bytesPerPixel);i++){
177                         if((i+1)%4||bytesPerPixel==3){
178                                 array[tempnum]=texture.data[i];
179                                 tempnum++;
180                         }
181                 }
182         }
183 }
184
185 bool Game::AddClothes(const char *fileName, GLubyte *array)
186 {
187         int i;
188         int bytesPerPixel;
189
190         LOGFUNC;
191
192         //upload_image( fileName );
193         //LoadTGA( fileName );
194         //Load Image
195         unsigned char fileNamep[256];
196         CopyCStringToPascal(fileName,fileNamep);
197         //Load Image
198         bool opened;
199         opened=upload_image( fileNamep ,1);
200
201         float alphanum;
202         //Is it valid?
203         if(opened){
204                 if(tintr>1)tintr=1;
205                 if(tintg>1)tintg=1;
206                 if(tintb>1)tintb=1;
207
208                 if(tintr<0)tintr=0;
209                 if(tintg<0)tintg=0;
210                 if(tintb<0)tintb=0;
211
212                 bytesPerPixel=texture.bpp/8;
213
214                 int tempnum=0;
215                 alphanum=255;
216                 for(i=0;i<(int)(texture.sizeY*texture.sizeX*bytesPerPixel);i++){
217                         if(bytesPerPixel==3)alphanum=255;
218                         else if((i+1)%4==0)alphanum=texture.data[i];
219                         //alphanum/=2;
220                         if((i+1)%4||bytesPerPixel==3){
221                                 if((i%4)==0)texture.data[i]*=tintr;
222                                 if((i%4)==1)texture.data[i]*=tintg;
223                                 if((i%4)==2)texture.data[i]*=tintb;
224                                 array[tempnum]=(float)array[tempnum]*(1-alphanum/255)+(float)texture.data[i]*(alphanum/255);
225                                 tempnum++;
226                         }
227                 }
228         }
229         else return 0;
230         return 1;
231 }
232
233
234 //***************> ResizeGLScene() <******/
235 GLvoid Game::ReSizeGLScene(float fov, float pnear)
236 {
237         if (screenheight==0)
238         {
239                 screenheight=1;
240         }
241
242         glViewport(0,0,screenwidth,screenheight);
243
244         glMatrixMode(GL_PROJECTION);
245         glLoadIdentity();
246
247         gluPerspective(fov,(GLfloat)screenwidth/(GLfloat)screenheight,pnear,viewdistance);
248
249         glMatrixMode(GL_MODELVIEW);
250         glLoadIdentity();
251 }
252
253 void Game::LoadingScreen()
254 {
255         static float loadprogress;
256         static AbsoluteTime time = {0,0};
257         static AbsoluteTime frametime = {0,0};
258         AbsoluteTime currTime = UpTime ();
259         double deltaTime = (float) AbsoluteDeltaToDuration (currTime, frametime);
260
261         if (0 > deltaTime)      // if negative microseconds
262                 deltaTime /= -1000000.0;
263         else                            // else milliseconds
264                 deltaTime /= 1000.0;
265
266         multiplier=deltaTime;
267         if(multiplier<.001)multiplier=.001;
268         if(multiplier>10)multiplier=10;
269         if(multiplier>.05){
270                 frametime = currTime;   // reset for next time interval
271
272                 glLoadIdentity();
273                 //Clear to black
274                 glClearColor(0,0,0,1);
275                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
276
277
278                 loadtime+=multiplier*4;
279
280                 loadprogress=loadtime;
281                 if(loadprogress>100)loadprogress=100;
282
283                 //loadprogress=abs(Random()%100);
284
285                 //Background
286
287                 glEnable(GL_TEXTURE_2D);
288                 glBindTexture( GL_TEXTURE_2D, loadscreentexture);
289                 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
290                 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
291                 glDisable(GL_DEPTH_TEST);                                                       // Disables Depth Testing
292                 glDisable(GL_CULL_FACE);
293                 glDisable(GL_LIGHTING);
294                 glDepthMask(0);
295                 glMatrixMode(GL_PROJECTION);                                            // Select The Projection Matrix
296                 glPushMatrix();                                                                         // Store The Projection Matrix
297                 glLoadIdentity();                                                                       // Reset The Projection Matrix
298                 glOrtho(0,screenwidth,0,screenheight,-100,100);                                         // Set Up An Ortho Screen
299                 glMatrixMode(GL_MODELVIEW);                                                     // Select The Modelview Matrix
300                 glPushMatrix();                                                                         // Store The Modelview Matrix
301                 glLoadIdentity();                                                               // Reset The Modelview Matrix
302                 glTranslatef(screenwidth/2,screenheight/2,0);
303                 glScalef((float)screenwidth/2,(float)screenheight/2,1);
304                 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
305                 glDisable(GL_BLEND);
306                 glColor4f(loadprogress/100,loadprogress/100,loadprogress/100,1);
307                 glPushMatrix();
308                 //glScalef(.25,.25,.25);
309                 glBegin(GL_QUADS);
310                 glTexCoord2f(.1-loadprogress/100,0+loadprogress/100+.3);
311                 glVertex3f(-1,          -1,      0.0f);
312                 glTexCoord2f(.1-loadprogress/100,0+loadprogress/100+.3);
313                 glVertex3f(1,   -1,      0.0f);
314                 glTexCoord2f(.1-loadprogress/100,1+loadprogress/100+.3);
315                 glVertex3f(1,   1, 0.0f);
316                 glTexCoord2f(.1-loadprogress/100,1+loadprogress/100+.3);
317                 glVertex3f(-1,  1, 0.0f);
318                 glEnd();
319                 glPopMatrix();
320                 glEnable(GL_BLEND);
321                 glPushMatrix();
322                 //glScalef(.25,.25,.25);
323                 glBegin(GL_QUADS);
324                 glTexCoord2f(.4+loadprogress/100,0+loadprogress/100);
325                 glVertex3f(-1,          -1,      0.0f);
326                 glTexCoord2f(.4+loadprogress/100,0+loadprogress/100);
327                 glVertex3f(1,   -1,      0.0f);
328                 glTexCoord2f(.4+loadprogress/100,1+loadprogress/100);
329                 glVertex3f(1,   1, 0.0f);
330                 glTexCoord2f(.4+loadprogress/100,1+loadprogress/100);
331                 glVertex3f(-1,  1, 0.0f);
332                 glEnd();
333                 glPopMatrix();
334                 glDisable(GL_TEXTURE_2D);
335                 glMatrixMode(GL_PROJECTION);                                            // Select The Projection Matrix
336                 glPopMatrix();                                                                          // Restore The Old Projection Matrix
337                 glMatrixMode(GL_MODELVIEW);                                                     // Select The Modelview Matrix
338                 glPopMatrix();                                                                          // Restore The Old Projection Matrix
339                 glDisable(GL_BLEND);
340                 glDepthMask(1);
341
342                 glEnable(GL_TEXTURE_2D);
343                 glBindTexture( GL_TEXTURE_2D, loadscreentexture);
344                 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
345                 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
346                 glDisable(GL_DEPTH_TEST);                                                       // Disables Depth Testing
347                 glDisable(GL_CULL_FACE);
348                 glDisable(GL_LIGHTING);
349                 glDepthMask(0);
350                 glMatrixMode(GL_PROJECTION);                                            // Select The Projection Matrix
351                 glPushMatrix();                                                                         // Store The Projection Matrix
352                 glLoadIdentity();                                                                       // Reset The Projection Matrix
353                 glOrtho(0,screenwidth,0,screenheight,-100,100);                                         // Set Up An Ortho Screen
354                 glMatrixMode(GL_MODELVIEW);                                                     // Select The Modelview Matrix
355                 glPushMatrix();                                                                         // Store The Modelview Matrix
356                 glLoadIdentity();                                                               // Reset The Modelview Matrix
357                 glTranslatef(screenwidth/2,screenheight/2,0);
358                 glScalef((float)screenwidth/2*(1.5-(loadprogress)/200),(float)screenheight/2*(1.5-(loadprogress)/200),1);
359                 glBlendFunc(GL_SRC_ALPHA,GL_ONE);
360                 glEnable(GL_BLEND);
361                 //glColor4f(loadprogress/100,loadprogress/100,loadprogress/100,1);
362                 glColor4f(loadprogress/100,loadprogress/100,loadprogress/100,1);
363                 glPushMatrix();
364                 //glScalef(.25,.25,.25);
365                 glBegin(GL_QUADS);
366                 glTexCoord2f(0+.5,0+.5);
367                 glVertex3f(-1,          -1,      0.0f);
368                 glTexCoord2f(1+.5,0+.5);
369                 glVertex3f(1,   -1,      0.0f);
370                 glTexCoord2f(1+.5,1+.5);
371                 glVertex3f(1,   1, 0.0f);
372                 glTexCoord2f(0+.5,1+.5);
373                 glVertex3f(-1,  1, 0.0f);
374                 glEnd();
375                 glPopMatrix();
376                 glDisable(GL_TEXTURE_2D);
377                 glMatrixMode(GL_PROJECTION);                                            // Select The Projection Matrix
378                 glPopMatrix();                                                                          // Restore The Old Projection Matrix
379                 glMatrixMode(GL_MODELVIEW);                                                     // Select The Modelview Matrix
380                 glPopMatrix();                                                                          // Restore The Old Projection Matrix
381                 glDisable(GL_BLEND);
382                 glDepthMask(1);
383
384                 glEnable(GL_TEXTURE_2D);
385                 glBindTexture( GL_TEXTURE_2D, loadscreentexture);
386                 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
387                 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
388                 glDisable(GL_DEPTH_TEST);                                                       // Disables Depth Testing
389                 glDisable(GL_CULL_FACE);
390                 glDisable(GL_LIGHTING);
391                 glDepthMask(0);
392                 glMatrixMode(GL_PROJECTION);                                            // Select The Projection Matrix
393                 glPushMatrix();                                                                         // Store The Projection Matrix
394                 glLoadIdentity();                                                                       // Reset The Projection Matrix
395                 glOrtho(0,screenwidth,0,screenheight,-100,100);                                         // Set Up An Ortho Screen
396                 glMatrixMode(GL_MODELVIEW);                                                     // Select The Modelview Matrix
397                 glPushMatrix();                                                                         // Store The Modelview Matrix
398                 glLoadIdentity();                                                               // Reset The Modelview Matrix
399                 glTranslatef(screenwidth/2,screenheight/2,0);
400                 glScalef((float)screenwidth/2*(100+loadprogress)/100,(float)screenheight/2*(100+loadprogress)/100,1);
401                 glBlendFunc(GL_SRC_ALPHA,GL_ONE);
402                 glEnable(GL_BLEND);
403                 glColor4f(loadprogress/100,loadprogress/100,loadprogress/100,.4);
404                 glPushMatrix();
405                 //glScalef(.25,.25,.25);
406                 glBegin(GL_QUADS);
407                 glTexCoord2f(0+.2,0+.8);
408                 glVertex3f(-1,          -1,      0.0f);
409                 glTexCoord2f(1+.2,0+.8);
410                 glVertex3f(1,   -1,      0.0f);
411                 glTexCoord2f(1+.2,1+.8);
412                 glVertex3f(1,   1, 0.0f);
413                 glTexCoord2f(0+.2,1+.8);
414                 glVertex3f(-1,  1, 0.0f);
415                 glEnd();
416                 glPopMatrix();
417                 glDisable(GL_TEXTURE_2D);
418                 glMatrixMode(GL_PROJECTION);                                            // Select The Projection Matrix
419                 glPopMatrix();                                                                          // Restore The Old Projection Matrix
420                 glMatrixMode(GL_MODELVIEW);                                                     // Select The Modelview Matrix
421                 glPopMatrix();                                                                          // Restore The Old Projection Matrix
422                 glDisable(GL_BLEND);
423                 glDepthMask(1);
424
425                 //Text
426
427                 if(flashamount>0){
428                         if(flashamount>1)flashamount=1;
429                         if(flashdelay<=0)flashamount-=multiplier;
430                         flashdelay--;
431                         if(flashamount<0)flashamount=0;
432                         glDisable(GL_DEPTH_TEST);                                                       // Disables Depth Testing
433                         glDisable(GL_CULL_FACE);
434                         glDisable(GL_LIGHTING);
435                         glDisable(GL_TEXTURE_2D);
436                         glDepthMask(0);
437                         glMatrixMode(GL_PROJECTION);                                            // Select The Projection Matrix
438                         glPushMatrix();                                                                         // Store The Projection Matrix
439                         glLoadIdentity();                                                                       // Reset The Projection Matrix
440                         glOrtho(0,screenwidth,0,screenheight,-100,100);                                         // Set Up An Ortho Screen
441                         glMatrixMode(GL_MODELVIEW);                                                     // Select The Modelview Matrix
442                         glPushMatrix();                                                                         // Store The Modelview Matrix
443                         glLoadIdentity();                                                               // Reset The Modelview Matrix
444                         glScalef(screenwidth,screenheight,1);
445                         glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
446                         glEnable(GL_BLEND);
447                         glColor4f(flashr,flashg,flashb,flashamount);
448                         glBegin(GL_QUADS);
449                         glVertex3f(0,           0,       0.0f);
450                         glVertex3f(256, 0,       0.0f);
451                         glVertex3f(256, 256, 0.0f);
452                         glVertex3f(0,   256, 0.0f);
453                         glEnd();
454                         glMatrixMode(GL_PROJECTION);                                            // Select The Projection Matrix
455                         glPopMatrix();                                                                          // Restore The Old Projection Matrix
456                         glMatrixMode(GL_MODELVIEW);                                                     // Select The Modelview Matrix
457                         glPopMatrix();                                                                          // Restore The Old Projection Matrix
458                         glEnable(GL_DEPTH_TEST);                                                        // Enables Depth Testing
459                         glEnable(GL_CULL_FACE);
460                         glDisable(GL_BLEND);
461                         glDepthMask(1);
462                 }
463
464                 swap_gl_buffers();
465         }
466 }
467
468 void Game::FadeLoadingScreen(float howmuch)
469 {
470         static float loadprogress;
471
472         glLoadIdentity();
473         //Clear to black
474         glClearColor(0,0,0,1);
475         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
476
477         loadprogress=howmuch;
478
479         //loadprogress=abs(Random()%100);
480
481         //Background
482
483         //glEnable(GL_TEXTURE_2D);
484         glDisable(GL_TEXTURE_2D);
485         //glBindTexture( GL_TEXTURE_2D, loadscreentexture);
486         glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
487         glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
488         glDisable(GL_DEPTH_TEST);                                                       // Disables Depth Testing
489         glDisable(GL_CULL_FACE);
490         glDisable(GL_LIGHTING);
491         glDepthMask(0);
492         glMatrixMode(GL_PROJECTION);                                            // Select The Projection Matrix
493         glPushMatrix();                                                                         // Store The Projection Matrix
494         glLoadIdentity();                                                                       // Reset The Projection Matrix
495         glOrtho(0,screenwidth,0,screenheight,-100,100);                                         // Set Up An Ortho Screen
496         glMatrixMode(GL_MODELVIEW);                                                     // Select The Modelview Matrix
497         glPushMatrix();                                                                         // Store The Modelview Matrix
498         glLoadIdentity();                                                               // Reset The Modelview Matrix
499         glTranslatef(screenwidth/2,screenheight/2,0);
500         glScalef((float)screenwidth/2,(float)screenheight/2,1);
501         glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
502         glDisable(GL_BLEND);
503         glColor4f(loadprogress/100,0,0,1);
504         glPushMatrix();
505         //glScalef(.25,.25,.25);
506         glBegin(GL_QUADS);
507         glTexCoord2f(0,0);
508         glVertex3f(-1,          -1,      0.0f);
509         glTexCoord2f(1,0);
510         glVertex3f(1,   -1,      0.0f);
511         glTexCoord2f(1,1);
512         glVertex3f(1,   1, 0.0f);
513         glTexCoord2f(0,1);
514         glVertex3f(-1,  1, 0.0f);
515         glEnd();
516         glPopMatrix();
517         glDisable(GL_TEXTURE_2D);
518         glMatrixMode(GL_PROJECTION);                                            // Select The Projection Matrix
519         glPopMatrix();                                                                          // Restore The Old Projection Matrix
520         glMatrixMode(GL_MODELVIEW);                                                     // Select The Modelview Matrix
521         glPopMatrix();                                                                          // Restore The Old Projection Matrix
522         glDisable(GL_BLEND);
523         glDepthMask(1);
524         //Text
525         swap_gl_buffers();
526 }
527
528
529 void Game::InitGame()
530 {
531 #if PLATFORM_MACOSX
532         ProcessSerialNumber PSN;
533         ProcessInfoRec pinfo;
534         FSSpec pspec;
535         OSStatus err;
536         /* set up process serial number */
537         PSN.highLongOfPSN = 0;
538         PSN.lowLongOfPSN = kCurrentProcess;
539         /* set up info block */
540         pinfo.processInfoLength = sizeof(pinfo);
541         pinfo.processName = NULL;
542         pinfo.processAppSpec = &pspec;
543         /* grab the vrefnum and directory */
544         err = GetProcessInformation(&PSN, &pinfo);
545         if (err == noErr) {
546                 vRefNum = pspec.vRefNum;
547                 dirID = pspec.parID;
548         }
549 #endif
550
551         LOGFUNC;
552
553         autocam=0;
554
555         numchallengelevels=14;
556
557         accountactive=Account::loadFile(":Data:Users");
558
559         tintr=1;
560         tintg=1;
561         tintb=1;
562
563         whichjointstartarray[0]=righthip;
564         whichjointendarray[0]=rightfoot;
565
566         whichjointstartarray[1]=righthip;
567         whichjointendarray[1]=rightankle;
568
569         whichjointstartarray[2]=righthip;
570         whichjointendarray[2]=rightknee;
571
572         whichjointstartarray[3]=rightknee;
573         whichjointendarray[3]=rightankle;
574
575         whichjointstartarray[4]=rightankle;
576         whichjointendarray[4]=rightfoot;
577
578         whichjointstartarray[5]=lefthip;
579         whichjointendarray[5]=leftfoot;
580
581         whichjointstartarray[6]=lefthip;
582         whichjointendarray[6]=leftankle;
583
584         whichjointstartarray[7]=lefthip;
585         whichjointendarray[7]=leftknee;
586
587         whichjointstartarray[8]=leftknee;
588         whichjointendarray[8]=leftankle;
589
590         whichjointstartarray[9]=leftankle;
591         whichjointendarray[9]=leftfoot;
592
593         whichjointstartarray[10]=abdomen;
594         whichjointendarray[10]=rightshoulder;
595
596         whichjointstartarray[11]=abdomen;
597         whichjointendarray[11]=rightelbow;
598
599         whichjointstartarray[12]=abdomen;
600         whichjointendarray[12]=rightwrist;
601
602         whichjointstartarray[13]=abdomen;
603         whichjointendarray[13]=righthand;
604
605         whichjointstartarray[14]=rightshoulder;
606         whichjointendarray[14]=rightelbow;
607
608         whichjointstartarray[15]=rightelbow;
609         whichjointendarray[15]=rightwrist;
610
611         whichjointstartarray[16]=rightwrist;
612         whichjointendarray[16]=righthand;
613
614         whichjointstartarray[17]=abdomen;
615         whichjointendarray[17]=leftshoulder;
616
617         whichjointstartarray[18]=abdomen;
618         whichjointendarray[18]=leftelbow;
619
620         whichjointstartarray[19]=abdomen;
621         whichjointendarray[19]=leftwrist;
622
623         whichjointstartarray[20]=abdomen;
624         whichjointendarray[20]=lefthand;
625
626         whichjointstartarray[21]=leftshoulder;
627         whichjointendarray[21]=leftelbow;
628
629         whichjointstartarray[22]=leftelbow;
630         whichjointendarray[22]=leftwrist;
631
632         whichjointstartarray[23]=leftwrist;
633         whichjointendarray[23]=lefthand;
634
635         whichjointstartarray[24]=abdomen;
636         whichjointendarray[24]=neck;
637
638         whichjointstartarray[25]=neck;
639         whichjointendarray[25]=head;
640
641         FadeLoadingScreen(0);
642
643         stillloading=1;
644
645         texture.data = ( GLubyte* )malloc( 1024*1024*4 );
646
647         int temptexdetail=texdetail;
648         texdetail=1;
649         text.LoadFontTexture(":Data:Textures:Font.png");
650         text.BuildFont();
651         texdetail=temptexdetail;
652
653         FadeLoadingScreen(10);
654
655         if(detail==2){
656                 texdetail=1;
657         }
658         if(detail==1){
659                 texdetail=2;
660         }
661         if(detail==0){
662                 texdetail=4;
663         }
664
665         LOG("Initializing sound system...");
666
667     #if PLATFORM_LINUX
668     int output = -1;
669     
670     extern bool cmdline(const char *cmd);
671     unsigned char rc = 0;
672     output = OPENAL_OUTPUT_ALSA;  // Try alsa first...
673     if (cmdline("forceoss"))      //  ...but let user override that.
674         output = OPENAL_OUTPUT_OSS;
675     else if (cmdline("nosound"))
676         output = OPENAL_OUTPUT_NOSOUND;
677
678     OPENAL_SetOutput(output);
679         if ((rc = OPENAL_Init(44100, 32, 0)) == false)
680     {
681         // if we tried ALSA and failed, fall back to OSS.
682         if ( (output == OPENAL_OUTPUT_ALSA) && (!cmdline("forcealsa")) )
683         {
684             OPENAL_Close();
685             output = OPENAL_OUTPUT_OSS;
686             OPENAL_SetOutput(output);
687                 rc = OPENAL_Init(44100, 32, 0);
688         }
689     }
690
691     if (rc == false)
692     {
693         OPENAL_Close();
694         output = OPENAL_OUTPUT_NOSOUND;  // we tried! just do silence.
695         OPENAL_SetOutput(output);
696             rc = OPENAL_Init(44100, 32, 0);
697     }
698     #else
699         OPENAL_Init(44100, 32, 0);
700     #endif
701
702         OPENAL_SetSFXMasterVolume((int)(volume*255));
703         loadAllSounds();
704
705         if(musictoggle)
706           emit_stream_np(stream_menutheme);
707
708         LoadTexture(":Data:Textures:Cursor.png",&cursortexture,0,1);
709
710         LoadTexture(":Data:Textures:MapCircle.png",&Mapcircletexture,0,1);
711         LoadTexture(":Data:Textures:MapBox.png",&Mapboxtexture,0,1);
712         LoadTexture(":Data:Textures:MapArrow.png",&Maparrowtexture,0,1);
713
714         temptexdetail=texdetail;
715         if(texdetail>2)texdetail=2;
716         LoadTexture(":Data:Textures:Lugaru.png",&Mainmenuitems[0],0,0);
717         LoadTexture(":Data:Textures:Newgame.png",&Mainmenuitems[1],0,0);
718         LoadTexture(":Data:Textures:Options.png",&Mainmenuitems[2],0,0);
719         LoadTexture(":Data:Textures:Quit.png",&Mainmenuitems[3],0,0);
720         LoadTexture(":Data:Textures:Eyelid.png",&Mainmenuitems[4],0,1);
721         LoadTexture(":Data:Textures:Resume.png",&Mainmenuitems[5],0,0);
722         LoadTexture(":Data:Textures:Endgame.png",&Mainmenuitems[6],0,0);
723         
724         //LoadTexture(":Data:Textures:Eye.jpg",&Mainmenuitems[5],0,1);
725         //~ LoadTexture(":Data:Textures:World.png",&Mainmenuitems[7],0,0); // LoadCampaign will take care of that
726         texdetail=temptexdetail;
727
728         FadeLoadingScreen(95);
729
730
731         gameon=0;
732         mainmenu=1;
733
734         stillloading=0;
735         firstload=0;
736         oldmainmenu=0;
737
738         newdetail=detail;
739         newscreenwidth=screenwidth;
740         newscreenheight=screenheight;
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