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