Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 458 was 458, checked in by nicolasc, 16 years ago
  • removed setupCEGUI as it is not used nor does it work
  • some minor changes in audio
File size: 11.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// 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                                auMan->ambientAdd("a1");
290                                auMan->ambientAdd("a2");
291                                auMan->ambientAdd("a3");
292                                //auMan->ambientAdd("ambient1");
293                                auMan->ambientStart();
294
295      string levelFile = "sp_level_moonstation.oxw";
296//       loader::LevelLoader* loader = new loader::LevelLoader(levelFile);
297    }
298
299
300    void setupScene()
301    {
302      SceneManager *mgr = mRoot->createSceneManager(ST_GENERIC, "Default SceneManager");
303      Camera *cam = mgr->createCamera("Camera");
304      cam->setPosition(Vector3(0,0,-250));
305      cam->lookAt(Vector3(0,0,0));
306      Viewport *vp = mRoot->getAutoCreatedWindow()->addViewport(cam);
307
308
309      mgr->setAmbientLight(ColourValue(1,1,1));
310      Entity* head = mgr->createEntity("head", "razor.mesh");
311      SceneNode *node = mgr->getRootSceneNode()->createChildSceneNode("OgreHeadNode", Vector3(0,0,0));
312      node->attachObject(head);
313      node->attachObject(cam);
314      mgr->setSkyBox(true, "Examples/SceneSkyBox2");
315
316      Entity* head1 = mgr->createEntity("head1", "ogrehead.mesh");
317      SceneNode *node1 = mgr->getRootSceneNode()->createChildSceneNode("OgreHeadNode1", Vector3(200,0,0));
318      node1->attachObject(head1);
319      Entity* head2 = mgr->createEntity("head2", "ogrehead.mesh");
320      SceneNode *node2 = mgr->getRootSceneNode()->createChildSceneNode("OgreHeadNode2", Vector3(200,400,-100));
321      node2->attachObject(head2);
322      Entity* head3 = mgr->createEntity("head3", "ogrehead.mesh");
323      SceneNode *node3 = mgr->getRootSceneNode()->createChildSceneNode("OgreHeadNode3", Vector3(0,400,200));
324      node3->attachObject(head3);
325      Entity* head4 = mgr->createEntity("head4", "ogrehead.mesh");
326      SceneNode *node4 = mgr->getRootSceneNode()->createChildSceneNode("OgreHeadNode4", Vector3(-400,-200,600));
327      node4->attachObject(head4);
328      Entity* head5 = mgr->createEntity("head5", "ogrehead.mesh");
329      SceneNode *node5 = mgr->getRootSceneNode()->createChildSceneNode("OgreHeadNode5", Vector3(0,0,-400));
330      node5->attachObject(head5);
331
332      steering = new SpaceshipSteering(500, 200, 200, 200);
333      steering->addNode(node);
334
335    }
336
337
338      void setupInputSystem()
339      {
340        size_t windowHnd = 0;
341        std::ostringstream windowHndStr;
342        OIS::ParamList pl;
343        Ogre::RenderWindow *win = mRoot->getAutoCreatedWindow();
344
345        win->getCustomAttribute("WINDOW", &windowHnd);
346        windowHndStr << windowHnd;
347        pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str()));
348        mInputManager = OIS::InputManager::createInputSystem(pl);
349
350        try
351        {
352          mKeyboard = static_cast<OIS::Keyboard*>(mInputManager->createInputObject(OIS::OISKeyboard, false));
353          mMouse = static_cast<OIS::Mouse*>(mInputManager->createInputObject(OIS::OISMouse, true));
354        }
355        catch (const OIS::Exception &e)
356        {
357          throw new Ogre::Exception(42, e.eText, "OrxApplication::setupInputSystem");
358        }
359      }
360
361//       void setupCEGUI()
362//       {
363//         Ogre::SceneManager *mgr = mRoot->getSceneManager("Default SceneManager");
364//         Ogre::RenderWindow *win = mRoot->getAutoCreatedWindow();
365
366        // CEGUI setup
367//        mRenderer = new CEGUI::OgreCEGUIRenderer(win, Ogre::RENDER_QUEUE_OVERLAY, false, 3000, mgr);
368//        mSystem = new CEGUI::System(mRenderer);
369
370        // Other CEGUI setup here.
371//       }
372
373
374    void createFrameListener()
375    {
376      mListener = new OrxExitListener(mKeyboard, mMouse);
377      mRoot->addFrameListener(mListener);
378    }
379      void startRenderLoop()
380      {
381        mRoot->startRendering();
382      }
383  };
384}
385
386using namespace Ogre;
387
388#if OGRE_PLATFORM == PLATFORM_WIN32 || OGRE_PLATFORM == OGRE_PLATFORM_WIN32
389#define WIN32_LEAN_AND_MEAN
390#include "windows.h"
391
392             INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT)
393#else
394             int main(int argc, char **argv)
395#endif
396{
397  try
398  {
399    orxonox::OrxApplication orxonox;
400    orxonox.go();
401  }
402  catch(Exception& e)
403  {
404#if OGRE_PLATFORM == PLATFORM_WIN32 || OGRE_PLATFORM == OGRE_PLATFORM_WIN32
405    MessageBoxA(NULL, e.getFullDescription().c_str(), "An exception has occurred!", MB_OK | MB_ICONERROR | MB_TASKMODAL);
406#else
407    fprintf(stderr, "An exception has occurred: %s\n",
408            e.getFullDescription().c_str());
409#endif
410  }
411
412  return 0;
413}
414
Note: See TracBrowser for help on using the repository browser.