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