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