Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Headerfile which contains the flocking algorithm

File size: 9.0 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#include "Flocking.h"
44
45// some tests to see if enet works without includsion
46//#include <enet/enet.h>
47//#include <enet/protocol.h>
48
49#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
50#include <CoreFoundation/CoreFoundation.h>
51
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()
56{
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
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
86class OrxExitListener : public FrameListener
87{
88  public:
89    OrxExitListener(OIS::Keyboard *keyboard, Root* root)
90  : mKeyboard(keyboard)
91    {
92        root_ = root;
93    }
94
95    bool frameStarted(const FrameEvent& evt)
96    {
97      moving(evt);
98      mKeyboard->capture();
99      return !mKeyboard->isKeyDown(OIS::KC_ESCAPE);
100    }
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    }
107
108  private:
109    OIS::Keyboard *mKeyboard;
110    Root* root_;
111};
112
113class OrxApplication
114{
115  public:
116    void go()
117    {
118      createRoot();
119      defineResources();
120      setupRenderSystem();
121      createRenderWindow();
122      initializeResourceGroups();
123      createScene();
124      setupScene();
125      setupInputSystem();
126      setupCEGUI();
127      createFrameListener();
128      startRenderLoop();
129    }
130
131    ~OrxApplication()
132    {
133      mInputManager->destroyInputObject(mKeyboard);
134      OIS::InputManager::destroyInputSystem(mInputManager);
135
136//       delete mRenderer;
137//       delete mSystem;
138
139      delete mListener;
140      delete mRoot;
141    }
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()
153    {
154#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
155      mRoot = new Root(macBundlePath() + "/Contents/Resources/plugins.cfg");
156#else
157      mRoot = new Root();
158#endif
159    }
160
161    void defineResources()
162    {
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
170
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    }
189
190    void setupRenderSystem()
191    {
192      if (!mRoot->restoreConfig() && !mRoot->showConfigDialog())
193        throw Exception(52, "User canceled the config dialog!", "OrxApplication::setupRenderSystem()");
194    }
195
196    void createRenderWindow()
197    {
198      mRoot->initialise(true, "Ogre Render Window");
199    }
200
201    void initializeResourceGroups()
202    {
203      TextureManager::getSingleton().setDefaultNumMipmaps(5);
204      ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
205    }
206
207    void createScene(void)
208    {
209
210      string levelFile = "sp_level_moonstation.oxw";
211      loader::LevelLoader* loader = new loader::LevelLoader(levelFile);
212    }
213   
214    void setupScene()
215    {
216      SceneManager *mgr = mRoot->createSceneManager(ST_GENERIC, "Default SceneManager");
217      Camera *cam = mgr->createCamera("Camera");
218      cam->setPosition(Vector3(0,0,500));
219      cam->lookAt(Vector3(0,0,0));
220      Viewport *vp = mRoot->getAutoCreatedWindow()->addViewport(cam);
221      example();  //my stuff
222    }
223
224    void setupInputSystem()
225    {
226      size_t windowHnd = 0;
227      std::ostringstream windowHndStr;
228      OIS::ParamList pl;
229      RenderWindow *win = mRoot->getAutoCreatedWindow();
230
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);
235
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      }
245    }
246
247    void setupCEGUI()
248    {
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.
257    }
258
259    void createFrameListener()
260    {
261      mListener = new OrxExitListener(mKeyboard, mRoot);
262      mRoot->addFrameListener(mListener);
263    }
264
265    void startRenderLoop()
266    {
267      mRoot->startRendering();
268    }
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    }
296};
297
298
299
300#if OGRE_PLATFORM == PLATFORM_WIN32 || OGRE_PLATFORM == OGRE_PLATFORM_WIN32
301#define WIN32_LEAN_AND_MEAN
302#include "windows.h"
303
304             INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT)
305#else
306             int main(int argc, char **argv)
307#endif
308{
309  try
310  {
311    OrxApplication orxonox;
312    orxonox.go();
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);
318#else
319    fprintf(stderr, "An exception has occurred: %s\n",
320            e.getFullDescription().c_str());
321#endif
322  }
323
324  return 0;
325}
326
Note: See TracBrowser for help on using the repository browser.