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