Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/AI/src/orxonox.cc @ 941

Last change on this file since 941 was 495, checked in by motth, 17 years ago

fixed a memory leak, added documentation

File size: 14.4 KB
RevLine 
[74]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
[142]28/**
29 @file  orxonox.cc
30 @brief Orxonox Main File
31 */
32
[137]33#include <Ogre.h>
34#include <OIS/OIS.h>
35#include <CEGUI/CEGUI.h>
36#include <OgreCEGUIRenderer.h>
[426]37#include <OgreMath.h>
[74]38
[164]39#include <string>
40#include <iostream>
41
[465]42#include "SignalHandler.h"
[164]43#include "xml/xmlParser.h"
44#include "loader/LevelLoader.h"
[212]45#include "Flocking.h"
[495]46#include "Wander.h"
[164]47
[151]48// some tests to see if enet works without includsion
49//#include <enet/enet.h>
50//#include <enet/protocol.h>
[142]51
[137]52#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
53#include <CoreFoundation/CoreFoundation.h>
[74]54
[137]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()
[74]59{
[137]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
[212]81//my-stuff
82//globale definition eines Arrays welches alle nodes enthält
[426]83Vector3 ElementLocationArray[9];
84Vector3 ElementSpeedArray[9];
85Vector3 ElementAccelerationArray[9];
[212]86
[426]87Element arrayOfElements[9];
[495]88Wander walker;
89int counter = 0;
90int times = 0;
[212]91
92
[426]93
[137]94class OrxExitListener : public FrameListener
95{
[74]96  public:
[212]97    OrxExitListener(OIS::Keyboard *keyboard, Root* root)
[137]98  : mKeyboard(keyboard)
[74]99    {
[212]100        root_ = root;
[74]101    }
102
[137]103    bool frameStarted(const FrameEvent& evt)
[74]104    {
[212]105      moving(evt);
[137]106      mKeyboard->capture();
107      return !mKeyboard->isKeyDown(OIS::KC_ESCAPE);
108    }
[212]109 
110    //code the movments of the nodes here
111    void moving(const FrameEvent& evt) {
112      SceneManager *mgr = root_->getSceneManager("Default SceneManager");
[325]113
[495]114/*    // RUN WANDER
[325]115
[495]116      walker.update();
117      walker.speed = walker.speed + 10*walker.acceleration*evt.timeSinceLastFrame;
118      walker.location = walker.location + 10*walker.speed*evt.timeSinceLastFrame;
119      walker.acceleration  = (0,0,0);
120      mgr->getSceneNode("HeadNode10")->setPosition(walker.location);
[426]121
[495]122*/   // END RUN WANDER
[426]123
124
125
126
[495]127     //  RUN FLOCKING
128
129      arrayOfElements[8].location = 100*Vector3(Math::Cos(Math::DegreesToRadians(counter)/10),Math::Sin(Math::DegreesToRadians(counter)/10),Math::Cos(Math::DegreesToRadians(counter+(counter-180)/2)/10));
130
131      arrayOfElements[0].update(arrayOfElements);
132      arrayOfElements[1].update(arrayOfElements);
133      arrayOfElements[2].update(arrayOfElements);
134      arrayOfElements[3].update(arrayOfElements);
135      arrayOfElements[4].update(arrayOfElements);
136      arrayOfElements[5].update(arrayOfElements);
137      arrayOfElements[6].update(arrayOfElements);
138      arrayOfElements[7].update(arrayOfElements);
139      arrayOfElements[8].update(arrayOfElements);
140
141      for(int i=0; i<9; i++) {
142        arrayOfElements[i].speed = 0.995*arrayOfElements[i].speed + arrayOfElements[i].acceleration*evt.timeSinceLastFrame;
143        arrayOfElements[i].location = arrayOfElements[i].location + arrayOfElements[i].speed*evt.timeSinceLastFrame;
144        arrayOfElements[i].acceleration  = (0,0,0);
[426]145      }
146
[325]147      mgr->getSceneNode("HeadNode1")->setPosition(arrayOfElements[0].location);
148      mgr->getSceneNode("HeadNode2")->setPosition(arrayOfElements[1].location);
149      mgr->getSceneNode("HeadNode3")->setPosition(arrayOfElements[2].location);
[426]150      mgr->getSceneNode("HeadNode4")->setPosition(arrayOfElements[3].location);
151      mgr->getSceneNode("HeadNode5")->setPosition(arrayOfElements[4].location);
152      mgr->getSceneNode("HeadNode6")->setPosition(arrayOfElements[5].location);
153      mgr->getSceneNode("HeadNode7")->setPosition(arrayOfElements[6].location);
154      mgr->getSceneNode("HeadNode8")->setPosition(arrayOfElements[7].location);
155      mgr->getSceneNode("HeadNode9")->setPosition(arrayOfElements[8].location);
[325]156
[495]157      counter = counter + 1;
158      counter = counter%7200;
[325]159
[495]160      // END RUN FLOCKING
[325]161
[212]162    }
[135]163
[74]164  private:
[137]165    OIS::Keyboard *mKeyboard;
[212]166    Root* root_;
[74]167};
168
[137]169class OrxApplication
[74]170{
171  public:
[137]172    void go()
[74]173    {
[137]174      createRoot();
175      defineResources();
176      setupRenderSystem();
177      createRenderWindow();
178      initializeResourceGroups();
[164]179      createScene();
[137]180      setupScene();
181      setupInputSystem();
182      setupCEGUI();
183      createFrameListener();
184      startRenderLoop();
[74]185    }
186
[137]187    ~OrxApplication()
[74]188    {
[137]189      mInputManager->destroyInputObject(mKeyboard);
190      OIS::InputManager::destroyInputSystem(mInputManager);
191
[148]192//       delete mRenderer;
193//       delete mSystem;
[137]194
195      delete mListener;
196      delete mRoot;
[74]197    }
[137]198
199  private:
200    Root *mRoot;
201    OIS::Keyboard *mKeyboard;
202    OIS::Mouse *mMouse;
203    OIS::InputManager *mInputManager;
204    CEGUI::OgreCEGUIRenderer *mRenderer;
205    CEGUI::System *mSystem;
206    OrxExitListener *mListener;
207
208    void createRoot()
[74]209    {
[137]210#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
211      mRoot = new Root(macBundlePath() + "/Contents/Resources/plugins.cfg");
212#else
213      mRoot = new Root();
214#endif
[74]215    }
216
[137]217    void defineResources()
[74]218    {
[137]219      String secName, typeName, archName;
220      ConfigFile cf;
221#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
222      cf.load(macBundlePath() + "/Contents/Resources/resources.cfg");
223#else
224      cf.load("resources.cfg");
225#endif
[74]226
[137]227      ConfigFile::SectionIterator seci = cf.getSectionIterator();
228      while (seci.hasMoreElements())
229      {
230        secName = seci.peekNextKey();
231        ConfigFile::SettingsMultiMap *settings = seci.getNext();
232        ConfigFile::SettingsMultiMap::iterator i;
233        for (i = settings->begin(); i != settings->end(); ++i)
234        {
235          typeName = i->first;
236          archName = i->second;
237#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
238          ResourceGroupManager::getSingleton().addResourceLocation( String(macBundlePath() + "/" + archName), typeName, secName);
239#else
240          ResourceGroupManager::getSingleton().addResourceLocation( archName, typeName, secName);
241#endif
242        }
243      }
244    }
[74]245
[137]246    void setupRenderSystem()
247    {
248      if (!mRoot->restoreConfig() && !mRoot->showConfigDialog())
249        throw Exception(52, "User canceled the config dialog!", "OrxApplication::setupRenderSystem()");
250    }
[74]251
[137]252    void createRenderWindow()
253    {
254      mRoot->initialise(true, "Ogre Render Window");
255    }
[74]256
[137]257    void initializeResourceGroups()
258    {
259      TextureManager::getSingleton().setDefaultNumMipmaps(5);
260      ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
261    }
[74]262
[164]263    void createScene(void)
264    {
265
266      string levelFile = "sp_level_moonstation.oxw";
267      loader::LevelLoader* loader = new loader::LevelLoader(levelFile);
268    }
269   
[137]270    void setupScene()
271    {
272      SceneManager *mgr = mRoot->createSceneManager(ST_GENERIC, "Default SceneManager");
273      Camera *cam = mgr->createCamera("Camera");
[233]274      cam->setPosition(Vector3(0,0,1000));
[212]275      cam->lookAt(Vector3(0,0,0));
[137]276      Viewport *vp = mRoot->getAutoCreatedWindow()->addViewport(cam);
[495]277
278      //Invoke example to test AI
279      example();
[137]280    }
[74]281
[137]282    void setupInputSystem()
283    {
284      size_t windowHnd = 0;
285      std::ostringstream windowHndStr;
286      OIS::ParamList pl;
287      RenderWindow *win = mRoot->getAutoCreatedWindow();
[74]288
[137]289      win->getCustomAttribute("WINDOW", &windowHnd);
290      windowHndStr << windowHnd;
291      pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str()));
292      mInputManager = OIS::InputManager::createInputSystem(pl);
[74]293
[137]294      try
295      {
296        mKeyboard = static_cast<OIS::Keyboard*>(mInputManager->createInputObject(OIS::OISKeyboard, false));
297        mMouse = static_cast<OIS::Mouse*>(mInputManager->createInputObject(OIS::OISMouse, false));
298      }
299      catch (const OIS::Exception &e)
300      {
301        throw new Exception(42, e.eText, "OrxApplication::setupInputSystem");
302      }
[74]303    }
304
[137]305    void setupCEGUI()
[74]306    {
[137]307      SceneManager *mgr = mRoot->getSceneManager("Default SceneManager");
308      RenderWindow *win = mRoot->getAutoCreatedWindow();
309
310      // CEGUI setup
311//       mRenderer = new CEGUI::OgreCEGUIRenderer(win, Ogre::RENDER_QUEUE_OVERLAY, false, 3000, mgr);
312//       mSystem = new CEGUI::System(mRenderer);
313
314      // Other CEGUI setup here.
[74]315    }
[137]316
317    void createFrameListener()
318    {
[212]319      mListener = new OrxExitListener(mKeyboard, mRoot);
[137]320      mRoot->addFrameListener(mListener);
321    }
322
323    void startRenderLoop()
324    {
325      mRoot->startRendering();
326    }
[212]327
328    void example() {
329    SceneManager *mgr = mRoot->getSceneManager("Default SceneManager");
330    mgr->setAmbientLight(ColourValue(1.0,1.0,1.0));
[468]331
[495]332/*  //TEST DATA WANDER
333
334    Entity* ent10 = mgr->createEntity("Head10", "ogrehead.mesh");
335    SceneNode *node10 = mgr->getRootSceneNode()->createChildSceneNode("HeadNode10", Vector3(0,0,0));
336    node10->attachObject(ent10);
337    Vector3 temp;
338    temp = (0,0,0);
339    walker.setValues(node10->getPosition(),temp,temp,true);
340
341*/  //END TEST DATA WANDER
342
343
344//   TEST DATA FLOCKING
345
[212]346    Entity* ent1 = mgr->createEntity("Head1", "ogrehead.mesh");
347    Entity* ent2 = mgr->createEntity("Head2", "ogrehead.mesh");
348    Entity* ent3 = mgr->createEntity("Head3", "ogrehead.mesh");
[426]349    Entity* ent4 = mgr->createEntity("Head4", "ogrehead.mesh");
350    Entity* ent5 = mgr->createEntity("Head5", "ogrehead.mesh");
351    Entity* ent6 = mgr->createEntity("Head6", "ogrehead.mesh");
352    Entity* ent7 = mgr->createEntity("Head7", "ogrehead.mesh");
353    Entity* ent8 = mgr->createEntity("Head8", "ogrehead.mesh");
354    Entity* ent9 = mgr->createEntity("Head9", "ogrehead.mesh");
[468]355
[426]356    SceneNode *node1 = mgr->getRootSceneNode()->createChildSceneNode("HeadNode1", Vector3(100,300,100));
357    SceneNode *node2 = mgr->getRootSceneNode()->createChildSceneNode("HeadNode2", Vector3(300,0,200));
358    SceneNode *node3 = mgr->getRootSceneNode()->createChildSceneNode("HeadNode3", Vector3(-300,0,-100));
359    SceneNode *node4 = mgr->getRootSceneNode()->createChildSceneNode("HeadNode4", Vector3(-100,-300,150));
360    SceneNode *node5 = mgr->getRootSceneNode()->createChildSceneNode("HeadNode5", Vector3(150,150,-100));
361    SceneNode *node6 = mgr->getRootSceneNode()->createChildSceneNode("HeadNode6", Vector3(150,-150,-100));
362    SceneNode *node7 = mgr->getRootSceneNode()->createChildSceneNode("HeadNode7", Vector3(-150,-150,0));
363    SceneNode *node8 = mgr->getRootSceneNode()->createChildSceneNode("HeadNode8", Vector3(-150,150,0));
[495]364    SceneNode *node9 = mgr->getRootSceneNode()->createChildSceneNode("HeadNode9", Vector3(0,0,0));
[426]365
[212]366    node1->attachObject(ent1);
367    node2->attachObject(ent2);
368    node3->attachObject(ent3);
[426]369    node4->attachObject(ent4);
370    node5->attachObject(ent5);
371    node6->attachObject(ent6);
372    node7->attachObject(ent7);
373    node8->attachObject(ent8);
374    node9->attachObject(ent9);
[468]375
[212]376    ElementLocationArray[0] = node1->getPosition();
377    ElementLocationArray[1] = node2->getPosition();
378    ElementLocationArray[2] = node3->getPosition();
[426]379    ElementLocationArray[3] = node4->getPosition();
380    ElementLocationArray[4] = node5->getPosition();
381    ElementLocationArray[5] = node6->getPosition();
382    ElementLocationArray[6] = node7->getPosition();
383    ElementLocationArray[7] = node8->getPosition();
384    ElementLocationArray[8] = node9->getPosition();
[495]385
[212]386    ElementSpeedArray[0] = (0,0,0);
387    ElementSpeedArray[1] = (0,0,0);
388    ElementSpeedArray[2] = (0,0,0);
[426]389    ElementSpeedArray[3] = (0,0,0);
390    ElementSpeedArray[4] = (0,0,0);
391    ElementSpeedArray[5] = (0,0,0);
392    ElementSpeedArray[6] = (0,0,0);
393    ElementSpeedArray[7] = (0,0,0);
394    ElementSpeedArray[8] = (0,0,0);
[495]395
[212]396    ElementAccelerationArray[0] = (0,0,0);
397    ElementAccelerationArray[1] = (0,0,0);
398    ElementAccelerationArray[2] = (0,0,0);
[426]399    ElementAccelerationArray[3] = (0,0,0);
400    ElementAccelerationArray[4] = (0,0,0);
401    ElementAccelerationArray[5] = (0,0,0);
402    ElementAccelerationArray[6] = (0,0,0);
403    ElementAccelerationArray[7] = (0,0,0);
404    ElementAccelerationArray[8] = (0,0,0);
[495]405
[426]406    arrayOfElements[0].setValues( ElementLocationArray[0], ElementSpeedArray[0], ElementAccelerationArray[0], true);
407    arrayOfElements[1].setValues( ElementLocationArray[1], ElementSpeedArray[1], ElementAccelerationArray[1], true);
408    arrayOfElements[2].setValues( ElementLocationArray[2], ElementSpeedArray[2], ElementAccelerationArray[2], true);
409    arrayOfElements[3].setValues( ElementLocationArray[3], ElementSpeedArray[3], ElementAccelerationArray[3], true);
410    arrayOfElements[4].setValues( ElementLocationArray[4], ElementSpeedArray[4], ElementAccelerationArray[4], true);
411    arrayOfElements[5].setValues( ElementLocationArray[5], ElementSpeedArray[5], ElementAccelerationArray[5], true);
412    arrayOfElements[6].setValues( ElementLocationArray[6], ElementSpeedArray[6], ElementAccelerationArray[6], true);
413    arrayOfElements[7].setValues( ElementLocationArray[7], ElementSpeedArray[7], ElementAccelerationArray[7], true);
414    arrayOfElements[8].setValues( ElementLocationArray[8], ElementSpeedArray[8], ElementAccelerationArray[8], false);
[233]415
[495]416   // END TEST DATA FLOCKING
[233]417
418
[212]419    }
[74]420};
421
[212]422
423
[137]424#if OGRE_PLATFORM == PLATFORM_WIN32 || OGRE_PLATFORM == OGRE_PLATFORM_WIN32
[74]425#define WIN32_LEAN_AND_MEAN
426#include "windows.h"
427
[137]428             INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT)
[74]429#else
[137]430             int main(int argc, char **argv)
[74]431#endif
432{
[137]433  try
434  {
[464]435    SignalHandler::getInstance()->doCatch(argv[0], "orxonox.log");
[137]436    OrxApplication orxonox;
[74]437    orxonox.go();
[137]438  }
439  catch(Exception& e)
440  {
441#if OGRE_PLATFORM == PLATFORM_WIN32 || OGRE_PLATFORM == OGRE_PLATFORM_WIN32
442    MessageBoxA(NULL, e.getFullDescription().c_str(), "An exception has occurred!", MB_OK | MB_ICONERROR | MB_TASKMODAL);
[74]443#else
444    fprintf(stderr, "An exception has occurred: %s\n",
445            e.getFullDescription().c_str());
446#endif
447  }
448
449  return 0;
450}
[137]451
Note: See TracBrowser for help on using the repository browser.