[74] | 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 modify |
---|
| 8 | * it under the terms of the GNU General Public License as published by |
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or |
---|
| 10 | * (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, see <http://www.gnu.org/licenses/>. |
---|
| 19 | * |
---|
| 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 |
---|
| 30 | @brief Orxonox Main File |
---|
| 31 | */ |
---|
| 32 | |
---|
[379] | 33 | //#include <Ogre.h> |
---|
| 34 | // 40% speed up: (instead of Ogre.h) |
---|
| 35 | #include <OgreSceneNode.h> |
---|
| 36 | #include <OgreSceneManager.h> |
---|
| 37 | #include <OgreRoot.h> |
---|
| 38 | #include <OgreFrameListener.h> |
---|
| 39 | #include <OgreConfigFile.h> |
---|
| 40 | #include <OgreTextureManager.h> |
---|
| 41 | #include <OgreEntity.h> |
---|
| 42 | #include <OgreRenderWindow.h> |
---|
| 43 | |
---|
[137] | 44 | #include <OIS/OIS.h> |
---|
[346] | 45 | //#include <CEGUI/CEGUI.h> |
---|
| 46 | //#include <OgreCEGUIRenderer.h> |
---|
[74] | 47 | |
---|
[164] | 48 | #include <string> |
---|
| 49 | #include <iostream> |
---|
| 50 | |
---|
[337] | 51 | #include "xml/xmlParser.h" |
---|
| 52 | #include "loader/LevelLoader.h" |
---|
[389] | 53 | #include "audio/AudioManager.h" |
---|
[164] | 54 | |
---|
[337] | 55 | #include "spaceship_steering.h" |
---|
| 56 | SpaceshipSteering* steering; |
---|
[164] | 57 | |
---|
[419] | 58 | audio::AudioManager* auMan; |
---|
[337] | 59 | |
---|
[151] | 60 | // some tests to see if enet works without includsion |
---|
| 61 | //#include <enet/enet.h> |
---|
| 62 | //#include <enet/protocol.h> |
---|
[142] | 63 | |
---|
[137] | 64 | #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE |
---|
| 65 | #include <CoreFoundation/CoreFoundation.h> |
---|
[74] | 66 | |
---|
[137] | 67 | // This function will locate the path to our application on OS X, |
---|
| 68 | // unlike windows you can not rely on the curent working directory |
---|
| 69 | // for locating your configuration files and resources. |
---|
| 70 | std::string macBundlePath() |
---|
[74] | 71 | { |
---|
[137] | 72 | char path[1024]; |
---|
| 73 | CFBundleRef mainBundle = CFBundleGetMainBundle(); |
---|
| 74 | assert(mainBundle); |
---|
| 75 | |
---|
| 76 | CFURLRef mainBundleURL = CFBundleCopyBundleURL(mainBundle); |
---|
| 77 | assert(mainBundleURL); |
---|
| 78 | |
---|
| 79 | CFStringRef cfStringRef = CFURLCopyFileSystemPath( mainBundleURL, kCFURLPOSIXPathStyle); |
---|
| 80 | assert(cfStringRef); |
---|
| 81 | |
---|
| 82 | CFStringGetCString(cfStringRef, path, 1024, kCFStringEncodingASCII); |
---|
| 83 | |
---|
| 84 | CFRelease(mainBundleURL); |
---|
| 85 | CFRelease(cfStringRef); |
---|
| 86 | |
---|
| 87 | return std::string(path); |
---|
| 88 | } |
---|
| 89 | #endif |
---|
| 90 | |
---|
[337] | 91 | namespace orxonox |
---|
| 92 | { |
---|
[74] | 93 | |
---|
[337] | 94 | using namespace Ogre; |
---|
| 95 | |
---|
| 96 | class OrxExitListener : public FrameListener, public OIS::MouseListener |
---|
| 97 | { |
---|
| 98 | public: |
---|
| 99 | OrxExitListener(OIS::Keyboard *keyboard, OIS::Mouse *mouse) |
---|
| 100 | : mKeyboard(keyboard), mMouse(mouse) |
---|
| 101 | { |
---|
| 102 | speed = 250; |
---|
| 103 | loop = 100; |
---|
| 104 | rotate = 10; |
---|
| 105 | mouseX = 0; |
---|
| 106 | mouseY = 0; |
---|
| 107 | maxMouseX = 0; |
---|
| 108 | minMouseX = 0; |
---|
| 109 | moved = false; |
---|
| 110 | steering->brakeRotate(rotate*10); |
---|
| 111 | steering->brakeLoop(loop); |
---|
| 112 | mMouse->setEventCallback(this); |
---|
| 113 | } |
---|
| 114 | bool frameStarted(const FrameEvent& evt) |
---|
| 115 | { |
---|
[419] | 116 | |
---|
| 117 | auMan->update(); |
---|
| 118 | |
---|
[337] | 119 | mKeyboard->capture(); |
---|
| 120 | mMouse->capture(); |
---|
| 121 | if (mKeyboard->isKeyDown(OIS::KC_UP) || mKeyboard->isKeyDown(OIS::KC_W)) |
---|
| 122 | steering->moveForward(speed); |
---|
| 123 | else |
---|
| 124 | steering->moveForward(0); |
---|
| 125 | if(mKeyboard->isKeyDown(OIS::KC_DOWN) || mKeyboard->isKeyDown(OIS::KC_S)) |
---|
| 126 | steering->brakeForward(speed); |
---|
| 127 | else |
---|
| 128 | steering->brakeForward(speed/10); |
---|
| 129 | if (mKeyboard->isKeyDown(OIS::KC_RIGHT) || mKeyboard->isKeyDown(OIS::KC_D)) |
---|
| 130 | steering->loopRight(loop); |
---|
| 131 | else |
---|
| 132 | steering->loopRight(0); |
---|
| 133 | if (mKeyboard->isKeyDown(OIS::KC_LEFT) || mKeyboard->isKeyDown(OIS::KC_A)) |
---|
| 134 | steering->loopLeft(loop); |
---|
| 135 | else |
---|
| 136 | steering->loopLeft(0); |
---|
| 137 | |
---|
| 138 | if(moved) { |
---|
| 139 | if (mouseY<0) |
---|
| 140 | steering->rotateUp(-mouseY*rotate); |
---|
| 141 | if (mouseY>0) |
---|
| 142 | steering->rotateDown(mouseY*rotate); |
---|
| 143 | if (mouseX>0) |
---|
| 144 | steering->rotateRight(mouseX*rotate); |
---|
| 145 | if (mouseX<0) |
---|
| 146 | steering->rotateLeft(-mouseX*rotate); |
---|
| 147 | moved = false; |
---|
[265] | 148 | } |
---|
[337] | 149 | else { |
---|
| 150 | steering->rotateUp(0); |
---|
| 151 | steering->rotateDown(0); |
---|
| 152 | steering->rotateRight(0); |
---|
| 153 | steering->rotateLeft(0); |
---|
| 154 | } |
---|
[135] | 155 | |
---|
[337] | 156 | steering->tick(evt.timeSinceLastFrame); |
---|
| 157 | // scenemanager->spacehip->tick(evt.timesincelastframe); |
---|
| 158 | if(mKeyboard->isKeyDown(OIS::KC_ESCAPE)) |
---|
| 159 | cout << "maximal MouseX: " << maxMouseX << "\tminMouseX: " << minMouseX << endl; |
---|
| 160 | return !mKeyboard->isKeyDown(OIS::KC_ESCAPE); |
---|
| 161 | } |
---|
[74] | 162 | |
---|
[337] | 163 | bool mouseMoved(const OIS::MouseEvent &e) |
---|
| 164 | { |
---|
| 165 | mouseX = e.state.X.rel; |
---|
| 166 | mouseY = e.state.Y.rel; |
---|
| 167 | if(mouseX>maxMouseX) maxMouseX = mouseX; |
---|
| 168 | if(mouseX<minMouseX) minMouseX = mouseX; |
---|
| 169 | cout << "mouseX: " << mouseX << "\tmouseY: " << mouseY << endl; |
---|
| 170 | moved = true; |
---|
| 171 | return true; |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | bool mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id) { return true; } |
---|
| 175 | bool mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id) { return true; } |
---|
| 176 | |
---|
| 177 | private: |
---|
| 178 | float speed; |
---|
| 179 | float rotate; |
---|
| 180 | float loop; |
---|
| 181 | float mouseY; |
---|
| 182 | float mouseX; |
---|
| 183 | float maxMouseX; |
---|
| 184 | float minMouseX; |
---|
| 185 | bool moved; |
---|
| 186 | OIS::Keyboard *mKeyboard; |
---|
| 187 | OIS::Mouse *mMouse; |
---|
| 188 | }; |
---|
| 189 | |
---|
| 190 | |
---|
[265] | 191 | class OrxApplication |
---|
| 192 | { |
---|
| 193 | public: |
---|
| 194 | void go() |
---|
| 195 | { |
---|
| 196 | createRoot(); |
---|
| 197 | defineResources(); |
---|
| 198 | setupRenderSystem(); |
---|
| 199 | createRenderWindow(); |
---|
| 200 | initializeResourceGroups(); |
---|
| 201 | createScene(); |
---|
| 202 | setupScene(); |
---|
| 203 | setupInputSystem(); |
---|
| 204 | setupCEGUI(); |
---|
| 205 | createFrameListener(); |
---|
| 206 | startRenderLoop(); |
---|
| 207 | } |
---|
[74] | 208 | |
---|
[265] | 209 | ~OrxApplication() |
---|
| 210 | { |
---|
| 211 | mInputManager->destroyInputObject(mKeyboard); |
---|
| 212 | OIS::InputManager::destroyInputSystem(mInputManager); |
---|
[137] | 213 | |
---|
[265] | 214 | // delete mRenderer; |
---|
| 215 | // delete mSystem; |
---|
[137] | 216 | |
---|
[265] | 217 | delete mListener; |
---|
| 218 | delete mRoot; |
---|
| 219 | } |
---|
[137] | 220 | |
---|
[265] | 221 | private: |
---|
| 222 | Ogre::Root *mRoot; |
---|
| 223 | OIS::Keyboard *mKeyboard; |
---|
| 224 | OIS::Mouse *mMouse; |
---|
| 225 | OIS::InputManager *mInputManager; |
---|
[346] | 226 | //CEGUI::OgreCEGUIRenderer *mRenderer; |
---|
| 227 | //CEGUI::System *mSystem; |
---|
[265] | 228 | OrxExitListener *mListener; |
---|
[137] | 229 | |
---|
[265] | 230 | void createRoot() |
---|
| 231 | { |
---|
| 232 | #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE |
---|
| 233 | mRoot = new Ogre::Root(macBundlePath() + "/Contents/Resources/plugins.cfg"); |
---|
| 234 | #else |
---|
| 235 | mRoot = new Ogre::Root(); |
---|
| 236 | #endif |
---|
| 237 | } |
---|
[74] | 238 | |
---|
[265] | 239 | void defineResources() |
---|
| 240 | { |
---|
| 241 | Ogre::String secName, typeName, archName; |
---|
| 242 | Ogre::ConfigFile cf; |
---|
| 243 | #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE |
---|
| 244 | cf.load(macBundlePath() + "/Contents/Resources/resources.cfg"); |
---|
| 245 | #else |
---|
| 246 | cf.load("resources.cfg"); |
---|
| 247 | #endif |
---|
| 248 | |
---|
| 249 | Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator(); |
---|
| 250 | while (seci.hasMoreElements()) |
---|
[263] | 251 | { |
---|
[265] | 252 | secName = seci.peekNextKey(); |
---|
| 253 | Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext(); |
---|
| 254 | Ogre::ConfigFile::SettingsMultiMap::iterator i; |
---|
| 255 | for (i = settings->begin(); i != settings->end(); ++i) |
---|
[263] | 256 | { |
---|
[265] | 257 | typeName = i->first; |
---|
| 258 | archName = i->second; |
---|
| 259 | #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE |
---|
| 260 | Ogre::ResourceGroupManager::getSingleton().addResourceLocation( String(macBundlePath() + "/" + archName), typeName, secName); |
---|
| 261 | #else |
---|
| 262 | Ogre::ResourceGroupManager::getSingleton().addResourceLocation( archName, typeName, secName); |
---|
| 263 | #endif |
---|
[263] | 264 | } |
---|
| 265 | } |
---|
[265] | 266 | } |
---|
[263] | 267 | |
---|
[265] | 268 | void setupRenderSystem() |
---|
| 269 | { |
---|
| 270 | if (!mRoot->restoreConfig() && !mRoot->showConfigDialog()) |
---|
[337] | 271 | throw Exception(52, "User canceled the config dialog!", "OrxApplication::setupRenderSystem()"); |
---|
[265] | 272 | } |
---|
[74] | 273 | |
---|
[265] | 274 | void createRenderWindow() |
---|
| 275 | { |
---|
[368] | 276 | mRoot->initialise(true, "OrxonoxV2"); |
---|
[265] | 277 | } |
---|
[74] | 278 | |
---|
[265] | 279 | void initializeResourceGroups() |
---|
| 280 | { |
---|
[337] | 281 | TextureManager::getSingleton().setDefaultNumMipmaps(5); |
---|
| 282 | ResourceGroupManager::getSingleton().initialiseAllResourceGroups(); |
---|
[265] | 283 | } |
---|
[74] | 284 | |
---|
[265] | 285 | void createScene(void) |
---|
| 286 | { |
---|
[419] | 287 | auMan = new audio::AudioManager(); |
---|
[74] | 288 | |
---|
[389] | 289 | |
---|
[347] | 290 | string levelFile = "sp_level_moonstation.oxw"; |
---|
| 291 | loader::LevelLoader* loader = new loader::LevelLoader(levelFile); |
---|
[337] | 292 | } |
---|
[164] | 293 | |
---|
[389] | 294 | |
---|
[337] | 295 | void setupScene() |
---|
| 296 | { |
---|
| 297 | SceneManager *mgr = mRoot->createSceneManager(ST_GENERIC, "Default SceneManager"); |
---|
| 298 | Camera *cam = mgr->createCamera("Camera"); |
---|
| 299 | cam->setPosition(Vector3(0,0,-250)); |
---|
| 300 | cam->lookAt(Vector3(0,0,0)); |
---|
| 301 | Viewport *vp = mRoot->getAutoCreatedWindow()->addViewport(cam); |
---|
[258] | 302 | |
---|
[337] | 303 | |
---|
| 304 | mgr->setAmbientLight(ColourValue(1,1,1)); |
---|
| 305 | Entity* head = mgr->createEntity("head", "ogrehead.mesh"); |
---|
| 306 | SceneNode *node = mgr->getRootSceneNode()->createChildSceneNode("OgreHeadNode", Vector3(0,0,0)); |
---|
| 307 | node->attachObject(head); |
---|
| 308 | node->attachObject(cam); |
---|
| 309 | mgr->setSkyBox(true, "Examples/SceneSkyBox2"); |
---|
| 310 | |
---|
| 311 | Entity* head1 = mgr->createEntity("head1", "ogrehead.mesh"); |
---|
| 312 | SceneNode *node1 = mgr->getRootSceneNode()->createChildSceneNode("OgreHeadNode1", Vector3(200,0,0)); |
---|
| 313 | node1->attachObject(head1); |
---|
| 314 | Entity* head2 = mgr->createEntity("head2", "ogrehead.mesh"); |
---|
| 315 | SceneNode *node2 = mgr->getRootSceneNode()->createChildSceneNode("OgreHeadNode2", Vector3(200,400,-100)); |
---|
| 316 | node2->attachObject(head2); |
---|
| 317 | Entity* head3 = mgr->createEntity("head3", "ogrehead.mesh"); |
---|
| 318 | SceneNode *node3 = mgr->getRootSceneNode()->createChildSceneNode("OgreHeadNode3", Vector3(0,400,200)); |
---|
| 319 | node3->attachObject(head3); |
---|
| 320 | Entity* head4 = mgr->createEntity("head4", "ogrehead.mesh"); |
---|
| 321 | SceneNode *node4 = mgr->getRootSceneNode()->createChildSceneNode("OgreHeadNode4", Vector3(-400,-200,600)); |
---|
| 322 | node4->attachObject(head4); |
---|
| 323 | Entity* head5 = mgr->createEntity("head5", "ogrehead.mesh"); |
---|
| 324 | SceneNode *node5 = mgr->getRootSceneNode()->createChildSceneNode("OgreHeadNode5", Vector3(0,0,-400)); |
---|
| 325 | node5->attachObject(head5); |
---|
| 326 | |
---|
| 327 | steering = new SpaceshipSteering(500, 200, 200, 200); |
---|
| 328 | steering->addNode(node); |
---|
| 329 | |
---|
| 330 | } |
---|
| 331 | |
---|
| 332 | |
---|
[265] | 333 | void setupInputSystem() |
---|
| 334 | { |
---|
| 335 | size_t windowHnd = 0; |
---|
| 336 | std::ostringstream windowHndStr; |
---|
| 337 | OIS::ParamList pl; |
---|
| 338 | Ogre::RenderWindow *win = mRoot->getAutoCreatedWindow(); |
---|
[74] | 339 | |
---|
[265] | 340 | win->getCustomAttribute("WINDOW", &windowHnd); |
---|
| 341 | windowHndStr << windowHnd; |
---|
| 342 | pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str())); |
---|
| 343 | mInputManager = OIS::InputManager::createInputSystem(pl); |
---|
[74] | 344 | |
---|
[265] | 345 | try |
---|
| 346 | { |
---|
| 347 | mKeyboard = static_cast<OIS::Keyboard*>(mInputManager->createInputObject(OIS::OISKeyboard, false)); |
---|
[337] | 348 | mMouse = static_cast<OIS::Mouse*>(mInputManager->createInputObject(OIS::OISMouse, true)); |
---|
[263] | 349 | } |
---|
[265] | 350 | catch (const OIS::Exception &e) |
---|
[263] | 351 | { |
---|
[265] | 352 | throw new Ogre::Exception(42, e.eText, "OrxApplication::setupInputSystem"); |
---|
| 353 | } |
---|
| 354 | } |
---|
[74] | 355 | |
---|
[265] | 356 | void setupCEGUI() |
---|
| 357 | { |
---|
| 358 | Ogre::SceneManager *mgr = mRoot->getSceneManager("Default SceneManager"); |
---|
| 359 | Ogre::RenderWindow *win = mRoot->getAutoCreatedWindow(); |
---|
[137] | 360 | |
---|
[265] | 361 | // CEGUI setup |
---|
| 362 | // mRenderer = new CEGUI::OgreCEGUIRenderer(win, Ogre::RENDER_QUEUE_OVERLAY, false, 3000, mgr); |
---|
| 363 | // mSystem = new CEGUI::System(mRenderer); |
---|
[137] | 364 | |
---|
[265] | 365 | // Other CEGUI setup here. |
---|
| 366 | } |
---|
[137] | 367 | |
---|
[265] | 368 | |
---|
[337] | 369 | void createFrameListener() |
---|
| 370 | { |
---|
| 371 | mListener = new OrxExitListener(mKeyboard, mMouse); |
---|
| 372 | mRoot->addFrameListener(mListener); |
---|
| 373 | } |
---|
[265] | 374 | void startRenderLoop() |
---|
| 375 | { |
---|
| 376 | mRoot->startRendering(); |
---|
| 377 | } |
---|
| 378 | }; |
---|
[337] | 379 | } |
---|
[137] | 380 | |
---|
[337] | 381 | using namespace Ogre; |
---|
| 382 | |
---|
[137] | 383 | #if OGRE_PLATFORM == PLATFORM_WIN32 || OGRE_PLATFORM == OGRE_PLATFORM_WIN32 |
---|
[74] | 384 | #define WIN32_LEAN_AND_MEAN |
---|
| 385 | #include "windows.h" |
---|
| 386 | |
---|
[137] | 387 | INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT) |
---|
[74] | 388 | #else |
---|
[137] | 389 | int main(int argc, char **argv) |
---|
[74] | 390 | #endif |
---|
| 391 | { |
---|
[137] | 392 | try |
---|
| 393 | { |
---|
[263] | 394 | orxonox::OrxApplication orxonox; |
---|
[74] | 395 | orxonox.go(); |
---|
[137] | 396 | } |
---|
| 397 | catch(Exception& e) |
---|
| 398 | { |
---|
| 399 | #if OGRE_PLATFORM == PLATFORM_WIN32 || OGRE_PLATFORM == OGRE_PLATFORM_WIN32 |
---|
| 400 | MessageBoxA(NULL, e.getFullDescription().c_str(), "An exception has occurred!", MB_OK | MB_ICONERROR | MB_TASKMODAL); |
---|
[74] | 401 | #else |
---|
| 402 | fprintf(stderr, "An exception has occurred: %s\n", |
---|
| 403 | e.getFullDescription().c_str()); |
---|
| 404 | #endif |
---|
| 405 | } |
---|
| 406 | |
---|
| 407 | return 0; |
---|
| 408 | } |
---|
[137] | 409 | |
---|