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