1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2004 orx |
---|
5 | |
---|
6 | This program 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, or (at your option) |
---|
9 | any later version. |
---|
10 | |
---|
11 | ### File Specific: |
---|
12 | main-programmer: Benjamin Grauer |
---|
13 | co-programmer: ... |
---|
14 | */ |
---|
15 | |
---|
16 | #include "framework.h" |
---|
17 | |
---|
18 | |
---|
19 | #include "p_node.h" |
---|
20 | #include "null_parent.h" |
---|
21 | #include "state.h" |
---|
22 | #include "debug.h" |
---|
23 | #include "light.h" |
---|
24 | #include "resource_manager.h" |
---|
25 | #include "camera.h" |
---|
26 | #include "ini_parser.h" |
---|
27 | |
---|
28 | |
---|
29 | int verbose; |
---|
30 | |
---|
31 | void Framework::init(void) |
---|
32 | { |
---|
33 | // create parser |
---|
34 | |
---|
35 | char* configFileName = ResourceManager::homeDirCheck(DEFAULT_CONFIG_FILE); |
---|
36 | |
---|
37 | IniParser iniParser (configFileName); |
---|
38 | delete configFileName; |
---|
39 | |
---|
40 | GraphicsEngine::getInstance()->initFromIniFile(&iniParser); |
---|
41 | |
---|
42 | LightManager::getInstance(); |
---|
43 | |
---|
44 | const char* dataPath; |
---|
45 | if ((dataPath = iniParser.getVar(CONFIG_NAME_DATADIR, CONFIG_SECTION_DATA))!= NULL) |
---|
46 | { |
---|
47 | if (!ResourceManager::getInstance()->setDataDir(dataPath)) |
---|
48 | { |
---|
49 | PRINTF(1)("Data Could not be located\n"); |
---|
50 | exit(-1); |
---|
51 | } |
---|
52 | } |
---|
53 | |
---|
54 | if (!ResourceManager::getInstance()->checkDataDir(DEFAULT_DATA_DIR_CHECKFILE)) |
---|
55 | { |
---|
56 | PRINTF(1)("The DataDirectory %s could not be verified\n" \ |
---|
57 | " Please Change in File %s Section %s Entry %s to a suitable value\n", |
---|
58 | ResourceManager::getInstance()->getDataDir(), |
---|
59 | DEFAULT_CONFIG_FILE, |
---|
60 | CONFIG_SECTION_DATA, |
---|
61 | CONFIG_NAME_DATADIR); |
---|
62 | exit(-1); |
---|
63 | } |
---|
64 | } |
---|
65 | |
---|
66 | |
---|
67 | void* Framework::mainLoop(void* tmp) |
---|
68 | { |
---|
69 | Framework* framework = Framework::getInstance(); |
---|
70 | while(!framework->isFinished) |
---|
71 | { |
---|
72 | #ifdef GUI_MODULE |
---|
73 | while(gtk_events_pending()) |
---|
74 | gtk_main_iteration(); |
---|
75 | #endif |
---|
76 | // keyhandler returns false if sdl gets quit by some event |
---|
77 | framework->eventHandler(); |
---|
78 | |
---|
79 | // tick the scene |
---|
80 | float dt = framework->tick(); |
---|
81 | |
---|
82 | NullParent::getInstance()->update(dt); |
---|
83 | |
---|
84 | // Draw the scene |
---|
85 | framework->draw(dt); |
---|
86 | |
---|
87 | } |
---|
88 | } |
---|
89 | |
---|
90 | bool Framework::draw(float dt) |
---|
91 | { |
---|
92 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); |
---|
93 | glLoadIdentity(); // Reset the view |
---|
94 | |
---|
95 | this->moduleDraw(); |
---|
96 | |
---|
97 | camera->apply(); |
---|
98 | |
---|
99 | SDL_GL_SwapBuffers(); // Swap the buffers |
---|
100 | } |
---|
101 | |
---|
102 | |
---|
103 | float Framework::tick() |
---|
104 | { |
---|
105 | currFrame = SDL_GetTicks(); |
---|
106 | float dt = (float)(currFrame - lastFrame) / 1000.0; |
---|
107 | lastFrame = currFrame; |
---|
108 | |
---|
109 | this->moduleTick(dt); |
---|
110 | |
---|
111 | return dt; |
---|
112 | } |
---|
113 | |
---|
114 | |
---|
115 | bool Framework::eventHandler() |
---|
116 | { |
---|
117 | // This is the main loop for the entire program and it will run until done==TRUE |
---|
118 | { |
---|
119 | // And poll for events |
---|
120 | SDL_Event event; |
---|
121 | while(SDL_PollEvent(&event)) |
---|
122 | { |
---|
123 | moduleEventHandler(&event); |
---|
124 | |
---|
125 | switch (event.type) { |
---|
126 | case SDL_MOUSEMOTION: |
---|
127 | { |
---|
128 | Vector view = camera->getTarget()->getAbsCoor() - camera->getAbsCoor(); |
---|
129 | Vector up = Vector(0, 1, 0); |
---|
130 | up = camera->getAbsDir().apply(up); |
---|
131 | Vector h = up.cross(view); |
---|
132 | Vector v = h.cross(view); |
---|
133 | h.normalize(); |
---|
134 | v.normalize(); |
---|
135 | float distance = view.len(); |
---|
136 | |
---|
137 | Vector newCameraPos = camera->getAbsCoor(); |
---|
138 | Vector newTargetPos = camera->getTarget()->getAbsCoor(); |
---|
139 | int changed = 0; |
---|
140 | |
---|
141 | if (mouseDown[1]) |
---|
142 | { |
---|
143 | newCameraPos = camera->getRelCoor()+ (h * event.motion.xrel - v * event.motion.yrel) * .005 * distance; |
---|
144 | changed += 1; |
---|
145 | } |
---|
146 | if (mouseDown[3]) |
---|
147 | { |
---|
148 | newTargetPos = camera->getTarget()->getRelCoor() + (h * event.motion.xrel - v * event.motion.yrel) * .005 * distance; |
---|
149 | changed += 2; |
---|
150 | } |
---|
151 | |
---|
152 | Vector newView = newTargetPos - newCameraPos; |
---|
153 | |
---|
154 | if (changed == 1) |
---|
155 | camera->setRelCoor(newCameraPos + newView * (1- distance/newView.len())); |
---|
156 | else if (changed == 2) |
---|
157 | camera->getTarget()->setRelCoor(newTargetPos - newView * (1-distance/newView.len())); |
---|
158 | else if (changed == 3) |
---|
159 | { |
---|
160 | camera->setRelCoor(newCameraPos); |
---|
161 | camera->getTarget()->setRelCoor(newTargetPos); |
---|
162 | } |
---|
163 | |
---|
164 | } |
---|
165 | break; |
---|
166 | case SDL_MOUSEBUTTONDOWN: |
---|
167 | switch (event.button.button) |
---|
168 | { |
---|
169 | case 4: |
---|
170 | PRINTF(4)("MouseWheel up\n"); |
---|
171 | camera->setRelCoor(camera->getRelCoor() + (camera->getTarget()->getAbsCoor() - camera->getAbsCoor())*.1); |
---|
172 | break; |
---|
173 | case 5: |
---|
174 | PRINTF(4)("MouseWheel down\n"); |
---|
175 | camera->setRelCoor(camera->getRelCoor() - (camera->getTarget()->getAbsCoor() - camera->getAbsCoor())*.1); |
---|
176 | break; |
---|
177 | case 1: |
---|
178 | case 2: |
---|
179 | case 3: |
---|
180 | mouseDown[event.button.button] = true; |
---|
181 | break; |
---|
182 | } |
---|
183 | |
---|
184 | break; |
---|
185 | case SDL_MOUSEBUTTONUP: |
---|
186 | switch (event.button.button) |
---|
187 | { |
---|
188 | case 1: |
---|
189 | case 2: |
---|
190 | case 3: |
---|
191 | mouseDown[event.button.button] = false; |
---|
192 | break; |
---|
193 | } |
---|
194 | break; |
---|
195 | case SDL_VIDEORESIZE: |
---|
196 | GraphicsEngine::getInstance()->resolutionChanged(event.resize); |
---|
197 | break; |
---|
198 | case SDL_KEYDOWN: |
---|
199 | switch (event.key.keysym.sym) |
---|
200 | { |
---|
201 | case SDLK_q: |
---|
202 | case SDLK_ESCAPE: |
---|
203 | #ifdef GUI_MODULE |
---|
204 | quitGui(NULL, NULL); |
---|
205 | #else |
---|
206 | this->quit(); |
---|
207 | #endif |
---|
208 | break; |
---|
209 | case SDLK_a: |
---|
210 | camera->setRelCoor(camera->getRelCoor() + (camera->getTarget()->getAbsCoor() - camera->getAbsCoor())*.1); |
---|
211 | break; |
---|
212 | case SDLK_z: |
---|
213 | camera->setRelCoor(camera->getRelCoor() - (camera->getTarget()->getAbsCoor() - camera->getAbsCoor())*.1); |
---|
214 | break; |
---|
215 | case SDLK_r: |
---|
216 | camera->setAbsCoor(Vector(10, 10, 10)); |
---|
217 | camera->getTarget()->setAbsCoor(Vector()); |
---|
218 | break; |
---|
219 | case SDLK_h: |
---|
220 | this->printHelp(); |
---|
221 | break; |
---|
222 | case SDLK_c: |
---|
223 | for (int i = 0; i < 3; i++) |
---|
224 | { |
---|
225 | backgroundColor[i] += .1; |
---|
226 | if (backgroundColor[i] > 1.0) |
---|
227 | backgroundColor[i] = 1.0; |
---|
228 | GraphicsEngine::setBackgroundColor(backgroundColor[0], backgroundColor[1], backgroundColor[2], backgroundColor[3]); |
---|
229 | } |
---|
230 | break; |
---|
231 | case SDLK_x: |
---|
232 | for (int i = 0; i < 3; i++) |
---|
233 | { |
---|
234 | backgroundColor[i] -= .1; |
---|
235 | if (backgroundColor[i] < 0.0) |
---|
236 | backgroundColor[i] = 0.0; |
---|
237 | GraphicsEngine::setBackgroundColor(backgroundColor[0], backgroundColor[1], backgroundColor[2], backgroundColor[3]); |
---|
238 | } |
---|
239 | break; |
---|
240 | } |
---|
241 | break; |
---|
242 | |
---|
243 | // If a quit event was recieved |
---|
244 | case SDL_QUIT: |
---|
245 | // then we're done and we'll end this program |
---|
246 | #ifdef GUI_MODULE |
---|
247 | quitGui(NULL, NULL); |
---|
248 | #else |
---|
249 | this->quit(); |
---|
250 | #endif |
---|
251 | break; |
---|
252 | default: |
---|
253 | break; |
---|
254 | } |
---|
255 | |
---|
256 | } |
---|
257 | |
---|
258 | // Get the state of the keyboard keys |
---|
259 | keys = SDL_GetKeyState(NULL); |
---|
260 | |
---|
261 | // and check if ESCAPE has been pressed. If so then quit |
---|
262 | if(keys[SDLK_ESCAPE]) return false; |
---|
263 | } |
---|
264 | return true; |
---|
265 | } |
---|
266 | |
---|
267 | void Framework::quit(void) |
---|
268 | { |
---|
269 | this->isFinished = true; |
---|
270 | } |
---|
271 | |
---|
272 | Framework* Framework::singletonRef = NULL; |
---|
273 | |
---|
274 | Framework::Framework() |
---|
275 | { |
---|
276 | this->init(); |
---|
277 | |
---|
278 | camera = new Camera(); |
---|
279 | State::setCamera(camera, camera->getTarget()); |
---|
280 | camera->setAbsCoor(Vector(10, 10, 10)); |
---|
281 | |
---|
282 | this->isFinished = false; |
---|
283 | |
---|
284 | this->lastFrame = 0; |
---|
285 | |
---|
286 | |
---|
287 | // Build the font from a TGA image font.tga in the data directory |
---|
288 | // Hide the mouse cursor |
---|
289 | SDL_ShowCursor(2); |
---|
290 | |
---|
291 | for (int i = 0; i < MOUSE_BUTTON_COUNT; i++) |
---|
292 | mouseDown[i] = false; |
---|
293 | for (int i = 0; i < 4; i++) |
---|
294 | backgroundColor[i] = 0; |
---|
295 | } |
---|
296 | |
---|
297 | Framework::~Framework() |
---|
298 | { |
---|
299 | delete GraphicsEngine::getInstance(); |
---|
300 | |
---|
301 | } |
---|
302 | |
---|
303 | |
---|
304 | |
---|
305 | void Framework::printHelp(void) const |
---|
306 | { |
---|
307 | PRINT(0)(" Help for the frameWork\n"); |
---|
308 | PRINT(0)("========================\n"); |
---|
309 | PRINT(0)("h - print this Help\n"); |
---|
310 | PRINT(0)("a - zoom in\n"); |
---|
311 | PRINT(0)("z - zoom out\n"); |
---|
312 | PRINT(0)("r - reset camera position\n"); |
---|
313 | PRINT(0)("x - background color darker\n"); |
---|
314 | PRINT(0)("c - background color brighter\n"); |
---|
315 | |
---|
316 | |
---|
317 | PRINT(0)("\n"); |
---|
318 | PRINT(0)("mouse wheel - zoom\n"); |
---|
319 | PRINT(0)("mouse left button - rotate the camera around its target\n"); |
---|
320 | PRINT(0)("mouse right button - rotate the camera's target around the camera\n"); |
---|
321 | PRINT(0)("mouse left-and-right button - move the camera and the target\n"); |
---|
322 | |
---|
323 | this->moduleHelp(); |
---|
324 | |
---|
325 | } |
---|
326 | |
---|
327 | #ifdef GUI_MODULE |
---|
328 | int quitGui(GtkWidget* widget, void* data) |
---|
329 | { |
---|
330 | #ifdef HAVE_GTK2 |
---|
331 | while(gtk_events_pending()) gtk_main_iteration(); |
---|
332 | Framework::getInstance()->quit(); |
---|
333 | #endif /* HAVE_GTK2 */ |
---|
334 | } |
---|
335 | #endif |
---|
336 | |
---|
337 | int main(int argc, char *argv[]) |
---|
338 | { |
---|
339 | verbose = 3; |
---|
340 | |
---|
341 | Framework* framework = Framework::getInstance(); |
---|
342 | |
---|
343 | framework->moduleInit(argc, argv); |
---|
344 | #ifdef GUI_MODULE |
---|
345 | framework->moduleInitGui(argc, argv); |
---|
346 | #endif |
---|
347 | framework->mainLoop(NULL); |
---|
348 | |
---|
349 | delete framework; |
---|
350 | // Kill the GL & SDL screens |
---|
351 | // And quit |
---|
352 | return 0; |
---|
353 | } |
---|