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