Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 379 was 379, checked in by rgrieder, 17 years ago
  • converted loader library to be compilable as a DLL
    • added loader_platform.h and loader_prereq.h to do that
File size: 11.6 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
54#include "spaceship_steering.h"
55SpaceshipSteering* steering;
56
57
58// some tests to see if enet works without includsion
59//#include <enet/enet.h>
60//#include <enet/protocol.h>
61
62#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
63#include <CoreFoundation/CoreFoundation.h>
64
65// This function will locate the path to our application on OS X,
66// unlike windows you can not rely on the curent working directory
67// for locating your configuration files and resources.
68std::string macBundlePath()
69{
70  char path[1024];
71  CFBundleRef mainBundle = CFBundleGetMainBundle();
72  assert(mainBundle);
73
74  CFURLRef mainBundleURL = CFBundleCopyBundleURL(mainBundle);
75  assert(mainBundleURL);
76
77  CFStringRef cfStringRef = CFURLCopyFileSystemPath( mainBundleURL, kCFURLPOSIXPathStyle);
78  assert(cfStringRef);
79
80  CFStringGetCString(cfStringRef, path, 1024, kCFStringEncodingASCII);
81
82  CFRelease(mainBundleURL);
83  CFRelease(cfStringRef);
84
85  return std::string(path);
86}
87#endif
88
89namespace orxonox
90{
91
92using namespace Ogre;
93
94class OrxExitListener : public FrameListener, public OIS::MouseListener
95{
96  public:
97    OrxExitListener(OIS::Keyboard *keyboard, OIS::Mouse *mouse)
98  : mKeyboard(keyboard), mMouse(mouse)
99    {
100      speed = 250;
101      loop = 100;
102      rotate = 10;
103      mouseX = 0;
104      mouseY = 0;
105      maxMouseX = 0;
106      minMouseX = 0;
107      moved = false;
108      steering->brakeRotate(rotate*10);
109      steering->brakeLoop(loop);
110      mMouse->setEventCallback(this);
111    }
112    bool frameStarted(const FrameEvent& evt)
113    {
114      mKeyboard->capture();
115      mMouse->capture();
116      if (mKeyboard->isKeyDown(OIS::KC_UP) || mKeyboard->isKeyDown(OIS::KC_W))
117        steering->moveForward(speed);
118      else
119        steering->moveForward(0);
120      if(mKeyboard->isKeyDown(OIS::KC_DOWN) || mKeyboard->isKeyDown(OIS::KC_S))
121        steering->brakeForward(speed);
122      else
123        steering->brakeForward(speed/10);
124      if (mKeyboard->isKeyDown(OIS::KC_RIGHT) || mKeyboard->isKeyDown(OIS::KC_D))
125        steering->loopRight(loop);
126      else
127        steering->loopRight(0);
128      if (mKeyboard->isKeyDown(OIS::KC_LEFT) || mKeyboard->isKeyDown(OIS::KC_A))
129        steering->loopLeft(loop);
130      else
131        steering->loopLeft(0);
132
133      if(moved) {
134        if (mouseY<0)
135          steering->rotateUp(-mouseY*rotate);
136        if (mouseY>0)
137          steering->rotateDown(mouseY*rotate);
138        if (mouseX>0)
139          steering->rotateRight(mouseX*rotate);
140        if (mouseX<0)
141          steering->rotateLeft(-mouseX*rotate);
142        moved = false;
143      }
144      else {
145        steering->rotateUp(0);
146        steering->rotateDown(0);
147        steering->rotateRight(0);
148        steering->rotateLeft(0);
149      }
150
151      steering->tick(evt.timeSinceLastFrame);
152//      scenemanager->spacehip->tick(evt.timesincelastframe);
153      if(mKeyboard->isKeyDown(OIS::KC_ESCAPE))
154        cout << "maximal MouseX: " << maxMouseX << "\tminMouseX: " << minMouseX << endl;
155      return !mKeyboard->isKeyDown(OIS::KC_ESCAPE);
156    }
157
158    bool mouseMoved(const OIS::MouseEvent &e)
159    {
160      mouseX = e.state.X.rel;
161      mouseY = e.state.Y.rel;
162      if(mouseX>maxMouseX) maxMouseX = mouseX;
163      if(mouseX<minMouseX) minMouseX = mouseX;
164      cout << "mouseX: " << mouseX << "\tmouseY: " << mouseY << endl;
165      moved = true;
166      return true;
167    }
168
169    bool mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id) { return true; }
170    bool mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id) { return true; }
171
172  private:
173    float speed;
174    float rotate;
175    float loop;
176    float mouseY;
177    float mouseX;
178    float maxMouseX;
179    float minMouseX;
180    bool moved;
181    OIS::Keyboard *mKeyboard;
182    OIS::Mouse *mMouse;
183};
184
185
186  class OrxApplication
187  {
188    public:
189      void go()
190      {
191        createRoot();
192        defineResources();
193        setupRenderSystem();
194        createRenderWindow();
195        initializeResourceGroups();
196        createScene();
197        setupScene();
198        setupInputSystem();
199        setupCEGUI();
200        createFrameListener();
201        startRenderLoop();
202      }
203
204      ~OrxApplication()
205      {
206        mInputManager->destroyInputObject(mKeyboard);
207        OIS::InputManager::destroyInputSystem(mInputManager);
208
209//        delete mRenderer;
210//        delete mSystem;
211
212        delete mListener;
213        delete mRoot;
214      }
215
216    private:
217      Ogre::Root *mRoot;
218      OIS::Keyboard *mKeyboard;
219      OIS::Mouse *mMouse;
220      OIS::InputManager *mInputManager;
221      //CEGUI::OgreCEGUIRenderer *mRenderer;
222      //CEGUI::System *mSystem;
223      OrxExitListener *mListener;
224
225      void createRoot()
226      {
227#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
228        mRoot = new Ogre::Root(macBundlePath() + "/Contents/Resources/plugins.cfg");
229#else
230        mRoot = new Ogre::Root();
231#endif
232      }
233
234      void defineResources()
235      {
236        Ogre::String secName, typeName, archName;
237        Ogre::ConfigFile cf;
238#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
239        cf.load(macBundlePath() + "/Contents/Resources/resources.cfg");
240#else
241        cf.load("resources.cfg");
242#endif
243
244        Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator();
245        while (seci.hasMoreElements())
246        {
247          secName = seci.peekNextKey();
248          Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext();
249          Ogre::ConfigFile::SettingsMultiMap::iterator i;
250          for (i = settings->begin(); i != settings->end(); ++i)
251          {
252            typeName = i->first;
253            archName = i->second;
254#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
255            Ogre::ResourceGroupManager::getSingleton().addResourceLocation( String(macBundlePath() + "/" + archName), typeName, secName);
256#else
257            Ogre::ResourceGroupManager::getSingleton().addResourceLocation( archName, typeName, secName);
258#endif
259          }
260        }
261      }
262
263      void setupRenderSystem()
264      {
265        if (!mRoot->restoreConfig() && !mRoot->showConfigDialog())
266          throw Exception(52, "User canceled the config dialog!", "OrxApplication::setupRenderSystem()");
267      }
268
269      void createRenderWindow()
270      {
271        mRoot->initialise(true, "OrxonoxV2");
272      }
273
274      void initializeResourceGroups()
275      {
276        TextureManager::getSingleton().setDefaultNumMipmaps(5);
277        ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
278      }
279
280      void createScene(void)
281      {
282
283      string levelFile = "sp_level_moonstation.oxw";
284      loader::LevelLoader* loader = new loader::LevelLoader(levelFile);
285    }
286
287    void setupScene()
288    {
289      SceneManager *mgr = mRoot->createSceneManager(ST_GENERIC, "Default SceneManager");
290      Camera *cam = mgr->createCamera("Camera");
291      cam->setPosition(Vector3(0,0,-250));
292      cam->lookAt(Vector3(0,0,0));
293      Viewport *vp = mRoot->getAutoCreatedWindow()->addViewport(cam);
294
295
296      mgr->setAmbientLight(ColourValue(1,1,1));
297      Entity* head = mgr->createEntity("head", "ogrehead.mesh");
298      SceneNode *node = mgr->getRootSceneNode()->createChildSceneNode("OgreHeadNode", Vector3(0,0,0));
299      node->attachObject(head);
300      node->attachObject(cam);
301      mgr->setSkyBox(true, "Examples/SceneSkyBox2");
302
303      Entity* head1 = mgr->createEntity("head1", "ogrehead.mesh");
304      SceneNode *node1 = mgr->getRootSceneNode()->createChildSceneNode("OgreHeadNode1", Vector3(200,0,0));
305      node1->attachObject(head1);
306      Entity* head2 = mgr->createEntity("head2", "ogrehead.mesh");
307      SceneNode *node2 = mgr->getRootSceneNode()->createChildSceneNode("OgreHeadNode2", Vector3(200,400,-100));
308      node2->attachObject(head2);
309      Entity* head3 = mgr->createEntity("head3", "ogrehead.mesh");
310      SceneNode *node3 = mgr->getRootSceneNode()->createChildSceneNode("OgreHeadNode3", Vector3(0,400,200));
311      node3->attachObject(head3);
312      Entity* head4 = mgr->createEntity("head4", "ogrehead.mesh");
313      SceneNode *node4 = mgr->getRootSceneNode()->createChildSceneNode("OgreHeadNode4", Vector3(-400,-200,600));
314      node4->attachObject(head4);
315      Entity* head5 = mgr->createEntity("head5", "ogrehead.mesh");
316      SceneNode *node5 = mgr->getRootSceneNode()->createChildSceneNode("OgreHeadNode5", Vector3(0,0,-400));
317      node5->attachObject(head5);
318
319      steering = new SpaceshipSteering(500, 200, 200, 200);
320      steering->addNode(node);
321
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, true));
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.