| 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 | |
|---|
| 28 | /** |
|---|
| 29 | @file orxonox.cc |
|---|
| 30 | @brief Orxonox Main File |
|---|
| 31 | */ |
|---|
| 32 | |
|---|
| 33 | #include <Ogre.h> |
|---|
| 34 | #include <OIS/OIS.h> |
|---|
| 35 | //#include <CEGUI/CEGUI.h> |
|---|
| 36 | //#include <OgreCEGUIRenderer.h> |
|---|
| 37 | |
|---|
| 38 | #include <string> |
|---|
| 39 | #include <iostream> |
|---|
| 40 | #include "Universe.h" |
|---|
| 41 | |
|---|
| 42 | #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE |
|---|
| 43 | #include <CoreFoundation/CoreFoundation.h> |
|---|
| 44 | #include "windows.h" |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 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() |
|---|
| 51 | { |
|---|
| 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 | |
|---|
| 71 | namespace orxonox |
|---|
| 72 | { |
|---|
| 73 | Ogre::RenderWindow * mWindow; |
|---|
| 74 | Ogre::SceneManager *mgr; |
|---|
| 75 | int stopper = 0; |
|---|
| 76 | |
|---|
| 77 | class OrxApplication |
|---|
| 78 | { |
|---|
| 79 | public: |
|---|
| 80 | void go() |
|---|
| 81 | { |
|---|
| 82 | createRoot(); |
|---|
| 83 | defineResources(); |
|---|
| 84 | setupRenderSystem(); |
|---|
| 85 | createRenderWindow(); |
|---|
| 86 | initializeResourceGroups(); |
|---|
| 87 | createScene(); |
|---|
| 88 | setupScene(); |
|---|
| 89 | createUniverse(); |
|---|
| 90 | setupInputSystem(); |
|---|
| 91 | setupCEGUI(); |
|---|
| 92 | createFrameListener(); |
|---|
| 93 | startRenderLoop(); |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | OrxApplication() |
|---|
| 97 | { |
|---|
| 98 | OrxApplication::pointer_s = this; |
|---|
| 99 | this->x = 0; |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | ~OrxApplication() |
|---|
| 103 | { |
|---|
| 104 | mInputManager->destroyInputObject(mKeyboard); |
|---|
| 105 | OIS::InputManager::destroyInputSystem(mInputManager); |
|---|
| 106 | |
|---|
| 107 | delete mListener; |
|---|
| 108 | delete mRoot; |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | void createRoot() |
|---|
| 113 | { |
|---|
| 114 | #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE |
|---|
| 115 | mRoot = new Ogre::Root(macBundlePath() + "/Contents/Resources/plugins.cfg"); |
|---|
| 116 | #else |
|---|
| 117 | mRoot = new Ogre::Root(); |
|---|
| 118 | #endif |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | void defineResources() |
|---|
| 122 | { |
|---|
| 123 | Ogre::String secName, typeName, archName; |
|---|
| 124 | Ogre::ConfigFile cf; |
|---|
| 125 | #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE |
|---|
| 126 | cf.load(macBundlePath() + "/Contents/Resources/resources.cfg"); |
|---|
| 127 | #else |
|---|
| 128 | cf.load("resources.cfg"); |
|---|
| 129 | #endif |
|---|
| 130 | |
|---|
| 131 | Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator(); |
|---|
| 132 | while (seci.hasMoreElements()) |
|---|
| 133 | { |
|---|
| 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; |
|---|
| 141 | #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE |
|---|
| 142 | Ogre::ResourceGroupManager::getSingleton().addResourceLocation( String(macBundlePath() + "/" + archName), typeName, secName); |
|---|
| 143 | #else |
|---|
| 144 | Ogre::ResourceGroupManager::getSingleton().addResourceLocation( archName, typeName, secName); |
|---|
| 145 | #endif |
|---|
| 146 | } |
|---|
| 147 | } |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | void setupRenderSystem() |
|---|
| 151 | { |
|---|
| 152 | if (!mRoot->restoreConfig() && !mRoot->showConfigDialog()) |
|---|
| 153 | throw Ogre::Exception(52, "User canceled the config dialog!", "OrxApplication::setupRenderSystem()"); |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | void createRenderWindow() |
|---|
| 157 | { |
|---|
| 158 | mWindow = mRoot->initialise(true, "Ogre Render Window"); |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | void initializeResourceGroups() |
|---|
| 162 | { |
|---|
| 163 | Ogre::TextureManager::getSingleton().setDefaultNumMipmaps(5); |
|---|
| 164 | Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups(); |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | void createScene(void) |
|---|
| 168 | { |
|---|
| 169 | |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | void setupScene() |
|---|
| 173 | { |
|---|
| 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)); |
|---|
| 179 | Ogre::Viewport *vp = mRoot->getAutoCreatedWindow()->addViewport(cam); |
|---|
| 180 | |
|---|
| 181 | Ogre::ColourValue fadeColour(0.0, 0.0, 0.0); |
|---|
| 182 | vp->setBackgroundColour(fadeColour); |
|---|
| 183 | mgr->setFog(Ogre::FOG_EXP, fadeColour, 0.0, 10000, 38000); |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 186 | //mgr->setSkyBox(true, "Examples/SpaceSkybox"); |
|---|
| 187 | //mgr->setSkyBox(true, "MySky"); |
|---|
| 188 | |
|---|
| 189 | mgr->setAmbientLight(Ogre::ColourValue(0.1, 1.0, 0.1)); |
|---|
| 190 | /*Ogre::Light *light = mgrtemp->createLight(lightnames); |
|---|
| 191 | light->setType(Ogre::Light::LT_POINT); |
|---|
| 192 | light->setPosition(Ogre::Vector3(0, 0, 0)); |
|---|
| 193 | |
|---|
| 194 | light->setDiffuseColour(1.0, 0.0, 0.0); |
|---|
| 195 | light->setSpecularColour(1.0, 0.0, 0.0); |
|---|
| 196 | */ |
|---|
| 197 | |
|---|
| 198 | |
|---|
| 199 | } |
|---|
| 200 | |
|---|
| 201 | void setupInputSystem() |
|---|
| 202 | { |
|---|
| 203 | size_t windowHnd = 0; |
|---|
| 204 | std::ostringstream windowHndStr; |
|---|
| 205 | OIS::ParamList pl; |
|---|
| 206 | Ogre::RenderWindow *win = mRoot->getAutoCreatedWindow(); |
|---|
| 207 | |
|---|
| 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); |
|---|
| 212 | |
|---|
| 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 Ogre::Exception(42, e.eText, "OrxApplication::setupInputSystem"); |
|---|
| 221 | } |
|---|
| 222 | } |
|---|
| 223 | |
|---|
| 224 | void setupCEGUI() |
|---|
| 225 | { |
|---|
| 226 | Ogre::SceneManager *mgr = mRoot->getSceneManager("Default SceneManager"); |
|---|
| 227 | Ogre::RenderWindow *win = mRoot->getAutoCreatedWindow(); |
|---|
| 228 | } |
|---|
| 229 | |
|---|
| 230 | void screenShots() |
|---|
| 231 | { |
|---|
| 232 | |
|---|
| 233 | /*unsigned int indice = 1; |
|---|
| 234 | char filename[30]; |
|---|
| 235 | char fn[1]; |
|---|
| 236 | // generate new names... |
|---|
| 237 | //sprintf(filename, "screenshot_%d.png", ++indice); |
|---|
| 238 | //sprintf(filename, fn, ++indice); |
|---|
| 239 | */ |
|---|
| 240 | |
|---|
| 241 | if(stopper == 1){ |
|---|
| 242 | //sprintf(filename, "stevecube_FR.bmp"); |
|---|
| 243 | mWindow->writeContentsToFile("MySky_FR.bmp"); |
|---|
| 244 | } |
|---|
| 245 | else if(stopper == 2){ |
|---|
| 246 | //sprintf(filename, "stevecube_LF.bmp"); |
|---|
| 247 | cam->pitch(Ogre::Degree(90)); |
|---|
| 248 | mWindow->writeContentsToFile("MySky_LF.bmp"); |
|---|
| 249 | } |
|---|
| 250 | else if(stopper == 3){ |
|---|
| 251 | //sprintf(filename, "stevecube_BK.bmp"); |
|---|
| 252 | cam->pitch(Ogre::Degree(90)); |
|---|
| 253 | mWindow->writeContentsToFile("MySky_BK.bmp"); |
|---|
| 254 | } |
|---|
| 255 | else if(stopper == 4){ |
|---|
| 256 | //sprintf(filename, "stevecube_RT.bmp"); |
|---|
| 257 | cam->pitch(Ogre::Degree(90)); |
|---|
| 258 | mWindow->writeContentsToFile("MySky_RT.bmp"); |
|---|
| 259 | } |
|---|
| 260 | else if(stopper == 5){ |
|---|
| 261 | //sprintf(filename, "stevecube_UP.bmp"); |
|---|
| 262 | cam->yaw(Ogre::Degree(90)); |
|---|
| 263 | mWindow->writeContentsToFile("MySky_UP.bmp"); |
|---|
| 264 | } |
|---|
| 265 | else if(stopper == 6){ |
|---|
| 266 | cam->yaw(Ogre::Degree(-90)); |
|---|
| 267 | mWindow->writeContentsToFile("MySky_DN.bmp"); |
|---|
| 268 | } |
|---|
| 269 | |
|---|
| 270 | stopper+=1; |
|---|
| 271 | if(stopper >= 7) |
|---|
| 272 | stopper = 1; |
|---|
| 273 | } |
|---|
| 274 | |
|---|
| 275 | |
|---|
| 276 | |
|---|
| 277 | |
|---|
| 278 | void createUniverse() |
|---|
| 279 | { |
|---|
| 280 | |
|---|
| 281 | // UNIVERSE-CLASS |
|---|
| 282 | std::cout << "1_1\n"; |
|---|
| 283 | Universe::Universe myUni = Universe(77000); |
|---|
| 284 | std::cout << "1_2\n"; |
|---|
| 285 | myUni.createCoordinates(); |
|---|
| 286 | std::cout << "1_3\n"; |
|---|
| 287 | myUni.createBillboards(mgr, cam, mRoot); |
|---|
| 288 | std::cout << "1_4\n"; |
|---|
| 289 | |
|---|
| 290 | |
|---|
| 291 | } |
|---|
| 292 | |
|---|
| 293 | |
|---|
| 294 | |
|---|
| 295 | |
|---|
| 296 | class OrxExitListener : public Ogre::FrameListener |
|---|
| 297 | { |
|---|
| 298 | public: |
|---|
| 299 | OrxExitListener(OIS::Keyboard *keyboard) |
|---|
| 300 | : mKeyboard(keyboard) |
|---|
| 301 | { |
|---|
| 302 | } |
|---|
| 303 | bool frameEnded(const Ogre::FrameEvent& evt) |
|---|
| 304 | { |
|---|
| 305 | if(mKeyboard->isKeyDown(OIS::KC_RETURN) or stopper > 1){ |
|---|
| 306 | OrxApplication::pointer_s->screenShots(); |
|---|
| 307 | } |
|---|
| 308 | |
|---|
| 309 | return true; |
|---|
| 310 | } |
|---|
| 311 | |
|---|
| 312 | bool frameStarted(const Ogre::FrameEvent& evt) |
|---|
| 313 | { |
|---|
| 314 | mKeyboard->capture(); |
|---|
| 315 | return !mKeyboard->isKeyDown(OIS::KC_ESCAPE); |
|---|
| 316 | } |
|---|
| 317 | |
|---|
| 318 | |
|---|
| 319 | private: |
|---|
| 320 | OIS::Keyboard *mKeyboard; |
|---|
| 321 | }; |
|---|
| 322 | |
|---|
| 323 | void createFrameListener() |
|---|
| 324 | { |
|---|
| 325 | mListener = new OrxExitListener(mKeyboard); |
|---|
| 326 | mRoot->addFrameListener(mListener); |
|---|
| 327 | } |
|---|
| 328 | |
|---|
| 329 | void startRenderLoop() |
|---|
| 330 | { |
|---|
| 331 | mRoot->startRendering(); |
|---|
| 332 | } |
|---|
| 333 | |
|---|
| 334 | |
|---|
| 335 | private: |
|---|
| 336 | Ogre::Root *mRoot; |
|---|
| 337 | OIS::Keyboard *mKeyboard; |
|---|
| 338 | OIS::Mouse *mMouse; |
|---|
| 339 | OIS::InputManager *mInputManager; |
|---|
| 340 | // CEGUI::OgreCEGUIRenderer *mRenderer; |
|---|
| 341 | // CEGUI::System *mSystem; |
|---|
| 342 | OrxExitListener *mListener; |
|---|
| 343 | Ogre::Camera * cam; |
|---|
| 344 | double x; |
|---|
| 345 | |
|---|
| 346 | public: |
|---|
| 347 | static OrxApplication* pointer_s; |
|---|
| 348 | }; |
|---|
| 349 | |
|---|
| 350 | OrxApplication* OrxApplication::pointer_s = 0; |
|---|
| 351 | } |
|---|
| 352 | |
|---|
| 353 | using namespace Ogre; |
|---|
| 354 | |
|---|
| 355 | #if OGRE_PLATFORM == PLATFORM_WIN32 || OGRE_PLATFORM == OGRE_PLATFORM_WIN32 |
|---|
| 356 | #define WIN32_LEAN_AND_MEAN |
|---|
| 357 | #include "windows.h" |
|---|
| 358 | |
|---|
| 359 | INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT) |
|---|
| 360 | #else |
|---|
| 361 | int main(int argc, char **argv) |
|---|
| 362 | #endif |
|---|
| 363 | { |
|---|
| 364 | srand(time(NULL)); |
|---|
| 365 | try |
|---|
| 366 | { |
|---|
| 367 | orxonox::OrxApplication orxonox; |
|---|
| 368 | orxonox.go(); |
|---|
| 369 | } |
|---|
| 370 | catch(Exception& e) |
|---|
| 371 | { |
|---|
| 372 | #if OGRE_PLATFORM == PLATFORM_WIN32 || OGRE_PLATFORM == OGRE_PLATFORM_WIN32 |
|---|
| 373 | MessageBoxA(NULL, e.getFullDescription().c_str(), "An exception has occurred!", MB_OK | MB_ICONERROR | MB_TASKMODAL); |
|---|
| 374 | #else |
|---|
| 375 | fprintf(stderr, "An exception has occurred: %s\n", |
|---|
| 376 | e.getFullDescription().c_str()); |
|---|
| 377 | #endif |
|---|
| 378 | } |
|---|
| 379 | |
|---|
| 380 | return 0; |
|---|
| 381 | } |
|---|
| 382 | |
|---|