]> git.jsancho.org Git - lugaru.git/blob - Source/Game.cpp
1c250ca1f48b9da39f9a7d269ae0fc4004d6e1a1
[lugaru.git] / Source / Game.cpp
1 #include "Game.h"
2 #include "openal_wrapper.h"
3 #include "SDL_thread.h"
4
5 extern int mainmenu;
6
7 int numdialogues;
8 int numdialogueboxes[max_dialogues];
9 int dialoguetype[max_dialogues];
10 int dialogueboxlocation[max_dialogues][max_dialoguelength];
11 float dialogueboxcolor[max_dialogues][max_dialoguelength][3];
12 int dialogueboxsound[max_dialogues][max_dialoguelength];
13 char dialoguetext[max_dialogues][max_dialoguelength][128];
14 char dialoguename[max_dialogues][max_dialoguelength][64];
15 XYZ dialoguecamera[max_dialogues][max_dialoguelength];
16 XYZ participantlocation[max_dialogues][10];
17 int participantfocus[max_dialogues][max_dialoguelength];
18 int participantaction[max_dialogues][max_dialoguelength];
19 float participantrotation[max_dialogues][10];
20 XYZ participantfacing[max_dialogues][max_dialoguelength][10];
21 float dialoguecamerarotation[max_dialogues][max_dialoguelength];
22 float dialoguecamerarotation2[max_dialogues][max_dialoguelength];
23 int indialogue;
24 int whichdialogue;
25 int directing;
26 float dialoguetime;
27 int dialoguegonethrough[20];
28
29 namespace Game{
30     GLuint terraintexture;
31     GLuint terraintexture2;
32     GLuint terraintexture3;
33     GLuint screentexture;
34     GLuint screentexture2;
35     GLuint logotexture;
36     GLuint loadscreentexture;
37     GLuint Maparrowtexture;
38     GLuint Mapboxtexture;
39     GLuint Mapcircletexture;
40     GLuint cursortexture;
41     GLuint Mainmenuitems[10];
42
43     int selected;
44     int keyselect;
45     int indemo;
46
47     bool won;
48
49     bool entername;
50
51     char menustring[100][256];
52     char registrationname[256];
53     float registrationnumber;
54
55     int newdetail;
56     int newscreenwidth;
57     int newscreenheight;
58
59     bool gameon;
60     float deltah,deltav;
61     int mousecoordh,mousecoordv;
62     int oldmousecoordh,oldmousecoordv;
63     float rotation,rotation2;
64     SkyBox skybox;
65     bool cameramode;
66     int olddrawmode;
67     int drawmode;
68     bool firstload;
69     bool oldbutton;
70
71     float leveltime;
72     float loadtime;
73
74     Model hawk;
75     XYZ hawkcoords;
76     XYZ realhawkcoords;
77     GLuint hawktexture;
78     float hawkrotation;
79     float hawkcalldelay;
80
81     Model eye;
82     Model iris;
83     Model cornea;
84
85     bool stealthloading;
86
87     std::vector<CampaignLevel> campaignlevels;
88     int whichchoice;
89     int actuallevel;
90     bool winhotspot;
91     bool windialogue;
92
93     bool minimap;
94
95     int musictype,oldmusictype,oldoldmusictype;
96     bool realthreat;
97
98     Model rabbit;
99     XYZ rabbitcoords;
100
101     XYZ mapcenter;
102     float mapradius;
103
104     Text* text;
105     float fps;
106
107     XYZ cameraloc;
108     float cameradist;
109
110     int drawtoggle;
111
112     bool editorenabled;
113     int editortype;
114     float editorsize;
115     float editorrotation;
116     float editorrotation2;
117
118     float brightness;
119
120     int quit;
121     int tryquit;
122
123     XYZ pathpoint[30];
124     int numpathpoints;
125     int numpathpointconnect[30];
126     int pathpointconnect[30][30];
127     int pathpointselected;
128
129     int endgame;
130     bool scoreadded;
131     int numchallengelevels;
132
133     bool console;
134     int archiveselected;
135     char consoletext[15][256];
136     int consolechars[15];
137     bool chatting;
138     char displaytext[15][256];
139     int displaychars[15];
140     float displaytime[15];
141     float displayblinkdelay;
142     bool displayblink;
143     int displayselected;
144     bool consolekeydown;
145     float consoleblinkdelay;
146     bool consoleblink;
147     int consoleselected;
148     bool autocam;
149
150     unsigned short crouchkey,jumpkey,forwardkey,chatkey,backkey,leftkey,rightkey,drawkey,throwkey,attackkey;
151     unsigned short consolekey;
152     bool oldattackkey;
153
154     int loading;
155     float talkdelay;
156     
157     int numboundaries;
158     XYZ boundary[360];
159
160     int whichlevel;
161     int oldenvironment;
162     int targetlevel;
163     float changedelay;
164
165     float musicvolume[4];
166     float oldmusicvolume[4];
167     int musicselected;
168     int change;
169
170     bool waiting;
171     Account* accountactive;
172 }
173
174 void Game::newGame()
175 {
176     text=NULL;
177     text=new Text();
178
179         terraintexture = 0;
180         terraintexture2 = 0;
181         terraintexture3 = 0;
182         screentexture = 0;
183         screentexture2 = 0;
184         logotexture = 0;
185         loadscreentexture = 0;
186         Maparrowtexture = 0;
187         Mapboxtexture = 0;
188         Mapcircletexture = 0;
189         cursortexture = 0;
190
191         memset(Mainmenuitems, 0, sizeof(Mainmenuitems));
192
193         selected = 0;
194         keyselect = 0;
195         indemo = 0;
196
197         won = 0;
198
199         entername = 0;
200
201         memset(menustring, 0, sizeof(menustring));
202         memset(registrationname, 0, sizeof(registrationname));
203         registrationnumber = 0;
204
205         newdetail = 0;
206         newscreenwidth = 0;
207         newscreenheight = 0;
208
209         gameon = 0;
210         deltah = 0,deltav = 0;
211         mousecoordh = 0,mousecoordv = 0;
212         oldmousecoordh = 0,oldmousecoordv = 0;
213         rotation = 0,rotation2 = 0;
214
215 //      SkyBox skybox;
216
217         cameramode = 0;
218         olddrawmode = 0;
219         drawmode = 0;
220         firstload = 0;
221         oldbutton = 0;
222
223         leveltime = 0;
224         loadtime = 0;
225
226 //      Model hawk;
227
228 //      XYZ hawkcoords;
229 //      XYZ realhawkcoords;
230
231         hawktexture = 0;
232         hawkrotation = 0;
233         hawkcalldelay = 0;
234 /*
235         Model eye;
236         Model iris;
237         Model cornea;
238 */
239         stealthloading = 0;
240
241         whichchoice = 0;
242         actuallevel = 0;
243         winhotspot = false;
244         windialogue = false;
245
246         minimap = 0;
247
248         musictype = 0,oldmusictype = 0,oldoldmusictype = 0;
249         realthreat = 0;
250
251 //      Model rabbit;
252 //      XYZ rabbitcoords;
253
254 //      XYZ mapcenter;
255         mapradius = 0;
256
257 //      Text text;
258         fps = 0;
259
260 //      XYZ cameraloc;
261         cameradist = 0;
262
263         drawtoggle = 0;
264
265         editorenabled = 0;
266         editortype = 0;
267         editorsize = 0;
268         editorrotation = 0;
269         editorrotation2 = 0;
270
271         brightness = 0;
272
273         quit = 0;
274         tryquit = 0;
275
276 //      XYZ pathpoint[30];
277         numpathpoints = 0;
278         memset(numpathpointconnect, 0, sizeof(numpathpointconnect));
279         memset(pathpointconnect, 0, sizeof(pathpointconnect));
280         pathpointselected = 0;
281
282         endgame = 0;
283         scoreadded = 0;
284         numchallengelevels = 0;
285
286         console = false;
287         archiveselected = 0;
288
289         memset(consoletext, 0, sizeof(consoletext));
290         memset(consolechars, 0, sizeof(consolechars));
291         chatting = 0;
292         memset(displaytext, 0, sizeof(displaytext));
293         memset(displaychars, 0, sizeof(displaychars));
294         memset(displaytime, 0, sizeof(displaytime));
295         displayblinkdelay = 0;
296         displayblink = 0;
297         displayselected = 0;
298         consolekeydown = 0;
299         consoleblinkdelay = 0;
300         consoleblink = 0;
301         consoleselected = 0;
302         autocam = 0;
303
304         crouchkey = 0,jumpkey = 0,forwardkey = 0,chatkey = 0,backkey = 0,leftkey = 0,rightkey = 0,drawkey = 0,throwkey = 0,attackkey = 0;
305         consolekey = 0;
306         oldattackkey = 0;
307
308         loading = 0;
309         talkdelay = 0;
310
311         numboundaries = 0;
312 //      XYZ boundary[360];
313
314         whichlevel = 0;
315         oldenvironment = 0;
316         targetlevel = 0;
317         changedelay = 0;
318
319         memset(musicvolume, 0, sizeof(musicvolume));
320         memset(oldmusicvolume, 0, sizeof(oldmusicvolume));
321         musicselected = 0;
322         change = 0;
323         
324 //------------
325
326         waiting = false;
327         mainmenu = 0;
328         
329         accountactive = NULL;
330 }
331
332 void Game::deleteGame(){
333     if(text)
334         delete text;
335     for(int i=0;i<10;i++){
336         if(Mainmenuitems[i])glDeleteTextures( 1, &Mainmenuitems[i] );
337     }
338     glDeleteTextures( 1, &cursortexture );
339     glDeleteTextures( 1, &Maparrowtexture );
340     glDeleteTextures( 1, &Mapboxtexture );
341     glDeleteTextures( 1, &Mapcircletexture );
342     glDeleteTextures( 1, &terraintexture );
343     glDeleteTextures( 1, &terraintexture2 );
344     if(screentexture>0)glDeleteTextures( 1, &screentexture );
345     if(screentexture2>0)glDeleteTextures( 1, &screentexture2 );
346     glDeleteTextures( 1, &hawktexture );
347     glDeleteTextures( 1, &logotexture );
348     glDeleteTextures( 1, &loadscreentexture );
349
350     Dispose();
351 }
352
353 void Game::fireSound(int sound) {
354         emit_sound_at(sound);
355 }
356
357 void Game::inputText(char* str, int* charselected, int* nb_chars) {
358         SDL_Event evenement;
359         
360         if(!waiting) {
361                 waiting=true;
362                 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY,SDL_DEFAULT_REPEAT_INTERVAL);
363                 SDL_EnableUNICODE(true);
364         }
365
366         while(SDL_PollEvent(&evenement)) {
367         
368                 switch(evenement.type) {
369                         case SDL_KEYDOWN:
370                                 if(evenement.key.keysym.sym == SDLK_ESCAPE) {
371                                         for(int i=0;i<255;i++)
372                                                 str[i]=0;
373                                         *nb_chars=0;
374                                         *charselected=0;
375                                         waiting=false;
376                                 } else if(evenement.key.keysym.sym==SDLK_BACKSPACE){
377                                         if((*charselected)!=0) {
378                                                 for(int i=(*charselected)-1;i<255;i++)
379                                                         str[i]=str[i+1];
380                                                 str[255]=0;
381                                                 (*charselected)--;
382                                                 (*nb_chars)--;
383                                         }
384                                 } else if(evenement.key.keysym.sym==SDLK_DELETE){
385                                         if((*charselected)<(*nb_chars)){
386                                                 for(int i=(*charselected);i<255;i++)
387                                                         str[i]=str[i+1];
388                                                 str[255]=0;
389                                                 (*nb_chars)--;
390                                         }
391                                 } else if(evenement.key.keysym.sym==SDLK_HOME){
392                                         (*charselected)=0;
393                                 } else if(evenement.key.keysym.sym==SDLK_END){
394                                         (*charselected)=(*nb_chars);
395                                 } else if(evenement.key.keysym.sym==SDLK_LEFT){
396                                         if((*charselected)!=0)
397                                                 (*charselected)--;
398                                 } else if(evenement.key.keysym.sym==SDLK_RIGHT){
399                                         if((*charselected)<(*nb_chars))
400                                                 (*charselected)++;
401                                 } else if(evenement.key.keysym.sym==SDLK_RETURN) {
402                                         waiting=false;
403                                 } else if(evenement.key.keysym.unicode>=32&&evenement.key.keysym.unicode<127&&(*nb_chars)<60){
404                                         for(int i=255;i>=(*charselected)+1;i--)
405                                                 str[i]=str[i-1];
406                                         str[*charselected]=evenement.key.keysym.unicode;
407                                         (*charselected)++;
408                                         (*nb_chars)++;
409                                 }
410                         break;
411                 }
412         }
413         
414         if(!waiting) {
415                 SDL_EnableKeyRepeat(0,0); // disable key repeat
416                 SDL_EnableUNICODE(false);
417         }
418 }
419
420 void Game::setKeySelected() {
421         waiting=true;
422     printf("launch thread\n");
423         SDL_Thread* thread = SDL_CreateThread(Game::setKeySelected_thread,NULL);
424     if ( thread == NULL ) {
425         fprintf(stderr, "Unable to create thread: %s\n", SDL_GetError());
426                 waiting=false;
427         return;
428     }
429 }
430
431 int Game::setKeySelected_thread(void* data) {
432         int keycode=-1;
433         SDL_Event evenement;
434         while(keycode==-1) {
435                 SDL_WaitEvent(&evenement);
436                 switch(evenement.type) {
437                         case SDL_KEYDOWN:
438                                 keycode = evenement.key.keysym.sym;
439                         break;
440                         case SDL_MOUSEBUTTONDOWN:
441                                 keycode = SDLK_LAST+evenement.button.button;
442                         break;
443                         default:
444                         break;
445                 }
446         }
447         if(keycode != SDLK_ESCAPE) {
448                 fireSound();
449                 switch(keyselect) {
450                         case 0: forwardkey=keycode;
451                         break;
452                         case 1: backkey=keycode;
453                         break;
454                         case 2: leftkey=keycode;
455                         break;
456                         case 3: rightkey=keycode;
457                         break;
458                         case 4: crouchkey=keycode;
459                         break;
460                         case 5: jumpkey=keycode;
461                         break;
462                         case 6: drawkey=keycode;
463                         break;
464                         case 7: throwkey=keycode;
465                         break;
466                         case 8: attackkey=keycode;
467                         break;
468                         case 9: consolekey=keycode;
469                         break;
470                         default:
471                         break;
472                 }
473         }
474         keyselect=-1;
475         waiting=false;
476     return 0;
477 }
478
479 void Game::DrawGL() {
480         if ( stereomode == stereoNone ) {
481                 DrawGLScene(stereoCenter);
482         } else {
483                 DrawGLScene(stereoLeft);
484                 DrawGLScene(stereoRight);
485         }
486 }