Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/skybox/src/orxonox.cc @ 399

Last change on this file since 399 was 399, checked in by landauf, 17 years ago

some changes

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