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