Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/hud/src/orxonox.cc @ 447

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