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 |
---|
8 | * modify it under the terms of the GNU General Public License |
---|
9 | * as published by the Free Software Foundation; either version 2 |
---|
10 | * of the License, or (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, write to the Free Software |
---|
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
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 Class |
---|
31 | */ |
---|
32 | |
---|
33 | #include "orxonox.h" |
---|
34 | #include "graphicsEngine.h" |
---|
35 | |
---|
36 | //#include <Ogre.h> |
---|
37 | // 40% speed up: (instead of Ogre.h) |
---|
38 | #include <OgreSceneNode.h> |
---|
39 | #include <OgreSceneManager.h> |
---|
40 | #include <OgreRoot.h> |
---|
41 | #include <OgreFrameListener.h> |
---|
42 | #include <OgreConfigFile.h> |
---|
43 | #include <OgreTextureManager.h> |
---|
44 | #include <OgreEntity.h> |
---|
45 | #include <OgreRenderWindow.h> |
---|
46 | |
---|
47 | #include <OIS/OIS.h> |
---|
48 | //#include <CEGUI/CEGUI.h> |
---|
49 | //#include <OgreCEGUIRenderer.h> |
---|
50 | |
---|
51 | #include <string> |
---|
52 | #include <iostream> |
---|
53 | |
---|
54 | #include "objects/Tickable.h" |
---|
55 | #include "objects/Timer.h" |
---|
56 | #include "core/Factory.h" |
---|
57 | |
---|
58 | #include "../loader/LevelLoader.h" |
---|
59 | #include "../audio/AudioManager.h" |
---|
60 | |
---|
61 | #include "spaceship_steering.h" |
---|
62 | |
---|
63 | #include "particle/ParticleInterface.h" |
---|
64 | |
---|
65 | //network stuff |
---|
66 | #include "../network/Server.h" |
---|
67 | #include "../network/Client.h" |
---|
68 | #include "../network/NetworkFrameListener.h" |
---|
69 | |
---|
70 | |
---|
71 | namespace orxonox |
---|
72 | { |
---|
73 | using namespace Ogre; |
---|
74 | |
---|
75 | // put this in seperate Class or solve the problem in another fashion |
---|
76 | class OrxListener : public FrameListener, public OIS::MouseListener |
---|
77 | { |
---|
78 | public: |
---|
79 | OrxListener(OIS::Keyboard *keyboard, OIS::Mouse *mouse, audio::AudioManager* auMan, SpaceshipSteering* steering) |
---|
80 | : mKeyboard(keyboard), mMouse(mouse) |
---|
81 | { |
---|
82 | |
---|
83 | |
---|
84 | |
---|
85 | speed = 250; |
---|
86 | loop = 100; |
---|
87 | rotate = 10; |
---|
88 | mouseX = 0; |
---|
89 | mouseY = 0; |
---|
90 | maxMouseX = 0; |
---|
91 | minMouseX = 0; |
---|
92 | moved = false; |
---|
93 | |
---|
94 | steering_ = steering; |
---|
95 | |
---|
96 | steering_->brakeRotate(rotate*10); |
---|
97 | steering_->brakeLoop(loop); |
---|
98 | |
---|
99 | |
---|
100 | mMouse->setEventCallback(this); |
---|
101 | auMan_ = auMan; |
---|
102 | } |
---|
103 | bool frameStarted(const FrameEvent& evt) |
---|
104 | { |
---|
105 | |
---|
106 | auMan_->update(); |
---|
107 | |
---|
108 | mKeyboard->capture(); |
---|
109 | mMouse->capture(); |
---|
110 | if (mKeyboard->isKeyDown(OIS::KC_UP) || mKeyboard->isKeyDown(OIS::KC_W)) |
---|
111 | steering_->moveForward(speed); |
---|
112 | else |
---|
113 | steering_->moveForward(0); |
---|
114 | if(mKeyboard->isKeyDown(OIS::KC_DOWN) || mKeyboard->isKeyDown(OIS::KC_S)) |
---|
115 | steering_->brakeForward(speed); |
---|
116 | else |
---|
117 | steering_->brakeForward(speed/10); |
---|
118 | if (mKeyboard->isKeyDown(OIS::KC_RIGHT) || mKeyboard->isKeyDown(OIS::KC_D)) |
---|
119 | steering_->loopRight(loop); |
---|
120 | else |
---|
121 | steering_->loopRight(0); |
---|
122 | if (mKeyboard->isKeyDown(OIS::KC_LEFT) || mKeyboard->isKeyDown(OIS::KC_A)) |
---|
123 | steering_->loopLeft(loop); |
---|
124 | else |
---|
125 | steering_->loopLeft(0); |
---|
126 | |
---|
127 | if(moved) { |
---|
128 | if (mouseY<=0) |
---|
129 | steering_->rotateUp(-mouseY*rotate); |
---|
130 | if (mouseY>0) |
---|
131 | steering_->rotateDown(mouseY*rotate); |
---|
132 | if (mouseX>0) |
---|
133 | steering_->rotateRight(mouseX*rotate); |
---|
134 | if (mouseX<=0) |
---|
135 | steering_->rotateLeft(-mouseX*rotate); |
---|
136 | mouseY = 0; |
---|
137 | mouseX = 0; |
---|
138 | moved = false; |
---|
139 | } |
---|
140 | else { |
---|
141 | steering_->rotateUp(0); |
---|
142 | steering_->rotateDown(0); |
---|
143 | steering_->rotateRight(0); |
---|
144 | steering_->rotateLeft(0); |
---|
145 | } |
---|
146 | |
---|
147 | steering_->tick(evt.timeSinceLastFrame); |
---|
148 | |
---|
149 | |
---|
150 | |
---|
151 | // scenemanager->spacehip->tick(evt.timesincelastframe); |
---|
152 | if(mKeyboard->isKeyDown(OIS::KC_ESCAPE)) |
---|
153 | cout << "maximal MouseX: " << maxMouseX << "\tminMouseX: " << minMouseX << endl; |
---|
154 | return !mKeyboard->isKeyDown(OIS::KC_ESCAPE); |
---|
155 | } |
---|
156 | |
---|
157 | bool mouseMoved(const OIS::MouseEvent &e) |
---|
158 | { |
---|
159 | mouseX += e.state.X.rel; |
---|
160 | mouseY += e.state.Y.rel; |
---|
161 | if(mouseX>maxMouseX) maxMouseX = mouseX; |
---|
162 | if(mouseX<minMouseX) minMouseX = mouseX; |
---|
163 | cout << "mouseX: " << mouseX << "\tmouseY: " << mouseY << endl; |
---|
164 | moved = true; |
---|
165 | return true; |
---|
166 | } |
---|
167 | |
---|
168 | bool mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id) { return true; } |
---|
169 | bool mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id) { return true; } |
---|
170 | |
---|
171 | private: |
---|
172 | float speed; |
---|
173 | float rotate; |
---|
174 | float loop; |
---|
175 | float mouseY; |
---|
176 | float mouseX; |
---|
177 | float maxMouseX; |
---|
178 | float minMouseX; |
---|
179 | bool moved; |
---|
180 | OIS::Keyboard *mKeyboard; |
---|
181 | OIS::Mouse *mMouse; |
---|
182 | audio::AudioManager* auMan_; |
---|
183 | SpaceshipSteering* steering_; |
---|
184 | }; |
---|
185 | // init static singleton reference of Orxonox |
---|
186 | Orxonox* Orxonox::singletonRef_ = NULL; |
---|
187 | |
---|
188 | /** |
---|
189 | * create a new instance of Orxonox |
---|
190 | */ |
---|
191 | Orxonox::Orxonox() |
---|
192 | { |
---|
193 | ogre_ = new GraphicsEngine(); |
---|
194 | } |
---|
195 | |
---|
196 | /** |
---|
197 | * destruct Orxonox |
---|
198 | */ |
---|
199 | Orxonox::~Orxonox() |
---|
200 | { |
---|
201 | // nothing to delete as for now |
---|
202 | } |
---|
203 | |
---|
204 | /** |
---|
205 | * initialization of Orxonox object |
---|
206 | * @param argc argument counter |
---|
207 | * @param argv list of arguments |
---|
208 | * @param path path to config (in home dir or something) |
---|
209 | */ |
---|
210 | void Orxonox::init(int argc, char **argv, std::string path) |
---|
211 | { |
---|
212 | //TODO: initialization with command line parameters |
---|
213 | //TODO: find config file (assuming executable directory) |
---|
214 | //TODO: read config file |
---|
215 | //TODO: give config file to Ogre |
---|
216 | |
---|
217 | if(argc >=2 && strcmp(argv[1], "server")==0) |
---|
218 | { |
---|
219 | serverInit(path); |
---|
220 | } |
---|
221 | else if(argc >=2 && strcmp(argv[1], "client")==0) |
---|
222 | { |
---|
223 | clientInit(path); |
---|
224 | } |
---|
225 | else if(argc >=2 && strcmp(argv[1], "presentation")==0) |
---|
226 | { |
---|
227 | playableServer(path); |
---|
228 | } |
---|
229 | else |
---|
230 | standalone(path); |
---|
231 | } |
---|
232 | |
---|
233 | /** |
---|
234 | * start modules |
---|
235 | */ |
---|
236 | void Orxonox::start() |
---|
237 | { |
---|
238 | //TODO: start modules |
---|
239 | ogre_->startRender(); |
---|
240 | //TODO: run engine |
---|
241 | createScene(); |
---|
242 | setupScene(); |
---|
243 | setupInputSystem(); |
---|
244 | createFrameListener(); |
---|
245 | Factory::createClassHierarchy(); |
---|
246 | startRenderLoop(); |
---|
247 | } |
---|
248 | |
---|
249 | /** |
---|
250 | * @return singleton object |
---|
251 | */ |
---|
252 | Orxonox* Orxonox::getSingleton() |
---|
253 | { |
---|
254 | if (!singletonRef_) |
---|
255 | singletonRef_ = new Orxonox(); |
---|
256 | return singletonRef_; |
---|
257 | } |
---|
258 | |
---|
259 | /** |
---|
260 | * error kills orxonox |
---|
261 | */ |
---|
262 | void Orxonox::die(/* some error code */) |
---|
263 | { |
---|
264 | //TODO: destroy and destruct everything and print nice error msg |
---|
265 | } |
---|
266 | |
---|
267 | void Orxonox::standalone(std::string path) |
---|
268 | { |
---|
269 | ogre_->setConfigPath(path); |
---|
270 | ogre_->setup(); |
---|
271 | root_ = ogre_->getRoot(); |
---|
272 | if(!ogre_->load()) die(/* unable to load */); |
---|
273 | |
---|
274 | //defineResources(); |
---|
275 | //setupRenderSystem(); |
---|
276 | //createRenderWindow(); |
---|
277 | //initializeResourceGroups(); |
---|
278 | /*createScene(); |
---|
279 | setupScene(); |
---|
280 | setupInputSystem(); |
---|
281 | createFrameListener(); |
---|
282 | Factory::createClassHierarchy(); |
---|
283 | startRenderLoop();*/ |
---|
284 | } |
---|
285 | |
---|
286 | void Orxonox::playableServer(std::string path) |
---|
287 | { |
---|
288 | ogre_->setConfigPath(path); |
---|
289 | ogre_->setup(); |
---|
290 | root_ = ogre_->getRoot(); |
---|
291 | defineResources(); |
---|
292 | setupRenderSystem(); |
---|
293 | createRenderWindow(); |
---|
294 | initializeResourceGroups(); |
---|
295 | createScene(); |
---|
296 | setupScene(); |
---|
297 | setupInputSystem(); |
---|
298 | Factory::createClassHierarchy(); |
---|
299 | createFrameListener(); |
---|
300 | try{ |
---|
301 | server_g = new network::Server(); // add port and bindadress |
---|
302 | server_g->open(); // open server and create listener thread |
---|
303 | if(ogre_ && ogre_->getRoot()) |
---|
304 | ogre_->getRoot()->addFrameListener(new network::ServerFrameListener()); // adds a framelistener for the server |
---|
305 | std::cout << "network framelistener added" << std::endl; |
---|
306 | } |
---|
307 | catch(exception &e) |
---|
308 | { |
---|
309 | std::cout << "There was a problem initialising the server :(" << std::endl; |
---|
310 | } |
---|
311 | startRenderLoop(); |
---|
312 | } |
---|
313 | |
---|
314 | void Orxonox::serverInit(std::string path) |
---|
315 | { |
---|
316 | ogre_->setConfigPath(path); |
---|
317 | ogre_->setup(); |
---|
318 | server_g = new network::Server(); // add some settings if wanted |
---|
319 | if(!ogre_->load()) die(/* unable to load */); |
---|
320 | ogre_->getRoot()->addFrameListener(new network::ServerFrameListener()); |
---|
321 | ogre_->startRender(); |
---|
322 | |
---|
323 | createScene(); |
---|
324 | setupScene(); |
---|
325 | } |
---|
326 | |
---|
327 | void Orxonox::clientInit(std::string path) |
---|
328 | { |
---|
329 | ogre_->setConfigPath(path); |
---|
330 | ogre_->setup(); |
---|
331 | client_g = new network::Client(); // address here |
---|
332 | if(!ogre_->load()) die(/* unable to load */); |
---|
333 | ogre_->getRoot()->addFrameListener(new network::ClientFrameListener()); |
---|
334 | ogre_->startRender(); |
---|
335 | |
---|
336 | createScene(); |
---|
337 | setupScene(); |
---|
338 | setupInputSystem(); |
---|
339 | createFrameListener(); |
---|
340 | startRenderLoop(); |
---|
341 | } |
---|
342 | |
---|
343 | void Orxonox::defineResources() |
---|
344 | { |
---|
345 | Ogre::String secName, typeName, archName; |
---|
346 | Ogre::ConfigFile cf; |
---|
347 | #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE |
---|
348 | cf.load(macBundlePath() + "/Contents/Resources/resources.cfg"); |
---|
349 | #else |
---|
350 | cf.load("resources.cfg"); |
---|
351 | #endif |
---|
352 | |
---|
353 | Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator(); |
---|
354 | while (seci.hasMoreElements()) |
---|
355 | { |
---|
356 | secName = seci.peekNextKey(); |
---|
357 | Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext(); |
---|
358 | Ogre::ConfigFile::SettingsMultiMap::iterator i; |
---|
359 | for (i = settings->begin(); i != settings->end(); ++i) |
---|
360 | { |
---|
361 | typeName = i->first; |
---|
362 | archName = i->second; |
---|
363 | #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE |
---|
364 | Ogre::ResourceGroupManager::getSingleton().addResourceLocation( String(macBundlePath() + "/" + archName), typeName, secName); |
---|
365 | #else |
---|
366 | Ogre::ResourceGroupManager::getSingleton().addResourceLocation( archName, typeName, secName); |
---|
367 | #endif |
---|
368 | } |
---|
369 | } |
---|
370 | } |
---|
371 | |
---|
372 | void Orxonox::setupRenderSystem() |
---|
373 | { |
---|
374 | if (!root_->restoreConfig() && !root_->showConfigDialog()) |
---|
375 | throw Exception(52, "User canceled the config dialog!", "OrxApplication::setupRenderSystem()"); |
---|
376 | } |
---|
377 | |
---|
378 | void Orxonox::createRenderWindow() |
---|
379 | { |
---|
380 | root_->initialise(true, "OrxonoxV2"); |
---|
381 | } |
---|
382 | |
---|
383 | void Orxonox::initializeResourceGroups() |
---|
384 | { |
---|
385 | TextureManager::getSingleton().setDefaultNumMipmaps(5); |
---|
386 | ResourceGroupManager::getSingleton().initialiseAllResourceGroups(); |
---|
387 | } |
---|
388 | |
---|
389 | /** |
---|
390 | * |
---|
391 | * @param |
---|
392 | */ |
---|
393 | void Orxonox::createScene(void) |
---|
394 | { |
---|
395 | // Init audio |
---|
396 | auMan_ = new audio::AudioManager(); |
---|
397 | |
---|
398 | // load this file from config |
---|
399 | loader_ = new loader::LevelLoader("sample.oxw"); |
---|
400 | loader_->loadLevel(); |
---|
401 | |
---|
402 | |
---|
403 | |
---|
404 | |
---|
405 | |
---|
406 | /* |
---|
407 | auMan_->ambientAdd("a1"); |
---|
408 | auMan_->ambientAdd("a2"); |
---|
409 | auMan_->ambientAdd("a3"); |
---|
410 | //auMan->ambientAdd("ambient1"); |
---|
411 | auMan_->ambientStart(); |
---|
412 | */ |
---|
413 | } |
---|
414 | |
---|
415 | |
---|
416 | /** |
---|
417 | * |
---|
418 | */ |
---|
419 | void Orxonox::setupScene() |
---|
420 | { |
---|
421 | SceneManager *mgr = ogre_->getSceneManager(); |
---|
422 | |
---|
423 | |
---|
424 | SceneNode* node = (SceneNode*)mgr->getRootSceneNode()->getChild("OgreHeadNode"); |
---|
425 | |
---|
426 | |
---|
427 | steering_ = new SpaceshipSteering(500, 200, 200, 200); |
---|
428 | steering_->addNode(node); |
---|
429 | |
---|
430 | |
---|
431 | particle::ParticleInterface *e = new particle::ParticleInterface(mgr,"engine","Orxonox/strahl"); |
---|
432 | e->particleSystem_->setParameter("local_space","true"); |
---|
433 | e->setPositionOfEmitter(0, Vector3(0,-10,200)); |
---|
434 | e->setDirection(Vector3(0,0,-1)); |
---|
435 | e->addToSceneNode(node); |
---|
436 | |
---|
437 | particle::ParticleInterface *w = new particle::ParticleInterface(mgr,"schuss","Orxonox/schuss"); |
---|
438 | w->particleSystem_->setParameter("local_space","true"); |
---|
439 | w->newEmitter(); |
---|
440 | w->setDirection(Vector3(0,0,1)); |
---|
441 | w->setPositionOfEmitter(0, Vector3(10,10,0)); |
---|
442 | w->setPositionOfEmitter(1, Vector3(-10,10,0)); |
---|
443 | w->addToSceneNode(node); |
---|
444 | } |
---|
445 | |
---|
446 | |
---|
447 | void Orxonox::setupInputSystem() |
---|
448 | { |
---|
449 | size_t windowHnd = 0; |
---|
450 | std::ostringstream windowHndStr; |
---|
451 | OIS::ParamList pl; |
---|
452 | Ogre::RenderWindow *win = ogre_->getRoot()->getAutoCreatedWindow(); |
---|
453 | |
---|
454 | win->getCustomAttribute("WINDOW", &windowHnd); |
---|
455 | windowHndStr << windowHnd; |
---|
456 | pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str())); |
---|
457 | inputManager_ = OIS::InputManager::createInputSystem(pl); |
---|
458 | |
---|
459 | try |
---|
460 | { |
---|
461 | keyboard_ = static_cast<OIS::Keyboard*>(inputManager_->createInputObject(OIS::OISKeyboard, false)); |
---|
462 | mouse_ = static_cast<OIS::Mouse*>(inputManager_->createInputObject(OIS::OISMouse, true)); |
---|
463 | } |
---|
464 | catch (const OIS::Exception &e) |
---|
465 | { |
---|
466 | throw new Ogre::Exception(42, e.eText, "OrxApplication::setupInputSystem"); |
---|
467 | } |
---|
468 | } |
---|
469 | |
---|
470 | // we actually want to do this differently... |
---|
471 | void Orxonox::createFrameListener() |
---|
472 | { |
---|
473 | TickFrameListener* TickFL = new TickFrameListener(); |
---|
474 | ogre_->getRoot()->addFrameListener(TickFL); |
---|
475 | |
---|
476 | TimerFrameListener* TimerFL = new TimerFrameListener(); |
---|
477 | ogre_->getRoot()->addFrameListener(TimerFL); |
---|
478 | |
---|
479 | frameListener_ = new OrxListener(keyboard_, mouse_, auMan_, steering_); |
---|
480 | ogre_->getRoot()->addFrameListener(frameListener_); |
---|
481 | } |
---|
482 | |
---|
483 | void Orxonox::startRenderLoop() |
---|
484 | { |
---|
485 | // this is a hack!!! |
---|
486 | // the call to reset the mouse clipping size should probably be somewhere |
---|
487 | // else, however this works for the moment. |
---|
488 | unsigned int width, height, depth; |
---|
489 | int left, top; |
---|
490 | ogre_->getRoot()->getAutoCreatedWindow()->getMetrics(width, height, depth, left, top); |
---|
491 | |
---|
492 | const OIS::MouseState &ms = mouse_->getMouseState(); |
---|
493 | ms.width = width; |
---|
494 | ms.height = height; |
---|
495 | |
---|
496 | ogre_->getRoot()->startRendering(); |
---|
497 | } |
---|
498 | } |
---|