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