Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

removed enet-headers
set plugins.cfg-init to a safe default (i.e removed cg-dep)

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