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