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