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