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