]> git.jsancho.org Git - lugaru.git/blob - Source/Game.cpp
524009d2500b504846f927c4223147d5fba8fe47
[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(char* str, int* charselected, int* nb_chars) {
233         SDL_Event evenement;
234         int i;
235         
236         if(!waiting) {
237                 waiting=true;
238                 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY,SDL_DEFAULT_REPEAT_INTERVAL);
239                 SDL_EnableUNICODE(true);
240         }
241
242         SDL_PollEvent(&evenement);
243         
244         switch(evenement.type) {
245                 case SDL_KEYDOWN:
246                         if(evenement.key.keysym.sym == SDLK_ESCAPE) {
247                                 for(i=0;i<255;i++){
248                                         str[i]=' ';
249                                 }
250                                 *nb_chars=0;
251                                 *charselected=0;
252                                 waiting=false;
253                         } else if(evenement.key.keysym.sym==SDLK_BACKSPACE){
254                                 if((*charselected)!=0) {
255                                         for(i=(*charselected)-1;i<255;i++){
256                                                 str[i]=str[i+1];
257                                         }
258                                         str[255]=' ';
259                                         (*charselected)--;
260                                         (*nb_chars)--;
261                                 }
262                         } else if(evenement.key.keysym.sym==SDLK_DELETE){
263                                 for(i=(*charselected);i<255;i++){
264                                         str[i]=str[i+1];
265                                 }
266                                 str[255]=' ';
267                                 (*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<127)&&((*nb_chars)<60)&&(evenement.key.keysym.sym!=SDLK_LSHIFT)&&(evenement.key.keysym.sym!=SDLK_RSHIFT)) {
277                                 for(i=255;i>=(*charselected)+1;i--){
278                                         str[i]=str[i-1];
279                                 }
280                                 str[*charselected]=evenement.key.keysym.unicode;
281                                 (*charselected)++;
282                                 (*nb_chars)++;
283                         }
284                 break;
285         }
286         
287         if(!waiting) {
288                 SDL_EnableKeyRepeat(0,0); // disable key repeat
289                 SDL_EnableUNICODE(false);
290         }
291 }
292
293 void Game::setKeySelected() {
294         waiting=true;
295         params_thread* data = new params_thread;
296     data->game = this;
297     data->method = &Game::setKeySelected_thread;
298     printf("launch thread\n");
299         SDL_Thread* thread = SDL_CreateThread(Game::thread, data);
300     if ( thread == NULL ) {
301         fprintf(stderr, "Unable to create thread: %s\n", SDL_GetError());
302                 waiting=false;
303         return;
304     }
305 }
306
307 void Game::setKeySelected_thread() {
308         int keycode=-1;
309         SDL_Event evenement;
310         while(keycode==-1) {
311                 SDL_WaitEvent(&evenement);
312                 switch(evenement.type) {
313                         case SDL_KEYDOWN:
314                                 keycode = evenement.key.keysym.sym;
315                         break;
316                         case SDL_MOUSEBUTTONDOWN:
317                                 keycode = SDLK_LAST+evenement.button.button;
318                         break;
319                         default:
320                         break;
321                 }
322         }
323         if(keycode != SDLK_ESCAPE) {
324                 fireSound();
325                 switch(keyselect) {
326                         case 0: forwardkey=keycode;
327                         break;
328                         case 1: backkey=keycode;
329                         break;
330                         case 2: leftkey=keycode;
331                         break;
332                         case 3: rightkey=keycode;
333                         break;
334                         case 4: crouchkey=keycode;
335                         break;
336                         case 5: jumpkey=keycode;
337                         break;
338                         case 6: drawkey=keycode;
339                         break;
340                         case 7: throwkey=keycode;
341                         break;
342                         case 8: attackkey=keycode;
343                         break;
344                         default:
345                         break;
346                 }
347         }
348         keyselect=-1;
349         waiting=false;
350 }
351
352 int Game::thread(void *data) {
353         params_thread* pt = (params_thread*)data;
354     if(pt) {
355         (pt->game->*(pt->method))();
356     }
357 }