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