[74] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * |
---|
| 4 | * |
---|
| 5 | * License notice: |
---|
| 6 | * |
---|
[462] | 7 | * This program is free software; you can redistribute it and/or |
---|
| 8 | * modify it under the terms of the GNU General Public License |
---|
| 9 | * as published by the Free Software Foundation; either version 2 |
---|
| 10 | * of the License, or (at your option) any later version. |
---|
[74] | 11 | * |
---|
| 12 | * This program is distributed in the hope that it will be useful, |
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 15 | * GNU General Public License for more details. |
---|
| 16 | * |
---|
| 17 | * You should have received a copy of the GNU General Public License |
---|
[462] | 18 | * along with this program; if not, write to the Free Software |
---|
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
[74] | 20 | * |
---|
| 21 | * Author: |
---|
| 22 | * Benjamin Knecht <beni_at_orxonox.net>, (C) 2007 |
---|
| 23 | * Co-authors: |
---|
| 24 | * ... |
---|
| 25 | * |
---|
| 26 | */ |
---|
| 27 | |
---|
[142] | 28 | /** |
---|
| 29 | @file orxonox.cc |
---|
[462] | 30 | @brief Orxonox Main Class |
---|
[142] | 31 | */ |
---|
| 32 | |
---|
[462] | 33 | #include "orxonox.h" |
---|
| 34 | #include "graphicsEngine.h" |
---|
| 35 | |
---|
[379] | 36 | //#include <Ogre.h> |
---|
| 37 | // 40% speed up: (instead of Ogre.h) |
---|
| 38 | #include <OgreSceneNode.h> |
---|
| 39 | #include <OgreSceneManager.h> |
---|
| 40 | #include <OgreRoot.h> |
---|
| 41 | #include <OgreFrameListener.h> |
---|
| 42 | #include <OgreConfigFile.h> |
---|
| 43 | #include <OgreTextureManager.h> |
---|
| 44 | #include <OgreEntity.h> |
---|
| 45 | #include <OgreRenderWindow.h> |
---|
| 46 | |
---|
[137] | 47 | #include <OIS/OIS.h> |
---|
[346] | 48 | //#include <CEGUI/CEGUI.h> |
---|
| 49 | //#include <OgreCEGUIRenderer.h> |
---|
[74] | 50 | |
---|
[164] | 51 | #include <string> |
---|
| 52 | #include <iostream> |
---|
| 53 | |
---|
[496] | 54 | #include "objects/Tickable.h" |
---|
| 55 | #include "objects/Timer.h" |
---|
[542] | 56 | #include "core/ArgReader.h" |
---|
[496] | 57 | #include "core/Factory.h" |
---|
[568] | 58 | #include "core/Debug.h" |
---|
[496] | 59 | |
---|
[462] | 60 | #include "../loader/LevelLoader.h" |
---|
| 61 | #include "../audio/AudioManager.h" |
---|
[164] | 62 | |
---|
[588] | 63 | #include "hud/HUD.h" |
---|
| 64 | |
---|
[459] | 65 | //network stuff |
---|
[462] | 66 | #include "../network/Server.h" |
---|
| 67 | #include "../network/Client.h" |
---|
[473] | 68 | #include "../network/NetworkFrameListener.h" |
---|
[588] | 69 | |
---|
| 70 | #ifdef WIN32 |
---|
| 71 | #include <windows.h> |
---|
| 72 | #define usleep(x) Sleep((x)/1000) |
---|
| 73 | #else |
---|
| 74 | #include <unistd.h> |
---|
[576] | 75 | #endif |
---|
[459] | 76 | |
---|
[337] | 77 | namespace orxonox |
---|
| 78 | { |
---|
[462] | 79 | using namespace Ogre; |
---|
[74] | 80 | |
---|
[462] | 81 | // put this in seperate Class or solve the problem in another fashion |
---|
[608] | 82 | class OrxListener : public FrameListener |
---|
[462] | 83 | { |
---|
| 84 | public: |
---|
[608] | 85 | OrxListener(OIS::Keyboard *keyboard, audio::AudioManager* auMan, gameMode mode) |
---|
| 86 | { |
---|
| 87 | mKeyboard = keyboard; |
---|
[601] | 88 | mode_=mode; |
---|
[608] | 89 | auMan_ = auMan; |
---|
| 90 | } |
---|
[530] | 91 | |
---|
[462] | 92 | bool frameStarted(const FrameEvent& evt) |
---|
| 93 | { |
---|
| 94 | auMan_->update(); |
---|
[74] | 95 | |
---|
[608] | 96 | if(mode_==PRESENTATION) |
---|
| 97 | server_g->tick(evt.timeSinceLastFrame); |
---|
| 98 | else if(mode_==CLIENT) |
---|
| 99 | client_g->tick(evt.timeSinceLastFrame); |
---|
[337] | 100 | |
---|
[608] | 101 | usleep(10); |
---|
[337] | 102 | |
---|
[608] | 103 | mKeyboard->capture(); |
---|
[462] | 104 | return !mKeyboard->isKeyDown(OIS::KC_ESCAPE); |
---|
| 105 | } |
---|
[337] | 106 | |
---|
[265] | 107 | private: |
---|
[601] | 108 | gameMode mode_; |
---|
[265] | 109 | OIS::Keyboard *mKeyboard; |
---|
[462] | 110 | audio::AudioManager* auMan_; |
---|
[608] | 111 | }; |
---|
| 112 | |
---|
[462] | 113 | // init static singleton reference of Orxonox |
---|
| 114 | Orxonox* Orxonox::singletonRef_ = NULL; |
---|
[137] | 115 | |
---|
[462] | 116 | /** |
---|
| 117 | * create a new instance of Orxonox |
---|
| 118 | */ |
---|
| 119 | Orxonox::Orxonox() |
---|
| 120 | { |
---|
[608] | 121 | this->ogre_ = new GraphicsEngine(); |
---|
| 122 | this->dataPath_ = ""; |
---|
| 123 | this->loader_ = 0; |
---|
| 124 | this->auMan_ = 0; |
---|
| 125 | this->singletonRef_ = 0; |
---|
| 126 | this->keyboard_ = 0; |
---|
| 127 | this->mouse_ = 0; |
---|
| 128 | this->inputManager_ = 0; |
---|
| 129 | this->frameListener_ = 0; |
---|
| 130 | this->root_ = 0; |
---|
[462] | 131 | } |
---|
[74] | 132 | |
---|
[462] | 133 | /** |
---|
| 134 | * destruct Orxonox |
---|
| 135 | */ |
---|
| 136 | Orxonox::~Orxonox() |
---|
| 137 | { |
---|
| 138 | // nothing to delete as for now |
---|
| 139 | } |
---|
[265] | 140 | |
---|
[462] | 141 | /** |
---|
| 142 | * initialization of Orxonox object |
---|
| 143 | * @param argc argument counter |
---|
| 144 | * @param argv list of arguments |
---|
| 145 | * @param path path to config (in home dir or something) |
---|
| 146 | */ |
---|
| 147 | void Orxonox::init(int argc, char **argv, std::string path) |
---|
| 148 | { |
---|
| 149 | //TODO: find config file (assuming executable directory) |
---|
| 150 | //TODO: read config file |
---|
| 151 | //TODO: give config file to Ogre |
---|
[568] | 152 | std::string mode; |
---|
[599] | 153 | // if(argc>=2) |
---|
| 154 | // mode = std::string(argv[1]); |
---|
| 155 | // else |
---|
| 156 | // mode = ""; |
---|
[542] | 157 | ArgReader ar = ArgReader(argc, argv); |
---|
| 158 | ar.checkArgument("mode", mode, false); |
---|
[546] | 159 | ar.checkArgument("data", this->dataPath_, false); |
---|
[599] | 160 | ar.checkArgument("ip", serverIp_, false); |
---|
[542] | 161 | if(ar.errorHandling()) die(); |
---|
| 162 | if(mode == std::string("server")) |
---|
[462] | 163 | { |
---|
| 164 | serverInit(path); |
---|
[599] | 165 | mode_ = SERVER; |
---|
[462] | 166 | } |
---|
[542] | 167 | else if(mode == std::string("client")) |
---|
[462] | 168 | { |
---|
| 169 | clientInit(path); |
---|
[599] | 170 | mode_ = CLIENT; |
---|
[534] | 171 | } |
---|
[542] | 172 | else if(mode == std::string("presentation")) |
---|
[531] | 173 | { |
---|
[568] | 174 | serverInit(path); |
---|
[599] | 175 | mode_ = PRESENTATION; |
---|
[534] | 176 | } |
---|
[568] | 177 | else{ |
---|
| 178 | standaloneInit(path); |
---|
[599] | 179 | mode_ = STANDALONE; |
---|
[568] | 180 | } |
---|
[462] | 181 | } |
---|
[263] | 182 | |
---|
[462] | 183 | /** |
---|
| 184 | * start modules |
---|
| 185 | */ |
---|
| 186 | void Orxonox::start() |
---|
| 187 | { |
---|
| 188 | //TODO: start modules |
---|
[534] | 189 | ogre_->startRender(); |
---|
[462] | 190 | //TODO: run engine |
---|
[534] | 191 | createScene(); |
---|
| 192 | setupScene(); |
---|
| 193 | setupInputSystem(); |
---|
[599] | 194 | if(mode_!=CLIENT){ // remove this in future ---- presentation hack |
---|
| 195 | } |
---|
| 196 | else |
---|
| 197 | std::cout << "client here" << std::endl; |
---|
[534] | 198 | createFrameListener(); |
---|
| 199 | Factory::createClassHierarchy(); |
---|
[568] | 200 | switch(mode_){ |
---|
| 201 | case PRESENTATION: |
---|
[601] | 202 | //ogre_->getRoot()->addFrameListener(new network::ServerFrameListener()); |
---|
| 203 | //std::cout << "could not add framelistener" << std::endl; |
---|
[568] | 204 | server_g->open(); |
---|
| 205 | break; |
---|
[599] | 206 | case CLIENT: |
---|
| 207 | client_g->establishConnection(); |
---|
| 208 | break; |
---|
[568] | 209 | case SERVER: |
---|
| 210 | case STANDALONE: |
---|
| 211 | default: |
---|
| 212 | break; |
---|
| 213 | } |
---|
[534] | 214 | startRenderLoop(); |
---|
[462] | 215 | } |
---|
[74] | 216 | |
---|
[462] | 217 | /** |
---|
| 218 | * @return singleton object |
---|
| 219 | */ |
---|
| 220 | Orxonox* Orxonox::getSingleton() |
---|
| 221 | { |
---|
| 222 | if (!singletonRef_) |
---|
| 223 | singletonRef_ = new Orxonox(); |
---|
| 224 | return singletonRef_; |
---|
| 225 | } |
---|
[74] | 226 | |
---|
[462] | 227 | /** |
---|
| 228 | * error kills orxonox |
---|
| 229 | */ |
---|
| 230 | void Orxonox::die(/* some error code */) |
---|
| 231 | { |
---|
| 232 | //TODO: destroy and destruct everything and print nice error msg |
---|
[542] | 233 | delete this; |
---|
[462] | 234 | } |
---|
[74] | 235 | |
---|
[568] | 236 | void Orxonox::standaloneInit(std::string path) |
---|
[462] | 237 | { |
---|
| 238 | ogre_->setConfigPath(path); |
---|
| 239 | ogre_->setup(); |
---|
[473] | 240 | root_ = ogre_->getRoot(); |
---|
[534] | 241 | if(!ogre_->load()) die(/* unable to load */); |
---|
[473] | 242 | |
---|
[534] | 243 | //defineResources(); |
---|
| 244 | //setupRenderSystem(); |
---|
| 245 | //createRenderWindow(); |
---|
| 246 | //initializeResourceGroups(); |
---|
| 247 | /*createScene(); |
---|
[473] | 248 | setupScene(); |
---|
| 249 | setupInputSystem(); |
---|
| 250 | createFrameListener(); |
---|
[496] | 251 | Factory::createClassHierarchy(); |
---|
[534] | 252 | startRenderLoop();*/ |
---|
[462] | 253 | } |
---|
[534] | 254 | |
---|
[531] | 255 | void Orxonox::playableServer(std::string path) |
---|
| 256 | { |
---|
| 257 | ogre_->setConfigPath(path); |
---|
| 258 | ogre_->setup(); |
---|
| 259 | root_ = ogre_->getRoot(); |
---|
| 260 | defineResources(); |
---|
| 261 | setupRenderSystem(); |
---|
| 262 | createRenderWindow(); |
---|
| 263 | initializeResourceGroups(); |
---|
[608] | 264 | setupInputSystem(); |
---|
| 265 | Factory::createClassHierarchy(); |
---|
[531] | 266 | createScene(); |
---|
| 267 | setupScene(); |
---|
| 268 | createFrameListener(); |
---|
| 269 | try{ |
---|
[534] | 270 | server_g = new network::Server(); // add port and bindadress |
---|
| 271 | server_g->open(); // open server and create listener thread |
---|
| 272 | if(ogre_ && ogre_->getRoot()) |
---|
| 273 | ogre_->getRoot()->addFrameListener(new network::ServerFrameListener()); // adds a framelistener for the server |
---|
[560] | 274 | COUT(3) << "Info: network framelistener added" << std::endl; |
---|
[534] | 275 | } |
---|
[531] | 276 | catch(exception &e) |
---|
| 277 | { |
---|
[560] | 278 | COUT(1) << "Error: There was a problem initialising the server :(" << std::endl; |
---|
[531] | 279 | } |
---|
| 280 | startRenderLoop(); |
---|
| 281 | } |
---|
[576] | 282 | |
---|
[568] | 283 | void Orxonox::standalone(){ |
---|
[576] | 284 | |
---|
| 285 | |
---|
| 286 | |
---|
[568] | 287 | } |
---|
[74] | 288 | |
---|
[462] | 289 | void Orxonox::serverInit(std::string path) |
---|
| 290 | { |
---|
[599] | 291 | COUT(2) << "initialising server" << std::endl; |
---|
[462] | 292 | ogre_->setConfigPath(path); |
---|
| 293 | ogre_->setup(); |
---|
[473] | 294 | server_g = new network::Server(); // add some settings if wanted |
---|
[462] | 295 | if(!ogre_->load()) die(/* unable to load */); |
---|
[568] | 296 | // add network framelistener |
---|
[462] | 297 | } |
---|
[164] | 298 | |
---|
[462] | 299 | void Orxonox::clientInit(std::string path) |
---|
| 300 | { |
---|
[599] | 301 | COUT(2) << "initialising client" << std::endl; |
---|
[462] | 302 | ogre_->setConfigPath(path); |
---|
| 303 | ogre_->setup(); |
---|
[599] | 304 | if(serverIp_.compare("")==0) |
---|
| 305 | client_g = new network::Client(); |
---|
| 306 | else |
---|
| 307 | client_g = new network::Client(serverIp_, 55556); |
---|
[462] | 308 | if(!ogre_->load()) die(/* unable to load */); |
---|
[505] | 309 | ogre_->getRoot()->addFrameListener(new network::ClientFrameListener()); |
---|
[462] | 310 | } |
---|
[258] | 311 | |
---|
[473] | 312 | void Orxonox::defineResources() |
---|
| 313 | { |
---|
| 314 | Ogre::String secName, typeName, archName; |
---|
| 315 | Ogre::ConfigFile cf; |
---|
| 316 | #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE |
---|
| 317 | cf.load(macBundlePath() + "/Contents/Resources/resources.cfg"); |
---|
| 318 | #else |
---|
[546] | 319 | cf.load(dataPath_ + "resources.cfg"); |
---|
[473] | 320 | #endif |
---|
| 321 | |
---|
| 322 | Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator(); |
---|
| 323 | while (seci.hasMoreElements()) |
---|
| 324 | { |
---|
| 325 | secName = seci.peekNextKey(); |
---|
| 326 | Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext(); |
---|
| 327 | Ogre::ConfigFile::SettingsMultiMap::iterator i; |
---|
| 328 | for (i = settings->begin(); i != settings->end(); ++i) |
---|
| 329 | { |
---|
| 330 | typeName = i->first; |
---|
| 331 | archName = i->second; |
---|
| 332 | #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE |
---|
| 333 | Ogre::ResourceGroupManager::getSingleton().addResourceLocation( String(macBundlePath() + "/" + archName), typeName, secName); |
---|
| 334 | #else |
---|
| 335 | Ogre::ResourceGroupManager::getSingleton().addResourceLocation( archName, typeName, secName); |
---|
| 336 | #endif |
---|
| 337 | } |
---|
| 338 | } |
---|
| 339 | } |
---|
| 340 | |
---|
| 341 | void Orxonox::setupRenderSystem() |
---|
| 342 | { |
---|
[588] | 343 | if (!root_->restoreConfig() && !root_->showConfigDialog()) |
---|
[473] | 344 | throw Exception(52, "User canceled the config dialog!", "OrxApplication::setupRenderSystem()"); |
---|
| 345 | } |
---|
| 346 | |
---|
| 347 | void Orxonox::createRenderWindow() |
---|
| 348 | { |
---|
| 349 | root_->initialise(true, "OrxonoxV2"); |
---|
| 350 | } |
---|
| 351 | |
---|
| 352 | void Orxonox::initializeResourceGroups() |
---|
| 353 | { |
---|
| 354 | TextureManager::getSingleton().setDefaultNumMipmaps(5); |
---|
| 355 | ResourceGroupManager::getSingleton().initialiseAllResourceGroups(); |
---|
| 356 | } |
---|
| 357 | |
---|
[537] | 358 | /** |
---|
| 359 | * |
---|
| 360 | * @param |
---|
| 361 | */ |
---|
[462] | 362 | void Orxonox::createScene(void) |
---|
| 363 | { |
---|
[480] | 364 | // Init audio |
---|
[462] | 365 | auMan_ = new audio::AudioManager(); |
---|
[487] | 366 | |
---|
[480] | 367 | // load this file from config |
---|
| 368 | loader_ = new loader::LevelLoader("sample.oxw"); |
---|
| 369 | loader_->loadLevel(); |
---|
[337] | 370 | |
---|
[588] | 371 | Overlay* hudOverlay = OverlayManager::getSingleton().getByName("Orxonox/HUD1.2"); |
---|
| 372 | hud::HUD* orxonoxHud; |
---|
| 373 | orxonoxHud = new hud::HUD(); |
---|
| 374 | orxonoxHud->setEnergyValue(20); |
---|
| 375 | orxonoxHud->setEnergyDistr(20,20,60); |
---|
| 376 | hudOverlay->show(); |
---|
| 377 | |
---|
| 378 | |
---|
| 379 | /* |
---|
| 380 | auMan_->ambientAdd("a1"); |
---|
| 381 | auMan_->ambientAdd("a2"); |
---|
| 382 | auMan_->ambientAdd("a3"); |
---|
| 383 | //auMan->ambientAdd("ambient1"); |
---|
| 384 | auMan_->ambientStart();*/ |
---|
[462] | 385 | } |
---|
[337] | 386 | |
---|
| 387 | |
---|
[537] | 388 | /** |
---|
| 389 | * |
---|
| 390 | */ |
---|
[462] | 391 | void Orxonox::setupScene() |
---|
| 392 | { |
---|
[608] | 393 | // SceneManager *mgr = ogre_->getSceneManager(); |
---|
[512] | 394 | |
---|
[515] | 395 | |
---|
[608] | 396 | // SceneNode* node = (SceneNode*)mgr->getRootSceneNode()->getChild("OgreHeadNode"); |
---|
[541] | 397 | // SceneNode *node = mgr->getRootSceneNode()->createChildSceneNode("OgreHeadNode", Vector3(0,0,0)); |
---|
[515] | 398 | |
---|
[337] | 399 | |
---|
[584] | 400 | /* |
---|
[537] | 401 | particle::ParticleInterface *e = new particle::ParticleInterface(mgr,"engine","Orxonox/strahl"); |
---|
[535] | 402 | e->particleSystem_->setParameter("local_space","true"); |
---|
[592] | 403 | e->setPositionOfEmitter(0, Vector3(0,-10,0)); |
---|
[537] | 404 | e->setDirection(Vector3(0,0,-1)); |
---|
| 405 | e->addToSceneNode(node); |
---|
[584] | 406 | */ |
---|
[462] | 407 | } |
---|
[137] | 408 | |
---|
| 409 | |
---|
[462] | 410 | void Orxonox::setupInputSystem() |
---|
| 411 | { |
---|
| 412 | size_t windowHnd = 0; |
---|
| 413 | std::ostringstream windowHndStr; |
---|
| 414 | OIS::ParamList pl; |
---|
| 415 | Ogre::RenderWindow *win = ogre_->getRoot()->getAutoCreatedWindow(); |
---|
[137] | 416 | |
---|
[462] | 417 | win->getCustomAttribute("WINDOW", &windowHnd); |
---|
| 418 | windowHndStr << windowHnd; |
---|
| 419 | pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str())); |
---|
| 420 | inputManager_ = OIS::InputManager::createInputSystem(pl); |
---|
[265] | 421 | |
---|
[462] | 422 | try |
---|
[337] | 423 | { |
---|
[462] | 424 | keyboard_ = static_cast<OIS::Keyboard*>(inputManager_->createInputObject(OIS::OISKeyboard, false)); |
---|
| 425 | mouse_ = static_cast<OIS::Mouse*>(inputManager_->createInputObject(OIS::OISMouse, true)); |
---|
[337] | 426 | } |
---|
[462] | 427 | catch (const OIS::Exception &e) |
---|
[459] | 428 | { |
---|
[462] | 429 | throw new Ogre::Exception(42, e.eText, "OrxApplication::setupInputSystem"); |
---|
[459] | 430 | } |
---|
[462] | 431 | } |
---|
[137] | 432 | |
---|
[462] | 433 | // we actually want to do this differently... |
---|
| 434 | void Orxonox::createFrameListener() |
---|
[137] | 435 | { |
---|
[496] | 436 | TickFrameListener* TickFL = new TickFrameListener(); |
---|
| 437 | ogre_->getRoot()->addFrameListener(TickFL); |
---|
| 438 | |
---|
| 439 | TimerFrameListener* TimerFL = new TimerFrameListener(); |
---|
| 440 | ogre_->getRoot()->addFrameListener(TimerFL); |
---|
| 441 | |
---|
[599] | 442 | //if(mode_!=CLIENT) // just a hack ------- remove this in future |
---|
[608] | 443 | frameListener_ = new OrxListener(keyboard_, auMan_, mode_); |
---|
[462] | 444 | ogre_->getRoot()->addFrameListener(frameListener_); |
---|
[137] | 445 | } |
---|
[462] | 446 | |
---|
| 447 | void Orxonox::startRenderLoop() |
---|
[137] | 448 | { |
---|
[511] | 449 | // this is a hack!!! |
---|
| 450 | // the call to reset the mouse clipping size should probably be somewhere |
---|
| 451 | // else, however this works for the moment. |
---|
[523] | 452 | unsigned int width, height, depth; |
---|
| 453 | int left, top; |
---|
| 454 | ogre_->getRoot()->getAutoCreatedWindow()->getMetrics(width, height, depth, left, top); |
---|
[511] | 455 | |
---|
[599] | 456 | if(mode_!=CLIENT){ |
---|
| 457 | const OIS::MouseState &ms = mouse_->getMouseState(); |
---|
| 458 | ms.width = width; |
---|
| 459 | ms.height = height; |
---|
| 460 | } |
---|
[462] | 461 | ogre_->getRoot()->startRendering(); |
---|
[74] | 462 | } |
---|
| 463 | } |
---|