1 | /* |
---|
2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
3 | * > www.orxonox.net < |
---|
4 | * |
---|
5 | * |
---|
6 | * License notice: |
---|
7 | * |
---|
8 | * This program is free software; you can redistribute it and/or |
---|
9 | * modify it under the terms of the GNU General Public License |
---|
10 | * as published by the Free Software Foundation; either version 2 |
---|
11 | * of the License, or (at your option) any later version. |
---|
12 | * |
---|
13 | * This program is distributed in the hope that it will be useful, |
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
16 | * GNU General Public License for more details. |
---|
17 | * |
---|
18 | * You should have received a copy of the GNU General Public License |
---|
19 | * along with this program; if not, write to the Free Software |
---|
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
21 | * |
---|
22 | * Author: |
---|
23 | * Reto Grieder |
---|
24 | * Co-authors: |
---|
25 | * Benjamin Knecht <beni_at_orxonox.net>, (C) 2007 |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | /** |
---|
30 | @file |
---|
31 | @brief Orxonox Main Class |
---|
32 | */ |
---|
33 | |
---|
34 | // Precompiled Headers |
---|
35 | #include "OrxonoxStableHeaders.h" |
---|
36 | #include "Orxonox.h" |
---|
37 | |
---|
38 | //****** STD ******* |
---|
39 | #include <deque> |
---|
40 | |
---|
41 | //****** OGRE ****** |
---|
42 | #include <OgreFrameListener.h> |
---|
43 | #include <OgreOverlay.h> |
---|
44 | #include <OgreOverlayManager.h> |
---|
45 | #include <OgreRoot.h> |
---|
46 | #include <OgreTimer.h> |
---|
47 | #include <OgreWindowEventUtilities.h> |
---|
48 | |
---|
49 | //***** ORXONOX **** |
---|
50 | // util |
---|
51 | //#include "util/Sleep.h" |
---|
52 | #include "util/ArgReader.h" |
---|
53 | |
---|
54 | // core |
---|
55 | #include "core/ConfigFileManager.h" |
---|
56 | #include "core/ConsoleCommand.h" |
---|
57 | #include "core/Debug.h" |
---|
58 | #include "core/Loader.h" |
---|
59 | #include "core/input/InputManager.h" |
---|
60 | #include "core/TclBind.h" |
---|
61 | #include "core/Core.h" |
---|
62 | |
---|
63 | // audio |
---|
64 | #include "audio/AudioManager.h" |
---|
65 | |
---|
66 | // network |
---|
67 | #include "network/Server.h" |
---|
68 | #include "network/Client.h" |
---|
69 | |
---|
70 | // objects and tools |
---|
71 | #include "hud/HUD.h" |
---|
72 | #include "objects/Tickable.h" |
---|
73 | #include "tools/ParticleInterface.h" |
---|
74 | |
---|
75 | #include "GraphicsEngine.h" |
---|
76 | #include "Settings.h" |
---|
77 | |
---|
78 | // FIXME: is this really file scope? |
---|
79 | // globals for the server or client |
---|
80 | network::Client *client_g = 0; |
---|
81 | network::Server *server_g = 0; |
---|
82 | |
---|
83 | namespace orxonox |
---|
84 | { |
---|
85 | SetConsoleCommandShortcut(Orxonox, exit).setKeybindMode(KeybindMode::OnPress); |
---|
86 | SetConsoleCommandShortcut(Orxonox, slomo).setAccessLevel(AccessLevel::Offline).setDefaultValue(0, 1.0).setAxisParamIndex(0).setIsAxisRelative(false); |
---|
87 | SetConsoleCommandShortcut(Orxonox, setTimeFactor).setAccessLevel(AccessLevel::Offline).setDefaultValue(0, 1.0); |
---|
88 | |
---|
89 | /** |
---|
90 | @brief Reference to the only instance of the class. |
---|
91 | */ |
---|
92 | Orxonox *Orxonox::singletonRef_s = 0; |
---|
93 | |
---|
94 | /** |
---|
95 | * Create a new instance of Orxonox. Avoid doing any actual work here. |
---|
96 | */ |
---|
97 | Orxonox::Orxonox() : |
---|
98 | ogre_(0), |
---|
99 | //auMan_(0), |
---|
100 | timer_(0), |
---|
101 | // turn on frame smoothing by setting a value different from 0 |
---|
102 | frameSmoothingTime_(0.0f), |
---|
103 | orxonoxHUD_(0), |
---|
104 | bAbort_(false), |
---|
105 | timefactor_(1.0f), |
---|
106 | mode_(STANDALONE), |
---|
107 | serverIp_(""), |
---|
108 | serverPort_(NETWORK_PORT) |
---|
109 | { |
---|
110 | } |
---|
111 | |
---|
112 | /** |
---|
113 | * Destruct Orxonox. |
---|
114 | */ |
---|
115 | Orxonox::~Orxonox() |
---|
116 | { |
---|
117 | // keep in mind: the order of deletion is very important! |
---|
118 | this->orxonoxHUD_->destroy(); |
---|
119 | Loader::close(); |
---|
120 | InputManager::destroy(); |
---|
121 | //if (this->auMan_) |
---|
122 | // delete this->auMan_; |
---|
123 | if (this->timer_) |
---|
124 | delete this->timer_; |
---|
125 | GraphicsEngine::getSingleton().destroy(); |
---|
126 | |
---|
127 | if (network::Client::getSingleton()) |
---|
128 | network::Client::destroySingleton(); |
---|
129 | if (server_g) |
---|
130 | delete network::Server::getSingleton(); |
---|
131 | } |
---|
132 | |
---|
133 | |
---|
134 | /** |
---|
135 | Asks the mainloop nicely to abort. |
---|
136 | */ |
---|
137 | void Orxonox::abortRequest() |
---|
138 | { |
---|
139 | COUT(3) << "Orxonox: Abort requested." << std::endl; |
---|
140 | bAbort_ = true; |
---|
141 | } |
---|
142 | |
---|
143 | /** |
---|
144 | * @return singleton reference |
---|
145 | */ |
---|
146 | Orxonox* Orxonox::getSingleton() |
---|
147 | { |
---|
148 | if (!singletonRef_s) |
---|
149 | singletonRef_s = new Orxonox(); |
---|
150 | return singletonRef_s; |
---|
151 | } |
---|
152 | |
---|
153 | /** |
---|
154 | @brief Destroys the Orxonox singleton. |
---|
155 | */ |
---|
156 | void Orxonox::destroySingleton() |
---|
157 | { |
---|
158 | if (singletonRef_s) |
---|
159 | delete singletonRef_s; |
---|
160 | singletonRef_s = 0; |
---|
161 | } |
---|
162 | |
---|
163 | /** |
---|
164 | @brief Changes the speed of Orxonox |
---|
165 | */ |
---|
166 | void Orxonox::setTimeFactor(float factor) |
---|
167 | { |
---|
168 | float change = factor / Orxonox::getSingleton()->getTimeFactor(); |
---|
169 | Orxonox::getSingleton()->timefactor_ = factor; |
---|
170 | |
---|
171 | for (Iterator<ParticleInterface> it = ObjectList<ParticleInterface>::begin(); it; ++it) |
---|
172 | it->setSpeedFactor(it->getSpeedFactor() * change); |
---|
173 | } |
---|
174 | |
---|
175 | /** |
---|
176 | * initialization of Orxonox object |
---|
177 | * @param argc argument counter |
---|
178 | * @param argv list of argumenst |
---|
179 | * @param path path to config (in home dir or something) |
---|
180 | */ |
---|
181 | bool Orxonox::init(int argc, char **argv) |
---|
182 | { |
---|
183 | #ifdef _DEBUG |
---|
184 | ConfigFileManager::getSingleton()->setFile(CFT_Settings, "orxonox_d.ini"); |
---|
185 | #else |
---|
186 | ConfigFileManager::getSingleton()->setFile(CFT_Settings, "orxonox.ini"); |
---|
187 | #endif |
---|
188 | Factory::createClassHierarchy(); |
---|
189 | |
---|
190 | std::string mode; |
---|
191 | std::string tempDataPath; |
---|
192 | |
---|
193 | ArgReader ar(argc, argv); |
---|
194 | ar.checkArgument("mode", &mode, false); |
---|
195 | ar.checkArgument("data", &tempDataPath, false); |
---|
196 | ar.checkArgument("ip", &serverIp_, false); |
---|
197 | ar.checkArgument("port", &serverPort_, false); |
---|
198 | if(ar.errorHandling()) |
---|
199 | { |
---|
200 | COUT(1) << "Error while parsing command line arguments" << std::endl; |
---|
201 | COUT(1) << ar.getErrorString(); |
---|
202 | COUT(0) << "Usage:" << std::endl << "orxonox [mode client|server|dedicated|standalone] " |
---|
203 | << "[--data PATH] [--ip IP] [--port PORT]" << std::endl; |
---|
204 | return false; |
---|
205 | } |
---|
206 | |
---|
207 | if (mode == "client") |
---|
208 | mode_ = CLIENT; |
---|
209 | else if (mode == "server") |
---|
210 | mode_ = SERVER; |
---|
211 | else if (mode == "dedicated") |
---|
212 | mode_ = DEDICATED; |
---|
213 | else |
---|
214 | { |
---|
215 | if (mode == "") |
---|
216 | mode = "standalone"; |
---|
217 | if (mode != "standalone") |
---|
218 | { |
---|
219 | COUT(2) << "Warning: mode \"" << mode << "\" doesn't exist. " |
---|
220 | << "Defaulting to standalone" << std::endl; |
---|
221 | mode = "standalone"; |
---|
222 | } |
---|
223 | mode_ = STANDALONE; |
---|
224 | } |
---|
225 | COUT(3) << "Orxonox: Mode is " << mode << "." << std::endl; |
---|
226 | |
---|
227 | if (tempDataPath != "") |
---|
228 | { |
---|
229 | if (tempDataPath[tempDataPath.size() - 1] != '/') |
---|
230 | tempDataPath += "/"; |
---|
231 | Settings::tsetDataPath(tempDataPath); |
---|
232 | } |
---|
233 | |
---|
234 | // initialise TCL |
---|
235 | TclBind::getInstance().setDataPath(Settings::getDataPath()); |
---|
236 | |
---|
237 | //if (mode_ == DEDICATED) |
---|
238 | // TODO: decide what to do here |
---|
239 | //else |
---|
240 | |
---|
241 | // for playable server, client and standalone, the startup |
---|
242 | // procedure until the GUI is identical |
---|
243 | |
---|
244 | ogre_ = &GraphicsEngine::getSingleton(); |
---|
245 | if (!ogre_->setup()) // creates ogre root and other essentials |
---|
246 | return false; |
---|
247 | |
---|
248 | return true; |
---|
249 | } |
---|
250 | |
---|
251 | /** |
---|
252 | * start modules |
---|
253 | */ |
---|
254 | bool Orxonox::start() |
---|
255 | { |
---|
256 | if (mode_ == DEDICATED) |
---|
257 | { |
---|
258 | // do something else |
---|
259 | } |
---|
260 | else |
---|
261 | { // not dedicated server |
---|
262 | if (!ogre_->loadRenderer()) // creates the render window |
---|
263 | return false; |
---|
264 | |
---|
265 | // Calls the InputManager which sets up the input devices. |
---|
266 | // The render window width and height are used to set up the mouse movement. |
---|
267 | if (!InputManager::initialise(ogre_->getWindowHandle(), |
---|
268 | ogre_->getWindowWidth(), ogre_->getWindowHeight(), true, true, true)) |
---|
269 | return false; |
---|
270 | |
---|
271 | // TODO: Spread this so that this call only initialises things needed for the GUI |
---|
272 | if (!ogre_->initialiseResources()) |
---|
273 | return false; |
---|
274 | |
---|
275 | // TOOD: load the GUI here |
---|
276 | // set InputManager to GUI mode |
---|
277 | InputManager::setInputState(InputManager::IS_GUI); |
---|
278 | // TODO: run GUI here |
---|
279 | |
---|
280 | // The following lines depend very much on the GUI output, so they're probably misplaced here.. |
---|
281 | |
---|
282 | InputManager::setInputState(InputManager::IS_NONE); |
---|
283 | |
---|
284 | // create Ogre SceneManager |
---|
285 | ogre_->createNewScene(); |
---|
286 | |
---|
287 | if (!loadPlayground()) |
---|
288 | return false; |
---|
289 | } |
---|
290 | |
---|
291 | switch (mode_) |
---|
292 | { |
---|
293 | case SERVER: |
---|
294 | if (!serverLoad()) |
---|
295 | return false; |
---|
296 | break; |
---|
297 | case CLIENT: |
---|
298 | if (!clientLoad()) |
---|
299 | return false; |
---|
300 | break; |
---|
301 | case DEDICATED: |
---|
302 | if (!serverLoad()) |
---|
303 | return false; |
---|
304 | break; |
---|
305 | default: |
---|
306 | if (!standaloneLoad()) |
---|
307 | return false; |
---|
308 | } |
---|
309 | |
---|
310 | InputManager::setInputState(InputManager::IS_NORMAL); |
---|
311 | |
---|
312 | return startRenderLoop(); |
---|
313 | } |
---|
314 | |
---|
315 | /** |
---|
316 | * Loads everything in the scene except for the actual objects. |
---|
317 | * This includes HUD, Console.. |
---|
318 | */ |
---|
319 | bool Orxonox::loadPlayground() |
---|
320 | { |
---|
321 | // Init audio |
---|
322 | //auMan_ = new audio::AudioManager(); |
---|
323 | //auMan_->ambientAdd("a1"); |
---|
324 | //auMan_->ambientAdd("a2"); |
---|
325 | //auMan_->ambientAdd("a3"); |
---|
326 | //auMan->ambientAdd("ambient1"); |
---|
327 | //auMan_->ambientStart(); |
---|
328 | |
---|
329 | // Load the HUD |
---|
330 | COUT(3) << "Orxonox: Loading HUD..." << std::endl; |
---|
331 | orxonoxHUD_ = &HUD::getSingleton(); |
---|
332 | orxonoxHUD_->initialise(); |
---|
333 | return true; |
---|
334 | } |
---|
335 | |
---|
336 | /** |
---|
337 | * Level loading method for server mode. |
---|
338 | */ |
---|
339 | bool Orxonox::serverLoad() |
---|
340 | { |
---|
341 | COUT(2) << "Loading level in server mode" << std::endl; |
---|
342 | |
---|
343 | //server_g = new network::Server(serverPort_); |
---|
344 | server_g = network::Server::createSingleton(serverPort_); |
---|
345 | |
---|
346 | if (!loadScene()) |
---|
347 | return false; |
---|
348 | |
---|
349 | server_g->open(); |
---|
350 | |
---|
351 | return true; |
---|
352 | } |
---|
353 | |
---|
354 | /** |
---|
355 | * Level loading method for client mode. |
---|
356 | */ |
---|
357 | bool Orxonox::clientLoad() |
---|
358 | { |
---|
359 | COUT(2) << "Loading level in client mode" << std::endl;\ |
---|
360 | |
---|
361 | if (serverIp_.compare("") == 0) |
---|
362 | client_g = network::Client::createSingleton(); |
---|
363 | else |
---|
364 | |
---|
365 | client_g = network::Client::createSingleton(serverIp_, serverPort_); |
---|
366 | |
---|
367 | if(!client_g->establishConnection()) |
---|
368 | return false; |
---|
369 | client_g->tick(0); |
---|
370 | |
---|
371 | return true; |
---|
372 | } |
---|
373 | |
---|
374 | /** |
---|
375 | * Level loading method for standalone mode. |
---|
376 | */ |
---|
377 | bool Orxonox::standaloneLoad() |
---|
378 | { |
---|
379 | COUT(2) << "Loading level in standalone mode" << std::endl; |
---|
380 | |
---|
381 | if (!loadScene()) |
---|
382 | return false; |
---|
383 | |
---|
384 | return true; |
---|
385 | } |
---|
386 | |
---|
387 | /** |
---|
388 | * Helper method to load a level. |
---|
389 | */ |
---|
390 | bool Orxonox::loadScene() |
---|
391 | { |
---|
392 | Level* startlevel = new Level("levels/sample.oxw"); |
---|
393 | Loader::open(startlevel); |
---|
394 | |
---|
395 | return true; |
---|
396 | } |
---|
397 | |
---|
398 | |
---|
399 | /** |
---|
400 | Main loop of the orxonox game. |
---|
401 | About the loop: The design is almost exactly like the one in ogre, so that |
---|
402 | if any part of ogre registers a framelisteners, it will still behave |
---|
403 | correctly. Furthermore the time smoothing feature from ogre has been |
---|
404 | implemented too. If turned on (see orxonox constructor), it will calculate |
---|
405 | the dt_n by means of the recent most dt_n-1, dt_n-2, etc. |
---|
406 | */ |
---|
407 | bool Orxonox::startRenderLoop() |
---|
408 | { |
---|
409 | // first check whether ogre root object has been created |
---|
410 | if (Ogre::Root::getSingletonPtr() == 0) |
---|
411 | { |
---|
412 | COUT(2) << "Orxonox Error: Could not start rendering. No Ogre root object found" << std::endl; |
---|
413 | return false; |
---|
414 | } |
---|
415 | Ogre::Root& ogreRoot = Ogre::Root::getSingleton(); |
---|
416 | |
---|
417 | |
---|
418 | // Contains the times of recently fired events |
---|
419 | // eventTimes[4] is the list for the times required for the fps counter |
---|
420 | std::deque<unsigned long> eventTimes[3]; |
---|
421 | // Clear event times |
---|
422 | for (int i = 0; i < 3; ++i) |
---|
423 | eventTimes[i].clear(); |
---|
424 | |
---|
425 | // use the ogre timer class to measure time. |
---|
426 | if (!timer_) |
---|
427 | timer_ = new Ogre::Timer(); |
---|
428 | timer_->reset(); |
---|
429 | |
---|
430 | float renderTime = 0.0f; |
---|
431 | float frameTime = 0.0f; |
---|
432 | // clock_t time = 0; |
---|
433 | |
---|
434 | //Ogre::SceneManager* mSceneMgr = GraphicsEngine::getSingleton().getSceneManager(); |
---|
435 | //Ogre::Viewport* mViewport = mSceneMgr->getCurrentViewport(); |
---|
436 | |
---|
437 | //Ogre::CompositorManager::getSingleton().addCompositor(mViewport, "Bloom"); |
---|
438 | //Ogre::CompositorManager::getSingleton().addCompositor(mViewport, "MotionBlur"); |
---|
439 | |
---|
440 | COUT(3) << "Orxonox: Starting the main loop." << std::endl; |
---|
441 | while (!bAbort_) |
---|
442 | { |
---|
443 | // get current time |
---|
444 | unsigned long now = timer_->getMilliseconds(); |
---|
445 | |
---|
446 | // create an event to pass to the frameStarted method in ogre |
---|
447 | Ogre::FrameEvent evt; |
---|
448 | evt.timeSinceLastEvent = calculateEventTime(now, eventTimes[0]); |
---|
449 | evt.timeSinceLastFrame = calculateEventTime(now, eventTimes[1]); |
---|
450 | frameTime += evt.timeSinceLastFrame; |
---|
451 | |
---|
452 | // show the current time in the HUD |
---|
453 | // HUD::getSingleton().setTime(now); |
---|
454 | if (mode_ != DEDICATED && frameTime > 0.4f) |
---|
455 | { |
---|
456 | HUD::getSingleton().setRenderTimeRatio(renderTime / frameTime); |
---|
457 | frameTime = 0.0f; |
---|
458 | renderTime = 0.0f; |
---|
459 | } |
---|
460 | |
---|
461 | // tick the core |
---|
462 | Core::tick((float)evt.timeSinceLastFrame); |
---|
463 | // Call those objects that need the real time |
---|
464 | for (Iterator<TickableReal> it = ObjectList<TickableReal>::begin(); it; ++it) |
---|
465 | it->tick((float)evt.timeSinceLastFrame); |
---|
466 | // Call the scene objects |
---|
467 | for (Iterator<Tickable> it = ObjectList<Tickable>::begin(); it; ++it) |
---|
468 | it->tick((float)evt.timeSinceLastFrame * this->timefactor_); |
---|
469 | //AudioManager::tick(); |
---|
470 | if (client_g) |
---|
471 | client_g->tick((float)evt.timeSinceLastFrame); |
---|
472 | if (server_g) |
---|
473 | server_g->tick((float)evt.timeSinceLastFrame); |
---|
474 | |
---|
475 | // don't forget to call _fireFrameStarted in ogre to make sure |
---|
476 | // everything goes smoothly |
---|
477 | ogreRoot._fireFrameStarted(evt); |
---|
478 | |
---|
479 | // get current time |
---|
480 | now = timer_->getMilliseconds(); |
---|
481 | calculateEventTime(now, eventTimes[2]); |
---|
482 | |
---|
483 | if (mode_ != DEDICATED) |
---|
484 | { |
---|
485 | // Pump messages in all registered RenderWindows |
---|
486 | // This calls the WindowEventListener objects. |
---|
487 | Ogre::WindowEventUtilities::messagePump(); |
---|
488 | |
---|
489 | // render |
---|
490 | ogreRoot._updateAllRenderTargets(); |
---|
491 | } |
---|
492 | |
---|
493 | // get current time |
---|
494 | now = timer_->getMilliseconds(); |
---|
495 | |
---|
496 | // create an event to pass to the frameEnded method in ogre |
---|
497 | evt.timeSinceLastEvent = calculateEventTime(now, eventTimes[0]); |
---|
498 | renderTime += calculateEventTime(now, eventTimes[2]); |
---|
499 | |
---|
500 | // again, just to be sure ogre works fine |
---|
501 | ogreRoot._fireFrameEnded(evt); |
---|
502 | //msleep(200); |
---|
503 | } |
---|
504 | |
---|
505 | if (mode_ == CLIENT) |
---|
506 | network::Client::getSingleton()->closeConnection(); |
---|
507 | else if (mode_ == SERVER) |
---|
508 | server_g->close(); |
---|
509 | |
---|
510 | return true; |
---|
511 | } |
---|
512 | |
---|
513 | /** |
---|
514 | Method for calculating the average time between recently fired events. |
---|
515 | Code directly taken from OgreRoot.cc |
---|
516 | @param now The current time in ms. |
---|
517 | @param type The type of event to be considered. |
---|
518 | */ |
---|
519 | float Orxonox::calculateEventTime(unsigned long now, std::deque<unsigned long> ×) |
---|
520 | { |
---|
521 | // Calculate the average time passed between events of the given type |
---|
522 | // during the last frameSmoothingTime_ seconds. |
---|
523 | |
---|
524 | times.push_back(now); |
---|
525 | |
---|
526 | if(times.size() == 1) |
---|
527 | return 0; |
---|
528 | |
---|
529 | // Times up to frameSmoothingTime_ seconds old should be kept |
---|
530 | unsigned long discardThreshold = (unsigned long)(frameSmoothingTime_ * 1000.0f); |
---|
531 | |
---|
532 | // Find the oldest time to keep |
---|
533 | std::deque<unsigned long>::iterator it = times.begin(); |
---|
534 | // We need at least two times |
---|
535 | std::deque<unsigned long>::iterator end = times.end() - 2; |
---|
536 | |
---|
537 | while(it != end) |
---|
538 | { |
---|
539 | if (now - *it > discardThreshold) |
---|
540 | ++it; |
---|
541 | else |
---|
542 | break; |
---|
543 | } |
---|
544 | |
---|
545 | // Remove old times |
---|
546 | times.erase(times.begin(), it); |
---|
547 | |
---|
548 | return (float)(times.back() - times.front()) / ((times.size() - 1) * 1000); |
---|
549 | } |
---|
550 | } |
---|