Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

global enet test

File size: 6.7 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 <enet/enet.h>
39#include <enet/protocol.h>
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
68using namespace Ogre;
69
70class OrxExitListener : public FrameListener
71{
72  public:
73    OrxExitListener(OIS::Keyboard *keyboard)
74  : mKeyboard(keyboard)
75    {
76    }
77
78    bool frameStarted(const FrameEvent& evt)
79    {
80      mKeyboard->capture();
81      return !mKeyboard->isKeyDown(OIS::KC_ESCAPE);
82    }
83
84  private:
85    OIS::Keyboard *mKeyboard;
86};
87
88class OrxApplication
89{
90  public:
91    void go()
92    {
93      createRoot();
94      defineResources();
95      setupRenderSystem();
96      createRenderWindow();
97      initializeResourceGroups();
98      setupScene();
99      setupInputSystem();
100      setupCEGUI();
101      createFrameListener();
102      startRenderLoop();
103    }
104
105    ~OrxApplication()
106    {
107      mInputManager->destroyInputObject(mKeyboard);
108      OIS::InputManager::destroyInputSystem(mInputManager);
109
110      delete mRenderer;
111      delete mSystem;
112
113      delete mListener;
114      delete mRoot;
115    }
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()
127    {
128#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
129      mRoot = new Root(macBundlePath() + "/Contents/Resources/plugins.cfg");
130#else
131      mRoot = new Root();
132#endif
133    }
134
135    void defineResources()
136    {
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
144
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    }
163
164    void setupRenderSystem()
165    {
166      if (!mRoot->restoreConfig() && !mRoot->showConfigDialog())
167        throw Exception(52, "User canceled the config dialog!", "OrxApplication::setupRenderSystem()");
168    }
169
170    void createRenderWindow()
171    {
172      mRoot->initialise(true, "Ogre Render Window");
173    }
174
175    void initializeResourceGroups()
176    {
177      TextureManager::getSingleton().setDefaultNumMipmaps(5);
178      ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
179    }
180
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    }
187
188    void setupInputSystem()
189    {
190      size_t windowHnd = 0;
191      std::ostringstream windowHndStr;
192      OIS::ParamList pl;
193      RenderWindow *win = mRoot->getAutoCreatedWindow();
194
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);
199
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      }
209    }
210
211    void setupCEGUI()
212    {
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.
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#if OGRE_PLATFORM == PLATFORM_WIN32 || OGRE_PLATFORM == OGRE_PLATFORM_WIN32
236#define WIN32_LEAN_AND_MEAN
237#include "windows.h"
238
239             INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT)
240#else
241             int main(int argc, char **argv)
242#endif
243{
244  try
245  {
246    OrxApplication orxonox;
247    orxonox.go();
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);
253#else
254    fprintf(stderr, "An exception has occurred: %s\n",
255            e.getFullDescription().c_str());
256#endif
257  }
258
259  return 0;
260}
261
Note: See TracBrowser for help on using the repository browser.