]> git.jsancho.org Git - lugaru.git/blob - Source/Game.cpp
Add copyright header to source files to match license for project
[lugaru.git] / Source / Game.cpp
1 /*
2 Copyright (C) 2003, 2010 - Wolfire Games
3
4 This file is part of Lugaru.
5
6 Lugaru is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
15 See the GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20 */
21
22 #include "Game.h"
23 #include "openal_wrapper.h"
24 #include "SDL_thread.h"
25
26 extern int mainmenu;
27
28 int numdialogues;
29 int numdialogueboxes[max_dialogues];
30 int dialoguetype[max_dialogues];
31 int dialogueboxlocation[max_dialogues][max_dialoguelength];
32 float dialogueboxcolor[max_dialogues][max_dialoguelength][3];
33 int dialogueboxsound[max_dialogues][max_dialoguelength];
34 char dialoguetext[max_dialogues][max_dialoguelength][128];
35 char dialoguename[max_dialogues][max_dialoguelength][64];
36 XYZ dialoguecamera[max_dialogues][max_dialoguelength];
37 XYZ participantlocation[max_dialogues][10];
38 int participantfocus[max_dialogues][max_dialoguelength];
39 int participantaction[max_dialogues][max_dialoguelength];
40 float participantyaw[max_dialogues][10];
41 XYZ participantfacing[max_dialogues][max_dialoguelength][10];
42 float dialoguecamerayaw[max_dialogues][max_dialoguelength];
43 float dialoguecamerapitch[max_dialogues][max_dialoguelength];
44 int indialogue;
45 int whichdialogue;
46 int directing;
47 float dialoguetime;
48 int dialoguegonethrough[20];
49
50 namespace Game
51 {
52 Texture terraintexture;
53 Texture terraintexture2;
54 Texture loadscreentexture;
55 Texture Maparrowtexture;
56 Texture Mapboxtexture;
57 Texture Mapcircletexture;
58 Texture cursortexture;
59 GLuint screentexture = 0;
60 GLuint screentexture2 = 0;
61 Texture Mainmenuitems[10];
62
63 int selected = 0;
64 int keyselect = 0;
65
66 int newdetail = 0;
67 int newscreenwidth = 0;
68 int newscreenheight = 0;
69
70 bool gameon = 0;
71 float deltah = 0;
72 float deltav = 0;
73 int mousecoordh = 0;
74 int mousecoordv = 0;
75 int oldmousecoordh = 0;
76 int oldmousecoordv = 0;
77 float yaw = 0;
78 float pitch = 0;
79 SkyBox *skybox = NULL;
80 bool cameramode = 0;
81 bool firstload = 0;
82
83 Texture hawktexture;
84 float hawkyaw = 0;
85 float hawkcalldelay = 0;
86 float leveltime = 0;
87 float loadtime = 0;
88
89 Model hawk;
90 XYZ hawkcoords;
91 XYZ realhawkcoords;
92
93 Model eye;
94 Model iris;
95 Model cornea;
96
97 bool stealthloading = 0;
98
99 int musictype = 0;
100
101 XYZ mapcenter;
102 float mapradius = 0;
103
104 Text *text = NULL;
105 float fps = 0;
106
107 bool editorenabled = 0;
108 int editortype = 0;
109 float editorsize = 0;
110 float editoryaw = 0;
111 float editorpitch = 0;
112
113 int tryquit = 0;
114
115 XYZ pathpoint[30];
116 int numpathpoints = 0;
117 int numpathpointconnect[30] = {};
118 int pathpointconnect[30][30] = {};
119 int pathpointselected = 0;
120
121 int endgame = 0;
122 bool scoreadded = 0;
123 int numchallengelevels = 0;
124
125 bool console = false;
126 char consoletext[15][256] = {};
127 int consolechars[15] = {};
128 bool chatting = 0;
129 char displaytext[15][256] = {};
130 int displaychars[15] = {};
131 float displaytime[15] = {};
132 float displayblinkdelay = 0;
133 bool displayblink = 0;
134 int displayselected = 0;
135 float consoleblinkdelay = 0;
136 bool consoleblink = 0;
137 int consoleselected = 0;
138
139 unsigned short crouchkey = 0, jumpkey = 0, forwardkey = 0, chatkey = 0, backkey = 0, leftkey = 0, rightkey = 0, drawkey = 0, throwkey = 0, attackkey = 0;
140 unsigned short consolekey = 0;
141
142 int loading = 0;
143
144 int oldenvironment = 0;
145 int targetlevel = 0;
146 float changedelay = 0;
147
148 bool waiting = false;
149 Account* accountactive = NULL;
150 }
151
152 void Game::fireSound(int sound)
153 {
154     emit_sound_at(sound);
155 }
156
157 void Game::inputText(char* str, int* charselected, int* nb_chars)
158 {
159     SDL_Event evenement;
160
161     if (!waiting) {
162         waiting = true;
163         SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
164         SDL_EnableUNICODE(true);
165     }
166
167     while (SDL_PollEvent(&evenement)) {
168
169         switch (evenement.type) {
170         case SDL_KEYDOWN:
171             if (evenement.key.keysym.sym == SDLK_ESCAPE) {
172                 for (int i = 0; i < 255; i++)
173                     str[i] = 0;
174                 *nb_chars = 0;
175                 *charselected = 0;
176                 waiting = false;
177             } else if (evenement.key.keysym.sym == SDLK_BACKSPACE) {
178                 if ((*charselected) != 0) {
179                     for (int i = (*charselected) - 1; i < 255; i++)
180                         str[i] = str[i + 1];
181                     str[255] = 0;
182                     (*charselected)--;
183                     (*nb_chars)--;
184                 }
185             } else if (evenement.key.keysym.sym == SDLK_DELETE) {
186                 if ((*charselected) < (*nb_chars)) {
187                     for (int i = (*charselected); i < 255; i++)
188                         str[i] = str[i + 1];
189                     str[255] = 0;
190                     (*nb_chars)--;
191                 }
192             } else if (evenement.key.keysym.sym == SDLK_HOME) {
193                 (*charselected) = 0;
194             } else if (evenement.key.keysym.sym == SDLK_END) {
195                 (*charselected) = (*nb_chars);
196             } else if (evenement.key.keysym.sym == SDLK_LEFT) {
197                 if ((*charselected) != 0)
198                     (*charselected)--;
199             } else if (evenement.key.keysym.sym == SDLK_RIGHT) {
200                 if ((*charselected) < (*nb_chars))
201                     (*charselected)++;
202             } else if (evenement.key.keysym.sym == SDLK_RETURN) {
203                 waiting = false;
204             } else if (evenement.key.keysym.unicode >= 32 && evenement.key.keysym.unicode < 127 && (*nb_chars) < 60) {
205                 for (int i = 255; i >= (*charselected) + 1; i--)
206                     str[i] = str[i - 1];
207                 str[*charselected] = evenement.key.keysym.unicode;
208                 (*charselected)++;
209                 (*nb_chars)++;
210             }
211             break;
212         }
213     }
214
215     if (!waiting) {
216         SDL_EnableKeyRepeat(0, 0); // disable key repeat
217         SDL_EnableUNICODE(false);
218     }
219 }
220
221 int setKeySelected_thread(void* data)
222 {
223     using namespace Game;
224     int keycode = -1;
225     SDL_Event evenement;
226     while (keycode == -1) {
227         SDL_WaitEvent(&evenement);
228         switch (evenement.type) {
229         case SDL_KEYDOWN:
230             keycode = evenement.key.keysym.sym;
231             break;
232         case SDL_MOUSEBUTTONDOWN:
233             keycode = SDLK_LAST + evenement.button.button;
234             break;
235         default:
236             break;
237         }
238     }
239     if (keycode != SDLK_ESCAPE) {
240         fireSound();
241         switch (keyselect) {
242         case 0:
243             forwardkey = keycode;
244             break;
245         case 1:
246             backkey = keycode;
247             break;
248         case 2:
249             leftkey = keycode;
250             break;
251         case 3:
252             rightkey = keycode;
253             break;
254         case 4:
255             crouchkey = keycode;
256             break;
257         case 5:
258             jumpkey = keycode;
259             break;
260         case 6:
261             drawkey = keycode;
262             break;
263         case 7:
264             throwkey = keycode;
265             break;
266         case 8:
267             attackkey = keycode;
268             break;
269         case 9:
270             consolekey = keycode;
271             break;
272         default:
273             break;
274         }
275     }
276     keyselect = -1;
277     waiting = false;
278     LoadMenu();
279     return 0;
280 }
281
282 void Game::setKeySelected()
283 {
284     waiting = true;
285     printf("launch thread\n");
286     SDL_Thread* thread = SDL_CreateThread(setKeySelected_thread, NULL);
287     if ( thread == NULL ) {
288         fprintf(stderr, "Unable to create thread: %s\n", SDL_GetError());
289         waiting = false;
290         return;
291     }
292 }