[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> |
---|
[399] | 35 | //#include <CEGUI/CEGUI.h> |
---|
| 36 | //#include <OgreCEGUIRenderer.h> |
---|
[74] | 37 | |
---|
[164] | 38 | #include <string> |
---|
| 39 | #include <iostream> |
---|
| 40 | |
---|
[137] | 41 | #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE |
---|
| 42 | #include <CoreFoundation/CoreFoundation.h> |
---|
[428] | 43 | #include "windows.h" |
---|
[74] | 44 | |
---|
[428] | 45 | |
---|
| 46 | |
---|
[137] | 47 | // This function will locate the path to our application on OS X, |
---|
| 48 | // unlike windows you can not rely on the curent working directory |
---|
| 49 | // for locating your configuration files and resources. |
---|
| 50 | std::string macBundlePath() |
---|
[74] | 51 | { |
---|
[137] | 52 | char path[1024]; |
---|
| 53 | CFBundleRef mainBundle = CFBundleGetMainBundle(); |
---|
| 54 | assert(mainBundle); |
---|
| 55 | |
---|
| 56 | CFURLRef mainBundleURL = CFBundleCopyBundleURL(mainBundle); |
---|
| 57 | assert(mainBundleURL); |
---|
| 58 | |
---|
| 59 | CFStringRef cfStringRef = CFURLCopyFileSystemPath( mainBundleURL, kCFURLPOSIXPathStyle); |
---|
| 60 | assert(cfStringRef); |
---|
| 61 | |
---|
| 62 | CFStringGetCString(cfStringRef, path, 1024, kCFStringEncodingASCII); |
---|
| 63 | |
---|
| 64 | CFRelease(mainBundleURL); |
---|
| 65 | CFRelease(cfStringRef); |
---|
| 66 | |
---|
| 67 | return std::string(path); |
---|
| 68 | } |
---|
| 69 | #endif |
---|
| 70 | |
---|
[399] | 71 | namespace orxonox |
---|
[137] | 72 | { |
---|
[428] | 73 | Ogre::RenderWindow * mWindow; |
---|
| 74 | Ogre::SceneManager *mgr; |
---|
| 75 | int stopper = 1; |
---|
[74] | 76 | |
---|
[135] | 77 | |
---|
[399] | 78 | class OrxApplication |
---|
| 79 | { |
---|
| 80 | public: |
---|
| 81 | void go() |
---|
| 82 | { |
---|
| 83 | createRoot(); |
---|
| 84 | defineResources(); |
---|
| 85 | setupRenderSystem(); |
---|
| 86 | createRenderWindow(); |
---|
| 87 | initializeResourceGroups(); |
---|
| 88 | createScene(); |
---|
| 89 | setupScene(); |
---|
| 90 | setupInputSystem(); |
---|
| 91 | setupCEGUI(); |
---|
| 92 | createFrameListener(); |
---|
| 93 | startRenderLoop(); |
---|
| 94 | } |
---|
[74] | 95 | |
---|
[428] | 96 | OrxApplication() |
---|
| 97 | { |
---|
| 98 | OrxApplication::pointer_s = this; |
---|
| 99 | this->x = 0; |
---|
| 100 | } |
---|
| 101 | |
---|
[399] | 102 | ~OrxApplication() |
---|
| 103 | { |
---|
| 104 | mInputManager->destroyInputObject(mKeyboard); |
---|
| 105 | OIS::InputManager::destroyInputSystem(mInputManager); |
---|
[137] | 106 | |
---|
[399] | 107 | delete mListener; |
---|
| 108 | delete mRoot; |
---|
| 109 | } |
---|
[137] | 110 | |
---|
| 111 | |
---|
[399] | 112 | void createRoot() |
---|
| 113 | { |
---|
[137] | 114 | #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE |
---|
[399] | 115 | mRoot = new Ogre::Root(macBundlePath() + "/Contents/Resources/plugins.cfg"); |
---|
[137] | 116 | #else |
---|
[399] | 117 | mRoot = new Ogre::Root(); |
---|
[137] | 118 | #endif |
---|
[399] | 119 | } |
---|
[74] | 120 | |
---|
[399] | 121 | void defineResources() |
---|
| 122 | { |
---|
| 123 | Ogre::String secName, typeName, archName; |
---|
| 124 | Ogre::ConfigFile cf; |
---|
[137] | 125 | #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE |
---|
[399] | 126 | cf.load(macBundlePath() + "/Contents/Resources/resources.cfg"); |
---|
[137] | 127 | #else |
---|
[399] | 128 | cf.load("resources.cfg"); |
---|
[137] | 129 | #endif |
---|
[74] | 130 | |
---|
[399] | 131 | Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator(); |
---|
| 132 | while (seci.hasMoreElements()) |
---|
[137] | 133 | { |
---|
[399] | 134 | secName = seci.peekNextKey(); |
---|
| 135 | Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext(); |
---|
| 136 | Ogre::ConfigFile::SettingsMultiMap::iterator i; |
---|
| 137 | for (i = settings->begin(); i != settings->end(); ++i) |
---|
| 138 | { |
---|
| 139 | typeName = i->first; |
---|
| 140 | archName = i->second; |
---|
[137] | 141 | #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE |
---|
[399] | 142 | Ogre::ResourceGroupManager::getSingleton().addResourceLocation( String(macBundlePath() + "/" + archName), typeName, secName); |
---|
[137] | 143 | #else |
---|
[399] | 144 | Ogre::ResourceGroupManager::getSingleton().addResourceLocation( archName, typeName, secName); |
---|
[137] | 145 | #endif |
---|
[399] | 146 | } |
---|
[137] | 147 | } |
---|
| 148 | } |
---|
[74] | 149 | |
---|
[399] | 150 | void setupRenderSystem() |
---|
| 151 | { |
---|
| 152 | if (!mRoot->restoreConfig() && !mRoot->showConfigDialog()) |
---|
| 153 | throw Ogre::Exception(52, "User canceled the config dialog!", "OrxApplication::setupRenderSystem()"); |
---|
| 154 | } |
---|
[74] | 155 | |
---|
[399] | 156 | void createRenderWindow() |
---|
| 157 | { |
---|
[428] | 158 | mWindow = mRoot->initialise(true, "Ogre Render Window"); |
---|
[399] | 159 | } |
---|
[74] | 160 | |
---|
[399] | 161 | void initializeResourceGroups() |
---|
| 162 | { |
---|
| 163 | Ogre::TextureManager::getSingleton().setDefaultNumMipmaps(5); |
---|
| 164 | Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups(); |
---|
| 165 | } |
---|
[74] | 166 | |
---|
[399] | 167 | void createScene(void) |
---|
| 168 | { |
---|
[164] | 169 | |
---|
[399] | 170 | } |
---|
[74] | 171 | |
---|
[399] | 172 | void setupScene() |
---|
| 173 | { |
---|
[428] | 174 | mgr = mRoot->createSceneManager(Ogre::ST_GENERIC, "Default SceneManager"); |
---|
| 175 | cam = mgr->createCamera("Camera"); |
---|
| 176 | cam->setPosition(Ogre::Vector3(0,0,0)); |
---|
| 177 | //cam->lookAt(Ogre::Vector3(0,0,0)); |
---|
| 178 | cam->setFOVy(Ogre::Degree(90)); |
---|
[399] | 179 | Ogre::Viewport *vp = mRoot->getAutoCreatedWindow()->addViewport(cam); |
---|
| 180 | mgr->setSkyBox(true, "Examples/SpaceSkyBox"); |
---|
[428] | 181 | |
---|
| 182 | // camera pitch here works |
---|
| 183 | |
---|
| 184 | |
---|
[399] | 185 | } |
---|
[74] | 186 | |
---|
[399] | 187 | void setupInputSystem() |
---|
| 188 | { |
---|
| 189 | size_t windowHnd = 0; |
---|
| 190 | std::ostringstream windowHndStr; |
---|
| 191 | OIS::ParamList pl; |
---|
| 192 | Ogre::RenderWindow *win = mRoot->getAutoCreatedWindow(); |
---|
[74] | 193 | |
---|
[399] | 194 | win->getCustomAttribute("WINDOW", &windowHnd); |
---|
| 195 | windowHndStr << windowHnd; |
---|
| 196 | pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str())); |
---|
| 197 | mInputManager = OIS::InputManager::createInputSystem(pl); |
---|
| 198 | |
---|
| 199 | try |
---|
| 200 | { |
---|
| 201 | mKeyboard = static_cast<OIS::Keyboard*>(mInputManager->createInputObject(OIS::OISKeyboard, false)); |
---|
| 202 | mMouse = static_cast<OIS::Mouse*>(mInputManager->createInputObject(OIS::OISMouse, false)); |
---|
| 203 | } |
---|
| 204 | catch (const OIS::Exception &e) |
---|
| 205 | { |
---|
| 206 | throw new Ogre::Exception(42, e.eText, "OrxApplication::setupInputSystem"); |
---|
| 207 | } |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | void setupCEGUI() |
---|
[137] | 211 | { |
---|
[399] | 212 | Ogre::SceneManager *mgr = mRoot->getSceneManager("Default SceneManager"); |
---|
| 213 | Ogre::RenderWindow *win = mRoot->getAutoCreatedWindow(); |
---|
[137] | 214 | } |
---|
[428] | 215 | |
---|
| 216 | void screenShots() |
---|
| 217 | { |
---|
[399] | 218 | |
---|
[428] | 219 | Ogre::Radian x1 = Ogre::Radian(x); |
---|
| 220 | |
---|
| 221 | unsigned int indice = 1; |
---|
| 222 | char filename[30]; |
---|
| 223 | char fn[1]; |
---|
| 224 | // generate new names... |
---|
| 225 | sprintf(filename, "SkyBox%d.png", stopper); |
---|
| 226 | //sprintf(filename, "screenshot_%d.png", ++indice); |
---|
| 227 | //sprintf(filename, fn, ++indice); |
---|
| 228 | |
---|
| 229 | |
---|
| 230 | Ogre::Radian x2 = Ogre::Radian((3.141592653589/2.0)*(stopper+1)); |
---|
| 231 | |
---|
| 232 | |
---|
| 233 | if(stopper == 1){ |
---|
| 234 | cam->yaw(Ogre::Degree(90)); |
---|
| 235 | sprintf(filename, "SkyBox%d.png", stopper+1035); |
---|
| 236 | mWindow->writeContentsToFile(filename); |
---|
| 237 | |
---|
| 238 | } |
---|
| 239 | else if(stopper == 2){ |
---|
| 240 | cam->yaw(Ogre::Degree(180)); |
---|
| 241 | mWindow->writeContentsToFile(filename); |
---|
| 242 | |
---|
| 243 | } |
---|
| 244 | else if(stopper == 3){ |
---|
| 245 | cam->yaw(Ogre::Degree(270)); |
---|
| 246 | mWindow->writeContentsToFile(filename); |
---|
| 247 | |
---|
| 248 | } |
---|
| 249 | else if(stopper == 4){ |
---|
| 250 | cam->yaw(Ogre::Degree(0)); |
---|
| 251 | mWindow->writeContentsToFile(filename); |
---|
| 252 | |
---|
| 253 | } |
---|
| 254 | |
---|
| 255 | else if(stopper == 5){ |
---|
| 256 | |
---|
| 257 | cam->yaw(Ogre::Degree(0)); |
---|
| 258 | mWindow->writeContentsToFile(filename); |
---|
| 259 | |
---|
| 260 | } |
---|
| 261 | else if(stopper == 6){ |
---|
| 262 | cam->yaw(Ogre::Degree(180)); |
---|
| 263 | mWindow->writeContentsToFile(filename); |
---|
| 264 | |
---|
| 265 | } |
---|
| 266 | |
---|
| 267 | /*else if(stopper == 6){ |
---|
| 268 | cam->roll(Ogre::Degree(270)); |
---|
| 269 | }*/ |
---|
| 270 | |
---|
| 271 | //cam->pitch(Ogre::Degree(90)*stopper); |
---|
| 272 | |
---|
| 273 | stopper+=1; |
---|
| 274 | if(stopper >= 7) |
---|
| 275 | stopper = 1; |
---|
| 276 | } |
---|
| 277 | |
---|
| 278 | class OrxExitListener : public Ogre::FrameListener |
---|
| 279 | { |
---|
| 280 | public: |
---|
| 281 | OrxExitListener(OIS::Keyboard *keyboard) |
---|
| 282 | : mKeyboard(keyboard) |
---|
| 283 | { |
---|
| 284 | } |
---|
| 285 | bool frameEnded(const Ogre::FrameEvent& evt) |
---|
| 286 | { |
---|
| 287 | if(mKeyboard->isKeyDown(OIS::KC_RETURN) or stopper > 1){ |
---|
| 288 | OrxApplication::pointer_s->screenShots(); |
---|
| 289 | } |
---|
| 290 | |
---|
| 291 | return true; |
---|
| 292 | } |
---|
| 293 | |
---|
| 294 | bool frameStarted(const Ogre::FrameEvent& evt) |
---|
| 295 | { |
---|
| 296 | mKeyboard->capture(); |
---|
| 297 | return !mKeyboard->isKeyDown(OIS::KC_ESCAPE); |
---|
| 298 | } |
---|
| 299 | |
---|
| 300 | |
---|
| 301 | private: |
---|
| 302 | OIS::Keyboard *mKeyboard; |
---|
| 303 | }; |
---|
| 304 | |
---|
[399] | 305 | void createFrameListener() |
---|
[137] | 306 | { |
---|
[399] | 307 | mListener = new OrxExitListener(mKeyboard); |
---|
| 308 | mRoot->addFrameListener(mListener); |
---|
[137] | 309 | } |
---|
[74] | 310 | |
---|
[399] | 311 | void startRenderLoop() |
---|
| 312 | { |
---|
| 313 | mRoot->startRendering(); |
---|
| 314 | } |
---|
[428] | 315 | |
---|
| 316 | |
---|
| 317 | private: |
---|
| 318 | Ogre::Root *mRoot; |
---|
| 319 | OIS::Keyboard *mKeyboard; |
---|
| 320 | OIS::Mouse *mMouse; |
---|
| 321 | OIS::InputManager *mInputManager; |
---|
| 322 | // CEGUI::OgreCEGUIRenderer *mRenderer; |
---|
| 323 | // CEGUI::System *mSystem; |
---|
| 324 | OrxExitListener *mListener; |
---|
| 325 | Ogre::Camera * cam; |
---|
| 326 | double x; |
---|
| 327 | |
---|
| 328 | public: |
---|
| 329 | static OrxApplication* pointer_s; |
---|
[399] | 330 | }; |
---|
[428] | 331 | |
---|
| 332 | OrxApplication* OrxApplication::pointer_s = 0; |
---|
[399] | 333 | } |
---|
[137] | 334 | |
---|
[399] | 335 | using namespace Ogre; |
---|
[137] | 336 | |
---|
| 337 | #if OGRE_PLATFORM == PLATFORM_WIN32 || OGRE_PLATFORM == OGRE_PLATFORM_WIN32 |
---|
[74] | 338 | #define WIN32_LEAN_AND_MEAN |
---|
| 339 | #include "windows.h" |
---|
| 340 | |
---|
[137] | 341 | INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT) |
---|
[74] | 342 | #else |
---|
[137] | 343 | int main(int argc, char **argv) |
---|
[74] | 344 | #endif |
---|
| 345 | { |
---|
[137] | 346 | try |
---|
| 347 | { |
---|
[399] | 348 | orxonox::OrxApplication orxonox; |
---|
[74] | 349 | orxonox.go(); |
---|
[137] | 350 | } |
---|
| 351 | catch(Exception& e) |
---|
| 352 | { |
---|
| 353 | #if OGRE_PLATFORM == PLATFORM_WIN32 || OGRE_PLATFORM == OGRE_PLATFORM_WIN32 |
---|
| 354 | MessageBoxA(NULL, e.getFullDescription().c_str(), "An exception has occurred!", MB_OK | MB_ICONERROR | MB_TASKMODAL); |
---|
[74] | 355 | #else |
---|
| 356 | fprintf(stderr, "An exception has occurred: %s\n", |
---|
| 357 | e.getFullDescription().c_str()); |
---|
| 358 | #endif |
---|
| 359 | } |
---|
| 360 | |
---|
| 361 | return 0; |
---|
| 362 | } |
---|
[137] | 363 | |
---|