| 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 |  | 
|---|
| 28 | /** | 
|---|
| 29 |  @file  orxonox.cc | 
|---|
| 30 |  @brief Orxonox Main File | 
|---|
| 31 |  */ | 
|---|
| 32 |  | 
|---|
| 33 | #include <Ogre.h> | 
|---|
| 34 | #include <OIS/OIS.h> | 
|---|
| 35 | #include <CEGUI/CEGUI.h> | 
|---|
| 36 | #include <OgreCEGUIRenderer.h> | 
|---|
| 37 |  | 
|---|
| 38 | #include <string> | 
|---|
| 39 | #include <iostream> | 
|---|
| 40 |  | 
|---|
| 41 | #include "xml/xmlParser.h" | 
|---|
| 42 | #include "loader/LevelLoader.h" | 
|---|
| 43 | #include "Flocking.h" | 
|---|
| 44 |  | 
|---|
| 45 | // some tests to see if enet works without includsion | 
|---|
| 46 | //#include <enet/enet.h> | 
|---|
| 47 | //#include <enet/protocol.h> | 
|---|
| 48 |  | 
|---|
| 49 | #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE | 
|---|
| 50 | #include <CoreFoundation/CoreFoundation.h> | 
|---|
| 51 |  | 
|---|
| 52 | // This function will locate the path to our application on OS X, | 
|---|
| 53 | // unlike windows you can not rely on the curent working directory | 
|---|
| 54 | // for locating your configuration files and resources. | 
|---|
| 55 | std::string macBundlePath() | 
|---|
| 56 | { | 
|---|
| 57 |   char path[1024]; | 
|---|
| 58 |   CFBundleRef mainBundle = CFBundleGetMainBundle(); | 
|---|
| 59 |   assert(mainBundle); | 
|---|
| 60 |  | 
|---|
| 61 |   CFURLRef mainBundleURL = CFBundleCopyBundleURL(mainBundle); | 
|---|
| 62 |   assert(mainBundleURL); | 
|---|
| 63 |  | 
|---|
| 64 |   CFStringRef cfStringRef = CFURLCopyFileSystemPath( mainBundleURL, kCFURLPOSIXPathStyle); | 
|---|
| 65 |   assert(cfStringRef); | 
|---|
| 66 |  | 
|---|
| 67 |   CFStringGetCString(cfStringRef, path, 1024, kCFStringEncodingASCII); | 
|---|
| 68 |  | 
|---|
| 69 |   CFRelease(mainBundleURL); | 
|---|
| 70 |   CFRelease(cfStringRef); | 
|---|
| 71 |  | 
|---|
| 72 |   return std::string(path); | 
|---|
| 73 | } | 
|---|
| 74 | #endif | 
|---|
| 75 |  | 
|---|
| 76 | using namespace Ogre; | 
|---|
| 77 |  | 
|---|
| 78 | //my-stuff | 
|---|
| 79 | //globale definition eines Arrays welches alle nodes enthält | 
|---|
| 80 | Vector3 ElementLocationArray[3]; | 
|---|
| 81 | Vector3 ElementSpeedArray[3]; | 
|---|
| 82 | Vector3 ElementAccelerationArray[3]; | 
|---|
| 83 |  | 
|---|
| 84 | Element arrayOfElements[3]; | 
|---|
| 85 |  | 
|---|
| 86 |  | 
|---|
| 87 | class OrxExitListener : public FrameListener | 
|---|
| 88 | { | 
|---|
| 89 |   public: | 
|---|
| 90 |     OrxExitListener(OIS::Keyboard *keyboard, Root* root) | 
|---|
| 91 |   : mKeyboard(keyboard) | 
|---|
| 92 |     { | 
|---|
| 93 |         root_ = root; | 
|---|
| 94 |     } | 
|---|
| 95 |  | 
|---|
| 96 |     bool frameStarted(const FrameEvent& evt) | 
|---|
| 97 |     { | 
|---|
| 98 |       moving(evt); | 
|---|
| 99 |       mKeyboard->capture(); | 
|---|
| 100 |       return !mKeyboard->isKeyDown(OIS::KC_ESCAPE); | 
|---|
| 101 |     } | 
|---|
| 102 |    | 
|---|
| 103 |     //code the movments of the nodes here | 
|---|
| 104 |     void moving(const FrameEvent& evt) { | 
|---|
| 105 |       SceneManager *mgr = root_->getSceneManager("Default SceneManager"); | 
|---|
| 106 |  | 
|---|
| 107 |  | 
|---|
| 108 |  | 
|---|
| 109 |       arrayOfElements[0].update(arrayOfElements, evt); | 
|---|
| 110 |       arrayOfElements[1].update(arrayOfElements, evt); | 
|---|
| 111 |       arrayOfElements[2].update(arrayOfElements, evt); | 
|---|
| 112 |  | 
|---|
| 113 |       mgr->getSceneNode("HeadNode1")->setPosition(arrayOfElements[0].location); | 
|---|
| 114 |       mgr->getSceneNode("HeadNode2")->setPosition(arrayOfElements[1].location); | 
|---|
| 115 |       mgr->getSceneNode("HeadNode3")->setPosition(arrayOfElements[2].location); | 
|---|
| 116 |  | 
|---|
| 117 |  | 
|---|
| 118 |  | 
|---|
| 119 |     //  mgr->getSceneNode("HeadNode1")->yaw((Radian)10*evt.timeSinceLastFrame); | 
|---|
| 120 |     } | 
|---|
| 121 |  | 
|---|
| 122 |   private: | 
|---|
| 123 |     OIS::Keyboard *mKeyboard; | 
|---|
| 124 |     Root* root_; | 
|---|
| 125 | }; | 
|---|
| 126 |  | 
|---|
| 127 | class OrxApplication | 
|---|
| 128 | { | 
|---|
| 129 |   public: | 
|---|
| 130 |     void go() | 
|---|
| 131 |     { | 
|---|
| 132 |       createRoot(); | 
|---|
| 133 |       defineResources(); | 
|---|
| 134 |       setupRenderSystem(); | 
|---|
| 135 |       createRenderWindow(); | 
|---|
| 136 |       initializeResourceGroups(); | 
|---|
| 137 |       createScene(); | 
|---|
| 138 |       setupScene(); | 
|---|
| 139 |       setupInputSystem(); | 
|---|
| 140 |       setupCEGUI(); | 
|---|
| 141 |       createFrameListener(); | 
|---|
| 142 |       startRenderLoop(); | 
|---|
| 143 |     } | 
|---|
| 144 |  | 
|---|
| 145 |     ~OrxApplication() | 
|---|
| 146 |     { | 
|---|
| 147 |       mInputManager->destroyInputObject(mKeyboard); | 
|---|
| 148 |       OIS::InputManager::destroyInputSystem(mInputManager); | 
|---|
| 149 |  | 
|---|
| 150 | //       delete mRenderer; | 
|---|
| 151 | //       delete mSystem; | 
|---|
| 152 |  | 
|---|
| 153 |       delete mListener; | 
|---|
| 154 |       delete mRoot; | 
|---|
| 155 |     } | 
|---|
| 156 |  | 
|---|
| 157 |   private: | 
|---|
| 158 |     Root *mRoot; | 
|---|
| 159 |     OIS::Keyboard *mKeyboard; | 
|---|
| 160 |     OIS::Mouse *mMouse; | 
|---|
| 161 |     OIS::InputManager *mInputManager; | 
|---|
| 162 |     CEGUI::OgreCEGUIRenderer *mRenderer; | 
|---|
| 163 |     CEGUI::System *mSystem; | 
|---|
| 164 |     OrxExitListener *mListener; | 
|---|
| 165 |  | 
|---|
| 166 |     void createRoot() | 
|---|
| 167 |     { | 
|---|
| 168 | #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE | 
|---|
| 169 |       mRoot = new Root(macBundlePath() + "/Contents/Resources/plugins.cfg"); | 
|---|
| 170 | #else | 
|---|
| 171 |       mRoot = new Root(); | 
|---|
| 172 | #endif | 
|---|
| 173 |     } | 
|---|
| 174 |  | 
|---|
| 175 |     void defineResources() | 
|---|
| 176 |     { | 
|---|
| 177 |       String secName, typeName, archName; | 
|---|
| 178 |       ConfigFile cf; | 
|---|
| 179 | #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE | 
|---|
| 180 |       cf.load(macBundlePath() + "/Contents/Resources/resources.cfg"); | 
|---|
| 181 | #else | 
|---|
| 182 |       cf.load("resources.cfg"); | 
|---|
| 183 | #endif | 
|---|
| 184 |  | 
|---|
| 185 |       ConfigFile::SectionIterator seci = cf.getSectionIterator(); | 
|---|
| 186 |       while (seci.hasMoreElements()) | 
|---|
| 187 |       { | 
|---|
| 188 |         secName = seci.peekNextKey(); | 
|---|
| 189 |         ConfigFile::SettingsMultiMap *settings = seci.getNext(); | 
|---|
| 190 |         ConfigFile::SettingsMultiMap::iterator i; | 
|---|
| 191 |         for (i = settings->begin(); i != settings->end(); ++i) | 
|---|
| 192 |         { | 
|---|
| 193 |           typeName = i->first; | 
|---|
| 194 |           archName = i->second; | 
|---|
| 195 | #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE | 
|---|
| 196 |           ResourceGroupManager::getSingleton().addResourceLocation( String(macBundlePath() + "/" + archName), typeName, secName); | 
|---|
| 197 | #else | 
|---|
| 198 |           ResourceGroupManager::getSingleton().addResourceLocation( archName, typeName, secName); | 
|---|
| 199 | #endif | 
|---|
| 200 |         } | 
|---|
| 201 |       } | 
|---|
| 202 |     } | 
|---|
| 203 |  | 
|---|
| 204 |     void setupRenderSystem() | 
|---|
| 205 |     { | 
|---|
| 206 |       if (!mRoot->restoreConfig() && !mRoot->showConfigDialog()) | 
|---|
| 207 |         throw Exception(52, "User canceled the config dialog!", "OrxApplication::setupRenderSystem()"); | 
|---|
| 208 |     } | 
|---|
| 209 |  | 
|---|
| 210 |     void createRenderWindow() | 
|---|
| 211 |     { | 
|---|
| 212 |       mRoot->initialise(true, "Ogre Render Window"); | 
|---|
| 213 |     } | 
|---|
| 214 |  | 
|---|
| 215 |     void initializeResourceGroups() | 
|---|
| 216 |     { | 
|---|
| 217 |       TextureManager::getSingleton().setDefaultNumMipmaps(5); | 
|---|
| 218 |       ResourceGroupManager::getSingleton().initialiseAllResourceGroups(); | 
|---|
| 219 |     } | 
|---|
| 220 |  | 
|---|
| 221 |     void createScene(void) | 
|---|
| 222 |     { | 
|---|
| 223 |  | 
|---|
| 224 |       string levelFile = "sp_level_moonstation.oxw"; | 
|---|
| 225 |       loader::LevelLoader* loader = new loader::LevelLoader(levelFile); | 
|---|
| 226 |     } | 
|---|
| 227 |      | 
|---|
| 228 |     void setupScene() | 
|---|
| 229 |     { | 
|---|
| 230 |       SceneManager *mgr = mRoot->createSceneManager(ST_GENERIC, "Default SceneManager"); | 
|---|
| 231 |       Camera *cam = mgr->createCamera("Camera"); | 
|---|
| 232 |       cam->setPosition(Vector3(0,0,1000)); | 
|---|
| 233 |       cam->lookAt(Vector3(0,0,0)); | 
|---|
| 234 |       Viewport *vp = mRoot->getAutoCreatedWindow()->addViewport(cam); | 
|---|
| 235 |       example();  //my stuff | 
|---|
| 236 |     } | 
|---|
| 237 |  | 
|---|
| 238 |     void setupInputSystem() | 
|---|
| 239 |     { | 
|---|
| 240 |       size_t windowHnd = 0; | 
|---|
| 241 |       std::ostringstream windowHndStr; | 
|---|
| 242 |       OIS::ParamList pl; | 
|---|
| 243 |       RenderWindow *win = mRoot->getAutoCreatedWindow(); | 
|---|
| 244 |  | 
|---|
| 245 |       win->getCustomAttribute("WINDOW", &windowHnd); | 
|---|
| 246 |       windowHndStr << windowHnd; | 
|---|
| 247 |       pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str())); | 
|---|
| 248 |       mInputManager = OIS::InputManager::createInputSystem(pl); | 
|---|
| 249 |  | 
|---|
| 250 |       try | 
|---|
| 251 |       { | 
|---|
| 252 |         mKeyboard = static_cast<OIS::Keyboard*>(mInputManager->createInputObject(OIS::OISKeyboard, false)); | 
|---|
| 253 |         mMouse = static_cast<OIS::Mouse*>(mInputManager->createInputObject(OIS::OISMouse, false)); | 
|---|
| 254 |       } | 
|---|
| 255 |       catch (const OIS::Exception &e) | 
|---|
| 256 |       { | 
|---|
| 257 |         throw new Exception(42, e.eText, "OrxApplication::setupInputSystem"); | 
|---|
| 258 |       } | 
|---|
| 259 |     } | 
|---|
| 260 |  | 
|---|
| 261 |     void setupCEGUI() | 
|---|
| 262 |     { | 
|---|
| 263 |       SceneManager *mgr = mRoot->getSceneManager("Default SceneManager"); | 
|---|
| 264 |       RenderWindow *win = mRoot->getAutoCreatedWindow(); | 
|---|
| 265 |  | 
|---|
| 266 |       // CEGUI setup | 
|---|
| 267 | //       mRenderer = new CEGUI::OgreCEGUIRenderer(win, Ogre::RENDER_QUEUE_OVERLAY, false, 3000, mgr); | 
|---|
| 268 | //       mSystem = new CEGUI::System(mRenderer); | 
|---|
| 269 |  | 
|---|
| 270 |       // Other CEGUI setup here. | 
|---|
| 271 |     } | 
|---|
| 272 |  | 
|---|
| 273 |     void createFrameListener() | 
|---|
| 274 |     { | 
|---|
| 275 |       mListener = new OrxExitListener(mKeyboard, mRoot); | 
|---|
| 276 |       mRoot->addFrameListener(mListener); | 
|---|
| 277 |     } | 
|---|
| 278 |  | 
|---|
| 279 |     void startRenderLoop() | 
|---|
| 280 |     { | 
|---|
| 281 |       mRoot->startRendering(); | 
|---|
| 282 |     } | 
|---|
| 283 |  | 
|---|
| 284 |     //declaration of the 3 Ogreheads | 
|---|
| 285 |    //muss leider global sein..... | 
|---|
| 286 |     //Element* arrayOfElements[2]; | 
|---|
| 287 |  | 
|---|
| 288 |     void example() { | 
|---|
| 289 |     SceneManager *mgr = mRoot->getSceneManager("Default SceneManager"); | 
|---|
| 290 |     mgr->setAmbientLight(ColourValue(1.0,1.0,1.0)); | 
|---|
| 291 |     Entity* ent1 = mgr->createEntity("Head1", "ogrehead.mesh"); | 
|---|
| 292 |     Entity* ent2 = mgr->createEntity("Head2", "ogrehead.mesh"); | 
|---|
| 293 |     Entity* ent3 = mgr->createEntity("Head3", "ogrehead.mesh"); | 
|---|
| 294 |     SceneNode *node1 = mgr->getRootSceneNode()->createChildSceneNode("HeadNode1", Vector3(0,100,0)); | 
|---|
| 295 |     SceneNode *node2 = mgr->getRootSceneNode()->createChildSceneNode("HeadNode2", Vector3(100,0,0)); | 
|---|
| 296 |     SceneNode *node3 = mgr->getRootSceneNode()->createChildSceneNode("HeadNode3", Vector3(-100,0,0)); | 
|---|
| 297 |     node1->attachObject(ent1); | 
|---|
| 298 |     node2->attachObject(ent2); | 
|---|
| 299 |     node3->attachObject(ent3); | 
|---|
| 300 |     ElementLocationArray[0] = node1->getPosition(); | 
|---|
| 301 |     ElementLocationArray[1] = node2->getPosition(); | 
|---|
| 302 |     ElementLocationArray[2] = node3->getPosition(); | 
|---|
| 303 |     ElementSpeedArray[0] = (0,0,0); | 
|---|
| 304 |     ElementSpeedArray[1] = (0,0,0); | 
|---|
| 305 |     ElementSpeedArray[2] = (0,0,0); | 
|---|
| 306 |     ElementAccelerationArray[0] = (0,0,0); | 
|---|
| 307 |     ElementAccelerationArray[1] = (0,0,0); | 
|---|
| 308 |     ElementAccelerationArray[2] = (0,0,0); | 
|---|
| 309 |     arrayOfElements[0].setValues( ElementLocationArray[0], ElementSpeedArray[0], ElementAccelerationArray[0] ); | 
|---|
| 310 |     arrayOfElements[1].setValues( ElementLocationArray[1], ElementSpeedArray[1], ElementAccelerationArray[1] ); | 
|---|
| 311 |     arrayOfElements[2].setValues( ElementLocationArray[2], ElementSpeedArray[2], ElementAccelerationArray[2] ); | 
|---|
| 312 |  | 
|---|
| 313 |  | 
|---|
| 314 |  | 
|---|
| 315 |  | 
|---|
| 316 |    /* for (int i=0; i<3; i++) { | 
|---|
| 317 |       Element* arrayOfElements[i] = new Element( ElementLocationArray[i], ElementSpeedArray[i], ElementAccelerationArray[i] ); | 
|---|
| 318 |     } */ | 
|---|
| 319 |    /* for (int i=0; i<3; i++) { | 
|---|
| 320 |     arrayOfElements[i]->update(arrayOfElements); | 
|---|
| 321 |     }  */ | 
|---|
| 322 |     } | 
|---|
| 323 | }; | 
|---|
| 324 |  | 
|---|
| 325 |  | 
|---|
| 326 |  | 
|---|
| 327 | #if OGRE_PLATFORM == PLATFORM_WIN32 || OGRE_PLATFORM == OGRE_PLATFORM_WIN32 | 
|---|
| 328 | #define WIN32_LEAN_AND_MEAN | 
|---|
| 329 | #include "windows.h" | 
|---|
| 330 |  | 
|---|
| 331 |              INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT) | 
|---|
| 332 | #else | 
|---|
| 333 |              int main(int argc, char **argv) | 
|---|
| 334 | #endif | 
|---|
| 335 | { | 
|---|
| 336 |   try | 
|---|
| 337 |   { | 
|---|
| 338 |     OrxApplication orxonox; | 
|---|
| 339 |     orxonox.go(); | 
|---|
| 340 |   } | 
|---|
| 341 |   catch(Exception& e) | 
|---|
| 342 |   { | 
|---|
| 343 | #if OGRE_PLATFORM == PLATFORM_WIN32 || OGRE_PLATFORM == OGRE_PLATFORM_WIN32 | 
|---|
| 344 |     MessageBoxA(NULL, e.getFullDescription().c_str(), "An exception has occurred!", MB_OK | MB_ICONERROR | MB_TASKMODAL); | 
|---|
| 345 | #else | 
|---|
| 346 |     fprintf(stderr, "An exception has occurred: %s\n", | 
|---|
| 347 |             e.getFullDescription().c_str()); | 
|---|
| 348 | #endif | 
|---|
| 349 |   } | 
|---|
| 350 |  | 
|---|
| 351 |   return 0; | 
|---|
| 352 | } | 
|---|
| 353 |  | 
|---|