Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 385 was 323, checked in by nicolasc, 17 years ago

steering merge complete - still some fixing needed

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