Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/merger/src/orxonox.cc @ 320

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

dont ask: bump

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