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