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