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