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

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