]> git.jsancho.org Git - lugaru.git/blob - Source/Game.cpp
07b3dd840f7b12577b6eb86551e26aca5c95df1a
[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 std::vector<TextureInfo> Game::textures;
30
31 Game::Game()
32 {
33         terraintexture = 0;
34         terraintexture2 = 0;
35         terraintexture3 = 0;
36         screentexture = 0;
37         screentexture2 = 0;
38         logotexture = 0;
39         loadscreentexture = 0;
40         Maparrowtexture = 0;
41         Mapboxtexture = 0;
42         Mapcircletexture = 0;
43         cursortexture = 0;
44
45         memset(Mainmenuitems, 0, sizeof(Mainmenuitems));
46
47         nummenuitems = 0;
48
49         memset(startx, 0, sizeof(startx));
50         memset(starty, 0, sizeof(starty));
51         memset(endx, 0, sizeof(endx));
52         memset(endy, 0, sizeof(endy));
53
54         memset(selectedlong, 0, sizeof(selectedlong));
55         memset(offsetx, 0, sizeof(offsetx));
56         memset(offsety, 0, sizeof(offsety));
57         memset(movex, 0, sizeof(movex));
58         memset(movey, 0, sizeof(movey));
59         memset(endy, 0, sizeof(endy));
60
61         transition = 0;
62         anim = 0;
63         selected = 0;
64         loaddistrib = 0;
65         keyselect = 0;
66         indemo = 0;
67
68         won = 0;
69
70         entername = 0;
71
72         memset(menustring, 0, sizeof(menustring));
73         memset(registrationname, 0, sizeof(registrationname));
74         registrationnumber = 0;
75
76         newdetail = 0;
77         newscreenwidth = 0;
78         newscreenheight = 0;
79
80         gameon = 0;
81         deltah = 0,deltav = 0;
82         mousecoordh = 0,mousecoordv = 0;
83         oldmousecoordh = 0,oldmousecoordv = 0;
84         rotation = 0,rotation2 = 0;
85
86 //      SkyBox skybox;
87
88         cameramode = 0;
89         olddrawmode = 0;
90         drawmode = 0;
91         firstload = 0;
92         oldbutton = 0;
93
94         leveltime = 0;
95         loadtime = 0;
96
97 //      Model hawk;
98
99 //      XYZ hawkcoords;
100 //      XYZ realhawkcoords;
101
102         hawktexture = 0;
103         hawkrotation = 0;
104         hawkcalldelay = 0;
105 /*
106         Model eye;
107         Model iris;
108         Model cornea;
109 */
110         stealthloading = 0;
111
112         campaignnumlevels = 0;
113
114         memset(campaignmapname, 0, sizeof(campaignmapname));
115         memset(campaigndescription, 0, sizeof(campaigndescription));
116         memset(campaignchoosenext, 0, sizeof(campaignchoosenext));
117         memset(campaignnumnext, 0, sizeof(campaignnumnext));
118         memset(campaignnextlevel, 0, sizeof(campaignnextlevel));
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]=0;
246                                 *nb_chars=0;
247                                 *charselected=0;
248                                 waiting=false;
249                         } else if(evenement.key.keysym.sym==SDLK_BACKSPACE){
250                                 if((*charselected)!=0) {
251                                         for(i=(*charselected)-1;i<255;i++)
252                                                 str[i]=str[i+1];
253                                         str[255]=0;
254                                         (*charselected)--;
255                                         (*nb_chars)--;
256                                 }
257                         } else if(evenement.key.keysym.sym==SDLK_DELETE){
258                                 if((*charselected)<(*nb_chars)){
259                     for(i=(*charselected);i<255;i++)
260                         str[i]=str[i+1];
261                     str[255]=0;
262                     (*nb_chars)--;
263                 }
264                         } else if(evenement.key.keysym.sym==SDLK_HOME){
265                 (*charselected)=0;
266                         } else if(evenement.key.keysym.sym==SDLK_END){
267                 (*charselected)=(*nb_chars);
268                         } else if(evenement.key.keysym.sym==SDLK_LEFT){
269                                 if((*charselected)!=0)
270                                         (*charselected)--;
271                         } else if(evenement.key.keysym.sym==SDLK_RIGHT){
272                                 if((*charselected)<(*nb_chars))
273                                         (*charselected)++;
274                         } else if(evenement.key.keysym.sym==SDLK_RETURN) {
275                                 waiting=false;
276                         } else if(evenement.key.keysym.unicode>=32&&evenement.key.keysym.unicode<127&&(*nb_chars)<60){
277                                 for(i=255;i>=(*charselected)+1;i--)
278                                         str[i]=str[i-1];
279                                 str[*charselected]=evenement.key.keysym.unicode;
280                                 (*charselected)++;
281                                 (*nb_chars)++;
282                         }
283                 break;
284         }
285         
286         if(!waiting) {
287                 SDL_EnableKeyRepeat(0,0); // disable key repeat
288                 SDL_EnableUNICODE(false);
289         }
290 }
291
292 void Game::setKeySelected() {
293         waiting=true;
294         params_thread* data = new params_thread;
295     data->game = this;
296     data->method = &Game::setKeySelected_thread;
297     printf("launch thread\n");
298         SDL_Thread* thread = SDL_CreateThread(Game::thread, data);
299     if ( thread == NULL ) {
300         fprintf(stderr, "Unable to create thread: %s\n", SDL_GetError());
301                 waiting=false;
302         return;
303     }
304 }
305
306 void Game::setKeySelected_thread() {
307         int keycode=-1;
308         SDL_Event evenement;
309         while(keycode==-1) {
310                 SDL_WaitEvent(&evenement);
311                 switch(evenement.type) {
312                         case SDL_KEYDOWN:
313                                 keycode = evenement.key.keysym.sym;
314                         break;
315                         case SDL_MOUSEBUTTONDOWN:
316                                 keycode = SDLK_LAST+evenement.button.button;
317                         break;
318                         default:
319                         break;
320                 }
321         }
322         if(keycode != SDLK_ESCAPE) {
323                 fireSound();
324                 switch(keyselect) {
325                         case 0: forwardkey=keycode;
326                         break;
327                         case 1: backkey=keycode;
328                         break;
329                         case 2: leftkey=keycode;
330                         break;
331                         case 3: rightkey=keycode;
332                         break;
333                         case 4: crouchkey=keycode;
334                         break;
335                         case 5: jumpkey=keycode;
336                         break;
337                         case 6: drawkey=keycode;
338                         break;
339                         case 7: throwkey=keycode;
340                         break;
341                         case 8: attackkey=keycode;
342                         break;
343                         default:
344                         break;
345                 }
346         }
347         keyselect=-1;
348         waiting=false;
349 }
350
351 int Game::thread(void *data) {
352         params_thread* pt = (params_thread*)data;
353     if(pt) {
354         (pt->game->*(pt->method))();
355     }
356 }
357
358 void Game::DrawGL() {
359         if ( stereomode == stereoNone ) {
360                 DrawGLScene(stereoCenter);
361         } else {
362                 DrawGLScene(stereoLeft);
363                 DrawGLScene(stereoRight);
364         }
365 }