]> git.jsancho.org Git - lugaru.git/blob - Source/Game.cpp
1002b0a9c4af0c71491840ee69422bdeca3c0003
[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 Game::Game()
30 {
31         terraintexture = 0;
32         terraintexture2 = 0;
33         terraintexture3 = 0;
34         screentexture = 0;
35         screentexture2 = 0;
36         logotexture = 0;
37         loadscreentexture = 0;
38         Maparrowtexture = 0;
39         Mapboxtexture = 0;
40         Mapcircletexture = 0;
41         cursortexture = 0;
42
43         memset(Mainmenuitems, 0, sizeof(Mainmenuitems));
44
45         nummenuitems = 0;
46
47         memset(startx, 0, sizeof(startx));
48         memset(starty, 0, sizeof(starty));
49         memset(endx, 0, sizeof(endx));
50         memset(endy, 0, sizeof(endy));
51
52         memset(selectedlong, 0, sizeof(selectedlong));
53         memset(offsetx, 0, sizeof(offsetx));
54         memset(offsety, 0, sizeof(offsety));
55         memset(movex, 0, sizeof(movex));
56         memset(movey, 0, sizeof(movey));
57         memset(endy, 0, sizeof(endy));
58
59         transition = 0;
60         anim = 0;
61         selected = 0;
62         loaddistrib = 0;
63         keyselect = 0;
64         indemo = 0;
65
66         won = 0;
67
68         entername = 0;
69
70         memset(menustring, 0, sizeof(menustring));
71         memset(registrationname, 0, sizeof(registrationname));
72         registrationnumber = 0;
73
74         newdetail = 0;
75         newscreenwidth = 0;
76         newscreenheight = 0;
77
78         gameon = 0;
79         deltah = 0,deltav = 0;
80         mousecoordh = 0,mousecoordv = 0;
81         oldmousecoordh = 0,oldmousecoordv = 0;
82         rotation = 0,rotation2 = 0;
83
84 //      SkyBox skybox;
85
86         cameramode = 0;
87         olddrawmode = 0;
88         drawmode = 0;
89         firstload = 0;
90         oldbutton = 0;
91
92         leveltime = 0;
93         loadtime = 0;
94
95 //      Model hawk;
96
97 //      XYZ hawkcoords;
98 //      XYZ realhawkcoords;
99
100         hawktexture = 0;
101         hawkrotation = 0;
102         hawkcalldelay = 0;
103 /*
104         Model eye;
105         Model iris;
106         Model cornea;
107 */
108         stealthloading = 0;
109
110         campaignnumlevels = 0;
111
112         memset(campaignmapname, 0, sizeof(campaignmapname));
113         memset(campaigndescription, 0, sizeof(campaigndescription));
114         memset(campaignchoosenext, 0, sizeof(campaignchoosenext));
115         memset(campaignnumnext, 0, sizeof(campaignnumnext));
116         memset(campaignnextlevel, 0, sizeof(campaignnextlevel));
117         int campaignchoicesmade;
118         memset(campaignchoices, 0, sizeof(campaignchoices));
119         memset(campaignlocationx, 0, sizeof(campaignlocationx));
120         memset(campaignlocationy, 0, sizeof(campaignlocationy));
121         memset(campaignlocationy, 0, sizeof(campaignlocationy));
122
123         campaignchoicenum = 0;
124
125         memset(campaignchoicewhich, 0, sizeof(campaignchoicewhich));
126
127         whichchoice = 0;
128
129         numlevelspassed = 0;
130
131         memset(levelorder, 0, sizeof(levelorder));
132         memset(levelvisible, 0, sizeof(levelvisible));
133         memset(levelhighlight, 0, sizeof(levelhighlight));
134
135         minimap = 0;
136
137         musictype = 0,oldmusictype = 0,oldoldmusictype = 0;
138         realthreat = 0;
139
140 //      Model rabbit;
141 //      XYZ rabbitcoords;
142
143 //      XYZ mapcenter;
144         mapradius = 0;
145
146 //      Text text;
147         fps = 0;
148
149 //      XYZ cameraloc;
150         cameradist = 0;
151
152         drawtoggle = 0;
153
154         editorenabled = 0;
155         editortype = 0;
156         editorsize = 0;
157         editorrotation = 0;
158         editorrotation2 = 0;
159
160         brightness = 0;
161
162         quit = 0;
163         tryquit = 0;
164
165 //      XYZ pathpoint[30];
166         numpathpoints = 0;
167         memset(numpathpointconnect, 0, sizeof(numpathpointconnect));
168         memset(pathpointconnect, 0, sizeof(pathpointconnect));
169         pathpointselected = 0;
170
171         endgame = 0;
172         scoreadded = 0;
173         numchallengelevels = 0;
174
175         console = 0;
176         archiveselected = 0;
177
178         memset(consoletext, 0, sizeof(consoletext));
179         memset(consolechars, 0, sizeof(consolechars));
180         chatting = 0;
181         memset(displaytext, 0, sizeof(displaytext));
182         memset(displaychars, 0, sizeof(displaychars));
183         memset(displaytime, 0, sizeof(displaytime));
184         displayblinkdelay = 0;
185         displayblink = 0;
186         displayselected = 0;
187         consolekeydown = 0;
188         consoleblinkdelay = 0;
189         consoleblink = 0;
190         consoleselected = 0;
191         autocam = 0;
192
193         crouchkey = 0,jumpkey = 0,forwardkey = 0,chatkey = 0,backkey = 0,leftkey = 0,rightkey = 0,drawkey = 0,throwkey = 0,attackkey = 0;
194         oldattackkey = 0;
195
196         loading = 0;
197         talkdelay = 0;
198
199         numboundaries = 0;
200 //      XYZ boundary[360];
201
202         whichlevel = 0;
203         oldenvironment = 0;
204         targetlevel = 0;
205         changedelay = 0;
206
207         memset(musicvolume, 0, sizeof(musicvolume));
208         memset(oldmusicvolume, 0, sizeof(oldmusicvolume));
209         musicselected = 0;
210         change = 0;
211         
212 //------------
213
214         waiting = false;
215         mainmenu = 0;
216         
217         accountactive = NULL;
218 }
219
220 typedef struct {
221         Game* game;
222         void (Game::*method)();
223 } params_thread;
224
225 void Game::fireSound(int sound) {
226         emit_sound_at(sound);
227 }
228
229 void Game::inputText(char* str, int* charselected, int* nb_chars) {
230         SDL_Event evenement;
231         int i;
232         
233         if(!waiting) {
234                 waiting=true;
235                 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY,SDL_DEFAULT_REPEAT_INTERVAL);
236                 SDL_EnableUNICODE(true);
237         }
238
239         SDL_PollEvent(&evenement);
240         
241         switch(evenement.type) {
242                 case SDL_KEYDOWN:
243                         if(evenement.key.keysym.sym == SDLK_ESCAPE) {
244                                 for(i=0;i<255;i++){
245                                         str[i]=' ';
246                                 }
247                                 *nb_chars=0;
248                                 *charselected=0;
249                                 waiting=false;
250                         } else if(evenement.key.keysym.sym==SDLK_BACKSPACE){
251                                 if((*charselected)!=0) {
252                                         for(i=(*charselected)-1;i<255;i++){
253                                                 str[i]=str[i+1];
254                                         }
255                                         str[255]=' ';
256                                         (*charselected)--;
257                                         (*nb_chars)--;
258                                 }
259                         } else if(evenement.key.keysym.sym==SDLK_DELETE){
260                                 for(i=(*charselected);i<255;i++){
261                                         str[i]=str[i+1];
262                                 }
263                                 str[255]=' ';
264                                 (*nb_chars)--;
265                         } else if(evenement.key.keysym.sym==SDLK_LEFT){
266                                 if((*charselected)!=0)
267                                         (*charselected)--;
268                         } else if(evenement.key.keysym.sym==SDLK_RIGHT){
269                                 if((*charselected)<(*nb_chars))
270                                         (*charselected)++;
271                         } else if(evenement.key.keysym.sym==SDLK_RETURN) {
272                                 waiting=false;
273                         } else if((evenement.key.keysym.unicode<127)&&((*nb_chars)<60)&&(evenement.key.keysym.sym!=SDLK_LSHIFT)&&(evenement.key.keysym.sym!=SDLK_RSHIFT)) {
274                                 for(i=255;i>=(*charselected)+1;i--){
275                                         str[i]=str[i-1];
276                                 }
277                                 str[*charselected]=evenement.key.keysym.unicode;
278                                 (*charselected)++;
279                                 (*nb_chars)++;
280                         }
281                 break;
282         }
283         
284         if(!waiting) {
285                 SDL_EnableKeyRepeat(0,0); // disable key repeat
286                 SDL_EnableUNICODE(false);
287         }
288 }
289
290 void Game::setKeySelected() {
291         waiting=true;
292         params_thread* data = new params_thread;
293     data->game = this;
294     data->method = &Game::setKeySelected_thread;
295     printf("launch thread\n");
296         SDL_Thread* thread = SDL_CreateThread(Game::thread, data);
297     if ( thread == NULL ) {
298         fprintf(stderr, "Unable to create thread: %s\n", SDL_GetError());
299                 waiting=false;
300         return;
301     }
302 }
303
304 void Game::setKeySelected_thread() {
305         int keycode=-1;
306         SDL_Event evenement;
307         while(keycode==-1) {
308                 SDL_WaitEvent(&evenement);
309                 switch(evenement.type) {
310                         case SDL_KEYDOWN:
311                                 keycode = evenement.key.keysym.sym;
312                         break;
313                         case SDL_MOUSEBUTTONDOWN:
314                                 keycode = SDLK_LAST+evenement.button.button;
315                         break;
316                         default:
317                         break;
318                 }
319         }
320         if(keycode != SDLK_ESCAPE) {
321                 fireSound();
322                 switch(keyselect) {
323                         case 0: forwardkey=keycode;
324                         break;
325                         case 1: backkey=keycode;
326                         break;
327                         case 2: leftkey=keycode;
328                         break;
329                         case 3: rightkey=keycode;
330                         break;
331                         case 4: crouchkey=keycode;
332                         break;
333                         case 5: jumpkey=keycode;
334                         break;
335                         case 6: drawkey=keycode;
336                         break;
337                         case 7: throwkey=keycode;
338                         break;
339                         case 8: attackkey=keycode;
340                         break;
341                         default:
342                         break;
343                 }
344         }
345         keyselect=-1;
346         waiting=false;
347 }
348
349 int Game::thread(void *data) {
350         params_thread* pt = (params_thread*)data;
351     if(pt) {
352         (pt->game->*(pt->method))();
353     }
354 }
355
356 void Game::DrawGL() {
357         if ( stereomode == stereoNone ) {
358                 DrawGLScene(stereoCenter);
359         } else {
360                 DrawGLScene(stereoLeft);
361                 DrawGLScene(stereoRight);
362         }
363 }