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