[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(); |
---|
| 194 | |
---|
[265] | 195 | string levelFile = "sp_level_moonstation.oxw"; |
---|
| 196 | loader::LevelLoader* loader = new loader::LevelLoader(levelFile); |
---|
[327] | 197 | |
---|
| 198 | |
---|
| 199 | |
---|
[265] | 200 | } |
---|
[164] | 201 | |
---|
[265] | 202 | void setupScene() |
---|
| 203 | { |
---|
| 204 | Ogre::SceneManager *mgr = mRoot->createSceneManager(Ogre::ST_GENERIC, "Default SceneManager"); |
---|
| 205 | Ogre::Camera *cam = mgr->createCamera("Camera"); |
---|
| 206 | Ogre::Viewport *vp = mRoot->getAutoCreatedWindow()->addViewport(cam); |
---|
| 207 | } |
---|
[258] | 208 | |
---|
[265] | 209 | void setupInputSystem() |
---|
| 210 | { |
---|
| 211 | size_t windowHnd = 0; |
---|
| 212 | std::ostringstream windowHndStr; |
---|
| 213 | OIS::ParamList pl; |
---|
| 214 | Ogre::RenderWindow *win = mRoot->getAutoCreatedWindow(); |
---|
[74] | 215 | |
---|
[265] | 216 | win->getCustomAttribute("WINDOW", &windowHnd); |
---|
| 217 | windowHndStr << windowHnd; |
---|
| 218 | pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str())); |
---|
| 219 | mInputManager = OIS::InputManager::createInputSystem(pl); |
---|
[74] | 220 | |
---|
[265] | 221 | try |
---|
| 222 | { |
---|
| 223 | mKeyboard = static_cast<OIS::Keyboard*>(mInputManager->createInputObject(OIS::OISKeyboard, false)); |
---|
| 224 | mMouse = static_cast<OIS::Mouse*>(mInputManager->createInputObject(OIS::OISMouse, false)); |
---|
[263] | 225 | } |
---|
[265] | 226 | catch (const OIS::Exception &e) |
---|
[263] | 227 | { |
---|
[265] | 228 | throw new Ogre::Exception(42, e.eText, "OrxApplication::setupInputSystem"); |
---|
| 229 | } |
---|
| 230 | } |
---|
[74] | 231 | |
---|
[265] | 232 | void setupCEGUI() |
---|
| 233 | { |
---|
| 234 | Ogre::SceneManager *mgr = mRoot->getSceneManager("Default SceneManager"); |
---|
| 235 | Ogre::RenderWindow *win = mRoot->getAutoCreatedWindow(); |
---|
[137] | 236 | |
---|
[265] | 237 | // CEGUI setup |
---|
| 238 | // mRenderer = new CEGUI::OgreCEGUIRenderer(win, Ogre::RENDER_QUEUE_OVERLAY, false, 3000, mgr); |
---|
| 239 | // mSystem = new CEGUI::System(mRenderer); |
---|
[137] | 240 | |
---|
[265] | 241 | // Other CEGUI setup here. |
---|
| 242 | } |
---|
[137] | 243 | |
---|
[265] | 244 | void createFrameListener() |
---|
| 245 | { |
---|
| 246 | mListener = new OrxExitListener(mKeyboard); |
---|
| 247 | mRoot->addFrameListener(mListener); |
---|
| 248 | } |
---|
| 249 | |
---|
| 250 | void startRenderLoop() |
---|
| 251 | { |
---|
| 252 | mRoot->startRendering(); |
---|
| 253 | } |
---|
| 254 | }; |
---|
[304] | 255 | } |
---|
[137] | 256 | |
---|
[304] | 257 | using namespace Ogre; |
---|
| 258 | |
---|
[137] | 259 | #if OGRE_PLATFORM == PLATFORM_WIN32 || OGRE_PLATFORM == OGRE_PLATFORM_WIN32 |
---|
[74] | 260 | #define WIN32_LEAN_AND_MEAN |
---|
| 261 | #include "windows.h" |
---|
| 262 | |
---|
[137] | 263 | INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT) |
---|
[74] | 264 | #else |
---|
[137] | 265 | int main(int argc, char **argv) |
---|
[74] | 266 | #endif |
---|
| 267 | { |
---|
[137] | 268 | try |
---|
| 269 | { |
---|
[263] | 270 | orxonox::OrxApplication orxonox; |
---|
[74] | 271 | orxonox.go(); |
---|
[137] | 272 | } |
---|
| 273 | catch(Exception& e) |
---|
| 274 | { |
---|
| 275 | #if OGRE_PLATFORM == PLATFORM_WIN32 || OGRE_PLATFORM == OGRE_PLATFORM_WIN32 |
---|
| 276 | MessageBoxA(NULL, e.getFullDescription().c_str(), "An exception has occurred!", MB_OK | MB_ICONERROR | MB_TASKMODAL); |
---|
[74] | 277 | #else |
---|
| 278 | fprintf(stderr, "An exception has occurred: %s\n", |
---|
| 279 | e.getFullDescription().c_str()); |
---|
| 280 | #endif |
---|
| 281 | } |
---|
| 282 | |
---|
| 283 | return 0; |
---|
| 284 | } |
---|
[137] | 285 | |
---|