[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" |
---|
[327] | 43 | #include "audio/Ambient.h" |
---|
[164] | 44 | |
---|
| 45 | |
---|
[151] | 46 | // some tests to see if enet works without includsion |
---|
| 47 | //#include <enet/enet.h> |
---|
| 48 | //#include <enet/protocol.h> |
---|
[142] | 49 | |
---|
[137] | 50 | #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE |
---|
| 51 | #include <CoreFoundation/CoreFoundation.h> |
---|
[74] | 52 | |
---|
[137] | 53 | // This function will locate the path to our application on OS X, |
---|
| 54 | // unlike windows you can not rely on the curent working directory |
---|
| 55 | // for locating your configuration files and resources. |
---|
| 56 | std::string macBundlePath() |
---|
[74] | 57 | { |
---|
[137] | 58 | char path[1024]; |
---|
| 59 | CFBundleRef mainBundle = CFBundleGetMainBundle(); |
---|
| 60 | assert(mainBundle); |
---|
| 61 | |
---|
| 62 | CFURLRef mainBundleURL = CFBundleCopyBundleURL(mainBundle); |
---|
| 63 | assert(mainBundleURL); |
---|
| 64 | |
---|
| 65 | CFStringRef cfStringRef = CFURLCopyFileSystemPath( mainBundleURL, kCFURLPOSIXPathStyle); |
---|
| 66 | assert(cfStringRef); |
---|
| 67 | |
---|
| 68 | CFStringGetCString(cfStringRef, path, 1024, kCFStringEncodingASCII); |
---|
| 69 | |
---|
| 70 | CFRelease(mainBundleURL); |
---|
| 71 | CFRelease(cfStringRef); |
---|
| 72 | |
---|
| 73 | return std::string(path); |
---|
| 74 | } |
---|
| 75 | #endif |
---|
| 76 | |
---|
[304] | 77 | namespace orxonox |
---|
| 78 | { |
---|
[265] | 79 | class OrxExitListener : public Ogre::FrameListener |
---|
| 80 | { |
---|
| 81 | public: |
---|
| 82 | OrxExitListener(OIS::Keyboard *keyboard) |
---|
| 83 | : mKeyboard(keyboard) |
---|
| 84 | { |
---|
| 85 | } |
---|
[74] | 86 | |
---|
[265] | 87 | bool frameStarted(const Ogre::FrameEvent& evt) |
---|
| 88 | { |
---|
| 89 | mKeyboard->capture(); |
---|
| 90 | return !mKeyboard->isKeyDown(OIS::KC_ESCAPE); |
---|
| 91 | } |
---|
[135] | 92 | |
---|
[265] | 93 | private: |
---|
| 94 | OIS::Keyboard *mKeyboard; |
---|
| 95 | }; |
---|
[74] | 96 | |
---|
[265] | 97 | class OrxApplication |
---|
| 98 | { |
---|
| 99 | public: |
---|
| 100 | void go() |
---|
| 101 | { |
---|
| 102 | createRoot(); |
---|
| 103 | defineResources(); |
---|
| 104 | setupRenderSystem(); |
---|
| 105 | createRenderWindow(); |
---|
| 106 | initializeResourceGroups(); |
---|
| 107 | createScene(); |
---|
| 108 | setupScene(); |
---|
| 109 | setupInputSystem(); |
---|
| 110 | setupCEGUI(); |
---|
| 111 | createFrameListener(); |
---|
| 112 | startRenderLoop(); |
---|
| 113 | } |
---|
[74] | 114 | |
---|
[265] | 115 | ~OrxApplication() |
---|
| 116 | { |
---|
| 117 | mInputManager->destroyInputObject(mKeyboard); |
---|
| 118 | OIS::InputManager::destroyInputSystem(mInputManager); |
---|
[137] | 119 | |
---|
[265] | 120 | // delete mRenderer; |
---|
| 121 | // delete mSystem; |
---|
[137] | 122 | |
---|
[265] | 123 | delete mListener; |
---|
| 124 | delete mRoot; |
---|
| 125 | } |
---|
[137] | 126 | |
---|
[265] | 127 | private: |
---|
| 128 | Ogre::Root *mRoot; |
---|
| 129 | OIS::Keyboard *mKeyboard; |
---|
| 130 | OIS::Mouse *mMouse; |
---|
| 131 | OIS::InputManager *mInputManager; |
---|
| 132 | CEGUI::OgreCEGUIRenderer *mRenderer; |
---|
| 133 | CEGUI::System *mSystem; |
---|
| 134 | OrxExitListener *mListener; |
---|
[137] | 135 | |
---|
[265] | 136 | void createRoot() |
---|
| 137 | { |
---|
| 138 | #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE |
---|
| 139 | mRoot = new Ogre::Root(macBundlePath() + "/Contents/Resources/plugins.cfg"); |
---|
| 140 | #else |
---|
| 141 | mRoot = new Ogre::Root(); |
---|
| 142 | #endif |
---|
| 143 | } |
---|
[74] | 144 | |
---|
[265] | 145 | void defineResources() |
---|
| 146 | { |
---|
| 147 | Ogre::String secName, typeName, archName; |
---|
| 148 | Ogre::ConfigFile cf; |
---|
| 149 | #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE |
---|
| 150 | cf.load(macBundlePath() + "/Contents/Resources/resources.cfg"); |
---|
| 151 | #else |
---|
| 152 | cf.load("resources.cfg"); |
---|
| 153 | #endif |
---|
| 154 | |
---|
| 155 | Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator(); |
---|
| 156 | while (seci.hasMoreElements()) |
---|
[263] | 157 | { |
---|
[265] | 158 | secName = seci.peekNextKey(); |
---|
| 159 | Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext(); |
---|
| 160 | Ogre::ConfigFile::SettingsMultiMap::iterator i; |
---|
| 161 | for (i = settings->begin(); i != settings->end(); ++i) |
---|
[263] | 162 | { |
---|
[265] | 163 | typeName = i->first; |
---|
| 164 | archName = i->second; |
---|
| 165 | #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE |
---|
| 166 | Ogre::ResourceGroupManager::getSingleton().addResourceLocation( String(macBundlePath() + "/" + archName), typeName, secName); |
---|
| 167 | #else |
---|
| 168 | Ogre::ResourceGroupManager::getSingleton().addResourceLocation( archName, typeName, secName); |
---|
| 169 | #endif |
---|
[263] | 170 | } |
---|
| 171 | } |
---|
[265] | 172 | } |
---|
[263] | 173 | |
---|
[265] | 174 | void setupRenderSystem() |
---|
| 175 | { |
---|
| 176 | if (!mRoot->restoreConfig() && !mRoot->showConfigDialog()) |
---|
| 177 | throw Ogre::Exception(52, "User canceled the config dialog!", "OrxApplication::setupRenderSystem()"); |
---|
| 178 | } |
---|
[74] | 179 | |
---|
[265] | 180 | void createRenderWindow() |
---|
| 181 | { |
---|
| 182 | mRoot->initialise(true, "Ogre Render Window"); |
---|
| 183 | } |
---|
[74] | 184 | |
---|
[265] | 185 | void initializeResourceGroups() |
---|
| 186 | { |
---|
| 187 | Ogre::TextureManager::getSingleton().setDefaultNumMipmaps(5); |
---|
| 188 | Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups(); |
---|
| 189 | } |
---|
[74] | 190 | |
---|
[265] | 191 | void createScene(void) |
---|
| 192 | { |
---|
[327] | 193 | audio::Ambient* bgsound = new audio::Ambient(); |
---|
[334] | 194 | bgsound->play(); |
---|
[327] | 195 | |
---|
[265] | 196 | string levelFile = "sp_level_moonstation.oxw"; |
---|
| 197 | loader::LevelLoader* loader = new loader::LevelLoader(levelFile); |
---|
[327] | 198 | |
---|
| 199 | |
---|
| 200 | |
---|
[265] | 201 | } |
---|
[164] | 202 | |
---|
[265] | 203 | void setupScene() |
---|
| 204 | { |
---|
| 205 | Ogre::SceneManager *mgr = mRoot->createSceneManager(Ogre::ST_GENERIC, "Default SceneManager"); |
---|
| 206 | Ogre::Camera *cam = mgr->createCamera("Camera"); |
---|
| 207 | Ogre::Viewport *vp = mRoot->getAutoCreatedWindow()->addViewport(cam); |
---|
| 208 | } |
---|
[258] | 209 | |
---|
[265] | 210 | void setupInputSystem() |
---|
| 211 | { |
---|
| 212 | size_t windowHnd = 0; |
---|
| 213 | std::ostringstream windowHndStr; |
---|
| 214 | OIS::ParamList pl; |
---|
| 215 | Ogre::RenderWindow *win = mRoot->getAutoCreatedWindow(); |
---|
[74] | 216 | |
---|
[265] | 217 | win->getCustomAttribute("WINDOW", &windowHnd); |
---|
| 218 | windowHndStr << windowHnd; |
---|
| 219 | pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str())); |
---|
| 220 | mInputManager = OIS::InputManager::createInputSystem(pl); |
---|
[74] | 221 | |
---|
[265] | 222 | try |
---|
| 223 | { |
---|
| 224 | mKeyboard = static_cast<OIS::Keyboard*>(mInputManager->createInputObject(OIS::OISKeyboard, false)); |
---|
| 225 | mMouse = static_cast<OIS::Mouse*>(mInputManager->createInputObject(OIS::OISMouse, false)); |
---|
[263] | 226 | } |
---|
[265] | 227 | catch (const OIS::Exception &e) |
---|
[263] | 228 | { |
---|
[265] | 229 | throw new Ogre::Exception(42, e.eText, "OrxApplication::setupInputSystem"); |
---|
| 230 | } |
---|
| 231 | } |
---|
[74] | 232 | |
---|
[265] | 233 | void setupCEGUI() |
---|
| 234 | { |
---|
| 235 | Ogre::SceneManager *mgr = mRoot->getSceneManager("Default SceneManager"); |
---|
| 236 | Ogre::RenderWindow *win = mRoot->getAutoCreatedWindow(); |
---|
[137] | 237 | |
---|
[265] | 238 | // CEGUI setup |
---|
| 239 | // mRenderer = new CEGUI::OgreCEGUIRenderer(win, Ogre::RENDER_QUEUE_OVERLAY, false, 3000, mgr); |
---|
| 240 | // mSystem = new CEGUI::System(mRenderer); |
---|
[137] | 241 | |
---|
[265] | 242 | // Other CEGUI setup here. |
---|
| 243 | } |
---|
[137] | 244 | |
---|
[265] | 245 | void createFrameListener() |
---|
| 246 | { |
---|
| 247 | mListener = new OrxExitListener(mKeyboard); |
---|
| 248 | mRoot->addFrameListener(mListener); |
---|
| 249 | } |
---|
| 250 | |
---|
| 251 | void startRenderLoop() |
---|
| 252 | { |
---|
| 253 | mRoot->startRendering(); |
---|
| 254 | } |
---|
| 255 | }; |
---|
[304] | 256 | } |
---|
[137] | 257 | |
---|
[304] | 258 | using namespace Ogre; |
---|
| 259 | |
---|
[137] | 260 | #if OGRE_PLATFORM == PLATFORM_WIN32 || OGRE_PLATFORM == OGRE_PLATFORM_WIN32 |
---|
[74] | 261 | #define WIN32_LEAN_AND_MEAN |
---|
| 262 | #include "windows.h" |
---|
| 263 | |
---|
[137] | 264 | INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT) |
---|
[74] | 265 | #else |
---|
[137] | 266 | int main(int argc, char **argv) |
---|
[74] | 267 | #endif |
---|
| 268 | { |
---|
[137] | 269 | try |
---|
| 270 | { |
---|
[263] | 271 | orxonox::OrxApplication orxonox; |
---|
[74] | 272 | orxonox.go(); |
---|
[137] | 273 | } |
---|
| 274 | catch(Exception& e) |
---|
| 275 | { |
---|
| 276 | #if OGRE_PLATFORM == PLATFORM_WIN32 || OGRE_PLATFORM == OGRE_PLATFORM_WIN32 |
---|
| 277 | MessageBoxA(NULL, e.getFullDescription().c_str(), "An exception has occurred!", MB_OK | MB_ICONERROR | MB_TASKMODAL); |
---|
[74] | 278 | #else |
---|
| 279 | fprintf(stderr, "An exception has occurred: %s\n", |
---|
| 280 | e.getFullDescription().c_str()); |
---|
| 281 | #endif |
---|
| 282 | } |
---|
| 283 | |
---|
| 284 | return 0; |
---|
| 285 | } |
---|
[137] | 286 | |
---|