Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/FICN/src/orxonox/orxonox.cc @ 400

Last change on this file since 400 was 389, checked in by nicolasc, 17 years ago

merged audio into FICN

File size: 11.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
[379]33//#include <Ogre.h>
34// 40% speed up: (instead of Ogre.h)
35#include <OgreSceneNode.h>
36#include <OgreSceneManager.h>
37#include <OgreRoot.h>
38#include <OgreFrameListener.h>
39#include <OgreConfigFile.h>
40#include <OgreTextureManager.h>
41#include <OgreEntity.h>
42#include <OgreRenderWindow.h>
43
[137]44#include <OIS/OIS.h>
[346]45//#include <CEGUI/CEGUI.h>
46//#include <OgreCEGUIRenderer.h>
[74]47
[164]48#include <string>
49#include <iostream>
50
[337]51#include "xml/xmlParser.h"
52#include "loader/LevelLoader.h"
[389]53#include "audio/AudioManager.h"
[164]54
[337]55#include "spaceship_steering.h"
56SpaceshipSteering* steering;
[164]57
[337]58
[151]59// some tests to see if enet works without includsion
60//#include <enet/enet.h>
61//#include <enet/protocol.h>
[142]62
[137]63#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
64#include <CoreFoundation/CoreFoundation.h>
[74]65
[137]66// This function will locate the path to our application on OS X,
67// unlike windows you can not rely on the curent working directory
68// for locating your configuration files and resources.
69std::string macBundlePath()
[74]70{
[137]71  char path[1024];
72  CFBundleRef mainBundle = CFBundleGetMainBundle();
73  assert(mainBundle);
74
75  CFURLRef mainBundleURL = CFBundleCopyBundleURL(mainBundle);
76  assert(mainBundleURL);
77
78  CFStringRef cfStringRef = CFURLCopyFileSystemPath( mainBundleURL, kCFURLPOSIXPathStyle);
79  assert(cfStringRef);
80
81  CFStringGetCString(cfStringRef, path, 1024, kCFStringEncodingASCII);
82
83  CFRelease(mainBundleURL);
84  CFRelease(cfStringRef);
85
86  return std::string(path);
87}
88#endif
89
[337]90namespace orxonox
91{
[74]92
[337]93using namespace Ogre;
94
95class OrxExitListener : public FrameListener, public OIS::MouseListener
96{
97  public:
98    OrxExitListener(OIS::Keyboard *keyboard, OIS::Mouse *mouse)
99  : mKeyboard(keyboard), mMouse(mouse)
100    {
101      speed = 250;
102      loop = 100;
103      rotate = 10;
104      mouseX = 0;
105      mouseY = 0;
106      maxMouseX = 0;
107      minMouseX = 0;
108      moved = false;
109      steering->brakeRotate(rotate*10);
110      steering->brakeLoop(loop);
111      mMouse->setEventCallback(this);
112    }
113    bool frameStarted(const FrameEvent& evt)
114    {
115      mKeyboard->capture();
116      mMouse->capture();
117      if (mKeyboard->isKeyDown(OIS::KC_UP) || mKeyboard->isKeyDown(OIS::KC_W))
118        steering->moveForward(speed);
119      else
120        steering->moveForward(0);
121      if(mKeyboard->isKeyDown(OIS::KC_DOWN) || mKeyboard->isKeyDown(OIS::KC_S))
122        steering->brakeForward(speed);
123      else
124        steering->brakeForward(speed/10);
125      if (mKeyboard->isKeyDown(OIS::KC_RIGHT) || mKeyboard->isKeyDown(OIS::KC_D))
126        steering->loopRight(loop);
127      else
128        steering->loopRight(0);
129      if (mKeyboard->isKeyDown(OIS::KC_LEFT) || mKeyboard->isKeyDown(OIS::KC_A))
130        steering->loopLeft(loop);
131      else
132        steering->loopLeft(0);
133
134      if(moved) {
135        if (mouseY<0)
136          steering->rotateUp(-mouseY*rotate);
137        if (mouseY>0)
138          steering->rotateDown(mouseY*rotate);
139        if (mouseX>0)
140          steering->rotateRight(mouseX*rotate);
141        if (mouseX<0)
142          steering->rotateLeft(-mouseX*rotate);
143        moved = false;
[265]144      }
[337]145      else {
146        steering->rotateUp(0);
147        steering->rotateDown(0);
148        steering->rotateRight(0);
149        steering->rotateLeft(0);
150      }
[135]151
[337]152      steering->tick(evt.timeSinceLastFrame);
153//      scenemanager->spacehip->tick(evt.timesincelastframe);
154      if(mKeyboard->isKeyDown(OIS::KC_ESCAPE))
155        cout << "maximal MouseX: " << maxMouseX << "\tminMouseX: " << minMouseX << endl;
156      return !mKeyboard->isKeyDown(OIS::KC_ESCAPE);
157    }
[74]158
[337]159    bool mouseMoved(const OIS::MouseEvent &e)
160    {
161      mouseX = e.state.X.rel;
162      mouseY = e.state.Y.rel;
163      if(mouseX>maxMouseX) maxMouseX = mouseX;
164      if(mouseX<minMouseX) minMouseX = mouseX;
165      cout << "mouseX: " << mouseX << "\tmouseY: " << mouseY << endl;
166      moved = true;
167      return true;
168    }
169
170    bool mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id) { return true; }
171    bool mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id) { return true; }
172
173  private:
174    float speed;
175    float rotate;
176    float loop;
177    float mouseY;
178    float mouseX;
179    float maxMouseX;
180    float minMouseX;
181    bool moved;
182    OIS::Keyboard *mKeyboard;
183    OIS::Mouse *mMouse;
184};
185
186
[265]187  class OrxApplication
188  {
189    public:
190      void go()
191      {
192        createRoot();
193        defineResources();
194        setupRenderSystem();
195        createRenderWindow();
196        initializeResourceGroups();
197        createScene();
198        setupScene();
199        setupInputSystem();
200        setupCEGUI();
201        createFrameListener();
202        startRenderLoop();
203      }
[74]204
[265]205      ~OrxApplication()
206      {
207        mInputManager->destroyInputObject(mKeyboard);
208        OIS::InputManager::destroyInputSystem(mInputManager);
[137]209
[265]210//        delete mRenderer;
211//        delete mSystem;
[137]212
[265]213        delete mListener;
214        delete mRoot;
215      }
[137]216
[265]217    private:
218      Ogre::Root *mRoot;
219      OIS::Keyboard *mKeyboard;
220      OIS::Mouse *mMouse;
221      OIS::InputManager *mInputManager;
[346]222      //CEGUI::OgreCEGUIRenderer *mRenderer;
223      //CEGUI::System *mSystem;
[265]224      OrxExitListener *mListener;
[137]225
[265]226      void createRoot()
227      {
228#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
229        mRoot = new Ogre::Root(macBundlePath() + "/Contents/Resources/plugins.cfg");
230#else
231        mRoot = new Ogre::Root();
232#endif
233      }
[74]234
[265]235      void defineResources()
236      {
237        Ogre::String secName, typeName, archName;
238        Ogre::ConfigFile cf;
239#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
240        cf.load(macBundlePath() + "/Contents/Resources/resources.cfg");
241#else
242        cf.load("resources.cfg");
243#endif
244
245        Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator();
246        while (seci.hasMoreElements())
[263]247        {
[265]248          secName = seci.peekNextKey();
249          Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext();
250          Ogre::ConfigFile::SettingsMultiMap::iterator i;
251          for (i = settings->begin(); i != settings->end(); ++i)
[263]252          {
[265]253            typeName = i->first;
254            archName = i->second;
255#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
256            Ogre::ResourceGroupManager::getSingleton().addResourceLocation( String(macBundlePath() + "/" + archName), typeName, secName);
257#else
258            Ogre::ResourceGroupManager::getSingleton().addResourceLocation( archName, typeName, secName);
259#endif
[263]260          }
261        }
[265]262      }
[263]263
[265]264      void setupRenderSystem()
265      {
266        if (!mRoot->restoreConfig() && !mRoot->showConfigDialog())
[337]267          throw Exception(52, "User canceled the config dialog!", "OrxApplication::setupRenderSystem()");
[265]268      }
[74]269
[265]270      void createRenderWindow()
271      {
[368]272        mRoot->initialise(true, "OrxonoxV2");
[265]273      }
[74]274
[265]275      void initializeResourceGroups()
276      {
[337]277        TextureManager::getSingleton().setDefaultNumMipmaps(5);
278        ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
[265]279      }
[74]280
[265]281      void createScene(void)
282      {
[389]283                audio::AudioManager* auMan = new audio::AudioManager();
[74]284
[389]285
[347]286      string levelFile = "sp_level_moonstation.oxw";
287      loader::LevelLoader* loader = new loader::LevelLoader(levelFile);
[337]288    }
[164]289
[389]290
[337]291    void setupScene()
292    {
293      SceneManager *mgr = mRoot->createSceneManager(ST_GENERIC, "Default SceneManager");
294      Camera *cam = mgr->createCamera("Camera");
295      cam->setPosition(Vector3(0,0,-250));
296      cam->lookAt(Vector3(0,0,0));
297      Viewport *vp = mRoot->getAutoCreatedWindow()->addViewport(cam);
[258]298
[337]299
300      mgr->setAmbientLight(ColourValue(1,1,1));
301      Entity* head = mgr->createEntity("head", "ogrehead.mesh");
302      SceneNode *node = mgr->getRootSceneNode()->createChildSceneNode("OgreHeadNode", Vector3(0,0,0));
303      node->attachObject(head);
304      node->attachObject(cam);
305      mgr->setSkyBox(true, "Examples/SceneSkyBox2");
306
307      Entity* head1 = mgr->createEntity("head1", "ogrehead.mesh");
308      SceneNode *node1 = mgr->getRootSceneNode()->createChildSceneNode("OgreHeadNode1", Vector3(200,0,0));
309      node1->attachObject(head1);
310      Entity* head2 = mgr->createEntity("head2", "ogrehead.mesh");
311      SceneNode *node2 = mgr->getRootSceneNode()->createChildSceneNode("OgreHeadNode2", Vector3(200,400,-100));
312      node2->attachObject(head2);
313      Entity* head3 = mgr->createEntity("head3", "ogrehead.mesh");
314      SceneNode *node3 = mgr->getRootSceneNode()->createChildSceneNode("OgreHeadNode3", Vector3(0,400,200));
315      node3->attachObject(head3);
316      Entity* head4 = mgr->createEntity("head4", "ogrehead.mesh");
317      SceneNode *node4 = mgr->getRootSceneNode()->createChildSceneNode("OgreHeadNode4", Vector3(-400,-200,600));
318      node4->attachObject(head4);
319      Entity* head5 = mgr->createEntity("head5", "ogrehead.mesh");
320      SceneNode *node5 = mgr->getRootSceneNode()->createChildSceneNode("OgreHeadNode5", Vector3(0,0,-400));
321      node5->attachObject(head5);
322
323      steering = new SpaceshipSteering(500, 200, 200, 200);
324      steering->addNode(node);
325
326    }
327
328
[265]329      void setupInputSystem()
330      {
331        size_t windowHnd = 0;
332        std::ostringstream windowHndStr;
333        OIS::ParamList pl;
334        Ogre::RenderWindow *win = mRoot->getAutoCreatedWindow();
[74]335
[265]336        win->getCustomAttribute("WINDOW", &windowHnd);
337        windowHndStr << windowHnd;
338        pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str()));
339        mInputManager = OIS::InputManager::createInputSystem(pl);
[74]340
[265]341        try
342        {
343          mKeyboard = static_cast<OIS::Keyboard*>(mInputManager->createInputObject(OIS::OISKeyboard, false));
[337]344          mMouse = static_cast<OIS::Mouse*>(mInputManager->createInputObject(OIS::OISMouse, true));
[263]345        }
[265]346        catch (const OIS::Exception &e)
[263]347        {
[265]348          throw new Ogre::Exception(42, e.eText, "OrxApplication::setupInputSystem");
349        }
350      }
[74]351
[265]352      void setupCEGUI()
353      {
354        Ogre::SceneManager *mgr = mRoot->getSceneManager("Default SceneManager");
355        Ogre::RenderWindow *win = mRoot->getAutoCreatedWindow();
[137]356
[265]357        // CEGUI setup
358//        mRenderer = new CEGUI::OgreCEGUIRenderer(win, Ogre::RENDER_QUEUE_OVERLAY, false, 3000, mgr);
359//        mSystem = new CEGUI::System(mRenderer);
[137]360
[265]361        // Other CEGUI setup here.
362      }
[137]363
[265]364
[337]365    void createFrameListener()
366    {
367      mListener = new OrxExitListener(mKeyboard, mMouse);
368      mRoot->addFrameListener(mListener);
369    }
[265]370      void startRenderLoop()
371      {
372        mRoot->startRendering();
373      }
374  };
[337]375}
[137]376
[337]377using namespace Ogre;
378
[137]379#if OGRE_PLATFORM == PLATFORM_WIN32 || OGRE_PLATFORM == OGRE_PLATFORM_WIN32
[74]380#define WIN32_LEAN_AND_MEAN
381#include "windows.h"
382
[137]383             INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT)
[74]384#else
[137]385             int main(int argc, char **argv)
[74]386#endif
387{
[137]388  try
389  {
[263]390    orxonox::OrxApplication orxonox;
[74]391    orxonox.go();
[137]392  }
393  catch(Exception& e)
394  {
395#if OGRE_PLATFORM == PLATFORM_WIN32 || OGRE_PLATFORM == OGRE_PLATFORM_WIN32
396    MessageBoxA(NULL, e.getFullDescription().c_str(), "An exception has occurred!", MB_OK | MB_ICONERROR | MB_TASKMODAL);
[74]397#else
398    fprintf(stderr, "An exception has occurred: %s\n",
399            e.getFullDescription().c_str());
400#endif
401  }
402
403  return 0;
404}
[137]405
Note: See TracBrowser for help on using the repository browser.