Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 326 was 325, checked in by motth, 17 years ago

added Flocking

File size: 9.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>
[74]37
[164]38#include <string>
39#include <iostream>
40
41#include "xml/xmlParser.h"
42#include "loader/LevelLoader.h"
[212]43#include "Flocking.h"
[164]44
[151]45// some tests to see if enet works without includsion
46//#include <enet/enet.h>
47//#include <enet/protocol.h>
[142]48
[137]49#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
50#include <CoreFoundation/CoreFoundation.h>
[74]51
[137]52// This function will locate the path to our application on OS X,
53// unlike windows you can not rely on the curent working directory
54// for locating your configuration files and resources.
55std::string macBundlePath()
[74]56{
[137]57  char path[1024];
58  CFBundleRef mainBundle = CFBundleGetMainBundle();
59  assert(mainBundle);
60
61  CFURLRef mainBundleURL = CFBundleCopyBundleURL(mainBundle);
62  assert(mainBundleURL);
63
64  CFStringRef cfStringRef = CFURLCopyFileSystemPath( mainBundleURL, kCFURLPOSIXPathStyle);
65  assert(cfStringRef);
66
67  CFStringGetCString(cfStringRef, path, 1024, kCFStringEncodingASCII);
68
69  CFRelease(mainBundleURL);
70  CFRelease(cfStringRef);
71
72  return std::string(path);
73}
74#endif
75
76using namespace Ogre;
77
[212]78//my-stuff
79//globale definition eines Arrays welches alle nodes enthält
[325]80Vector3 ElementLocationArray[3];
81Vector3 ElementSpeedArray[3];
82Vector3 ElementAccelerationArray[3];
[212]83
[325]84Element arrayOfElements[3];
[212]85
86
[137]87class OrxExitListener : public FrameListener
88{
[74]89  public:
[212]90    OrxExitListener(OIS::Keyboard *keyboard, Root* root)
[137]91  : mKeyboard(keyboard)
[74]92    {
[212]93        root_ = root;
[74]94    }
95
[137]96    bool frameStarted(const FrameEvent& evt)
[74]97    {
[212]98      moving(evt);
[137]99      mKeyboard->capture();
100      return !mKeyboard->isKeyDown(OIS::KC_ESCAPE);
101    }
[212]102 
103    //code the movments of the nodes here
104    void moving(const FrameEvent& evt) {
105      SceneManager *mgr = root_->getSceneManager("Default SceneManager");
[325]106
107
108
109      arrayOfElements[0].update(arrayOfElements, evt);
110      arrayOfElements[1].update(arrayOfElements, evt);
111      arrayOfElements[2].update(arrayOfElements, evt);
112
113      mgr->getSceneNode("HeadNode1")->setPosition(arrayOfElements[0].location);
114      mgr->getSceneNode("HeadNode2")->setPosition(arrayOfElements[1].location);
115      mgr->getSceneNode("HeadNode3")->setPosition(arrayOfElements[2].location);
116
117
118
119    //  mgr->getSceneNode("HeadNode1")->yaw((Radian)10*evt.timeSinceLastFrame);
[212]120    }
[135]121
[74]122  private:
[137]123    OIS::Keyboard *mKeyboard;
[212]124    Root* root_;
[74]125};
126
[137]127class OrxApplication
[74]128{
129  public:
[137]130    void go()
[74]131    {
[137]132      createRoot();
133      defineResources();
134      setupRenderSystem();
135      createRenderWindow();
136      initializeResourceGroups();
[164]137      createScene();
[137]138      setupScene();
139      setupInputSystem();
140      setupCEGUI();
141      createFrameListener();
142      startRenderLoop();
[74]143    }
144
[137]145    ~OrxApplication()
[74]146    {
[137]147      mInputManager->destroyInputObject(mKeyboard);
148      OIS::InputManager::destroyInputSystem(mInputManager);
149
[148]150//       delete mRenderer;
151//       delete mSystem;
[137]152
153      delete mListener;
154      delete mRoot;
[74]155    }
[137]156
157  private:
158    Root *mRoot;
159    OIS::Keyboard *mKeyboard;
160    OIS::Mouse *mMouse;
161    OIS::InputManager *mInputManager;
162    CEGUI::OgreCEGUIRenderer *mRenderer;
163    CEGUI::System *mSystem;
164    OrxExitListener *mListener;
165
166    void createRoot()
[74]167    {
[137]168#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
169      mRoot = new Root(macBundlePath() + "/Contents/Resources/plugins.cfg");
170#else
171      mRoot = new Root();
172#endif
[74]173    }
174
[137]175    void defineResources()
[74]176    {
[137]177      String secName, typeName, archName;
178      ConfigFile cf;
179#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
180      cf.load(macBundlePath() + "/Contents/Resources/resources.cfg");
181#else
182      cf.load("resources.cfg");
183#endif
[74]184
[137]185      ConfigFile::SectionIterator seci = cf.getSectionIterator();
186      while (seci.hasMoreElements())
187      {
188        secName = seci.peekNextKey();
189        ConfigFile::SettingsMultiMap *settings = seci.getNext();
190        ConfigFile::SettingsMultiMap::iterator i;
191        for (i = settings->begin(); i != settings->end(); ++i)
192        {
193          typeName = i->first;
194          archName = i->second;
195#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
196          ResourceGroupManager::getSingleton().addResourceLocation( String(macBundlePath() + "/" + archName), typeName, secName);
197#else
198          ResourceGroupManager::getSingleton().addResourceLocation( archName, typeName, secName);
199#endif
200        }
201      }
202    }
[74]203
[137]204    void setupRenderSystem()
205    {
206      if (!mRoot->restoreConfig() && !mRoot->showConfigDialog())
207        throw Exception(52, "User canceled the config dialog!", "OrxApplication::setupRenderSystem()");
208    }
[74]209
[137]210    void createRenderWindow()
211    {
212      mRoot->initialise(true, "Ogre Render Window");
213    }
[74]214
[137]215    void initializeResourceGroups()
216    {
217      TextureManager::getSingleton().setDefaultNumMipmaps(5);
218      ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
219    }
[74]220
[164]221    void createScene(void)
222    {
223
224      string levelFile = "sp_level_moonstation.oxw";
225      loader::LevelLoader* loader = new loader::LevelLoader(levelFile);
226    }
227   
[137]228    void setupScene()
229    {
230      SceneManager *mgr = mRoot->createSceneManager(ST_GENERIC, "Default SceneManager");
231      Camera *cam = mgr->createCamera("Camera");
[233]232      cam->setPosition(Vector3(0,0,1000));
[212]233      cam->lookAt(Vector3(0,0,0));
[137]234      Viewport *vp = mRoot->getAutoCreatedWindow()->addViewport(cam);
[212]235      example();  //my stuff
[137]236    }
[74]237
[137]238    void setupInputSystem()
239    {
240      size_t windowHnd = 0;
241      std::ostringstream windowHndStr;
242      OIS::ParamList pl;
243      RenderWindow *win = mRoot->getAutoCreatedWindow();
[74]244
[137]245      win->getCustomAttribute("WINDOW", &windowHnd);
246      windowHndStr << windowHnd;
247      pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str()));
248      mInputManager = OIS::InputManager::createInputSystem(pl);
[74]249
[137]250      try
251      {
252        mKeyboard = static_cast<OIS::Keyboard*>(mInputManager->createInputObject(OIS::OISKeyboard, false));
253        mMouse = static_cast<OIS::Mouse*>(mInputManager->createInputObject(OIS::OISMouse, false));
254      }
255      catch (const OIS::Exception &e)
256      {
257        throw new Exception(42, e.eText, "OrxApplication::setupInputSystem");
258      }
[74]259    }
260
[137]261    void setupCEGUI()
[74]262    {
[137]263      SceneManager *mgr = mRoot->getSceneManager("Default SceneManager");
264      RenderWindow *win = mRoot->getAutoCreatedWindow();
265
266      // CEGUI setup
267//       mRenderer = new CEGUI::OgreCEGUIRenderer(win, Ogre::RENDER_QUEUE_OVERLAY, false, 3000, mgr);
268//       mSystem = new CEGUI::System(mRenderer);
269
270      // Other CEGUI setup here.
[74]271    }
[137]272
273    void createFrameListener()
274    {
[212]275      mListener = new OrxExitListener(mKeyboard, mRoot);
[137]276      mRoot->addFrameListener(mListener);
277    }
278
279    void startRenderLoop()
280    {
281      mRoot->startRendering();
282    }
[212]283
284    //declaration of the 3 Ogreheads
[233]285   //muss leider global sein.....
286    //Element* arrayOfElements[2];
287
[212]288    void example() {
289    SceneManager *mgr = mRoot->getSceneManager("Default SceneManager");
290    mgr->setAmbientLight(ColourValue(1.0,1.0,1.0));
291    Entity* ent1 = mgr->createEntity("Head1", "ogrehead.mesh");
292    Entity* ent2 = mgr->createEntity("Head2", "ogrehead.mesh");
293    Entity* ent3 = mgr->createEntity("Head3", "ogrehead.mesh");
[233]294    SceneNode *node1 = mgr->getRootSceneNode()->createChildSceneNode("HeadNode1", Vector3(0,100,0));
[212]295    SceneNode *node2 = mgr->getRootSceneNode()->createChildSceneNode("HeadNode2", Vector3(100,0,0));
296    SceneNode *node3 = mgr->getRootSceneNode()->createChildSceneNode("HeadNode3", Vector3(-100,0,0));
297    node1->attachObject(ent1);
298    node2->attachObject(ent2);
299    node3->attachObject(ent3);
300    ElementLocationArray[0] = node1->getPosition();
301    ElementLocationArray[1] = node2->getPosition();
302    ElementLocationArray[2] = node3->getPosition();
303    ElementSpeedArray[0] = (0,0,0);
304    ElementSpeedArray[1] = (0,0,0);
305    ElementSpeedArray[2] = (0,0,0);
306    ElementAccelerationArray[0] = (0,0,0);
307    ElementAccelerationArray[1] = (0,0,0);
308    ElementAccelerationArray[2] = (0,0,0);
[325]309    arrayOfElements[0].setValues( ElementLocationArray[0], ElementSpeedArray[0], ElementAccelerationArray[0] );
310    arrayOfElements[1].setValues( ElementLocationArray[1], ElementSpeedArray[1], ElementAccelerationArray[1] );
311    arrayOfElements[2].setValues( ElementLocationArray[2], ElementSpeedArray[2], ElementAccelerationArray[2] );
[233]312
313
314
315
316   /* for (int i=0; i<3; i++) {
317      Element* arrayOfElements[i] = new Element( ElementLocationArray[i], ElementSpeedArray[i], ElementAccelerationArray[i] );
318    } */
319   /* for (int i=0; i<3; i++) {
320    arrayOfElements[i]->update(arrayOfElements);
321    }  */
[212]322    }
[74]323};
324
[212]325
326
[137]327#if OGRE_PLATFORM == PLATFORM_WIN32 || OGRE_PLATFORM == OGRE_PLATFORM_WIN32
[74]328#define WIN32_LEAN_AND_MEAN
329#include "windows.h"
330
[137]331             INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT)
[74]332#else
[137]333             int main(int argc, char **argv)
[74]334#endif
335{
[137]336  try
337  {
338    OrxApplication orxonox;
[74]339    orxonox.go();
[137]340  }
341  catch(Exception& e)
342  {
343#if OGRE_PLATFORM == PLATFORM_WIN32 || OGRE_PLATFORM == OGRE_PLATFORM_WIN32
344    MessageBoxA(NULL, e.getFullDescription().c_str(), "An exception has occurred!", MB_OK | MB_ICONERROR | MB_TASKMODAL);
[74]345#else
346    fprintf(stderr, "An exception has occurred: %s\n",
347            e.getFullDescription().c_str());
348#endif
349  }
350
351  return 0;
352}
[137]353
Note: See TracBrowser for help on using the repository browser.