[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" |
---|
| 56 | #include "core/Factory.h" |
---|
| 57 | |
---|
[462] | 58 | #include "../loader/LevelLoader.h" |
---|
| 59 | #include "../audio/AudioManager.h" |
---|
[164] | 60 | |
---|
[337] | 61 | #include "spaceship_steering.h" |
---|
[164] | 62 | |
---|
[337] | 63 | |
---|
[459] | 64 | //network stuff |
---|
[462] | 65 | #include "../network/Server.h" |
---|
| 66 | #include "../network/Client.h" |
---|
[473] | 67 | #include "../network/NetworkFrameListener.h" |
---|
[459] | 68 | |
---|
[151] | 69 | // some tests to see if enet works without includsion |
---|
| 70 | //#include <enet/enet.h> |
---|
| 71 | //#include <enet/protocol.h> |
---|
[142] | 72 | |
---|
[74] | 73 | |
---|
[337] | 74 | namespace orxonox |
---|
| 75 | { |
---|
[462] | 76 | using namespace Ogre; |
---|
[74] | 77 | |
---|
[462] | 78 | // put this in seperate Class or solve the problem in another fashion |
---|
| 79 | class OrxListener : public FrameListener, public OIS::MouseListener |
---|
| 80 | { |
---|
| 81 | public: |
---|
[519] | 82 | OrxListener(OIS::Keyboard *keyboard, OIS::Mouse *mouse, audio::AudioManager* auMan, SpaceshipSteering* steering) |
---|
[462] | 83 | : mKeyboard(keyboard), mMouse(mouse) |
---|
| 84 | { |
---|
[530] | 85 | |
---|
| 86 | |
---|
| 87 | |
---|
[462] | 88 | speed = 250; |
---|
| 89 | loop = 100; |
---|
| 90 | rotate = 10; |
---|
| 91 | mouseX = 0; |
---|
| 92 | mouseY = 0; |
---|
| 93 | maxMouseX = 0; |
---|
| 94 | minMouseX = 0; |
---|
[337] | 95 | moved = false; |
---|
[530] | 96 | |
---|
[519] | 97 | steering_ = steering; |
---|
| 98 | |
---|
| 99 | steering_->brakeRotate(rotate*10); |
---|
| 100 | steering_->brakeLoop(loop); |
---|
| 101 | |
---|
[530] | 102 | |
---|
[462] | 103 | mMouse->setEventCallback(this); |
---|
| 104 | auMan_ = auMan; |
---|
[265] | 105 | } |
---|
[462] | 106 | bool frameStarted(const FrameEvent& evt) |
---|
| 107 | { |
---|
[135] | 108 | |
---|
[462] | 109 | auMan_->update(); |
---|
[74] | 110 | |
---|
[462] | 111 | mKeyboard->capture(); |
---|
| 112 | mMouse->capture(); |
---|
| 113 | if (mKeyboard->isKeyDown(OIS::KC_UP) || mKeyboard->isKeyDown(OIS::KC_W)) |
---|
[519] | 114 | steering_->moveForward(speed); |
---|
[462] | 115 | else |
---|
[519] | 116 | steering_->moveForward(0); |
---|
[462] | 117 | if(mKeyboard->isKeyDown(OIS::KC_DOWN) || mKeyboard->isKeyDown(OIS::KC_S)) |
---|
[519] | 118 | steering_->brakeForward(speed); |
---|
[462] | 119 | else |
---|
[519] | 120 | steering_->brakeForward(speed/10); |
---|
[462] | 121 | if (mKeyboard->isKeyDown(OIS::KC_RIGHT) || mKeyboard->isKeyDown(OIS::KC_D)) |
---|
[519] | 122 | steering_->loopRight(loop); |
---|
[462] | 123 | else |
---|
[519] | 124 | steering_->loopRight(0); |
---|
[462] | 125 | if (mKeyboard->isKeyDown(OIS::KC_LEFT) || mKeyboard->isKeyDown(OIS::KC_A)) |
---|
[519] | 126 | steering_->loopLeft(loop); |
---|
[462] | 127 | else |
---|
[519] | 128 | steering_->loopLeft(0); |
---|
[337] | 129 | |
---|
[462] | 130 | if(moved) { |
---|
[512] | 131 | if (mouseY<=0) |
---|
[519] | 132 | steering_->rotateUp(-mouseY*rotate); |
---|
[462] | 133 | if (mouseY>0) |
---|
[519] | 134 | steering_->rotateDown(mouseY*rotate); |
---|
[462] | 135 | if (mouseX>0) |
---|
[519] | 136 | steering_->rotateRight(mouseX*rotate); |
---|
[512] | 137 | if (mouseX<=0) |
---|
[519] | 138 | steering_->rotateLeft(-mouseX*rotate); |
---|
[512] | 139 | mouseY = 0; |
---|
| 140 | mouseX = 0; |
---|
[462] | 141 | moved = false; |
---|
| 142 | } |
---|
| 143 | else { |
---|
[519] | 144 | steering_->rotateUp(0); |
---|
| 145 | steering_->rotateDown(0); |
---|
| 146 | steering_->rotateRight(0); |
---|
| 147 | steering_->rotateLeft(0); |
---|
[462] | 148 | } |
---|
[337] | 149 | |
---|
[519] | 150 | steering_->tick(evt.timeSinceLastFrame); |
---|
[530] | 151 | |
---|
| 152 | |
---|
| 153 | |
---|
[462] | 154 | // scenemanager->spacehip->tick(evt.timesincelastframe); |
---|
| 155 | if(mKeyboard->isKeyDown(OIS::KC_ESCAPE)) |
---|
| 156 | cout << "maximal MouseX: " << maxMouseX << "\tminMouseX: " << minMouseX << endl; |
---|
| 157 | return !mKeyboard->isKeyDown(OIS::KC_ESCAPE); |
---|
| 158 | } |
---|
[337] | 159 | |
---|
[462] | 160 | bool mouseMoved(const OIS::MouseEvent &e) |
---|
[265] | 161 | { |
---|
[511] | 162 | mouseX += e.state.X.rel; |
---|
| 163 | mouseY += e.state.Y.rel; |
---|
[462] | 164 | if(mouseX>maxMouseX) maxMouseX = mouseX; |
---|
| 165 | if(mouseX<minMouseX) minMouseX = mouseX; |
---|
| 166 | cout << "mouseX: " << mouseX << "\tmouseY: " << mouseY << endl; |
---|
| 167 | moved = true; |
---|
| 168 | return true; |
---|
[265] | 169 | } |
---|
[74] | 170 | |
---|
[462] | 171 | bool mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id) { return true; } |
---|
| 172 | bool mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id) { return true; } |
---|
[137] | 173 | |
---|
[265] | 174 | private: |
---|
[462] | 175 | float speed; |
---|
| 176 | float rotate; |
---|
| 177 | float loop; |
---|
| 178 | float mouseY; |
---|
| 179 | float mouseX; |
---|
| 180 | float maxMouseX; |
---|
| 181 | float minMouseX; |
---|
| 182 | bool moved; |
---|
[265] | 183 | OIS::Keyboard *mKeyboard; |
---|
| 184 | OIS::Mouse *mMouse; |
---|
[462] | 185 | audio::AudioManager* auMan_; |
---|
[519] | 186 | SpaceshipSteering* steering_; |
---|
[462] | 187 | }; |
---|
| 188 | // init static singleton reference of Orxonox |
---|
| 189 | Orxonox* Orxonox::singletonRef_ = NULL; |
---|
[137] | 190 | |
---|
[462] | 191 | /** |
---|
| 192 | * create a new instance of Orxonox |
---|
| 193 | */ |
---|
| 194 | Orxonox::Orxonox() |
---|
| 195 | { |
---|
| 196 | ogre_ = new GraphicsEngine(); |
---|
| 197 | } |
---|
[74] | 198 | |
---|
[462] | 199 | /** |
---|
| 200 | * destruct Orxonox |
---|
| 201 | */ |
---|
| 202 | Orxonox::~Orxonox() |
---|
| 203 | { |
---|
| 204 | // nothing to delete as for now |
---|
| 205 | } |
---|
[265] | 206 | |
---|
[462] | 207 | /** |
---|
| 208 | * initialization of Orxonox object |
---|
| 209 | * @param argc argument counter |
---|
| 210 | * @param argv list of arguments |
---|
| 211 | * @param path path to config (in home dir or something) |
---|
| 212 | */ |
---|
| 213 | void Orxonox::init(int argc, char **argv, std::string path) |
---|
| 214 | { |
---|
| 215 | //TODO: initialization with command line parameters |
---|
| 216 | //TODO: find config file (assuming executable directory) |
---|
| 217 | //TODO: read config file |
---|
| 218 | //TODO: give config file to Ogre |
---|
[531] | 219 | |
---|
[462] | 220 | if(argc >=2 && strcmp(argv[1], "server")==0) |
---|
| 221 | { |
---|
| 222 | serverInit(path); |
---|
| 223 | } |
---|
| 224 | else if(argc >=2 && strcmp(argv[1], "client")==0) |
---|
| 225 | { |
---|
| 226 | clientInit(path); |
---|
[531] | 227 | } else if(argc >=2 && strcmp(argv[1], "presentation")==0) |
---|
| 228 | { |
---|
| 229 | playableServer(path); |
---|
| 230 | } else |
---|
| 231 | standalone(path); |
---|
[462] | 232 | } |
---|
[263] | 233 | |
---|
[462] | 234 | /** |
---|
| 235 | * start modules |
---|
| 236 | */ |
---|
| 237 | void Orxonox::start() |
---|
| 238 | { |
---|
| 239 | //TODO: start modules |
---|
[480] | 240 | |
---|
[462] | 241 | //TODO: run engine |
---|
| 242 | } |
---|
[74] | 243 | |
---|
[462] | 244 | /** |
---|
| 245 | * @return singleton object |
---|
| 246 | */ |
---|
| 247 | Orxonox* Orxonox::getSingleton() |
---|
| 248 | { |
---|
| 249 | if (!singletonRef_) |
---|
| 250 | singletonRef_ = new Orxonox(); |
---|
| 251 | return singletonRef_; |
---|
| 252 | } |
---|
[74] | 253 | |
---|
[462] | 254 | /** |
---|
| 255 | * error kills orxonox |
---|
| 256 | */ |
---|
| 257 | void Orxonox::die(/* some error code */) |
---|
| 258 | { |
---|
| 259 | //TODO: destroy and destruct everything and print nice error msg |
---|
| 260 | } |
---|
[74] | 261 | |
---|
[462] | 262 | void Orxonox::standalone(std::string path) |
---|
| 263 | { |
---|
| 264 | ogre_->setConfigPath(path); |
---|
| 265 | ogre_->setup(); |
---|
[473] | 266 | root_ = ogre_->getRoot(); |
---|
| 267 | //if(!ogre_->load()) die(/* unable to load */); |
---|
| 268 | |
---|
| 269 | defineResources(); |
---|
| 270 | setupRenderSystem(); |
---|
| 271 | createRenderWindow(); |
---|
| 272 | initializeResourceGroups(); |
---|
| 273 | createScene(); |
---|
| 274 | setupScene(); |
---|
| 275 | setupInputSystem(); |
---|
| 276 | createFrameListener(); |
---|
[496] | 277 | Factory::createClassHierarchy(); |
---|
[473] | 278 | startRenderLoop(); |
---|
[462] | 279 | } |
---|
[531] | 280 | |
---|
| 281 | void Orxonox::playableServer(std::string path) |
---|
| 282 | { |
---|
| 283 | ogre_->setConfigPath(path); |
---|
| 284 | ogre_->setup(); |
---|
| 285 | root_ = ogre_->getRoot(); |
---|
| 286 | defineResources(); |
---|
| 287 | setupRenderSystem(); |
---|
| 288 | createRenderWindow(); |
---|
| 289 | initializeResourceGroups(); |
---|
| 290 | createScene(); |
---|
| 291 | setupScene(); |
---|
| 292 | setupInputSystem(); |
---|
| 293 | Factory::createClassHierarchy(); |
---|
| 294 | createFrameListener(); |
---|
| 295 | try{ |
---|
| 296 | server_g = new network::Server(); // add port and bindadress |
---|
| 297 | server_g->open(); // open server and create listener thread |
---|
| 298 | if(ogre_ && ogre_->getRoot()) |
---|
| 299 | ogre_->getRoot()->addFrameListener(new network::ServerFrameListener()); // adds a framelistener for the server |
---|
| 300 | std::cout << "network framelistener added" << std::endl; |
---|
| 301 | } |
---|
| 302 | catch(exception &e) |
---|
| 303 | { |
---|
| 304 | std::cout << "There was a problem initialising the server :(" << std::endl; |
---|
| 305 | } |
---|
| 306 | startRenderLoop(); |
---|
| 307 | } |
---|
[74] | 308 | |
---|
[462] | 309 | void Orxonox::serverInit(std::string path) |
---|
| 310 | { |
---|
| 311 | ogre_->setConfigPath(path); |
---|
| 312 | ogre_->setup(); |
---|
[473] | 313 | server_g = new network::Server(); // add some settings if wanted |
---|
[462] | 314 | if(!ogre_->load()) die(/* unable to load */); |
---|
[505] | 315 | ogre_->getRoot()->addFrameListener(new network::ServerFrameListener()); |
---|
[462] | 316 | ogre_->startRender(); |
---|
[389] | 317 | |
---|
[462] | 318 | createScene(); |
---|
| 319 | setupScene(); |
---|
| 320 | } |
---|
[164] | 321 | |
---|
[462] | 322 | void Orxonox::clientInit(std::string path) |
---|
| 323 | { |
---|
| 324 | ogre_->setConfigPath(path); |
---|
| 325 | ogre_->setup(); |
---|
[473] | 326 | client_g = new network::Client(); // address here |
---|
[462] | 327 | if(!ogre_->load()) die(/* unable to load */); |
---|
[505] | 328 | ogre_->getRoot()->addFrameListener(new network::ClientFrameListener()); |
---|
[462] | 329 | ogre_->startRender(); |
---|
[389] | 330 | |
---|
[462] | 331 | createScene(); |
---|
| 332 | setupScene(); |
---|
| 333 | setupInputSystem(); |
---|
| 334 | createFrameListener(); |
---|
| 335 | startRenderLoop(); |
---|
| 336 | } |
---|
[258] | 337 | |
---|
[473] | 338 | void Orxonox::defineResources() |
---|
| 339 | { |
---|
| 340 | Ogre::String secName, typeName, archName; |
---|
| 341 | Ogre::ConfigFile cf; |
---|
| 342 | #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE |
---|
| 343 | cf.load(macBundlePath() + "/Contents/Resources/resources.cfg"); |
---|
| 344 | #else |
---|
| 345 | cf.load("resources.cfg"); |
---|
| 346 | #endif |
---|
| 347 | |
---|
| 348 | Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator(); |
---|
| 349 | while (seci.hasMoreElements()) |
---|
| 350 | { |
---|
| 351 | secName = seci.peekNextKey(); |
---|
| 352 | Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext(); |
---|
| 353 | Ogre::ConfigFile::SettingsMultiMap::iterator i; |
---|
| 354 | for (i = settings->begin(); i != settings->end(); ++i) |
---|
| 355 | { |
---|
| 356 | typeName = i->first; |
---|
| 357 | archName = i->second; |
---|
| 358 | #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE |
---|
| 359 | Ogre::ResourceGroupManager::getSingleton().addResourceLocation( String(macBundlePath() + "/" + archName), typeName, secName); |
---|
| 360 | #else |
---|
| 361 | Ogre::ResourceGroupManager::getSingleton().addResourceLocation( archName, typeName, secName); |
---|
| 362 | #endif |
---|
| 363 | } |
---|
| 364 | } |
---|
| 365 | } |
---|
| 366 | |
---|
| 367 | void Orxonox::setupRenderSystem() |
---|
| 368 | { |
---|
| 369 | if (!root_->restoreConfig() && !root_->showConfigDialog()) |
---|
| 370 | throw Exception(52, "User canceled the config dialog!", "OrxApplication::setupRenderSystem()"); |
---|
| 371 | } |
---|
| 372 | |
---|
| 373 | void Orxonox::createRenderWindow() |
---|
| 374 | { |
---|
| 375 | root_->initialise(true, "OrxonoxV2"); |
---|
| 376 | } |
---|
| 377 | |
---|
| 378 | void Orxonox::initializeResourceGroups() |
---|
| 379 | { |
---|
| 380 | TextureManager::getSingleton().setDefaultNumMipmaps(5); |
---|
| 381 | ResourceGroupManager::getSingleton().initialiseAllResourceGroups(); |
---|
| 382 | } |
---|
| 383 | |
---|
[462] | 384 | void Orxonox::createScene(void) |
---|
| 385 | { |
---|
[480] | 386 | // Init audio |
---|
[462] | 387 | auMan_ = new audio::AudioManager(); |
---|
[487] | 388 | |
---|
[480] | 389 | // load this file from config |
---|
| 390 | loader_ = new loader::LevelLoader("sample.oxw"); |
---|
| 391 | loader_->loadLevel(); |
---|
[337] | 392 | |
---|
[515] | 393 | |
---|
| 394 | |
---|
| 395 | |
---|
| 396 | |
---|
[480] | 397 | /* |
---|
[462] | 398 | auMan_->ambientAdd("a1"); |
---|
| 399 | auMan_->ambientAdd("a2"); |
---|
| 400 | auMan_->ambientAdd("a3"); |
---|
| 401 | //auMan->ambientAdd("ambient1"); |
---|
| 402 | auMan_->ambientStart(); |
---|
[480] | 403 | */ |
---|
[462] | 404 | } |
---|
[337] | 405 | |
---|
| 406 | |
---|
[462] | 407 | void Orxonox::setupScene() |
---|
| 408 | { |
---|
| 409 | SceneManager *mgr = ogre_->getSceneManager(); |
---|
[512] | 410 | |
---|
[515] | 411 | |
---|
[525] | 412 | SceneNode* node = (SceneNode*)mgr->getRootSceneNode()->getChild("OgreHeadNode"); |
---|
[515] | 413 | |
---|
[337] | 414 | |
---|
[525] | 415 | steering_ = new SpaceshipSteering(500, 200, 200, 200); |
---|
| 416 | steering_->addNode(node); |
---|
[74] | 417 | |
---|
[462] | 418 | } |
---|
[137] | 419 | |
---|
| 420 | |
---|
[462] | 421 | void Orxonox::setupInputSystem() |
---|
| 422 | { |
---|
| 423 | size_t windowHnd = 0; |
---|
| 424 | std::ostringstream windowHndStr; |
---|
| 425 | OIS::ParamList pl; |
---|
| 426 | Ogre::RenderWindow *win = ogre_->getRoot()->getAutoCreatedWindow(); |
---|
[137] | 427 | |
---|
[462] | 428 | win->getCustomAttribute("WINDOW", &windowHnd); |
---|
| 429 | windowHndStr << windowHnd; |
---|
| 430 | pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str())); |
---|
| 431 | inputManager_ = OIS::InputManager::createInputSystem(pl); |
---|
[265] | 432 | |
---|
[462] | 433 | try |
---|
[337] | 434 | { |
---|
[462] | 435 | keyboard_ = static_cast<OIS::Keyboard*>(inputManager_->createInputObject(OIS::OISKeyboard, false)); |
---|
| 436 | mouse_ = static_cast<OIS::Mouse*>(inputManager_->createInputObject(OIS::OISMouse, true)); |
---|
[337] | 437 | } |
---|
[462] | 438 | catch (const OIS::Exception &e) |
---|
[459] | 439 | { |
---|
[462] | 440 | throw new Ogre::Exception(42, e.eText, "OrxApplication::setupInputSystem"); |
---|
[459] | 441 | } |
---|
[462] | 442 | } |
---|
[137] | 443 | |
---|
[462] | 444 | // we actually want to do this differently... |
---|
| 445 | void Orxonox::createFrameListener() |
---|
[137] | 446 | { |
---|
[496] | 447 | TickFrameListener* TickFL = new TickFrameListener(); |
---|
| 448 | ogre_->getRoot()->addFrameListener(TickFL); |
---|
| 449 | |
---|
| 450 | TimerFrameListener* TimerFL = new TimerFrameListener(); |
---|
| 451 | ogre_->getRoot()->addFrameListener(TimerFL); |
---|
| 452 | |
---|
[519] | 453 | frameListener_ = new OrxListener(keyboard_, mouse_, auMan_, steering_); |
---|
[462] | 454 | ogre_->getRoot()->addFrameListener(frameListener_); |
---|
[137] | 455 | } |
---|
[462] | 456 | |
---|
| 457 | void Orxonox::startRenderLoop() |
---|
[137] | 458 | { |
---|
[511] | 459 | // this is a hack!!! |
---|
| 460 | // the call to reset the mouse clipping size should probably be somewhere |
---|
| 461 | // else, however this works for the moment. |
---|
[523] | 462 | unsigned int width, height, depth; |
---|
| 463 | int left, top; |
---|
| 464 | ogre_->getRoot()->getAutoCreatedWindow()->getMetrics(width, height, depth, left, top); |
---|
[511] | 465 | |
---|
[523] | 466 | const OIS::MouseState &ms = mouse_->getMouseState(); |
---|
| 467 | ms.width = width; |
---|
| 468 | ms.height = height; |
---|
[511] | 469 | |
---|
[462] | 470 | ogre_->getRoot()->startRendering(); |
---|
[74] | 471 | } |
---|
| 472 | } |
---|