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