]> git.jsancho.org Git - lugaru.git/blob - Source/Game.cpp
almost nothing
[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         campaignnumlevels = 0;
107
108         memset(campaignmapname, 0, sizeof(campaignmapname));
109         memset(campaigndescription, 0, sizeof(campaigndescription));
110         memset(campaignchoosenext, 0, sizeof(campaignchoosenext));
111         memset(campaignnumnext, 0, sizeof(campaignnumnext));
112         memset(campaignnextlevel, 0, sizeof(campaignnextlevel));
113         memset(campaignlocationx, 0, sizeof(campaignlocationx));
114         memset(campaignlocationy, 0, sizeof(campaignlocationy));
115         memset(campaignlocationy, 0, sizeof(campaignlocationy));
116
117         campaignchoicenum = 0;
118
119         memset(campaignchoicewhich, 0, sizeof(campaignchoicewhich));
120
121         whichchoice = 0;
122
123         numlevelspassed = 0;
124
125         memset(levelorder, 0, sizeof(levelorder));
126         memset(levelvisible, 0, sizeof(levelvisible));
127         memset(levelhighlight, 0, sizeof(levelhighlight));
128
129         minimap = 0;
130
131         musictype = 0,oldmusictype = 0,oldoldmusictype = 0;
132         realthreat = 0;
133
134 //      Model rabbit;
135 //      XYZ rabbitcoords;
136
137 //      XYZ mapcenter;
138         mapradius = 0;
139
140 //      Text text;
141         fps = 0;
142
143 //      XYZ cameraloc;
144         cameradist = 0;
145
146         drawtoggle = 0;
147
148         editorenabled = 0;
149         editortype = 0;
150         editorsize = 0;
151         editorrotation = 0;
152         editorrotation2 = 0;
153
154         brightness = 0;
155
156         quit = 0;
157         tryquit = 0;
158
159 //      XYZ pathpoint[30];
160         numpathpoints = 0;
161         memset(numpathpointconnect, 0, sizeof(numpathpointconnect));
162         memset(pathpointconnect, 0, sizeof(pathpointconnect));
163         pathpointselected = 0;
164
165         endgame = 0;
166         scoreadded = 0;
167         numchallengelevels = 0;
168
169         console = false;
170         archiveselected = 0;
171
172         memset(consoletext, 0, sizeof(consoletext));
173         memset(consolechars, 0, sizeof(consolechars));
174         chatting = 0;
175         memset(displaytext, 0, sizeof(displaytext));
176         memset(displaychars, 0, sizeof(displaychars));
177         memset(displaytime, 0, sizeof(displaytime));
178         displayblinkdelay = 0;
179         displayblink = 0;
180         displayselected = 0;
181         consolekeydown = 0;
182         consoleblinkdelay = 0;
183         consoleblink = 0;
184         consoleselected = 0;
185         autocam = 0;
186
187         crouchkey = 0,jumpkey = 0,forwardkey = 0,chatkey = 0,backkey = 0,leftkey = 0,rightkey = 0,drawkey = 0,throwkey = 0,attackkey = 0;
188         oldattackkey = 0;
189
190         loading = 0;
191         talkdelay = 0;
192
193         numboundaries = 0;
194 //      XYZ boundary[360];
195
196         whichlevel = 0;
197         oldenvironment = 0;
198         targetlevel = 0;
199         changedelay = 0;
200
201         memset(musicvolume, 0, sizeof(musicvolume));
202         memset(oldmusicvolume, 0, sizeof(oldmusicvolume));
203         musicselected = 0;
204         change = 0;
205         
206 //------------
207
208         waiting = false;
209         mainmenu = 0;
210         
211         accountactive = NULL;
212 }
213
214 typedef struct {
215         Game* game;
216         void (Game::*method)();
217 } params_thread;
218
219 void Game::fireSound(int sound) {
220         emit_sound_at(sound);
221 }
222
223 void Game::inputText(char* str, int* charselected, int* nb_chars) {
224         SDL_Event evenement;
225         int i;
226         
227         if(!waiting) {
228                 waiting=true;
229                 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY,SDL_DEFAULT_REPEAT_INTERVAL);
230                 SDL_EnableUNICODE(true);
231         }
232
233         SDL_PollEvent(&evenement);
234         
235         switch(evenement.type) {
236                 case SDL_KEYDOWN:
237                         if(evenement.key.keysym.sym == SDLK_ESCAPE) {
238                                 for(i=0;i<255;i++)
239                                         str[i]=0;
240                                 *nb_chars=0;
241                                 *charselected=0;
242                                 waiting=false;
243                         } else if(evenement.key.keysym.sym==SDLK_BACKSPACE){
244                                 if((*charselected)!=0) {
245                                         for(i=(*charselected)-1;i<255;i++)
246                                                 str[i]=str[i+1];
247                                         str[255]=0;
248                                         (*charselected)--;
249                                         (*nb_chars)--;
250                                 }
251                         } else if(evenement.key.keysym.sym==SDLK_DELETE){
252                                 if((*charselected)<(*nb_chars)){
253                     for(i=(*charselected);i<255;i++)
254                         str[i]=str[i+1];
255                     str[255]=0;
256                     (*nb_chars)--;
257                 }
258                         } else if(evenement.key.keysym.sym==SDLK_HOME){
259                 (*charselected)=0;
260                         } else if(evenement.key.keysym.sym==SDLK_END){
261                 (*charselected)=(*nb_chars);
262                         } else if(evenement.key.keysym.sym==SDLK_LEFT){
263                                 if((*charselected)!=0)
264                                         (*charselected)--;
265                         } else if(evenement.key.keysym.sym==SDLK_RIGHT){
266                                 if((*charselected)<(*nb_chars))
267                                         (*charselected)++;
268                         } else if(evenement.key.keysym.sym==SDLK_RETURN) {
269                                 waiting=false;
270                         } else if(evenement.key.keysym.unicode>=32&&evenement.key.keysym.unicode<127&&(*nb_chars)<60){
271                                 for(i=255;i>=(*charselected)+1;i--)
272                                         str[i]=str[i-1];
273                                 str[*charselected]=evenement.key.keysym.unicode;
274                                 (*charselected)++;
275                                 (*nb_chars)++;
276                         }
277                 break;
278         }
279         
280         if(!waiting) {
281                 SDL_EnableKeyRepeat(0,0); // disable key repeat
282                 SDL_EnableUNICODE(false);
283         }
284 }
285
286 void Game::setKeySelected() {
287         waiting=true;
288         params_thread* data = new params_thread;
289     data->game = this;
290     data->method = &Game::setKeySelected_thread;
291     printf("launch thread\n");
292         SDL_Thread* thread = SDL_CreateThread(Game::thread, data);
293     if ( thread == NULL ) {
294         fprintf(stderr, "Unable to create thread: %s\n", SDL_GetError());
295                 waiting=false;
296         return;
297     }
298 }
299
300 void Game::setKeySelected_thread() {
301         int keycode=-1;
302         SDL_Event evenement;
303         while(keycode==-1) {
304                 SDL_WaitEvent(&evenement);
305                 switch(evenement.type) {
306                         case SDL_KEYDOWN:
307                                 keycode = evenement.key.keysym.sym;
308                         break;
309                         case SDL_MOUSEBUTTONDOWN:
310                                 keycode = SDLK_LAST+evenement.button.button;
311                         break;
312                         default:
313                         break;
314                 }
315         }
316         if(keycode != SDLK_ESCAPE) {
317                 fireSound();
318                 switch(keyselect) {
319                         case 0: forwardkey=keycode;
320                         break;
321                         case 1: backkey=keycode;
322                         break;
323                         case 2: leftkey=keycode;
324                         break;
325                         case 3: rightkey=keycode;
326                         break;
327                         case 4: crouchkey=keycode;
328                         break;
329                         case 5: jumpkey=keycode;
330                         break;
331                         case 6: drawkey=keycode;
332                         break;
333                         case 7: throwkey=keycode;
334                         break;
335                         case 8: attackkey=keycode;
336                         break;
337                         default:
338                         break;
339                 }
340         }
341         keyselect=-1;
342         waiting=false;
343 }
344
345 int Game::thread(void *data) {
346         params_thread* pt = (params_thread*)data;
347     if(pt) {
348         (pt->game->*(pt->method))();
349     }
350 }
351
352 void Game::DrawGL() {
353         if ( stereomode == stereoNone ) {
354                 DrawGLScene(stereoCenter);
355         } else {
356                 DrawGLScene(stereoLeft);
357                 DrawGLScene(stereoRight);
358         }
359 }