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