Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 423 was 419, checked in by nicolape, 17 years ago

We now have a nice backgroundsound playing

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