Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 241 was 209, checked in by mbiber, 17 years ago

added steering class

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