Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox.cc @ 148

Last change on this file since 148 was 148, checked in by nicolasc, 16 years ago

fixed sigsegv

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