]> git.jsancho.org Git - lugaru.git/blob - Source/Game.cpp
BEAUTIFIED ALL SOURCE CODE
[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 participantyaw[max_dialogues][10];
20 XYZ participantfacing[max_dialogues][max_dialoguelength][10];
21 float dialoguecamerayaw[max_dialogues][max_dialoguelength];
22 float dialoguecamerapitch[max_dialogues][max_dialoguelength];
23 int indialogue;
24 int whichdialogue;
25 int directing;
26 float dialoguetime;
27 int dialoguegonethrough[20];
28
29 namespace Game
30 {
31 Texture terraintexture;
32 Texture terraintexture2;
33 Texture loadscreentexture;
34 Texture Maparrowtexture;
35 Texture Mapboxtexture;
36 Texture Mapcircletexture;
37 Texture cursortexture;
38 GLuint screentexture = 0;
39 GLuint screentexture2 = 0;
40 Texture Mainmenuitems[10];
41
42 int selected = 0;
43 int keyselect = 0;
44
45 int newdetail = 0;
46 int newscreenwidth = 0;
47 int newscreenheight = 0;
48
49 bool gameon = 0;
50 float deltah = 0;
51 float deltav = 0;
52 int mousecoordh = 0;
53 int mousecoordv = 0;
54 int oldmousecoordh = 0;
55 int oldmousecoordv = 0;
56 float yaw = 0;
57 float pitch = 0;
58 SkyBox *skybox = NULL;
59 bool cameramode = 0;
60 bool firstload = 0;
61
62 Texture hawktexture;
63 float hawkyaw = 0;
64 float hawkcalldelay = 0;
65 float leveltime = 0;
66 float loadtime = 0;
67
68 Model hawk;
69 XYZ hawkcoords;
70 XYZ realhawkcoords;
71
72 Model eye;
73 Model iris;
74 Model cornea;
75
76 bool stealthloading = 0;
77
78 int musictype = 0;
79
80 XYZ mapcenter;
81 float mapradius = 0;
82
83 Text *text = NULL;
84 float fps = 0;
85
86 bool editorenabled = 0;
87 int editortype = 0;
88 float editorsize = 0;
89 float editoryaw = 0;
90 float editorpitch = 0;
91
92 int tryquit = 0;
93
94 XYZ pathpoint[30];
95 int numpathpoints = 0;
96 int numpathpointconnect[30] = {};
97 int pathpointconnect[30][30] = {};
98 int pathpointselected = 0;
99
100 int endgame = 0;
101 bool scoreadded = 0;
102 int numchallengelevels = 0;
103
104 bool console = false;
105 char consoletext[15][256] = {};
106 int consolechars[15] = {};
107 bool chatting = 0;
108 char displaytext[15][256] = {};
109 int displaychars[15] = {};
110 float displaytime[15] = {};
111 float displayblinkdelay = 0;
112 bool displayblink = 0;
113 int displayselected = 0;
114 float consoleblinkdelay = 0;
115 bool consoleblink = 0;
116 int consoleselected = 0;
117
118 unsigned short crouchkey = 0, jumpkey = 0, forwardkey = 0, chatkey = 0, backkey = 0, leftkey = 0, rightkey = 0, drawkey = 0, throwkey = 0, attackkey = 0;
119 unsigned short consolekey = 0;
120
121 int loading = 0;
122
123 int oldenvironment = 0;
124 int targetlevel = 0;
125 float changedelay = 0;
126
127 bool waiting = false;
128 Account* accountactive = NULL;
129 }
130
131 void Game::fireSound(int sound)
132 {
133     emit_sound_at(sound);
134 }
135
136 void Game::inputText(char* str, int* charselected, int* nb_chars)
137 {
138     SDL_Event evenement;
139
140     if (!waiting) {
141         waiting = true;
142         SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
143         SDL_EnableUNICODE(true);
144     }
145
146     while (SDL_PollEvent(&evenement)) {
147
148         switch (evenement.type) {
149         case SDL_KEYDOWN:
150             if (evenement.key.keysym.sym == SDLK_ESCAPE) {
151                 for (int i = 0; i < 255; i++)
152                     str[i] = 0;
153                 *nb_chars = 0;
154                 *charselected = 0;
155                 waiting = false;
156             } else if (evenement.key.keysym.sym == SDLK_BACKSPACE) {
157                 if ((*charselected) != 0) {
158                     for (int i = (*charselected) - 1; i < 255; i++)
159                         str[i] = str[i + 1];
160                     str[255] = 0;
161                     (*charselected)--;
162                     (*nb_chars)--;
163                 }
164             } else if (evenement.key.keysym.sym == SDLK_DELETE) {
165                 if ((*charselected) < (*nb_chars)) {
166                     for (int i = (*charselected); i < 255; i++)
167                         str[i] = str[i + 1];
168                     str[255] = 0;
169                     (*nb_chars)--;
170                 }
171             } else if (evenement.key.keysym.sym == SDLK_HOME) {
172                 (*charselected) = 0;
173             } else if (evenement.key.keysym.sym == SDLK_END) {
174                 (*charselected) = (*nb_chars);
175             } else if (evenement.key.keysym.sym == SDLK_LEFT) {
176                 if ((*charselected) != 0)
177                     (*charselected)--;
178             } else if (evenement.key.keysym.sym == SDLK_RIGHT) {
179                 if ((*charselected) < (*nb_chars))
180                     (*charselected)++;
181             } else if (evenement.key.keysym.sym == SDLK_RETURN) {
182                 waiting = false;
183             } else if (evenement.key.keysym.unicode >= 32 && evenement.key.keysym.unicode < 127 && (*nb_chars) < 60) {
184                 for (int i = 255; i >= (*charselected) + 1; i--)
185                     str[i] = str[i - 1];
186                 str[*charselected] = evenement.key.keysym.unicode;
187                 (*charselected)++;
188                 (*nb_chars)++;
189             }
190             break;
191         }
192     }
193
194     if (!waiting) {
195         SDL_EnableKeyRepeat(0, 0); // disable key repeat
196         SDL_EnableUNICODE(false);
197     }
198 }
199
200 int setKeySelected_thread(void* data)
201 {
202     using namespace Game;
203     int keycode = -1;
204     SDL_Event evenement;
205     while (keycode == -1) {
206         SDL_WaitEvent(&evenement);
207         switch (evenement.type) {
208         case SDL_KEYDOWN:
209             keycode = evenement.key.keysym.sym;
210             break;
211         case SDL_MOUSEBUTTONDOWN:
212             keycode = SDLK_LAST + evenement.button.button;
213             break;
214         default:
215             break;
216         }
217     }
218     if (keycode != SDLK_ESCAPE) {
219         fireSound();
220         switch (keyselect) {
221         case 0:
222             forwardkey = keycode;
223             break;
224         case 1:
225             backkey = keycode;
226             break;
227         case 2:
228             leftkey = keycode;
229             break;
230         case 3:
231             rightkey = keycode;
232             break;
233         case 4:
234             crouchkey = keycode;
235             break;
236         case 5:
237             jumpkey = keycode;
238             break;
239         case 6:
240             drawkey = keycode;
241             break;
242         case 7:
243             throwkey = keycode;
244             break;
245         case 8:
246             attackkey = keycode;
247             break;
248         case 9:
249             consolekey = keycode;
250             break;
251         default:
252             break;
253         }
254     }
255     keyselect = -1;
256     waiting = false;
257     LoadMenu();
258     return 0;
259 }
260
261 void Game::setKeySelected()
262 {
263     waiting = true;
264     printf("launch thread\n");
265     SDL_Thread* thread = SDL_CreateThread(setKeySelected_thread, NULL);
266     if ( thread == NULL ) {
267         fprintf(stderr, "Unable to create thread: %s\n", SDL_GetError());
268         waiting = false;
269         return;
270     }
271 }