| 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 "../xml/xmlParser.h" |
|---|
| 55 | #include "../loader/LevelLoader.h" |
|---|
| 56 | #include "../audio/AudioManager.h" |
|---|
| 57 | |
|---|
| 58 | #include "spaceship_steering.h" |
|---|
| 59 | SpaceshipSteering* steering; |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | //network stuff |
|---|
| 63 | #include "../network/Server.h" |
|---|
| 64 | #include "../network/Client.h" |
|---|
| 65 | //#include "../network/NetworkFrameListener.h" |
|---|
| 66 | |
|---|
| 67 | // some tests to see if enet works without includsion |
|---|
| 68 | //#include <enet/enet.h> |
|---|
| 69 | //#include <enet/protocol.h> |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | namespace orxonox |
|---|
| 73 | { |
|---|
| 74 | using namespace Ogre; |
|---|
| 75 | |
|---|
| 76 | // put this in seperate Class or solve the problem in another fashion |
|---|
| 77 | class OrxListener : public FrameListener, public OIS::MouseListener |
|---|
| 78 | { |
|---|
| 79 | public: |
|---|
| 80 | OrxListener(OIS::Keyboard *keyboard, OIS::Mouse *mouse, audio::AudioManager* auMan) |
|---|
| 81 | : mKeyboard(keyboard), mMouse(mouse) |
|---|
| 82 | { |
|---|
| 83 | speed = 250; |
|---|
| 84 | loop = 100; |
|---|
| 85 | rotate = 10; |
|---|
| 86 | mouseX = 0; |
|---|
| 87 | mouseY = 0; |
|---|
| 88 | maxMouseX = 0; |
|---|
| 89 | minMouseX = 0; |
|---|
| 90 | moved = false; |
|---|
| 91 | steering->brakeRotate(rotate*10); |
|---|
| 92 | steering->brakeLoop(loop); |
|---|
| 93 | mMouse->setEventCallback(this); |
|---|
| 94 | auMan_ = auMan; |
|---|
| 95 | } |
|---|
| 96 | bool frameStarted(const FrameEvent& evt) |
|---|
| 97 | { |
|---|
| 98 | |
|---|
| 99 | auMan_->update(); |
|---|
| 100 | |
|---|
| 101 | mKeyboard->capture(); |
|---|
| 102 | mMouse->capture(); |
|---|
| 103 | if (mKeyboard->isKeyDown(OIS::KC_UP) || mKeyboard->isKeyDown(OIS::KC_W)) |
|---|
| 104 | steering->moveForward(speed); |
|---|
| 105 | else |
|---|
| 106 | steering->moveForward(0); |
|---|
| 107 | if(mKeyboard->isKeyDown(OIS::KC_DOWN) || mKeyboard->isKeyDown(OIS::KC_S)) |
|---|
| 108 | steering->brakeForward(speed); |
|---|
| 109 | else |
|---|
| 110 | steering->brakeForward(speed/10); |
|---|
| 111 | if (mKeyboard->isKeyDown(OIS::KC_RIGHT) || mKeyboard->isKeyDown(OIS::KC_D)) |
|---|
| 112 | steering->loopRight(loop); |
|---|
| 113 | else |
|---|
| 114 | steering->loopRight(0); |
|---|
| 115 | if (mKeyboard->isKeyDown(OIS::KC_LEFT) || mKeyboard->isKeyDown(OIS::KC_A)) |
|---|
| 116 | steering->loopLeft(loop); |
|---|
| 117 | else |
|---|
| 118 | steering->loopLeft(0); |
|---|
| 119 | |
|---|
| 120 | if(moved) { |
|---|
| 121 | if (mouseY<0) |
|---|
| 122 | steering->rotateUp(-mouseY*rotate); |
|---|
| 123 | if (mouseY>0) |
|---|
| 124 | steering->rotateDown(mouseY*rotate); |
|---|
| 125 | if (mouseX>0) |
|---|
| 126 | steering->rotateRight(mouseX*rotate); |
|---|
| 127 | if (mouseX<0) |
|---|
| 128 | steering->rotateLeft(-mouseX*rotate); |
|---|
| 129 | moved = false; |
|---|
| 130 | } |
|---|
| 131 | else { |
|---|
| 132 | steering->rotateUp(0); |
|---|
| 133 | steering->rotateDown(0); |
|---|
| 134 | steering->rotateRight(0); |
|---|
| 135 | steering->rotateLeft(0); |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | steering->tick(evt.timeSinceLastFrame); |
|---|
| 139 | // scenemanager->spacehip->tick(evt.timesincelastframe); |
|---|
| 140 | if(mKeyboard->isKeyDown(OIS::KC_ESCAPE)) |
|---|
| 141 | cout << "maximal MouseX: " << maxMouseX << "\tminMouseX: " << minMouseX << endl; |
|---|
| 142 | return !mKeyboard->isKeyDown(OIS::KC_ESCAPE); |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | bool mouseMoved(const OIS::MouseEvent &e) |
|---|
| 146 | { |
|---|
| 147 | mouseX = e.state.X.rel; |
|---|
| 148 | mouseY = e.state.Y.rel; |
|---|
| 149 | if(mouseX>maxMouseX) maxMouseX = mouseX; |
|---|
| 150 | if(mouseX<minMouseX) minMouseX = mouseX; |
|---|
| 151 | cout << "mouseX: " << mouseX << "\tmouseY: " << mouseY << endl; |
|---|
| 152 | moved = true; |
|---|
| 153 | return true; |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | bool mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id) { return true; } |
|---|
| 157 | bool mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id) { return true; } |
|---|
| 158 | |
|---|
| 159 | private: |
|---|
| 160 | float speed; |
|---|
| 161 | float rotate; |
|---|
| 162 | float loop; |
|---|
| 163 | float mouseY; |
|---|
| 164 | float mouseX; |
|---|
| 165 | float maxMouseX; |
|---|
| 166 | float minMouseX; |
|---|
| 167 | bool moved; |
|---|
| 168 | OIS::Keyboard *mKeyboard; |
|---|
| 169 | OIS::Mouse *mMouse; |
|---|
| 170 | audio::AudioManager* auMan_; |
|---|
| 171 | }; |
|---|
| 172 | // init static singleton reference of Orxonox |
|---|
| 173 | Orxonox* Orxonox::singletonRef_ = NULL; |
|---|
| 174 | |
|---|
| 175 | /** |
|---|
| 176 | * create a new instance of Orxonox |
|---|
| 177 | */ |
|---|
| 178 | Orxonox::Orxonox() |
|---|
| 179 | { |
|---|
| 180 | ogre_ = new GraphicsEngine(); |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | /** |
|---|
| 184 | * destruct Orxonox |
|---|
| 185 | */ |
|---|
| 186 | Orxonox::~Orxonox() |
|---|
| 187 | { |
|---|
| 188 | // nothing to delete as for now |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | /** |
|---|
| 192 | * initialization of Orxonox object |
|---|
| 193 | * @param argc argument counter |
|---|
| 194 | * @param argv list of arguments |
|---|
| 195 | * @param path path to config (in home dir or something) |
|---|
| 196 | */ |
|---|
| 197 | void Orxonox::init(int argc, char **argv, std::string path) |
|---|
| 198 | { |
|---|
| 199 | //TODO: initialization with command line parameters |
|---|
| 200 | //TODO: find config file (assuming executable directory) |
|---|
| 201 | //TODO: read config file |
|---|
| 202 | //TODO: give config file to Ogre |
|---|
| 203 | if(argc >=2 && strcmp(argv[1], "server")==0) |
|---|
| 204 | { |
|---|
| 205 | serverInit(path); |
|---|
| 206 | } |
|---|
| 207 | else if(argc >=2 && strcmp(argv[1], "client")==0) |
|---|
| 208 | { |
|---|
| 209 | clientInit(path); |
|---|
| 210 | } |
|---|
| 211 | standalone(path); |
|---|
| 212 | } |
|---|
| 213 | |
|---|
| 214 | /** |
|---|
| 215 | * start modules |
|---|
| 216 | */ |
|---|
| 217 | void Orxonox::start() |
|---|
| 218 | { |
|---|
| 219 | //TODO: start modules |
|---|
| 220 | ogre_->startRender(); |
|---|
| 221 | auMan_ = new audio::AudioManager(); |
|---|
| 222 | // load this file from config |
|---|
| 223 | string levelFile = "sample.oxw"; |
|---|
| 224 | loader_ = new loader::LevelLoader(levelFile); |
|---|
| 225 | //TODO: run engine |
|---|
| 226 | } |
|---|
| 227 | |
|---|
| 228 | /** |
|---|
| 229 | * @return singleton object |
|---|
| 230 | */ |
|---|
| 231 | Orxonox* Orxonox::getSingleton() |
|---|
| 232 | { |
|---|
| 233 | if (!singletonRef_) |
|---|
| 234 | singletonRef_ = new Orxonox(); |
|---|
| 235 | return singletonRef_; |
|---|
| 236 | } |
|---|
| 237 | |
|---|
| 238 | /** |
|---|
| 239 | * error kills orxonox |
|---|
| 240 | */ |
|---|
| 241 | void Orxonox::die(/* some error code */) |
|---|
| 242 | { |
|---|
| 243 | //TODO: destroy and destruct everything and print nice error msg |
|---|
| 244 | } |
|---|
| 245 | |
|---|
| 246 | void Orxonox::standalone(std::string path) |
|---|
| 247 | { |
|---|
| 248 | ogre_->setConfigPath(path); |
|---|
| 249 | ogre_->setup(); |
|---|
| 250 | if(!ogre_->load()) die(/* unable to load */); |
|---|
| 251 | } |
|---|
| 252 | |
|---|
| 253 | void Orxonox::serverInit(std::string path) |
|---|
| 254 | { |
|---|
| 255 | ogre_->setConfigPath(path); |
|---|
| 256 | ogre_->setup(); |
|---|
| 257 | //server_g = new network::Server(); // add some settings if wanted |
|---|
| 258 | if(!ogre_->load()) die(/* unable to load */); |
|---|
| 259 | //ogre_->getRoot()->addFrameListener(new network::ServerFrameListener()); |
|---|
| 260 | ogre_->startRender(); |
|---|
| 261 | |
|---|
| 262 | createScene(); |
|---|
| 263 | setupScene(); |
|---|
| 264 | } |
|---|
| 265 | |
|---|
| 266 | void Orxonox::clientInit(std::string path) |
|---|
| 267 | { |
|---|
| 268 | ogre_->setConfigPath(path); |
|---|
| 269 | ogre_->setup(); |
|---|
| 270 | //client_g = new network::Client(); // address here |
|---|
| 271 | if(!ogre_->load()) die(/* unable to load */); |
|---|
| 272 | //ogre_->getRoot()->addFrameListener(new network::ClientFrameListener()); |
|---|
| 273 | ogre_->startRender(); |
|---|
| 274 | |
|---|
| 275 | createScene(); |
|---|
| 276 | setupScene(); |
|---|
| 277 | setupInputSystem(); |
|---|
| 278 | createFrameListener(); |
|---|
| 279 | startRenderLoop(); |
|---|
| 280 | } |
|---|
| 281 | |
|---|
| 282 | void Orxonox::createScene(void) |
|---|
| 283 | { |
|---|
| 284 | auMan_ = new audio::AudioManager(); |
|---|
| 285 | |
|---|
| 286 | auMan_->ambientAdd("a1"); |
|---|
| 287 | auMan_->ambientAdd("a2"); |
|---|
| 288 | auMan_->ambientAdd("a3"); |
|---|
| 289 | //auMan->ambientAdd("ambient1"); |
|---|
| 290 | auMan_->ambientStart(); |
|---|
| 291 | |
|---|
| 292 | string levelFile = "sp_level_moonstation.oxw"; |
|---|
| 293 | // loader::LevelLoader* loader = new loader::LevelLoader(levelFile); |
|---|
| 294 | } |
|---|
| 295 | |
|---|
| 296 | |
|---|
| 297 | void Orxonox::setupScene() |
|---|
| 298 | { |
|---|
| 299 | SceneManager *mgr = ogre_->getSceneManager(); |
|---|
| 300 | Camera *cam = mgr->createCamera("Camera"); |
|---|
| 301 | cam->setPosition(Vector3(0,0,-250)); |
|---|
| 302 | cam->lookAt(Vector3(0,0,0)); |
|---|
| 303 | Viewport *vp = ogre_->getRoot()->getAutoCreatedWindow()->addViewport(cam); |
|---|
| 304 | |
|---|
| 305 | |
|---|
| 306 | mgr->setAmbientLight(ColourValue(1,1,1)); |
|---|
| 307 | Entity* head = mgr->createEntity("head", "razor.mesh"); |
|---|
| 308 | SceneNode *node = mgr->getRootSceneNode()->createChildSceneNode("OgreHeadNode", Vector3(0,0,0)); |
|---|
| 309 | node->attachObject(head); |
|---|
| 310 | node->attachObject(cam); |
|---|
| 311 | mgr->setSkyBox(true, "Examples/SceneSkyBox2"); |
|---|
| 312 | |
|---|
| 313 | Entity* head1 = mgr->createEntity("head1", "ogrehead.mesh"); |
|---|
| 314 | SceneNode *node1 = mgr->getRootSceneNode()->createChildSceneNode("OgreHeadNode1", Vector3(200,0,0)); |
|---|
| 315 | node1->attachObject(head1); |
|---|
| 316 | Entity* head2 = mgr->createEntity("head2", "ogrehead.mesh"); |
|---|
| 317 | SceneNode *node2 = mgr->getRootSceneNode()->createChildSceneNode("OgreHeadNode2", Vector3(200,400,-100)); |
|---|
| 318 | node2->attachObject(head2); |
|---|
| 319 | Entity* head3 = mgr->createEntity("head3", "ogrehead.mesh"); |
|---|
| 320 | SceneNode *node3 = mgr->getRootSceneNode()->createChildSceneNode("OgreHeadNode3", Vector3(0,400,200)); |
|---|
| 321 | node3->attachObject(head3); |
|---|
| 322 | Entity* head4 = mgr->createEntity("head4", "ogrehead.mesh"); |
|---|
| 323 | SceneNode *node4 = mgr->getRootSceneNode()->createChildSceneNode("OgreHeadNode4", Vector3(-400,-200,600)); |
|---|
| 324 | node4->attachObject(head4); |
|---|
| 325 | Entity* head5 = mgr->createEntity("head5", "ogrehead.mesh"); |
|---|
| 326 | SceneNode *node5 = mgr->getRootSceneNode()->createChildSceneNode("OgreHeadNode5", Vector3(0,0,-400)); |
|---|
| 327 | node5->attachObject(head5); |
|---|
| 328 | |
|---|
| 329 | steering = new SpaceshipSteering(500, 200, 200, 200); |
|---|
| 330 | steering->addNode(node); |
|---|
| 331 | |
|---|
| 332 | } |
|---|
| 333 | |
|---|
| 334 | |
|---|
| 335 | void Orxonox::setupInputSystem() |
|---|
| 336 | { |
|---|
| 337 | size_t windowHnd = 0; |
|---|
| 338 | std::ostringstream windowHndStr; |
|---|
| 339 | OIS::ParamList pl; |
|---|
| 340 | Ogre::RenderWindow *win = ogre_->getRoot()->getAutoCreatedWindow(); |
|---|
| 341 | |
|---|
| 342 | win->getCustomAttribute("WINDOW", &windowHnd); |
|---|
| 343 | windowHndStr << windowHnd; |
|---|
| 344 | pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str())); |
|---|
| 345 | inputManager_ = OIS::InputManager::createInputSystem(pl); |
|---|
| 346 | |
|---|
| 347 | try |
|---|
| 348 | { |
|---|
| 349 | keyboard_ = static_cast<OIS::Keyboard*>(inputManager_->createInputObject(OIS::OISKeyboard, false)); |
|---|
| 350 | mouse_ = static_cast<OIS::Mouse*>(inputManager_->createInputObject(OIS::OISMouse, true)); |
|---|
| 351 | } |
|---|
| 352 | catch (const OIS::Exception &e) |
|---|
| 353 | { |
|---|
| 354 | throw new Ogre::Exception(42, e.eText, "OrxApplication::setupInputSystem"); |
|---|
| 355 | } |
|---|
| 356 | } |
|---|
| 357 | |
|---|
| 358 | // we actually want to do this differently... |
|---|
| 359 | void Orxonox::createFrameListener() |
|---|
| 360 | { |
|---|
| 361 | frameListener_ = new OrxListener(keyboard_, mouse_, auMan_); |
|---|
| 362 | ogre_->getRoot()->addFrameListener(frameListener_); |
|---|
| 363 | } |
|---|
| 364 | |
|---|
| 365 | void Orxonox::startRenderLoop() |
|---|
| 366 | { |
|---|
| 367 | ogre_->getRoot()->startRendering(); |
|---|
| 368 | } |
|---|
| 369 | } |
|---|
| 370 | |
|---|
| 371 | |
|---|