[612] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * |
---|
| 4 | * |
---|
| 5 | * License notice: |
---|
| 6 | * |
---|
| 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. |
---|
| 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 |
---|
| 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. |
---|
| 20 | * |
---|
| 21 | * Author: |
---|
| 22 | * Benjamin Knecht <beni_at_orxonox.net>, (C) 2007 |
---|
| 23 | * Co-authors: |
---|
| 24 | * ... |
---|
| 25 | * |
---|
| 26 | */ |
---|
| 27 | |
---|
| 28 | /** |
---|
| 29 | @file Orxonox.cc |
---|
| 30 | @brief Orxonox Main Class |
---|
| 31 | */ |
---|
| 32 | |
---|
[784] | 33 | // Precompiled Headers |
---|
[888] | 34 | #include "OrxonoxStableHeaders.h" |
---|
[784] | 35 | |
---|
[612] | 36 | //****** OGRE ****** |
---|
| 37 | #include <OgreException.h> |
---|
| 38 | #include <OgreRoot.h> |
---|
| 39 | #include <OgreFrameListener.h> |
---|
| 40 | #include <OgreRenderWindow.h> |
---|
| 41 | #include <OgreTextureManager.h> |
---|
| 42 | #include <OgreResourceGroupManager.h> |
---|
| 43 | #include <OgreConfigFile.h> |
---|
| 44 | #include <OgreOverlay.h> |
---|
| 45 | #include <OgreOverlayManager.h> |
---|
| 46 | |
---|
| 47 | //****** OIS ******* |
---|
| 48 | #include <OIS/OIS.h> |
---|
| 49 | |
---|
| 50 | //****** STD ******* |
---|
| 51 | #include <iostream> |
---|
[673] | 52 | #include <exception> |
---|
[612] | 53 | |
---|
| 54 | //***** ORXONOX **** |
---|
[888] | 55 | //misc |
---|
[742] | 56 | #include "util/Sleep.h" |
---|
[708] | 57 | |
---|
[612] | 58 | // loader and audio |
---|
[871] | 59 | //#include "loader/LevelLoader.h" |
---|
[612] | 60 | #include "audio/AudioManager.h" |
---|
| 61 | |
---|
| 62 | // network |
---|
| 63 | #include "network/Server.h" |
---|
| 64 | #include "network/Client.h" |
---|
[888] | 65 | #include "network/NetworkPrereqs.h" |
---|
| 66 | network::Client *client_g; |
---|
| 67 | network::Server *server_g; |
---|
[612] | 68 | |
---|
[888] | 69 | |
---|
[612] | 70 | // objects |
---|
| 71 | #include "objects/Tickable.h" |
---|
[748] | 72 | #include "tools/Timer.h" |
---|
[627] | 73 | #include "objects/NPC.h" |
---|
[612] | 74 | #include "core/ArgReader.h" |
---|
| 75 | #include "core/Factory.h" |
---|
| 76 | #include "core/Debug.h" |
---|
[888] | 77 | #include "core/Loader.h" |
---|
[612] | 78 | #include "hud/HUD.h" |
---|
[768] | 79 | #include "objects/weapon/BulletManager.h" |
---|
[729] | 80 | #include "GraphicsEngine.h" |
---|
[612] | 81 | |
---|
[888] | 82 | #include "Orxonox.h" |
---|
[612] | 83 | |
---|
| 84 | namespace orxonox |
---|
| 85 | { |
---|
| 86 | // put this in a seperate Class or solve the problem in another fashion |
---|
[708] | 87 | class OrxListener : public Ogre::FrameListener |
---|
[612] | 88 | { |
---|
| 89 | public: |
---|
| 90 | OrxListener(OIS::Keyboard *keyboard, audio::AudioManager* auMan, gameMode mode) |
---|
[616] | 91 | { |
---|
[612] | 92 | mKeyboard = keyboard; |
---|
| 93 | mode_=mode; |
---|
| 94 | auMan_ = auMan; |
---|
[616] | 95 | } |
---|
[612] | 96 | |
---|
[708] | 97 | bool frameStarted(const Ogre::FrameEvent& evt) |
---|
[612] | 98 | { |
---|
| 99 | auMan_->update(); |
---|
[627] | 100 | updateAI(); |
---|
[612] | 101 | |
---|
[888] | 102 | // if(mode_ == SERVER) |
---|
| 103 | // server_g->tick(evt.timeSinceLastFrame); |
---|
| 104 | // else if(mode_ == CLIENT) |
---|
| 105 | // client_g->tick(evt.timeSinceLastFrame); |
---|
[612] | 106 | |
---|
[616] | 107 | usleep(10); |
---|
[612] | 108 | |
---|
[616] | 109 | mKeyboard->capture(); |
---|
[612] | 110 | return !mKeyboard->isKeyDown(OIS::KC_ESCAPE); |
---|
| 111 | } |
---|
| 112 | |
---|
[627] | 113 | void updateAI() |
---|
| 114 | { |
---|
| 115 | for(Iterator<NPC> it = ObjectList<NPC>::start(); it; ++it) |
---|
| 116 | { |
---|
| 117 | it->update(); |
---|
| 118 | } |
---|
| 119 | } |
---|
| 120 | |
---|
[612] | 121 | private: |
---|
| 122 | gameMode mode_; |
---|
| 123 | OIS::Keyboard *mKeyboard; |
---|
| 124 | audio::AudioManager* auMan_; |
---|
[616] | 125 | }; |
---|
[612] | 126 | |
---|
| 127 | // init static singleton reference of Orxonox |
---|
| 128 | Orxonox* Orxonox::singletonRef_ = NULL; |
---|
| 129 | |
---|
| 130 | /** |
---|
| 131 | * create a new instance of Orxonox |
---|
| 132 | */ |
---|
| 133 | Orxonox::Orxonox() |
---|
| 134 | { |
---|
| 135 | this->ogre_ = new GraphicsEngine(); |
---|
| 136 | this->dataPath_ = ""; |
---|
[871] | 137 | // this->loader_ = 0; |
---|
[616] | 138 | this->auMan_ = 0; |
---|
| 139 | this->singletonRef_ = 0; |
---|
| 140 | this->keyboard_ = 0; |
---|
| 141 | this->mouse_ = 0; |
---|
| 142 | this->inputManager_ = 0; |
---|
| 143 | this->frameListener_ = 0; |
---|
| 144 | this->root_ = 0; |
---|
[612] | 145 | } |
---|
| 146 | |
---|
| 147 | /** |
---|
| 148 | * destruct Orxonox |
---|
| 149 | */ |
---|
| 150 | Orxonox::~Orxonox() |
---|
| 151 | { |
---|
| 152 | // nothing to delete as for now |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | /** |
---|
| 156 | * initialization of Orxonox object |
---|
| 157 | * @param argc argument counter |
---|
| 158 | * @param argv list of arguments |
---|
| 159 | * @param path path to config (in home dir or something) |
---|
| 160 | */ |
---|
[715] | 161 | void Orxonox::init(int argc, char **argv, std::string path) |
---|
[612] | 162 | { |
---|
| 163 | //TODO: find config file (assuming executable directory) |
---|
| 164 | //TODO: read config file |
---|
| 165 | //TODO: give config file to Ogre |
---|
[715] | 166 | std::string mode; |
---|
[888] | 167 | |
---|
| 168 | |
---|
[612] | 169 | ArgReader ar = ArgReader(argc, argv); |
---|
| 170 | ar.checkArgument("mode", mode, false); |
---|
| 171 | ar.checkArgument("data", this->dataPath_, false); |
---|
| 172 | ar.checkArgument("ip", serverIp_, false); |
---|
| 173 | if(ar.errorHandling()) die(); |
---|
[888] | 174 | if(mode == std::string("client")) |
---|
[612] | 175 | { |
---|
[888] | 176 | mode_ = CLIENT; |
---|
[612] | 177 | clientInit(path); |
---|
| 178 | } |
---|
[888] | 179 | else if(mode== std::string("server")){ |
---|
| 180 | mode_ = SERVER; |
---|
[612] | 181 | serverInit(path); |
---|
| 182 | } |
---|
| 183 | else{ |
---|
[888] | 184 | mode_ = STANDALONE; |
---|
[612] | 185 | standaloneInit(path); |
---|
| 186 | } |
---|
| 187 | } |
---|
| 188 | |
---|
[888] | 189 | |
---|
[612] | 190 | /** |
---|
| 191 | * start modules |
---|
| 192 | */ |
---|
| 193 | void Orxonox::start() |
---|
| 194 | { |
---|
[888] | 195 | switch(mode_){ |
---|
| 196 | case CLIENT: |
---|
| 197 | clientStart(); |
---|
| 198 | break; |
---|
| 199 | case SERVER: |
---|
| 200 | serverStart(); |
---|
| 201 | break; |
---|
| 202 | default: |
---|
| 203 | standaloneStart(); |
---|
| 204 | } |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | void Orxonox::clientStart(){ |
---|
| 208 | |
---|
| 209 | |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | void Orxonox::serverStart(){ |
---|
[612] | 213 | //TODO: start modules |
---|
| 214 | ogre_->startRender(); |
---|
| 215 | //TODO: run engine |
---|
[888] | 216 | Factory::createClassHierarchy(); |
---|
[612] | 217 | createScene(); |
---|
| 218 | setupScene(); |
---|
| 219 | setupInputSystem(); |
---|
[888] | 220 | |
---|
[612] | 221 | createFrameListener(); |
---|
[888] | 222 | server_g->open(); |
---|
| 223 | |
---|
[612] | 224 | startRenderLoop(); |
---|
| 225 | } |
---|
[888] | 226 | |
---|
| 227 | void Orxonox::standaloneStart(){ |
---|
| 228 | //TODO: start modules |
---|
| 229 | ogre_->startRender(); |
---|
| 230 | //TODO: run engine |
---|
| 231 | Factory::createClassHierarchy(); |
---|
| 232 | createScene(); |
---|
| 233 | setupScene(); |
---|
| 234 | setupInputSystem(); |
---|
| 235 | |
---|
| 236 | createFrameListener(); |
---|
| 237 | |
---|
| 238 | startRenderLoop(); |
---|
| 239 | } |
---|
[612] | 240 | |
---|
| 241 | /** |
---|
| 242 | * @return singleton object |
---|
| 243 | */ |
---|
| 244 | Orxonox* Orxonox::getSingleton() |
---|
| 245 | { |
---|
| 246 | if (!singletonRef_) |
---|
| 247 | singletonRef_ = new Orxonox(); |
---|
| 248 | return singletonRef_; |
---|
| 249 | } |
---|
| 250 | |
---|
| 251 | /** |
---|
| 252 | * error kills orxonox |
---|
| 253 | */ |
---|
| 254 | void Orxonox::die(/* some error code */) |
---|
| 255 | { |
---|
| 256 | //TODO: destroy and destruct everything and print nice error msg |
---|
| 257 | delete this; |
---|
| 258 | } |
---|
| 259 | |
---|
| 260 | |
---|
[715] | 261 | void Orxonox::serverInit(std::string path) |
---|
[612] | 262 | { |
---|
| 263 | COUT(2) << "initialising server" << std::endl; |
---|
[888] | 264 | |
---|
[612] | 265 | ogre_->setConfigPath(path); |
---|
| 266 | ogre_->setup(); |
---|
[888] | 267 | root_ = ogre_->getRoot(); |
---|
[612] | 268 | if(!ogre_->load()) die(/* unable to load */); |
---|
[888] | 269 | |
---|
| 270 | server_g = new network::Server(); |
---|
[612] | 271 | } |
---|
| 272 | |
---|
[715] | 273 | void Orxonox::clientInit(std::string path) |
---|
[612] | 274 | { |
---|
[888] | 275 | COUT(2) << "initialising client" << std::endl;\ |
---|
| 276 | |
---|
[612] | 277 | ogre_->setConfigPath(path); |
---|
| 278 | ogre_->setup(); |
---|
| 279 | if(serverIp_.compare("")==0) |
---|
| 280 | client_g = new network::Client(); |
---|
| 281 | else |
---|
[888] | 282 | client_g = new network::Client(serverIp_, NETWORK_PORT); |
---|
[612] | 283 | if(!ogre_->load()) die(/* unable to load */); |
---|
| 284 | } |
---|
[888] | 285 | |
---|
| 286 | void Orxonox::standaloneInit(std::string path) |
---|
| 287 | { |
---|
| 288 | COUT(2) << "initialising standalone mode" << std::endl; |
---|
| 289 | |
---|
| 290 | ogre_->setConfigPath(path); |
---|
| 291 | ogre_->setup(); |
---|
| 292 | root_ = ogre_->getRoot(); |
---|
| 293 | if(!ogre_->load()) die(/* unable to load */); |
---|
| 294 | } |
---|
[612] | 295 | |
---|
| 296 | void Orxonox::defineResources() |
---|
| 297 | { |
---|
[715] | 298 | std::string secName, typeName, archName; |
---|
[612] | 299 | Ogre::ConfigFile cf; |
---|
| 300 | #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE |
---|
| 301 | cf.load(macBundlePath() + "/Contents/Resources/resources.cfg"); |
---|
| 302 | #else |
---|
| 303 | cf.load(dataPath_ + "resources.cfg"); |
---|
| 304 | #endif |
---|
| 305 | |
---|
| 306 | Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator(); |
---|
| 307 | while (seci.hasMoreElements()) |
---|
| 308 | { |
---|
| 309 | secName = seci.peekNextKey(); |
---|
| 310 | Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext(); |
---|
| 311 | Ogre::ConfigFile::SettingsMultiMap::iterator i; |
---|
| 312 | for (i = settings->begin(); i != settings->end(); ++i) |
---|
| 313 | { |
---|
| 314 | typeName = i->first; |
---|
| 315 | archName = i->second; |
---|
| 316 | #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE |
---|
[715] | 317 | Ogre::ResourceGroupManager::getSingleton().addResourceLocation( std::string(macBundlePath() + "/" + archName), typeName, secName); |
---|
[612] | 318 | #else |
---|
[708] | 319 | Ogre::ResourceGroupManager::getSingleton().addResourceLocation( archName, typeName, secName); |
---|
[612] | 320 | #endif |
---|
| 321 | } |
---|
| 322 | } |
---|
| 323 | } |
---|
| 324 | |
---|
| 325 | void Orxonox::setupRenderSystem() |
---|
| 326 | { |
---|
| 327 | if (!root_->restoreConfig() && !root_->showConfigDialog()) |
---|
[708] | 328 | throw Ogre::Exception(52, "User canceled the config dialog!", "OrxApplication::setupRenderSystem()"); |
---|
[612] | 329 | } |
---|
| 330 | |
---|
| 331 | void Orxonox::createRenderWindow() |
---|
| 332 | { |
---|
| 333 | root_->initialise(true, "OrxonoxV2"); |
---|
| 334 | } |
---|
| 335 | |
---|
| 336 | void Orxonox::initializeResourceGroups() |
---|
| 337 | { |
---|
[708] | 338 | Ogre::TextureManager::getSingleton().setDefaultNumMipmaps(5); |
---|
| 339 | Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups(); |
---|
[612] | 340 | } |
---|
| 341 | |
---|
| 342 | /** |
---|
| 343 | * |
---|
| 344 | * @param |
---|
| 345 | */ |
---|
| 346 | void Orxonox::createScene(void) |
---|
| 347 | { |
---|
| 348 | // Init audio |
---|
| 349 | auMan_ = new audio::AudioManager(); |
---|
| 350 | |
---|
[637] | 351 | bulletMgr_ = new BulletManager(); |
---|
| 352 | |
---|
[612] | 353 | // load this file from config |
---|
[871] | 354 | // loader_ = new loader::LevelLoader("sample.oxw"); |
---|
| 355 | // loader_->loadLevel(); |
---|
[888] | 356 | Level* startlevel = new Level("levels/sample.oxw"); |
---|
| 357 | Loader::open(startlevel); |
---|
[612] | 358 | |
---|
[708] | 359 | Ogre::Overlay* hudOverlay = Ogre::OverlayManager::getSingleton().getByName("Orxonox/HUD1.2"); |
---|
| 360 | HUD* orxonoxHud; |
---|
| 361 | orxonoxHud = new HUD(); |
---|
[612] | 362 | orxonoxHud->setEnergyValue(20); |
---|
| 363 | orxonoxHud->setEnergyDistr(20,20,60); |
---|
| 364 | hudOverlay->show(); |
---|
| 365 | |
---|
| 366 | /* |
---|
| 367 | auMan_->ambientAdd("a1"); |
---|
| 368 | auMan_->ambientAdd("a2"); |
---|
| 369 | auMan_->ambientAdd("a3"); |
---|
| 370 | //auMan->ambientAdd("ambient1"); |
---|
| 371 | auMan_->ambientStart();*/ |
---|
| 372 | } |
---|
| 373 | |
---|
| 374 | |
---|
| 375 | /** |
---|
| 376 | * |
---|
| 377 | */ |
---|
| 378 | void Orxonox::setupScene() |
---|
| 379 | { |
---|
| 380 | // SceneManager *mgr = ogre_->getSceneManager(); |
---|
| 381 | |
---|
| 382 | |
---|
| 383 | // SceneNode* node = (SceneNode*)mgr->getRootSceneNode()->getChild("OgreHeadNode"); |
---|
| 384 | // SceneNode *node = mgr->getRootSceneNode()->createChildSceneNode("OgreHeadNode", Vector3(0,0,0)); |
---|
| 385 | |
---|
| 386 | |
---|
| 387 | /* |
---|
| 388 | particle::ParticleInterface *e = new particle::ParticleInterface(mgr,"engine","Orxonox/strahl"); |
---|
| 389 | e->particleSystem_->setParameter("local_space","true"); |
---|
| 390 | e->setPositionOfEmitter(0, Vector3(0,-10,0)); |
---|
| 391 | e->setDirection(Vector3(0,0,-1)); |
---|
| 392 | e->addToSceneNode(node); |
---|
| 393 | */ |
---|
| 394 | } |
---|
| 395 | |
---|
| 396 | |
---|
| 397 | void Orxonox::setupInputSystem() |
---|
| 398 | { |
---|
| 399 | size_t windowHnd = 0; |
---|
| 400 | std::ostringstream windowHndStr; |
---|
| 401 | OIS::ParamList pl; |
---|
[663] | 402 | |
---|
| 403 | // fixes auto repeat problem |
---|
| 404 | #if defined OIS_LINUX_PLATFORM |
---|
[715] | 405 | pl.insert(std::make_pair(std::string("XAutoRepeatOn"), std::string("true"))); |
---|
[663] | 406 | #endif |
---|
| 407 | |
---|
[708] | 408 | Ogre::RenderWindow *win = ogre_->getRoot()->getAutoCreatedWindow(); |
---|
[612] | 409 | win->getCustomAttribute("WINDOW", &windowHnd); |
---|
| 410 | windowHndStr << windowHnd; |
---|
[715] | 411 | pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str())); |
---|
[612] | 412 | inputManager_ = OIS::InputManager::createInputSystem(pl); |
---|
| 413 | |
---|
| 414 | try |
---|
| 415 | { |
---|
| 416 | keyboard_ = static_cast<OIS::Keyboard*>(inputManager_->createInputObject(OIS::OISKeyboard, false)); |
---|
| 417 | mouse_ = static_cast<OIS::Mouse*>(inputManager_->createInputObject(OIS::OISMouse, true)); |
---|
| 418 | } |
---|
| 419 | catch (const OIS::Exception &e) |
---|
| 420 | { |
---|
| 421 | throw new Ogre::Exception(42, e.eText, "OrxApplication::setupInputSystem"); |
---|
| 422 | } |
---|
| 423 | } |
---|
| 424 | |
---|
[665] | 425 | // FIXME we actually want to do this differently... |
---|
[612] | 426 | void Orxonox::createFrameListener() |
---|
| 427 | { |
---|
| 428 | TickFrameListener* TickFL = new TickFrameListener(); |
---|
| 429 | ogre_->getRoot()->addFrameListener(TickFL); |
---|
| 430 | |
---|
| 431 | TimerFrameListener* TimerFL = new TimerFrameListener(); |
---|
| 432 | ogre_->getRoot()->addFrameListener(TimerFL); |
---|
| 433 | |
---|
[665] | 434 | //if(mode_!=CLIENT) // FIXME just a hack ------- remove this in future |
---|
[612] | 435 | frameListener_ = new OrxListener(keyboard_, auMan_, mode_); |
---|
| 436 | ogre_->getRoot()->addFrameListener(frameListener_); |
---|
| 437 | } |
---|
| 438 | |
---|
| 439 | void Orxonox::startRenderLoop() |
---|
| 440 | { |
---|
[665] | 441 | // FIXME |
---|
[612] | 442 | // this is a hack!!! |
---|
| 443 | // the call to reset the mouse clipping size should probably be somewhere |
---|
| 444 | // else, however this works for the moment. |
---|
| 445 | unsigned int width, height, depth; |
---|
| 446 | int left, top; |
---|
| 447 | ogre_->getRoot()->getAutoCreatedWindow()->getMetrics(width, height, depth, left, top); |
---|
| 448 | |
---|
| 449 | if(mode_!=CLIENT){ |
---|
| 450 | const OIS::MouseState &ms = mouse_->getMouseState(); |
---|
| 451 | ms.width = width; |
---|
| 452 | ms.height = height; |
---|
| 453 | } |
---|
| 454 | ogre_->getRoot()->startRendering(); |
---|
| 455 | } |
---|
| 456 | } |
---|