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 | // 40% speed up: (instead of Ogre.h) |
---|
35 | #include <OgreSceneNode.h> |
---|
36 | #include <OgreSceneManager.h> |
---|
37 | #include <OgreRoot.h> |
---|
38 | #include <OgreFrameListener.h> |
---|
39 | #include <OgreConfigFile.h> |
---|
40 | #include <OgreTextureManager.h> |
---|
41 | #include <OgreEntity.h> |
---|
42 | #include <OgreRenderWindow.h> |
---|
43 | |
---|
44 | #include <OIS/OIS.h> |
---|
45 | //#include <CEGUI/CEGUI.h> |
---|
46 | //#include <OgreCEGUIRenderer.h> |
---|
47 | |
---|
48 | #include <string> |
---|
49 | #include <iostream> |
---|
50 | |
---|
51 | #include "xml/xmlParser.h" |
---|
52 | #include "loader/LevelLoader.h" |
---|
53 | #include "audio/AudioManager.h" |
---|
54 | |
---|
55 | #include "spaceship_steering.h" |
---|
56 | SpaceshipSteering* steering; |
---|
57 | |
---|
58 | audio::AudioManager* auMan; |
---|
59 | |
---|
60 | //network stuff |
---|
61 | //#include "network/Server.h" |
---|
62 | //#include "network/Client.h" |
---|
63 | #include "network/NetworkFrameListener.h" |
---|
64 | |
---|
65 | // some tests to see if enet works without includsion |
---|
66 | //#include <enet/enet.h> |
---|
67 | //#include <enet/protocol.h> |
---|
68 | |
---|
69 | #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE |
---|
70 | #include <CoreFoundation/CoreFoundation.h> |
---|
71 | |
---|
72 | // This function will locate the path to our application on OS X, |
---|
73 | // unlike windows you can not rely on the curent working directory |
---|
74 | // for locating your configuration files and resources. |
---|
75 | std::string macBundlePath() |
---|
76 | { |
---|
77 | char path[1024]; |
---|
78 | CFBundleRef mainBundle = CFBundleGetMainBundle(); |
---|
79 | assert(mainBundle); |
---|
80 | |
---|
81 | CFURLRef mainBundleURL = CFBundleCopyBundleURL(mainBundle); |
---|
82 | assert(mainBundleURL); |
---|
83 | |
---|
84 | CFStringRef cfStringRef = CFURLCopyFileSystemPath( mainBundleURL, kCFURLPOSIXPathStyle); |
---|
85 | assert(cfStringRef); |
---|
86 | |
---|
87 | CFStringGetCString(cfStringRef, path, 1024, kCFStringEncodingASCII); |
---|
88 | |
---|
89 | CFRelease(mainBundleURL); |
---|
90 | CFRelease(cfStringRef); |
---|
91 | |
---|
92 | return std::string(path); |
---|
93 | } |
---|
94 | #endif |
---|
95 | |
---|
96 | namespace orxonox |
---|
97 | { |
---|
98 | |
---|
99 | using namespace Ogre; |
---|
100 | |
---|
101 | class OrxExitListener : public FrameListener, public OIS::MouseListener |
---|
102 | { |
---|
103 | public: |
---|
104 | OrxExitListener(OIS::Keyboard *keyboard, OIS::Mouse *mouse) |
---|
105 | : mKeyboard(keyboard), mMouse(mouse) |
---|
106 | { |
---|
107 | speed = 250; |
---|
108 | loop = 100; |
---|
109 | rotate = 10; |
---|
110 | mouseX = 0; |
---|
111 | mouseY = 0; |
---|
112 | maxMouseX = 0; |
---|
113 | minMouseX = 0; |
---|
114 | moved = false; |
---|
115 | steering->brakeRotate(rotate*10); |
---|
116 | steering->brakeLoop(loop); |
---|
117 | mMouse->setEventCallback(this); |
---|
118 | } |
---|
119 | bool frameStarted(const FrameEvent& evt) |
---|
120 | { |
---|
121 | |
---|
122 | auMan->update(); |
---|
123 | |
---|
124 | mKeyboard->capture(); |
---|
125 | mMouse->capture(); |
---|
126 | if (mKeyboard->isKeyDown(OIS::KC_UP) || mKeyboard->isKeyDown(OIS::KC_W)) |
---|
127 | steering->moveForward(speed); |
---|
128 | else |
---|
129 | steering->moveForward(0); |
---|
130 | if(mKeyboard->isKeyDown(OIS::KC_DOWN) || mKeyboard->isKeyDown(OIS::KC_S)) |
---|
131 | steering->brakeForward(speed); |
---|
132 | else |
---|
133 | steering->brakeForward(speed/10); |
---|
134 | if (mKeyboard->isKeyDown(OIS::KC_RIGHT) || mKeyboard->isKeyDown(OIS::KC_D)) |
---|
135 | steering->loopRight(loop); |
---|
136 | else |
---|
137 | steering->loopRight(0); |
---|
138 | if (mKeyboard->isKeyDown(OIS::KC_LEFT) || mKeyboard->isKeyDown(OIS::KC_A)) |
---|
139 | steering->loopLeft(loop); |
---|
140 | else |
---|
141 | steering->loopLeft(0); |
---|
142 | |
---|
143 | if(moved) { |
---|
144 | if (mouseY<0) |
---|
145 | steering->rotateUp(-mouseY*rotate); |
---|
146 | if (mouseY>0) |
---|
147 | steering->rotateDown(mouseY*rotate); |
---|
148 | if (mouseX>0) |
---|
149 | steering->rotateRight(mouseX*rotate); |
---|
150 | if (mouseX<0) |
---|
151 | steering->rotateLeft(-mouseX*rotate); |
---|
152 | moved = false; |
---|
153 | } |
---|
154 | else { |
---|
155 | steering->rotateUp(0); |
---|
156 | steering->rotateDown(0); |
---|
157 | steering->rotateRight(0); |
---|
158 | steering->rotateLeft(0); |
---|
159 | } |
---|
160 | |
---|
161 | steering->tick(evt.timeSinceLastFrame); |
---|
162 | // scenemanager->spacehip->tick(evt.timesincelastframe); |
---|
163 | if(mKeyboard->isKeyDown(OIS::KC_ESCAPE)) |
---|
164 | cout << "maximal MouseX: " << maxMouseX << "\tminMouseX: " << minMouseX << endl; |
---|
165 | return !mKeyboard->isKeyDown(OIS::KC_ESCAPE); |
---|
166 | } |
---|
167 | |
---|
168 | bool mouseMoved(const OIS::MouseEvent &e) |
---|
169 | { |
---|
170 | mouseX = e.state.X.rel; |
---|
171 | mouseY = e.state.Y.rel; |
---|
172 | if(mouseX>maxMouseX) maxMouseX = mouseX; |
---|
173 | if(mouseX<minMouseX) minMouseX = mouseX; |
---|
174 | cout << "mouseX: " << mouseX << "\tmouseY: " << mouseY << endl; |
---|
175 | moved = true; |
---|
176 | return true; |
---|
177 | } |
---|
178 | |
---|
179 | bool mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id) { return true; } |
---|
180 | bool mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id) { return true; } |
---|
181 | |
---|
182 | private: |
---|
183 | float speed; |
---|
184 | float rotate; |
---|
185 | float loop; |
---|
186 | float mouseY; |
---|
187 | float mouseX; |
---|
188 | float maxMouseX; |
---|
189 | float minMouseX; |
---|
190 | bool moved; |
---|
191 | OIS::Keyboard *mKeyboard; |
---|
192 | OIS::Mouse *mMouse; |
---|
193 | }; |
---|
194 | |
---|
195 | |
---|
196 | class OrxApplication |
---|
197 | { |
---|
198 | public: |
---|
199 | void go() |
---|
200 | { |
---|
201 | if(function==0) |
---|
202 | standalone(); |
---|
203 | else if(function==1) |
---|
204 | server(); |
---|
205 | else |
---|
206 | client(); |
---|
207 | } |
---|
208 | |
---|
209 | OrxApplication(int func){ |
---|
210 | function = func; |
---|
211 | } |
---|
212 | OrxApplication(){ |
---|
213 | function = 0; |
---|
214 | } |
---|
215 | |
---|
216 | ~OrxApplication() |
---|
217 | { |
---|
218 | mInputManager->destroyInputObject(mKeyboard); |
---|
219 | OIS::InputManager::destroyInputSystem(mInputManager); |
---|
220 | |
---|
221 | // delete mRenderer; |
---|
222 | // delete mSystem; |
---|
223 | |
---|
224 | // delete mListener; |
---|
225 | // delete mRoot; |
---|
226 | } |
---|
227 | |
---|
228 | private: |
---|
229 | Ogre::Root *mRoot; |
---|
230 | OIS::Keyboard *mKeyboard; |
---|
231 | OIS::Mouse *mMouse; |
---|
232 | OIS::InputManager *mInputManager; |
---|
233 | //CEGUI::OgreCEGUIRenderer *mRenderer; |
---|
234 | //CEGUI::System *mSystem; |
---|
235 | OrxExitListener *mListener; |
---|
236 | int function; // 0 for standalone, 1 for server, 2 for client |
---|
237 | |
---|
238 | void client() |
---|
239 | { |
---|
240 | client_g = new network::Client(); // address here |
---|
241 | mRoot->addFrameListener(new network::ClientFrameListener()); |
---|
242 | createRoot(); |
---|
243 | defineResources(); |
---|
244 | setupRenderSystem(); |
---|
245 | createRenderWindow(); |
---|
246 | initializeResourceGroups(); |
---|
247 | createScene(); |
---|
248 | setupScene(); |
---|
249 | setupInputSystem(); |
---|
250 | // setupCEGUI(); |
---|
251 | createFrameListener(); |
---|
252 | startRenderLoop(); |
---|
253 | } |
---|
254 | void server() |
---|
255 | { |
---|
256 | server_g = new network::Server(); // add some settings if wanted |
---|
257 | mRoot->addFrameListener(new network::ServerFrameListener()); |
---|
258 | createRoot(); |
---|
259 | defineResources(); |
---|
260 | setupRenderSystem(); |
---|
261 | createRenderWindow(); |
---|
262 | initializeResourceGroups(); |
---|
263 | createScene(); |
---|
264 | setupScene(); |
---|
265 | //setupInputSystem(); |
---|
266 | //setupCEGUI(); |
---|
267 | //createFrameListener(); |
---|
268 | //startRenderLoop(); |
---|
269 | } |
---|
270 | void standalone() |
---|
271 | { |
---|
272 | createRoot(); |
---|
273 | defineResources(); |
---|
274 | setupRenderSystem(); |
---|
275 | createRenderWindow(); |
---|
276 | initializeResourceGroups(); |
---|
277 | createScene(); |
---|
278 | setupScene(); |
---|
279 | setupInputSystem(); |
---|
280 | setupCEGUI(); |
---|
281 | createFrameListener(); |
---|
282 | startRenderLoop(); |
---|
283 | } |
---|
284 | |
---|
285 | void createRoot() |
---|
286 | { |
---|
287 | #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE |
---|
288 | mRoot = new Ogre::Root(macBundlePath() + "/Contents/Resources/plugins.cfg"); |
---|
289 | #else |
---|
290 | mRoot = new Ogre::Root(); |
---|
291 | #endif |
---|
292 | } |
---|
293 | |
---|
294 | void defineResources() |
---|
295 | { |
---|
296 | Ogre::String secName, typeName, archName; |
---|
297 | Ogre::ConfigFile cf; |
---|
298 | #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE |
---|
299 | cf.load(macBundlePath() + "/Contents/Resources/resources.cfg"); |
---|
300 | #else |
---|
301 | cf.load("resources.cfg"); |
---|
302 | #endif |
---|
303 | |
---|
304 | Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator(); |
---|
305 | while (seci.hasMoreElements()) |
---|
306 | { |
---|
307 | secName = seci.peekNextKey(); |
---|
308 | Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext(); |
---|
309 | Ogre::ConfigFile::SettingsMultiMap::iterator i; |
---|
310 | for (i = settings->begin(); i != settings->end(); ++i) |
---|
311 | { |
---|
312 | typeName = i->first; |
---|
313 | archName = i->second; |
---|
314 | #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE |
---|
315 | Ogre::ResourceGroupManager::getSingleton().addResourceLocation( String(macBundlePath() + "/" + archName), typeName, secName); |
---|
316 | #else |
---|
317 | Ogre::ResourceGroupManager::getSingleton().addResourceLocation( archName, typeName, secName); |
---|
318 | #endif |
---|
319 | } |
---|
320 | } |
---|
321 | } |
---|
322 | |
---|
323 | void setupRenderSystem() |
---|
324 | { |
---|
325 | if (!mRoot->restoreConfig() && !mRoot->showConfigDialog()) |
---|
326 | throw Exception(52, "User canceled the config dialog!", "OrxApplication::setupRenderSystem()"); |
---|
327 | } |
---|
328 | |
---|
329 | void createRenderWindow() |
---|
330 | { |
---|
331 | mRoot->initialise(true, "OrxonoxV2"); |
---|
332 | } |
---|
333 | |
---|
334 | void initializeResourceGroups() |
---|
335 | { |
---|
336 | TextureManager::getSingleton().setDefaultNumMipmaps(5); |
---|
337 | ResourceGroupManager::getSingleton().initialiseAllResourceGroups(); |
---|
338 | } |
---|
339 | |
---|
340 | void createScene(void) |
---|
341 | { |
---|
342 | auMan = new audio::AudioManager(); |
---|
343 | |
---|
344 | auMan->ambientAdd("a1"); |
---|
345 | auMan->ambientAdd("a2"); |
---|
346 | auMan->ambientAdd("a3"); |
---|
347 | //auMan->ambientAdd("ambient1"); |
---|
348 | auMan->ambientStart(); |
---|
349 | |
---|
350 | string levelFile = "sp_level_moonstation.oxw"; |
---|
351 | // loader::LevelLoader* loader = new loader::LevelLoader(levelFile); |
---|
352 | } |
---|
353 | |
---|
354 | |
---|
355 | void setupScene() |
---|
356 | { |
---|
357 | SceneManager *mgr = mRoot->createSceneManager(ST_GENERIC, "Default SceneManager"); |
---|
358 | Camera *cam = mgr->createCamera("Camera"); |
---|
359 | cam->setPosition(Vector3(0,0,-250)); |
---|
360 | cam->lookAt(Vector3(0,0,0)); |
---|
361 | Viewport *vp = mRoot->getAutoCreatedWindow()->addViewport(cam); |
---|
362 | |
---|
363 | |
---|
364 | mgr->setAmbientLight(ColourValue(1,1,1)); |
---|
365 | Entity* head = mgr->createEntity("head", "razor.mesh"); |
---|
366 | SceneNode *node = mgr->getRootSceneNode()->createChildSceneNode("OgreHeadNode", Vector3(0,0,0)); |
---|
367 | node->attachObject(head); |
---|
368 | node->attachObject(cam); |
---|
369 | mgr->setSkyBox(true, "Examples/SceneSkyBox2"); |
---|
370 | |
---|
371 | Entity* head1 = mgr->createEntity("head1", "ogrehead.mesh"); |
---|
372 | SceneNode *node1 = mgr->getRootSceneNode()->createChildSceneNode("OgreHeadNode1", Vector3(200,0,0)); |
---|
373 | node1->attachObject(head1); |
---|
374 | Entity* head2 = mgr->createEntity("head2", "ogrehead.mesh"); |
---|
375 | SceneNode *node2 = mgr->getRootSceneNode()->createChildSceneNode("OgreHeadNode2", Vector3(200,400,-100)); |
---|
376 | node2->attachObject(head2); |
---|
377 | Entity* head3 = mgr->createEntity("head3", "ogrehead.mesh"); |
---|
378 | SceneNode *node3 = mgr->getRootSceneNode()->createChildSceneNode("OgreHeadNode3", Vector3(0,400,200)); |
---|
379 | node3->attachObject(head3); |
---|
380 | Entity* head4 = mgr->createEntity("head4", "ogrehead.mesh"); |
---|
381 | SceneNode *node4 = mgr->getRootSceneNode()->createChildSceneNode("OgreHeadNode4", Vector3(-400,-200,600)); |
---|
382 | node4->attachObject(head4); |
---|
383 | Entity* head5 = mgr->createEntity("head5", "ogrehead.mesh"); |
---|
384 | SceneNode *node5 = mgr->getRootSceneNode()->createChildSceneNode("OgreHeadNode5", Vector3(0,0,-400)); |
---|
385 | node5->attachObject(head5); |
---|
386 | |
---|
387 | steering = new SpaceshipSteering(500, 200, 200, 200); |
---|
388 | steering->addNode(node); |
---|
389 | |
---|
390 | } |
---|
391 | |
---|
392 | |
---|
393 | void setupInputSystem() |
---|
394 | { |
---|
395 | size_t windowHnd = 0; |
---|
396 | std::ostringstream windowHndStr; |
---|
397 | OIS::ParamList pl; |
---|
398 | Ogre::RenderWindow *win = mRoot->getAutoCreatedWindow(); |
---|
399 | |
---|
400 | win->getCustomAttribute("WINDOW", &windowHnd); |
---|
401 | windowHndStr << windowHnd; |
---|
402 | pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str())); |
---|
403 | mInputManager = OIS::InputManager::createInputSystem(pl); |
---|
404 | |
---|
405 | try |
---|
406 | { |
---|
407 | mKeyboard = static_cast<OIS::Keyboard*>(mInputManager->createInputObject(OIS::OISKeyboard, false)); |
---|
408 | mMouse = static_cast<OIS::Mouse*>(mInputManager->createInputObject(OIS::OISMouse, true)); |
---|
409 | } |
---|
410 | catch (const OIS::Exception &e) |
---|
411 | { |
---|
412 | throw new Ogre::Exception(42, e.eText, "OrxApplication::setupInputSystem"); |
---|
413 | } |
---|
414 | } |
---|
415 | |
---|
416 | // void setupCEGUI() |
---|
417 | // { |
---|
418 | // Ogre::SceneManager *mgr = mRoot->getSceneManager("Default SceneManager"); |
---|
419 | // Ogre::RenderWindow *win = mRoot->getAutoCreatedWindow(); |
---|
420 | |
---|
421 | // CEGUI setup |
---|
422 | // mRenderer = new CEGUI::OgreCEGUIRenderer(win, Ogre::RENDER_QUEUE_OVERLAY, false, 3000, mgr); |
---|
423 | // mSystem = new CEGUI::System(mRenderer); |
---|
424 | |
---|
425 | // Other CEGUI setup here. |
---|
426 | // } |
---|
427 | |
---|
428 | |
---|
429 | void createFrameListener() |
---|
430 | { |
---|
431 | mListener = new OrxExitListener(mKeyboard, mMouse); |
---|
432 | mRoot->addFrameListener(mListener); |
---|
433 | } |
---|
434 | void startRenderLoop() |
---|
435 | { |
---|
436 | mRoot->startRendering(); |
---|
437 | } |
---|
438 | }; |
---|
439 | } |
---|
440 | |
---|
441 | using namespace Ogre; |
---|
442 | |
---|
443 | #if OGRE_PLATFORM == PLATFORM_WIN32 || OGRE_PLATFORM == OGRE_PLATFORM_WIN32 |
---|
444 | #define WIN32_LEAN_AND_MEAN |
---|
445 | #include "windows.h" |
---|
446 | |
---|
447 | INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT) |
---|
448 | #else |
---|
449 | int main(int argc, char **argv) |
---|
450 | #endif |
---|
451 | { |
---|
452 | try |
---|
453 | { |
---|
454 | if(argc >=2 && strcmp(argv[1], "server")==0){ |
---|
455 | orxonox::OrxApplication orxonox(1); |
---|
456 | orxonox.go(); |
---|
457 | } |
---|
458 | else if(argc >=2 && strcmp(argv[1], "client")==0){ |
---|
459 | orxonox::OrxApplication orxonox(2); |
---|
460 | orxonox.go(); |
---|
461 | } |
---|
462 | else{ |
---|
463 | orxonox::OrxApplication orxonox(0); |
---|
464 | orxonox.go(); |
---|
465 | } |
---|
466 | } |
---|
467 | catch(Exception& e) |
---|
468 | { |
---|
469 | #if OGRE_PLATFORM == PLATFORM_WIN32 || OGRE_PLATFORM == OGRE_PLATFORM_WIN32 |
---|
470 | MessageBoxA(NULL, e.getFullDescription().c_str(), "An exception has occurred!", MB_OK | MB_ICONERROR | MB_TASKMODAL); |
---|
471 | #else |
---|
472 | fprintf(stderr, "An exception has occurred: %s\n", |
---|
473 | e.getFullDescription().c_str()); |
---|
474 | #endif |
---|
475 | } |
---|
476 | |
---|
477 | return 0; |
---|
478 | } |
---|
479 | |
---|