Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 225 was 212, checked in by motth, 18 years ago

Headerfile which contains the flocking algorithm

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