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