GraphicsInteractionManager.cpp

Go to the documentation of this file.
00001 /*
00002  * GraphicsInteractionManager.cpp
00003  *
00004  *  Created on: Dec 20, 2011
00005  *      Author: sushil
00006  ----------------------------------------------------------------------------
00007 Filename:    GraphicsInteractionManager.cpp originally  named BaseApplication.cpp
00008 -----------------------------------------------------------------------------
00009 
00010 This source file is part of the
00011    ___                 __    __ _ _    _
00012   /___\__ _ _ __ ___  / / /\ \ (_) | _(_)
00013  //  // _` | '__/ _ \ \ \/  \/ / | |/ / |
00014 / \_// (_| | | |  __/  \  /\  /| |   <| |
00015 \___/ \__, |_|  \___|   \/  \/ |_|_|\_\_|
00016       |___/
00017       Tutorial Framework
00018       http://www.ogre3d.org/tikiwiki/
00019 -----------------------------------------------------------------------------
00020 */
00021 
00022 #include <OgreMeshManager.h>
00023 #include <OgreEntity.h>
00024 #include <OgreSceneNode.h>
00025 
00026 //#include <engine.h>
00027 #include <GraphicsInteractionManager.h>
00028 #include <uiMgr.h>
00029 #include <unitWeapons.h>
00030 #include <map>
00031 #include <SoundMgr.h>
00032 
00033 
00034 using namespace OgreGFX;
00035 
00036 //-------------------------------------------------------------------------------------
00037 GraphicsInteractionManager::GraphicsInteractionManager(FastEcslent::Engine* eng):
00038         mRoot(0),
00039         mSceneMgr(0),
00040         mWindow(0),
00041         mCamera(0),
00042         mResourcesCfg(Ogre::StringUtil::BLANK),
00043         mPluginsCfg(Ogre::StringUtil::BLANK),
00044         mCursorWasVisible(false),
00045         mShutDown(false)
00046 {
00047         this->engine = eng;
00048 }
00049 
00050 //-------------------------------------------------------------------------------------
00051 GraphicsInteractionManager::~GraphicsInteractionManager(void)
00052 {
00053     //if (mTrayMgr) delete mTrayMgr;
00054     //if (mCameraMan) delete mCameraMan;
00055 
00056     //Remove ourself as a Window listener
00057     Ogre::WindowEventUtilities::removeWindowEventListener(mWindow, this);
00058     windowClosed(mWindow);
00059     delete mRoot;
00060 }
00061 
00062 //-------------------------------------------------------------------------------------
00063 bool GraphicsInteractionManager::configure(void)
00064 {
00065     // Show the configuration dialog and initialise the system
00066     // You can skip this and use root.restoreConfig() to load configuration
00067     // settings if you were sure there are valid ones saved in ogre.cfg
00068         if (!(mRoot->restoreConfig() || mRoot->showConfigDialog())){
00069                 return false;
00070         }
00071         mWindow = mRoot->initialise(true, "FastEcslent::GraphicsInteractionManager's Render Window");
00072         return true;
00073 
00074 }
00075 //-------------------------------------------------------------------------------------
00076 void GraphicsInteractionManager::chooseSceneManager(void)
00077 {
00078     // Get the SceneManager, in this case a generic one
00079         //mSceneMgr = mRoot->createSceneManager(Ogre::ST_GENERIC);
00080         mSceneMgr = mRoot->createSceneManager("DefaultSceneManager");
00081 }
00082 //-------------------------------------------------------------------------------------
00083 void GraphicsInteractionManager::createCamera(void)
00084 {
00085     // Create the camera
00086     mCamera = mSceneMgr->createCamera("PlayerCam");
00087 
00088     // Position it at 500 in Z direction
00089     //mCamera->setPosition(Ogre::Vector3(100,500,100));
00090     // Look back along -Z
00091     //mCamera->lookAt(Ogre::Vector3(0,0,0));
00092     mCamera->setNearClipDistance(5);
00093 
00094     //mCameraMan = new OgreBites::SdkCameraMan(mCamera);   // create a default camera controller
00095 }
00096 //-------------------------------------------------------------------------------------
00097 //void GraphicsInteractionManager::createFrameListener(void)
00098 void GraphicsInteractionManager::createUIMgr(void)
00099 {
00100         uiMgr = new OgreGFX::UIMgr(this);
00101         mRoot->addFrameListener(uiMgr);
00102 }
00103 
00104 void GraphicsInteractionManager::createSoundMgr(void)
00105 {
00106         this->soundMgr = new OgreSND::SoundMgr(this);
00107         mRoot->addFrameListener(this->soundMgr);
00108         this->soundMgr->initialize();
00109 }
00110 
00111 void GraphicsInteractionManager::createWidgetMgr(void)
00112 {
00113         widgetMgr = new OgreGFX::WidgetMgr(this);
00114         mRoot->addFrameListener(widgetMgr);
00115 }
00116 
00117 void GraphicsInteractionManager::createParticleSystem(void)
00118 {
00119         explosionParticleSystem = mSceneMgr->createParticleSystem("explode", "explosionTemplate");
00120         explosionParticleSystem->setSpeedFactor(50);
00121 }
00122 
00123 
00124 /*void GraphicsInteractionManager::createInputSystem(void)
00125 {
00126         inputSystem = new FastEcslent::InputSystem(this, uiMgr->keyboard, uiMgr->mouse);
00127         mRoot->addFrameListener(inputSystem);
00128 }*/
00129 
00130 
00131 void GraphicsInteractionManager::loadGameGFX(void) {
00132         setupGame();
00133         mRoot->addFrameListener(this);
00134 }
00135 
00136 void GraphicsInteractionManager::createFrameListener(void) {
00137         mRoot->addFrameListener(this);
00138 }
00139 
00140 //-------------------------------------------------------------------------------------
00141 void GraphicsInteractionManager::destroyScene(void)
00142 {
00143 }
00144 //-------------------------------------------------------------------------------------
00145 void GraphicsInteractionManager::createViewports(void)
00146 {
00147     // Create one viewport, entire window
00148     Ogre::Viewport* vp = mWindow->addViewport(mCamera);
00149 
00150     vp->setVisibilityMask(OgreGFX::PLAYER_CAMERA_MASK);
00151     //vp->setBackgroundColour(Ogre::ColourValue(0,0,0));
00152     vp->setBackgroundColour(Ogre::ColourValue(0,0,0));
00153 
00154     // Alter the camera aspect ratio to match the viewport
00155     mCamera->setAspectRatio(Ogre::Real(vp->getActualWidth()) / Ogre::Real(vp->getActualHeight()));
00156 }
00157 //-------------------------------------------------------------------------------------
00158 void GraphicsInteractionManager::setupResources(void)
00159 {
00160     // Load resource paths from config file
00161     Ogre::ConfigFile cf;
00162     cf.load(mResourcesCfg);
00163 
00164     // Go through all sections & settings in the file
00165     Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator();
00166 
00167     Ogre::String secName, typeName, archName;
00168     while (seci.hasMoreElements())  {
00169         secName = seci.peekNextKey();
00170         Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext();
00171         Ogre::ConfigFile::SettingsMultiMap::iterator i;
00172         for (i = settings->begin(); i != settings->end(); ++i){
00173             typeName = i->first;
00174             archName = i->second;
00175             Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
00176                 archName, typeName, secName);
00177         }
00178     }
00179 }
00180 //-------------------------------------------------------------------------------------
00181 void GraphicsInteractionManager::createResourceListener(void)
00182 {
00183 
00184 }
00185 //-------------------------------------------------------------------------------------
00186 void GraphicsInteractionManager::loadResources(void)
00187 {
00188     Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
00189 }
00190 //-------------------------------------------------------------------------------------
00191 void GraphicsInteractionManager::go(void)
00192 {
00193 #ifdef _DEBUG
00194     mResourcesCfg = "config/resources_d.cfg";
00195     mPluginsCfg = "config/plugins_d.cfg";
00196 #else
00197     mResourcesCfg = "config/resources.cfg";
00198     mPluginsCfg = "config/plugins.cfg";
00199 #endif
00200 
00201     if (!setup())
00202         return;
00203 
00204     mRoot->startRendering();
00205 
00206     // clean up
00207     destroyScene();
00208 }
00209 //-------------------------------------------------------------------------------------
00210 void GraphicsInteractionManager::initGFXManagers(void) {
00211         uiMgr->initialize();
00212         widgetMgr->initialize();
00213         
00214         mRoot->addFrameListener(uiMgr);
00215 }
00216 
00217 bool GraphicsInteractionManager::setup(void)
00218 {
00219     mRoot = new Ogre::Root(mPluginsCfg);
00220     setupResources();
00221     bool carryOn = configure();
00222     if (!carryOn) return false;
00223     chooseSceneManager();
00224     createCamera();
00225     createViewports();
00226 
00227     // Set default mipmap level (NB some APIs ignore this)
00228     Ogre::TextureManager::getSingleton().setDefaultNumMipmaps(5);
00229     // Create any resource listeners (for loading screens)
00230     createResourceListener();
00231     // Load resources
00232     loadResources();
00233     createUIMgr();
00234     
00235     createSoundMgr();
00236 
00237     //create Lobby Manager
00238     createLobbyMgr();
00239 
00240     createParticleSystem();
00241 
00242     //Load Lobby GUI
00243     loadLobbyGFX();
00244 
00245     return true;
00246 };
00247 
00248 bool GraphicsInteractionManager::setupGame(void)
00249 {
00250         createScene();
00251     createWidgetMgr();
00252     
00253     createFrameListener();
00254 
00255     initGFXManagers();
00256 
00257     return true;
00258 };
00259 //-------------------------------------------------------------------------------------
00260 
00261 void GraphicsInteractionManager::handleBornEnts(){
00262         if (this->engine->entityMgr->nEnts > this->nGFXNodes) {
00263                 for (int i = nGFXNodes; i < this->engine->entityMgr->nEnts; i++){
00264                                 makeNode(this->engine->entityMgr->ents[i]);
00265                 }
00266                 this->nGFXNodes = engine->entityMgr->nEnts;
00267         }
00268 }
00269 
00270 
00271 void GraphicsInteractionManager::handleDevelopingEnts(){
00272 
00273 }
00274 
00275 
00276 bool GraphicsInteractionManager::frameRenderingQueued(const Ogre::FrameEvent& evt){
00277     if(mWindow->isClosed())
00278         return false;
00279     mShutDown = uiMgr->shutDown;
00280     if(mShutDown){
00281         if(uiMgr) {
00282                 uiMgr->kill();
00283                 delete uiMgr;
00284                 boost::mutex::scoped_lock scoped_lock(engine->quitLock);
00285                 engine->quit = true;
00286         }
00287         return false;
00288     }
00289 
00290     handleDevelopingEnts(); // create nodes
00291     handleBornEnts();       // move   nodes
00292     copySimState(); //handleLivingEnts
00293 
00294     return true;
00295 }
00296 
00297 //Adjust mouse clipping area
00298 void GraphicsInteractionManager::windowResized(Ogre::RenderWindow* rw){
00299            //check input
00300            unsigned int width, height, depth;
00301            int left, top;
00302            rw->getMetrics(width, height, depth, left, top);
00303 
00304            const OIS::MouseState &ms = uiMgr->mouse->getMouseState();
00305            ms.width = width;
00306            ms.height = height;
00307 }
00308 
00309 //Unattach OIS before window shutdown (very important under Linux)
00310 void GraphicsInteractionManager::windowClosed(Ogre::RenderWindow* rw){
00311     //Only close for window that created OIS (the main window in these demos)
00312     if( rw == mWindow ) {
00313         if(uiMgr) {
00314                 uiMgr->kill();
00315         }
00316     }
00317 }
00318 
00319 
00320 
00321 void GraphicsInteractionManager::makeInitialEntityNodes(){
00322         this->nGFXNodes = engine->entityMgr->nEnts;
00323         std::cout << "GFX: Number of ents: " << nGFXNodes << std::endl;
00324         for (int i = 0; i < nGFXNodes; i++){
00325                 makeNode(engine->entityMgr->ents[i]);
00326         }
00327 }
00328 
00329 
00330 void GraphicsInteractionManager::makeNode(FastEcslent::Entity *feEnt){
00331         std::cout << "Name: " << feEnt->uiname << std::endl;
00332         Ogre::Entity* ent = mSceneMgr->createEntity(feEnt->uiname, feEnt->meshName);
00333 
00334         if(feEnt->entityId.side == FastEcslent::RED && feEnt->entityClass == FastEcslent::SURFACE){
00335                 ent->setMaterialName("color2/SOLID/TEX/ecsl.bmp");
00336         }else if(feEnt->entityId.side == FastEcslent::BLUE &&  feEnt->entityClass == FastEcslent::SURFACE){
00337                 ent->setMaterialName("RED/SOLID/TEX/ecslDark.bmp/VertCol");
00338         }
00339 
00340         ent->setVisibilityFlags(OgreGFX::ENT_VISIBILITY_FLAG);
00341 //              if (engine->entityMgr->ents[i]->entityClass == FastEcslent::STATIC){
00342 //                      ent->setVisibilityFlags(OgreGFX::STATIC_VISIBILITY_FLAG);
00343 //              }
00344         std::cout << "GFX creating: " << feEnt->uiname << std::endl;
00345 
00346         Ogre::SceneNode* sceneNode = mSceneMgr->getRootSceneNode()->createChildSceneNode(feEnt->pos);
00347         std::stringstream ss;
00348         ss << feEnt->uiname << "_mini" ;
00349         Ogre::Entity* minimapEnt = mSceneMgr->createEntity(ss.str(), "cube.mesh" );
00350         minimapEnt->setVisibilityFlags(OgreGFX::MINIMAP_VISIBILITY_FLAG);
00351         Ogre::SceneNode* minimapNode = sceneNode->createChildSceneNode();
00352         minimapNode->attachObject(minimapEnt);
00353         minimapNode->setScale(2.0, 2.0, 2.0);
00354         int id = feEnt->entityId.id;
00355         gfxNodes[id].node = sceneNode;
00356         gfxNodes[id].node->attachObject(ent);
00357         gfxNodes[id].node->resetOrientation();
00358         gfxNodes[id].node->yaw(Ogre::Radian(feEnt->heading));
00359         gfxNodes[id].id = id;
00360         gfxNodes[id].selectable = feEnt->selectable;
00361         gfxNodes[id].entType = feEnt->entityType;
00362         sceneNodeToEntIdMap[sceneNode] = id;
00363 }
00364 
00365 
00366 void GraphicsInteractionManager::createScene(){
00367 
00368         //this->mShutDown = false;
00369         Ogre::Entity*         mOceanSurfaceEnt;
00370         makeInitialEntityNodes();
00371 
00372         //mSceneMgr->setAmbientLight(Ogre::ColourValue(0.5, 0.5, 0.5));
00373         mSceneMgr->setAmbientLight(Ogre::ColourValue(0.8, 0.8, 0.8));
00374         Ogre::Light* l1 = mSceneMgr->createLight("LightOne");
00375         l1->setPosition(20, 80, 50);
00376 
00377         //mSceneMgr->setSkyBox(true, "SkyBox", 1000);
00378         mSceneMgr->setSkyDome(true, "Examples/CloudySky", 10, 8);
00379         //mSceneMgr->setSkyBox(true, "Examples/CloudyNoonSkyBox", 50000, false);
00380 
00381 
00382     // Define a plane mesh that will be used for the ocean surface
00383         //Ogre::Plane oceanSurface;
00384     oceanSurface.normal = Ogre::Vector3::UNIT_Y;
00385     oceanSurface.d = 0;
00386     Ogre::MeshManager::getSingleton().createPlane("OceanSurface",
00387         Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
00388         oceanSurface,
00389         10000, 10000, 50, 50, true, 1, 1, 1, Ogre::Vector3::UNIT_Z);
00390 
00391     mOceanSurfaceEnt = mSceneMgr->createEntity( "OceanSurface", "OceanSurface" );
00392     mSceneMgr->getRootSceneNode()->createChildSceneNode()->attachObject(mOceanSurfaceEnt);
00393     mOceanSurfaceEnt->setCastShadows(false);
00394     mOceanSurfaceEnt->setMaterialName("OceanHLSL_GLSL");
00395     //mOceanSurfaceEnt->setMaterialName("OceanCg");
00396     //mOceanSurfaceEnt->setMaterialName("Ocean2_Cg");
00397 }
00398 
00399 void GraphicsInteractionManager::explode(Ogre::SceneNode *node){
00400         explosionParticleSystem->getEmitter(0)->setEnabled(true);
00401         node->detachObject(explosionParticleSystem);
00402         node->attachObject(explosionParticleSystem);
00403 }
00404 
00405 void GraphicsInteractionManager::copySimState(){
00406 
00407         for(int i = 0; i < nGFXNodes; i++){
00408                 //std::cout << engine->entityMgr->ents[i]->pos.x << ", " << engine->entityMgr->ents[0]->pos.y << ", " <<  engine->entityMgr->ents[0]->pos.z << std::endl;
00409                 gfxNodes[i].node->setPosition(engine->entityMgr->ents[i]->pos);//.x, engine->entityMgr->ents[0]->pos.y, engine->entityMgr->ents[0]->pos.z);
00410                 gfxNodes[i].node->resetOrientation();
00411                 gfxNodes[i].node->yaw(Ogre::Radian(engine->entityMgr->ents[i]->heading));
00412                 gfxNodes[i].selectable = engine->entityMgr->ents[i]->selectable;
00413 
00414                 //Check the entity state, if the entity is being attacked, show the explosion on the entity and reset the flag.
00415                 FastEcslent::Weapon *uw = dynamic_cast<FastEcslent::Weapon *>(engine->entityMgr->ents[i]->getAspect(FastEcslent::WEAPON));
00416                 if(uw->isBeingAttacked()){
00417                         uw->resetBeingAttack();
00418                         this->explode(gfxNodes[i].node);
00419                         this->soundMgr->playExplosionSound(&(gfxNodes[i]));
00420                 }
00421         }
00422 }
00423 
00424 void GraphicsInteractionManager::createLobbyMgr()
00425 {
00426         lobbyMgr = new LobbyMgr(engine, this, uiMgr->mouse, uiMgr->keyboard);
00427 }
00428 
00429 void GraphicsInteractionManager::loadLobbyGFX(void) {
00430         mRoot->addFrameListener(lobbyMgr);
00431 }

Generated on Fri Dec 13 14:54:17 2013 for FastECSLent by  doxygen 1.5.4