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