Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 468 was 468, checked in by landauf, 17 years ago

changed resources.cfg

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