Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/steering/src/orxonox.cc @ 353

Last change on this file since 353 was 317, checked in by mbiber, 17 years ago

update steering

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