Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

merged audio into FICN

File size: 11.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// 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
44#include <OIS/OIS.h>
45//#include <CEGUI/CEGUI.h>
46//#include <OgreCEGUIRenderer.h>
47
48#include <string>
49#include <iostream>
50
51#include "xml/xmlParser.h"
52#include "loader/LevelLoader.h"
53#include "audio/AudioManager.h"
54
55#include "spaceship_steering.h"
56SpaceshipSteering* steering;
57
58
59// some tests to see if enet works without includsion
60//#include <enet/enet.h>
61//#include <enet/protocol.h>
62
63#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
64#include <CoreFoundation/CoreFoundation.h>
65
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()
70{
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
90namespace orxonox
91{
92
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;
144      }
145      else {
146        steering->rotateUp(0);
147        steering->rotateDown(0);
148        steering->rotateRight(0);
149        steering->rotateLeft(0);
150      }
151
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    }
158
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
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      }
204
205      ~OrxApplication()
206      {
207        mInputManager->destroyInputObject(mKeyboard);
208        OIS::InputManager::destroyInputSystem(mInputManager);
209
210//        delete mRenderer;
211//        delete mSystem;
212
213        delete mListener;
214        delete mRoot;
215      }
216
217    private:
218      Ogre::Root *mRoot;
219      OIS::Keyboard *mKeyboard;
220      OIS::Mouse *mMouse;
221      OIS::InputManager *mInputManager;
222      //CEGUI::OgreCEGUIRenderer *mRenderer;
223      //CEGUI::System *mSystem;
224      OrxExitListener *mListener;
225
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      }
234
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())
247        {
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)
252          {
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
260          }
261        }
262      }
263
264      void setupRenderSystem()
265      {
266        if (!mRoot->restoreConfig() && !mRoot->showConfigDialog())
267          throw Exception(52, "User canceled the config dialog!", "OrxApplication::setupRenderSystem()");
268      }
269
270      void createRenderWindow()
271      {
272        mRoot->initialise(true, "OrxonoxV2");
273      }
274
275      void initializeResourceGroups()
276      {
277        TextureManager::getSingleton().setDefaultNumMipmaps(5);
278        ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
279      }
280
281      void createScene(void)
282      {
283                audio::AudioManager* auMan = new audio::AudioManager();
284
285
286      string levelFile = "sp_level_moonstation.oxw";
287      loader::LevelLoader* loader = new loader::LevelLoader(levelFile);
288    }
289
290
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);
298
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
329      void setupInputSystem()
330      {
331        size_t windowHnd = 0;
332        std::ostringstream windowHndStr;
333        OIS::ParamList pl;
334        Ogre::RenderWindow *win = mRoot->getAutoCreatedWindow();
335
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);
340
341        try
342        {
343          mKeyboard = static_cast<OIS::Keyboard*>(mInputManager->createInputObject(OIS::OISKeyboard, false));
344          mMouse = static_cast<OIS::Mouse*>(mInputManager->createInputObject(OIS::OISMouse, true));
345        }
346        catch (const OIS::Exception &e)
347        {
348          throw new Ogre::Exception(42, e.eText, "OrxApplication::setupInputSystem");
349        }
350      }
351
352      void setupCEGUI()
353      {
354        Ogre::SceneManager *mgr = mRoot->getSceneManager("Default SceneManager");
355        Ogre::RenderWindow *win = mRoot->getAutoCreatedWindow();
356
357        // CEGUI setup
358//        mRenderer = new CEGUI::OgreCEGUIRenderer(win, Ogre::RENDER_QUEUE_OVERLAY, false, 3000, mgr);
359//        mSystem = new CEGUI::System(mRenderer);
360
361        // Other CEGUI setup here.
362      }
363
364
365    void createFrameListener()
366    {
367      mListener = new OrxExitListener(mKeyboard, mMouse);
368      mRoot->addFrameListener(mListener);
369    }
370      void startRenderLoop()
371      {
372        mRoot->startRendering();
373      }
374  };
375}
376
377using namespace Ogre;
378
379#if OGRE_PLATFORM == PLATFORM_WIN32 || OGRE_PLATFORM == OGRE_PLATFORM_WIN32
380#define WIN32_LEAN_AND_MEAN
381#include "windows.h"
382
383             INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT)
384#else
385             int main(int argc, char **argv)
386#endif
387{
388  try
389  {
390    orxonox::OrxApplication orxonox;
391    orxonox.go();
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);
397#else
398    fprintf(stderr, "An exception has occurred: %s\n",
399            e.getFullDescription().c_str());
400#endif
401  }
402
403  return 0;
404}
405
Note: See TracBrowser for help on using the repository browser.