Changeset 1104
- Timestamp:
- Apr 17, 2008, 3:52:38 PM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/network2/src/orxonox/Orxonox.cc
r1103 r1104 1 1 /* 2 2 * ORXONOX - the hottest 3D action shooter ever to exist 3 * > www.orxonox.net <4 3 * 5 4 * … … 28 27 29 28 /** 30 @file 29 @file Orxonox.cc 31 30 @brief Orxonox Main Class 32 31 */ … … 34 33 // Precompiled Headers 35 34 #include "OrxonoxStableHeaders.h" 36 #include "Orxonox.h"37 38 //****** STD *******39 //#include <iostream>40 //#include <exception>41 #include <deque>42 35 43 36 //****** OGRE ****** … … 46 39 #include <OgreOverlay.h> 47 40 #include <OgreOverlayManager.h> 48 #include <OgreRoot.h>49 41 #include <OgreTimer.h> 50 42 #include <OgreWindowEventUtilities.h> 51 43 44 //****** STD ******* 45 //#include <iostream> 46 //#include <exception> 47 #include <deque> 48 52 49 //***** ORXONOX **** 53 // util50 //misc 54 51 //#include "util/Sleep.h" 55 #include "util/ArgReader.h" 56 57 // core 58 #include "core/ConfigFileManager.h" 59 #include "core/ConsoleCommand.h" 52 53 // audio 54 #include "audio/AudioManager.h" 55 56 // network 57 #include "network/Server.h" 58 #include "network/Client.h" 59 network::Client *client_g; 60 network::Server *server_g; 61 62 // objects 63 #include "core/ArgReader.h" 60 64 #include "core/Debug.h" 61 65 #include "core/Factory.h" 62 66 #include "core/Loader.h" 63 67 #include "core/Tickable.h" 64 #include "core/InputBuffer.h" 65 #include "core/InputManager.h" 66 67 // audio 68 #include "audio/AudioManager.h" 69 70 // network 71 #include "network/Server.h" 72 #include "network/Client.h" 73 74 // objects and tools 68 #include "hud/HUD.h" 75 69 #include "tools/Timer.h" 76 #include "hud/HUD.h" 77 78 // FIXME: is this really file scope? 79 // globals for the server or client 80 network::Client *client_g; 81 network::Server *server_g; 70 #include "objects/weapon/BulletManager.h" 71 72 #include "InputHandler.h" 73 74 #include "Orxonox.h" 82 75 83 76 namespace orxonox 84 77 { 85 ConsoleCommand(Orxonox, exit, AccessLevel::None, true);86 ConsoleCommand(Orxonox, slomo, AccessLevel::Offline, true).setDefaultValue(0, 1.0);87 ConsoleCommand(Orxonox, setTimeFactor, AccessLevel::Offline, false).setDefaultValue(0, 1.0);88 89 class Testconsole : public InputBufferListener90 {91 public:92 Testconsole(InputBuffer* ib) : ib_(ib) {}93 void listen() const94 {95 std::cout << "> " << this->ib_->get() << std::endl;96 }97 void execute() const98 {99 std::cout << ">> " << this->ib_->get() << std::endl;100 if (!CommandExecutor::execute(this->ib_->get()))101 std::cout << "Error" << std::endl;102 this->ib_->clear();103 }104 void hintandcomplete() const105 {106 std::cout << CommandExecutor::hint(this->ib_->get()) << std::endl;107 this->ib_->set(CommandExecutor::complete(this->ib_->get()));108 }109 void clear() const110 {111 this->ib_->clear();112 }113 void removeLast() const114 {115 this->ib_->removeLast();116 }117 void exit() const118 {119 CommandExecutor::execute("setInputMode 2");120 }121 122 private:123 InputBuffer* ib_;124 };125 126 78 /** 127 79 @brief Reference to the only instance of the class. … … 134 86 Orxonox::Orxonox() 135 87 { 136 this->ogre_ = &GraphicsEngine::getSingleton();88 this->ogre_ = new GraphicsEngine(); 137 89 this->timer_ = 0; 138 90 this->dataPath_ = ""; 139 91 this->auMan_ = 0; 140 92 this->inputHandler_ = 0; 93 //this->root_ = 0; 141 94 // turn on frame smoothing by setting a value different from 0 142 95 this->frameSmoothingTime_ = 0.0f; 143 96 this->bAbort_ = false; 144 this->timefactor_ = 1.0f;145 97 } 146 98 … … 151 103 { 152 104 // keep in mind: the order of deletion is very important! 105 if (this->bulletMgr_) 106 delete this->bulletMgr_; 153 107 if (this->orxonoxHUD_) 154 108 delete this->orxonoxHUD_; 155 109 Loader::close(); 156 Input Manager::getSingleton().destroy();110 InputHandler::destroy(); 157 111 if (this->auMan_) 158 112 delete this->auMan_; 159 113 if (this->timer_) 160 114 delete this->timer_; 161 GraphicsEngine::getSingleton().destroy(); 115 if (this->ogre_) 116 delete this->ogre_; 162 117 163 118 if (client_g) … … 168 123 169 124 /** 170 @brief Immediately deletes the orxonox object. 171 Never use if you can help it while rendering! 172 */ 173 void Orxonox::abortImmediateForce() 174 { 175 COUT(1) << "*** Orxonox Error: Orxonox object was unexpectedly destroyed." << std::endl; 125 * error kills orxonox 126 */ 127 void Orxonox::abortImmediate(/* some error code */) 128 { 129 //TODO: destroy and destruct everything and print nice error msg 176 130 delete this; 177 131 } … … 182 136 void Orxonox::abortRequest() 183 137 { 184 COUT(3) << "*** Orxonox: Abort requested." << std::endl;185 138 bAbort_ = true; 186 139 } … … 194 147 singletonRef_s = new Orxonox(); 195 148 return singletonRef_s; 149 //static Orxonox theOnlyInstance; 150 //return &theOnlyInstance; 196 151 } 197 152 … … 199 154 @brief Destroys the Orxonox singleton. 200 155 */ 201 void Orxonox::destroy Singleton()156 void Orxonox::destroy() 202 157 { 203 158 if (singletonRef_s) … … 219 174 std::string mode; 220 175 221 ArgReader ar (argc, argv);176 ArgReader ar = ArgReader(argc, argv); 222 177 ar.checkArgument("mode", mode, false); 223 178 ar.checkArgument("data", this->dataPath_, false); 224 179 ar.checkArgument("ip", serverIp_, false); 225 if(ar.errorHandling()) abortImmediate Force();180 if(ar.errorHandling()) abortImmediate(); 226 181 if(mode == std::string("client")) 227 182 { … … 242 197 { 243 198 COUT(2) << "initialising server" << std::endl; 244 199 245 200 ogre_->setConfigPath(path); 246 201 ogre_->setup(); 247 202 //root_ = ogre_->getRoot(); 248 if(!ogre_->load(this->dataPath_)) abortImmediate Force(/* unable to load */);249 203 if(!ogre_->load(this->dataPath_)) abortImmediate(/* unable to load */); 204 250 205 server_g = new network::Server(); 251 206 } … … 254 209 { 255 210 COUT(2) << "initialising client" << std::endl;\ 256 211 257 212 ogre_->setConfigPath(path); 258 213 ogre_->setup(); … … 261 216 else 262 217 client_g = new network::Client(serverIp_, NETWORK_PORT); 263 if(!ogre_->load(this->dataPath_)) abortImmediate Force(/* unable to load */);264 } 265 218 if(!ogre_->load(this->dataPath_)) abortImmediate(/* unable to load */); 219 } 220 266 221 void Orxonox::standaloneInit(std::string path) 267 222 { 268 223 COUT(2) << "initialising standalone mode" << std::endl; 269 224 270 225 ogre_->setConfigPath(path); 271 226 ogre_->setup(); 272 if(!ogre_->load(this->dataPath_)) abortImmediateForce(/* unable to load */); 273 } 274 227 //root_ = ogre_->getRoot(); 228 if(!ogre_->load(this->dataPath_)) abortImmediate(/* unable to load */); 229 } 230 275 231 /** 276 232 * start modules … … 289 245 } 290 246 } 291 247 292 248 void Orxonox::clientStart(){ 293 249 ogre_->initialise(); 294 ConfigFileManager::getSingleton()->setFile(CFT_Settings, "orxonox.ini");295 250 Factory::createClassHierarchy(); 296 297 251 252 298 253 auMan_ = new audio::AudioManager(); 299 254 255 bulletMgr_ = new BulletManager(); 256 300 257 Ogre::Overlay* hudOverlay = Ogre::OverlayManager::getSingleton().getByName("Orxonox/HUD1.2"); 301 258 orxonoxHUD_ = new HUD(); … … 303 260 orxonoxHUD_->setEnergyDistr(20,20,60); 304 261 hudOverlay->show(); 305 262 306 263 if( !client_g->establishConnection() ) 307 COUT(1) <<"CLIENT COULD NOT ESTABLISH CONNECTION " << std::endl;264 COUT(1) <<"CLIENT COULD NOT ESTABLISH CONNECTION :-)" << std::endl; 308 265 client_g->tick(0); 309 310 266 267 311 268 //setupInputSystem(); 312 269 313 270 startRenderLoop(); 314 271 } 315 272 316 273 void Orxonox::serverStart(){ 317 274 //TODO: start modules 318 275 ogre_->initialise(); 319 276 //TODO: run engine 320 ConfigFileManager::getSingleton()->setFile(CFT_Settings, "orxonox.ini");321 277 Factory::createClassHierarchy(); 322 278 createScene(); 323 279 setupInputSystem(); 324 280 325 281 server_g->open(); 326 282 327 283 startRenderLoop(); 328 284 } 329 285 330 286 void Orxonox::standaloneStart(){ 331 287 //TODO: start modules 332 288 ogre_->initialise(); 333 289 //TODO: run engine 334 ConfigFileManager::getSingleton()->setFile(CFT_Settings, "orxonox.ini");335 290 Factory::createClassHierarchy(); 336 291 createScene(); 337 292 setupInputSystem(); 338 293 339 294 startRenderLoop(); 340 295 } 341 296 342 void Orxonox::createScene( )297 void Orxonox::createScene(void) 343 298 { 344 299 // Init audio 345 300 auMan_ = new audio::AudioManager(); 301 302 bulletMgr_ = new BulletManager(); 346 303 347 304 // load this file from config … … 370 327 void Orxonox::setupInputSystem() 371 328 { 372 inputHandler_ = &InputManager::getSingleton();329 inputHandler_ = InputHandler::getSingleton(); 373 330 if (!inputHandler_->initialise(ogre_->getWindowHandle(), 374 331 ogre_->getWindowWidth(), ogre_->getWindowHeight())) 375 abortImmediateForce(); 376 inputHandler_->setInputMode(IM_INGAME); 332 abortImmediate(); 377 333 } 378 334 … … 387 343 About the loop: The design is almost exactly like the one in ogre, so that 388 344 if any part of ogre registers a framelisteners, it will still behave 389 correctly. Furthermore the time smoothing feature from ogre has been390 implemented too. If turned on (see orxonox constructor), it will calculate391 the dt_n bymeans of the recent most dt_n-1, dt_n-2, etc.345 correctly. Furthermore I have taken over the time smoothing feature from 346 ogre. If turned on (see orxonox constructor), it will calculate the dt_n by 347 means of the recent most dt_n-1, dt_n-2, etc. 392 348 */ 393 349 void Orxonox::startRenderLoop() 394 350 { 395 InputBuffer* ib = new InputBuffer();396 InputManager::getSingleton().feedInputBuffer(ib);397 Testconsole* console = new Testconsole(ib);398 ib->registerListener(console, &Testconsole::listen, true);399 ib->registerListener(console, &Testconsole::execute, '\r', false);400 ib->registerListener(console, &Testconsole::execute, '\n', false);401 ib->registerListener(console, &Testconsole::hintandcomplete, '\t', true);402 ib->registerListener(console, &Testconsole::clear, '§', true);403 ib->registerListener(console, &Testconsole::removeLast, '\b', true);404 ib->registerListener(console, &Testconsole::exit, (char)0x1B, true);405 406 // first check whether ogre root object has been created407 if (Ogre::Root::getSingletonPtr() == 0)408 {409 COUT(2) << "*** Orxonox Error: Could not start rendering. No Ogre root object found" << std::endl;410 return;411 }412 413 // Contains the times of recently fired events414 // eventTimes[4] is the list for the times required for the fps counter415 std::deque<unsigned long> eventTimes[4];416 // Clear event times417 for (int i = 0; i < 4; ++i)418 eventTimes[i].clear();419 // fill the fps time list with zeros420 for (int i = 0; i < 20; i++)421 eventTimes[3].push_back(0);422 423 351 // use the ogre timer class to measure time. 424 352 if (!timer_) … … 426 354 timer_->reset(); 427 355 356 // Contains the times of recently fired events 357 std::deque<unsigned long> eventTimes[3]; 358 // Clear event times 359 for (int i = 0; i < 3; ++i) 360 eventTimes[i].clear(); 361 428 362 while (!bAbort_) 429 363 { … … 433 367 // get current time 434 368 unsigned long now = timer_->getMilliseconds(); 435 eventTimes[3].push_back(now);436 eventTimes[3].erase(eventTimes[3].begin());437 369 438 370 // create an event to pass to the frameStarted method in ogre … … 442 374 443 375 // show the current time in the HUD 444 orxonoxHUD_->setTime((int)now, 0); 445 if (eventTimes[3].back() - eventTimes[3].front() != 0) 446 orxonoxHUD_->setRocket1((int)(20000.0f/(eventTimes[3].back() - eventTimes[3].front()))); 376 //orxonoxHUD_->setTime((int)now, 0); 447 377 448 378 // Iterate through all Tickables and call their tick(dt) function 449 for (Iterator<Tickable> it = ObjectList<Tickable>::start(); it; ++it)450 it->tick((float)evt.timeSinceLastFrame * this->timefactor_);379 for (Iterator<Tickable> it = ObjectList<Tickable>::start(); it; ) 380 (it++)->tick((float)evt.timeSinceLastFrame); 451 381 452 382 // don't forget to call _fireFrameStarted in ogre to make sure 453 383 // everything goes smoothly 454 Ogre::Root::getSingleton()._fireFrameStarted(evt); 455 456 // server still renders at the moment 384 ogre_->frameStarted(evt); 385 457 386 //if (mode_ != SERVER) 458 Ogre::Root::getSingleton()._updateAllRenderTargets(); // only render in non-server mode387 ogre_->renderOneFrame(); // only render in non-server mode 459 388 460 389 // get current time … … 466 395 467 396 // again, just to be sure ogre works fine 468 Ogre::Root::getSingleton()._fireFrameEnded(evt);397 ogre_->frameEnded(evt); 469 398 } 470 399 } … … 479 408 { 480 409 // Calculate the average time passed between events of the given type 481 // during the last frameSmoothingTime_seconds.410 // during the last mFrameSmoothingTime seconds. 482 411 483 412 times.push_back(now); … … 486 415 return 0; 487 416 488 // Times up to frameSmoothingTime_ seconds old should be kept 489 unsigned long discardThreshold = (unsigned long)(frameSmoothingTime_ * 1000.0f); 417 // Times up to mFrameSmoothingTime seconds old should be kept 418 unsigned long discardThreshold = 419 static_cast<unsigned long>(frameSmoothingTime_ * 1000.0f); 490 420 491 421 // Find the oldest time to keep 492 std::deque<unsigned long>::iterator it = times.begin(); 493 // We need at least two times 494 std::deque<unsigned long>::iterator end = times.end() - 2; 495 422 std::deque<unsigned long>::iterator it = times.begin(), 423 end = times.end()-2; // We need at least two times 496 424 while(it != end) 497 425 { … … 505 433 times.erase(times.begin(), it); 506 434 507 return (float)(times.back() - times.front()) / ((times.size() -1) * 1000);435 return (float)(times.back() - times.front()) / ((times.size()-1) * 1000); 508 436 } 509 437
Note: See TracChangeset
for help on using the changeset viewer.