Changeset 1263 for code/branches/merge/src/orxonox/Orxonox.cc
- Timestamp:
- May 13, 2008, 3:45:19 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/merge/src/orxonox/Orxonox.cc
r1219 r1263 37 37 38 38 //****** STD ******* 39 //#include <iostream>40 //#include <exception>41 39 #include <deque> 42 40 43 41 //****** OGRE ****** 44 //#include <OgreException.h>45 42 #include <OgreFrameListener.h> 46 43 #include <OgreOverlay.h> … … 65 62 #include "core/InputBuffer.h" 66 63 #include "core/InputManager.h" 64 #include "core/TclBind.h" 67 65 68 66 // audio … … 158 156 159 157 /** 160 * create a new instance of Orxonox 161 */ 162 Orxonox::Orxonox() 163 { 164 this->mode_ = STANDALONE; 165 this->serverIp_ = ""; 166 this->ogre_ = &GraphicsEngine::getSingleton(); 167 this->timer_ = 0; 168 this->dataPath_ = ""; 169 this->auMan_ = 0; 170 this->orxonoxHUD_ = 0; 171 this->inputHandler_ = 0; 158 * Create a new instance of Orxonox. Avoid doing any actual work here. 159 */ 160 Orxonox::Orxonox() : 161 ogre_(&GraphicsEngine::getSingleton()), 162 //auMan_(0), 163 timer_(0), 172 164 // turn on frame smoothing by setting a value different from 0 173 this->frameSmoothingTime_ = 0.0f; 174 this->bAbort_ = false; 175 this->timefactor_ = 1.0f; 176 } 177 178 /** 179 * destruct Orxonox 165 frameSmoothingTime_(0.0f), 166 orxonoxConsole_(0), 167 orxonoxHUD_(0), 168 bAbort_(false), 169 timefactor_(1.0f), 170 mode_(STANDALONE), 171 serverIp_("") 172 { 173 } 174 175 /** 176 * Destruct Orxonox. 180 177 */ 181 178 Orxonox::~Orxonox() … … 186 183 Loader::close(); 187 184 InputManager::destroy(); 188 if (this->auMan_)189 delete this->auMan_;185 //if (this->auMan_) 186 // delete this->auMan_; 190 187 if (this->timer_) 191 188 delete this->timer_; … … 199 196 200 197 /** 201 @brief Immediately deletes the orxonox object.202 Never use if you can help it while rendering!203 */204 void Orxonox::abortImmediateForce()205 {206 COUT(1) << "*** Orxonox Error: Orxonox object was unexpectedly destroyed." << std::endl;207 delete this;208 }209 210 /**211 198 Asks the mainloop nicely to abort. 212 199 */ … … 218 205 219 206 /** 220 * @return singleton object207 * @return singleton reference 221 208 */ 222 209 Orxonox* Orxonox::getSingleton() … … 243 230 * @param path path to config (in home dir or something) 244 231 */ 245 voidOrxonox::init(int argc, char **argv, std::string path)232 bool Orxonox::init(int argc, char **argv, std::string path) 246 233 { 247 234 //TODO: find config file (assuming executable directory) … … 249 236 //TODO: give config file to Ogre 250 237 std::string mode; 238 std::string dataPath; 251 239 252 240 ArgReader ar(argc, argv); 253 241 ar.checkArgument("mode", mode, false); 254 ar.checkArgument("data", this->dataPath_, false);242 ar.checkArgument("data", dataPath, false); 255 243 ar.checkArgument("ip", serverIp_, false); 256 if(ar.errorHandling()) abortImmediateForce(); 257 if(mode == std::string("client")) 244 if(ar.errorHandling()) 245 return false; 246 247 COUT(3) << "*** Orxonox: Mode is " << mode << "." << std::endl; 248 if (mode == "client") 249 mode_ = CLIENT; 250 else if (mode == "server") 251 mode_ = SERVER; 252 else 253 mode_ = STANDALONE; 254 255 //if (mode_ == DEDICATED) 256 // TODO: decide what to do here 257 //else 258 259 // for playable server, client and standalone, the startup 260 // procedure until the GUI is identical 261 262 TclBind::getInstance().setDataPath(dataPath); 263 ConfigFileManager::getSingleton()->setFile(CFT_Settings, "orxonox.ini"); 264 Factory::createClassHierarchy(); 265 266 if (!ogre_->setup(path)) // creates ogre root and other essentials 267 return false; 268 269 return true; 270 } 271 272 /** 273 * start modules 274 */ 275 bool Orxonox::start() 276 { 277 //if (mode == DEDICATED) 278 // do something else 279 //else 280 281 if (!ogre_->loadRenderer()) // creates the render window 282 return false; 283 284 // Calls the InputManager which sets up the input devices. 285 // The render window width and height are used to set up the mouse movement. 286 if (!InputManager::initialise(ogre_->getWindowHandle(), 287 ogre_->getWindowWidth(), ogre_->getWindowHeight())) 288 return false; 289 290 // TODO: Spread this so that this call only initialises things needed for the GUI 291 if (!ogre_->initialiseResources()) 292 return false; 293 294 // TOOD: load the GUI here 295 // set InputManager to GUI mode 296 InputManager::setInputState(InputManager::IS_GUI); 297 // TODO: run GUI here 298 299 // The following lines depend very much on the GUI output, so they're probably misplaced here.. 300 301 InputManager::setInputState(InputManager::IS_NONE); 302 303 if (!loadPlayground()) 304 return false; 305 306 switch (mode_) 258 307 { 259 mode_ = CLIENT; 260 clientInit(path); 261 } 262 else if(mode== std::string("server")){ 263 mode_ = SERVER; 264 serverInit(path); 265 } 266 else{ 267 mode_ = STANDALONE; 268 standaloneInit(path); 269 } 270 } 271 272 void Orxonox::serverInit(std::string path) 273 { 274 COUT(2) << "initialising server" << std::endl; 275 276 ogre_->setConfigPath(path); 277 ogre_->setup(); 278 //root_ = ogre_->getRoot(); 279 if(!ogre_->load(this->dataPath_)) abortImmediateForce(/* unable to load */); 280 281 server_g = new network::Server(); 282 } 283 284 void Orxonox::clientInit(std::string path) 285 { 286 COUT(2) << "initialising client" << std::endl;\ 287 288 ogre_->setConfigPath(path); 289 ogre_->setup(); 290 if(serverIp_.compare("")==0) 291 client_g = new network::Client(); 292 else 293 client_g = new network::Client(serverIp_, NETWORK_PORT); 294 if(!ogre_->load(this->dataPath_)) abortImmediateForce(/* unable to load */); 295 } 296 297 void Orxonox::standaloneInit(std::string path) 298 { 299 COUT(2) << "initialising standalone mode" << std::endl; 300 301 ogre_->setConfigPath(path); 302 ogre_->setup(); 303 if(!ogre_->load(this->dataPath_)) abortImmediateForce(/* unable to load */); 304 } 305 306 /** 307 * start modules 308 */ 309 void Orxonox::start() 310 { 311 switch(mode_){ 308 case SERVER: 309 if (!serverLoad()) 310 return false; 311 break; 312 312 case CLIENT: 313 clientStart(); 314 break; 315 case SERVER: 316 serverStart(); 313 if (!clientLoad()) 314 return false; 317 315 break; 318 316 default: 319 standaloneStart(); 317 if (!standaloneLoad()) 318 return false; 320 319 } 321 } 322 323 void Orxonox::clientStart(){ 324 ogre_->initialise(); 325 ConfigFileManager::getSingleton()->setFile(CFT_Settings, "orxonox.ini"); 326 Factory::createClassHierarchy(); 327 328 329 auMan_ = new audio::AudioManager(); 330 320 321 InputManager::setInputState(InputManager::IS_NORMAL); 322 323 return startRenderLoop(); 324 } 325 326 /** 327 * Loads everything in the scene except for the actual objects. 328 * This includes HUD, Console.. 329 */ 330 bool Orxonox::loadPlayground() 331 { 332 ogre_->createNewScene(); 333 334 // Init audio 335 //auMan_ = new audio::AudioManager(); 336 //auMan_->ambientAdd("a1"); 337 //auMan_->ambientAdd("a2"); 338 //auMan_->ambientAdd("a3"); 339 //auMan->ambientAdd("ambient1"); 340 //auMan_->ambientStart(); 341 342 // Load the HUD 343 COUT(3) << "*** Orxonox: Loading HUD..." << std::endl; 331 344 Ogre::Overlay* hudOverlay = Ogre::OverlayManager::getSingleton().getByName("Orxonox/HUD1.2"); 332 345 orxonoxHUD_ = new HUD(); … … 335 348 hudOverlay->show(); 336 349 337 client_g->establishConnection(); 338 client_g->tick(0); 339 340 341 //setupInputSystem(); 342 343 startRenderLoop(); 344 } 345 346 void Orxonox::serverStart(){ 347 //TODO: start modules 348 ogre_->initialise(); 349 //TODO: run engine 350 ConfigFileManager::getSingleton()->setFile(CFT_Settings, "orxonox.ini"); 351 Factory::createClassHierarchy(); 352 createScene(); 353 setupInputSystem(); 354 355 server_g->open(); 356 357 startRenderLoop(); 358 } 359 360 void Orxonox::standaloneStart(){ 361 //TODO: start modules 362 ogre_->initialise(); 363 //TODO: run engine 364 ConfigFileManager::getSingleton()->setFile(CFT_Settings, "orxonox.ini"); 365 Factory::createClassHierarchy(); 366 createScene(); 367 setupInputSystem(); 368 369 startRenderLoop(); 370 } 371 372 void Orxonox::createScene(void) 373 { 374 // Init audio 375 auMan_ = new audio::AudioManager(); 376 377 // load this file from config 378 Level* startlevel = new Level("levels/sample.oxw"); 379 Loader::open(startlevel); 380 381 Ogre::Overlay* hudOverlay = Ogre::OverlayManager::getSingleton().getByName("Orxonox/HUD1.2"); 382 orxonoxHUD_ = new HUD(); 383 orxonoxHUD_->setEnergyValue(20); 384 orxonoxHUD_->setEnergyDistr(20,20,60); 385 hudOverlay->show(); 386 387 /* 388 auMan_->ambientAdd("a1"); 389 auMan_->ambientAdd("a2"); 390 auMan_->ambientAdd("a3"); 391 //auMan->ambientAdd("ambient1"); 392 auMan_->ambientStart(); 393 */ 394 } 395 396 /** 397 @brief Calls the InputHandler which sets up the input devices. 398 The render window width and height are used to set up the mouse movement. 399 */ 400 void Orxonox::setupInputSystem() 401 { 402 if (!InputManager::initialise(ogre_->getWindowHandle(), 403 ogre_->getWindowWidth(), ogre_->getWindowHeight())) 404 abortImmediateForce(); 405 InputManager::setInputState(InputManager::IS_NORMAL); 406 } 407 408 /** 409 Main loop of the orxonox game. 410 This is a new solution, using the ogre engine instead of being used by it. 411 An alternative solution would be to simply use the timer of the Root object, 412 but that implies using Ogre in any case. There would be no way to test 413 our code without the help of the root object. 414 There's even a chance that we can dispose of the root object entirely 415 in server mode. 416 About the loop: The design is almost exactly like the one in ogre, so that 417 if any part of ogre registers a framelisteners, it will still behave 418 correctly. Furthermore the time smoothing feature from ogre has been 419 implemented too. If turned on (see orxonox constructor), it will calculate 420 the dt_n by means of the recent most dt_n-1, dt_n-2, etc. 421 */ 422 void Orxonox::startRenderLoop() 423 { 350 COUT(3) << "*** Orxonox: Loading Console..." << std::endl; 424 351 InputBuffer* ib = dynamic_cast<InputBuffer*>(InputManager::getKeyHandler("buffer")); 425 352 /* … … 432 359 ib->registerListener(console, &Testconsole::exit, (char)0x1B, true); 433 360 */ 434 435 361 orxonoxConsole_ = new InGameConsole(ib); 436 362 ib->registerListener(orxonoxConsole_, &InGameConsole::listen, true); … … 441 367 ib->registerListener(orxonoxConsole_, &InGameConsole::exit, (char)0x1B, true); 442 368 369 return true; 370 } 371 372 /** 373 * Level loading method for server mode. 374 */ 375 bool Orxonox::serverLoad() 376 { 377 COUT(2) << "Loading level in server mode" << std::endl; 378 379 server_g = new network::Server(); 380 381 if (!loadScene()) 382 return false; 383 384 server_g->open(); 385 386 return true; 387 } 388 389 /** 390 * Level loading method for client mode. 391 */ 392 bool Orxonox::clientLoad() 393 { 394 COUT(2) << "Loading level in client mode" << std::endl;\ 395 396 if (serverIp_.compare("") == 0) 397 client_g = new network::Client(); 398 else 399 client_g = new network::Client(serverIp_, NETWORK_PORT); 400 401 client_g->establishConnection(); 402 client_g->tick(0); 403 404 return true; 405 } 406 407 /** 408 * Level loading method for standalone mode. 409 */ 410 bool Orxonox::standaloneLoad() 411 { 412 COUT(2) << "Loading level in standalone mode" << std::endl; 413 414 if (!loadScene()) 415 return false; 416 417 return true; 418 } 419 420 /** 421 * Helper method to load a level. 422 */ 423 bool Orxonox::loadScene() 424 { 425 Level* startlevel = new Level("levels/sample.oxw"); 426 Loader::open(startlevel); 427 428 return true; 429 } 430 431 432 /** 433 Main loop of the orxonox game. 434 About the loop: The design is almost exactly like the one in ogre, so that 435 if any part of ogre registers a framelisteners, it will still behave 436 correctly. Furthermore the time smoothing feature from ogre has been 437 implemented too. If turned on (see orxonox constructor), it will calculate 438 the dt_n by means of the recent most dt_n-1, dt_n-2, etc. 439 */ 440 bool Orxonox::startRenderLoop() 441 { 443 442 // first check whether ogre root object has been created 444 443 if (Ogre::Root::getSingletonPtr() == 0) 445 444 { 446 445 COUT(2) << "*** Orxonox Error: Could not start rendering. No Ogre root object found" << std::endl; 447 return ;446 return false; 448 447 } 449 448 Ogre::Root& ogreRoot = Ogre::Root::getSingleton(); … … 465 464 timer_->reset(); 466 465 466 COUT(3) << "*** Orxonox: Starting the main loop." << std::endl; 467 467 while (!bAbort_) 468 468 { … … 510 510 ogreRoot._fireFrameEnded(evt); 511 511 } 512 513 return true; 512 514 } 513 515 … … 550 552 } 551 553 554 /** 555 * Static function that shows the console in game mode. 556 */ 552 557 void Orxonox::activateConsole() 553 558 { 559 // currently, the console shows itself when feeded with input. 554 560 InputManager::setInputState(InputManager::IS_CONSOLE); 555 561 }
Note: See TracChangeset
for help on using the changeset viewer.