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