Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

some changes

File size: 7.1 KB
Line 
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
41#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
42#include <CoreFoundation/CoreFoundation.h>
43
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()
48{
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
68namespace orxonox
69{
70  class OrxExitListener : public Ogre::FrameListener
71  {
72    public:
73      OrxExitListener(OIS::Keyboard *keyboard)
74    : mKeyboard(keyboard)
75      {
76      }
77
78      bool frameStarted(const Ogre::FrameEvent& evt)
79      {
80        mKeyboard->capture();
81        return !mKeyboard->isKeyDown(OIS::KC_ESCAPE);
82      }
83
84    private:
85      OIS::Keyboard *mKeyboard;
86  };
87
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      }
105
106      ~OrxApplication()
107      {
108        mInputManager->destroyInputObject(mKeyboard);
109        OIS::InputManager::destroyInputSystem(mInputManager);
110
111        delete mListener;
112        delete mRoot;
113      }
114
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;
123
124      void createRoot()
125      {
126#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
127        mRoot = new Ogre::Root(macBundlePath() + "/Contents/Resources/plugins.cfg");
128#else
129        mRoot = new Ogre::Root();
130#endif
131      }
132
133      void defineResources()
134      {
135        Ogre::String secName, typeName, archName;
136        Ogre::ConfigFile cf;
137#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
138        cf.load(macBundlePath() + "/Contents/Resources/resources.cfg");
139#else
140        cf.load("resources.cfg");
141#endif
142
143        Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator();
144        while (seci.hasMoreElements())
145        {
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;
153#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
154            Ogre::ResourceGroupManager::getSingleton().addResourceLocation( String(macBundlePath() + "/" + archName), typeName, secName);
155#else
156            Ogre::ResourceGroupManager::getSingleton().addResourceLocation( archName, typeName, secName);
157#endif
158          }
159        }
160      }
161
162      void setupRenderSystem()
163      {
164        if (!mRoot->restoreConfig() && !mRoot->showConfigDialog())
165          throw Ogre::Exception(52, "User canceled the config dialog!", "OrxApplication::setupRenderSystem()");
166      }
167
168      void createRenderWindow()
169      {
170        mRoot->initialise(true, "Ogre Render Window");
171      }
172
173      void initializeResourceGroups()
174      {
175        Ogre::TextureManager::getSingleton().setDefaultNumMipmaps(5);
176        Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
177      }
178
179      void createScene(void)
180      {
181
182      }
183
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      }
193
194      void setupInputSystem()
195      {
196        size_t windowHnd = 0;
197        std::ostringstream windowHndStr;
198        OIS::ParamList pl;
199        Ogre::RenderWindow *win = mRoot->getAutoCreatedWindow();
200
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()
218      {
219        Ogre::SceneManager *mgr = mRoot->getSceneManager("Default SceneManager");
220        Ogre::RenderWindow *win = mRoot->getAutoCreatedWindow();
221      }
222
223      void createFrameListener()
224      {
225        mListener = new OrxExitListener(mKeyboard);
226        mRoot->addFrameListener(mListener);
227      }
228
229      void startRenderLoop()
230      {
231        mRoot->startRendering();
232      }
233  };
234}
235
236using namespace Ogre;
237
238#if OGRE_PLATFORM == PLATFORM_WIN32 || OGRE_PLATFORM == OGRE_PLATFORM_WIN32
239#define WIN32_LEAN_AND_MEAN
240#include "windows.h"
241
242             INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT)
243#else
244             int main(int argc, char **argv)
245#endif
246{
247  try
248  {
249    orxonox::OrxApplication orxonox;
250    orxonox.go();
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);
256#else
257    fprintf(stderr, "An exception has occurred: %s\n",
258            e.getFullDescription().c_str());
259#endif
260  }
261
262  return 0;
263}
264
Note: See TracBrowser for help on using the repository browser.