一个简单的 Ogre FrameWork

来源:互联网 发布:心理学书籍推荐知乎 编辑:程序博客网 时间:2024/05/18 00:37

///FrameApp.h

 

#ifndef _FRAME_APP_H
#define _FRAME_APP_H


class World;

class App
{
public:
 App(void);
 ~App(void);

 void go(void);
private:
 World* m_pWorld;
};
#endif // _FRAME_APP_H

 

///FrameApp.cpp

 

#include <Ogre.h>
#include "FrameApp.h"
#include "FrameWorld.h"

App::App():
m_pWorld(0)
{
}


App::~App()
{
 if(m_pWorld != NULL)
 {
  delete m_pWorld;
  m_pWorld = NULL;
 }
}


void App::go()
{
 m_pWorld = new World();
 assert(NULL != m_pWorld);
 if(NULL == m_pWorld)
 {
  return;
 }
 m_pWorld->createWorld();
 m_pWorld->m_pRoot->startRendering();
}


INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )
{
 App app;
 try
 {
  app.go();
 }
 catch(Ogre::Exception& e)
 {
  MessageBox(NULL, e.getFullDescription().c_str(), "Exception", MB_OK | MB_ICONERROR | MB_TASKMODAL);
 }
}

///FrameListener.h

 

#ifndef _FRAME_LISTENER_H
#define _FRAME_LISTENER_H
#include <Ogre.h>
#include <OIS/Ois.h>

class World;

class Listener : public Ogre::FrameListener, public OIS::KeyListener, OIS::MouseListener
{

public:
 Listener(World* pWorld);
 ~Listener();

 void initialise(void);
private:
 virtual bool mouseMoved ( const OIS::MouseEvent &arg );
 virtual bool mousePressed ( const OIS::MouseEvent &arg, OIS::MouseButtonID id );
 virtual bool mouseReleased ( const OIS::MouseEvent &arg, OIS::MouseButtonID id );

 virtual bool keyPressed ( const OIS::KeyEvent &arg );
 virtual bool keyReleased ( const OIS::KeyEvent &arg );

 bool frameStarted (const Ogre::FrameEvent &evt);
 bool frameEnded (const Ogre::FrameEvent &evt );   
 bool processKeyInput(const Ogre::FrameEvent& evt);
 bool processMouseInput(const Ogre::FrameEvent& evt);
private:
 World       *m_pWorld;

 //OIS Input devices
 OIS::InputManager    *m_pInputManager;
 OIS::Mouse      *m_pMouse;
 OIS::Keyboard     *m_pKeyboard;

 Ogre::Radian   m_RotX, m_RotY;
};


#endif //_FRAME_LISTENER_H

 

///FrameListener.cpp

 

#include "FrameListener.h"
#include "FrameWorld.h"

Listener::Listener(World* pWorld)
: m_pWorld(pWorld),
m_pInputManager(0),
m_pMouse(0),
m_pKeyboard(0)
{


}


Listener::~Listener()
{
 if (m_pWorld->m_pWindow)
 {
  m_pInputManager->destroyInputObject(m_pMouse);
  m_pInputManager->destroyInputObject(m_pKeyboard);

  OIS::InputManager::destroyInputSystem(m_pInputManager);
  m_pInputManager = 0;
 }
}

void Listener::initialise(void)
{

 OIS::ParamList pl;
 size_t windowHnd = 0;
 std::ostringstream windowHndStr;

 m_pWorld->m_pWindow->getCustomAttribute("WINDOW", &windowHnd);
 windowHndStr << windowHnd;
 pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str()));

 m_pInputManager = OIS::InputManager::createInputSystem( pl );

 // Create all devices (We only catch joystick exceptions here, as, most people have Key/Mouse)
 m_pKeyboard = static_cast<OIS::Keyboard*>(m_pInputManager->createInputObject( OIS::OISKeyboard, false ));
 m_pMouse = static_cast<OIS::Mouse*>(m_pInputManager->createInputObject( OIS::OISMouse, false ));

 unsigned int width, height, depth;
 int left, top;
 m_pWorld->m_pWindow->getMetrics(width, height, depth, left, top);

 const OIS::MouseState &ms = m_pMouse->getMouseState();
 ms.width = width;
 ms.height = height;

 m_pMouse->setEventCallback(this);
 m_pKeyboard->setEventCallback(this);
}

bool Listener::frameStarted(const Ogre::FrameEvent &evt)
{

 if (m_pWorld->m_pWindow->isClosed())
  return false;

 m_RotX = m_RotY = 0.0f;

 //Need to capture/update each device
 m_pKeyboard->capture();
 m_pMouse->capture();

 //Check to see which device is not buffered, and handle it
 if(!processKeyInput(evt))
  return false;

 if (!processMouseInput(evt))
  return false;

 //if(!m_pMouse->buffered())
  //moveCamera(); 

 return true;
}


bool Listener::frameEnded(const Ogre::FrameEvent &evt)
{
 if(m_pKeyboard->isKeyDown(OIS::KC_SYSRQ))
 {
  std::ostringstream ss;

  time_t ltime;
  time(&ltime);
  struct tm* now = localtime(&ltime);
  char szDate[128];
  strftime(szDate, 128, "%Y_%m_%d_%H_%M_%S/0", now);
  ss << "screenshot_" << szDate << ".png";
  m_pWorld->m_pWindow->writeContentsToFile(ss.str());
 }

 return true;


bool Listener::processKeyInput(const Ogre::FrameEvent& evt)
{
 if (m_pKeyboard->isKeyDown(OIS::KC_ESCAPE))///关闭窗口
  return false;


 if (m_pKeyboard->isKeyDown(OIS::KC_F12))///最大化窗口
 {
  static bool s_bFullScreen = false;
  s_bFullScreen = !s_bFullScreen;
  m_pWorld->m_pWindow->setFullscreen(s_bFullScreen, 800, 600);
 }

 return true;
}


bool Listener::processMouseInput(const Ogre::FrameEvent& evt)
{
 const float factor = -0.5f;
 const OIS::MouseState &ms = m_pMouse->getMouseState();
    if (ms.buttonDown(OIS::MB_Left))
 {
  m_RotX = Ogre::Degree(-ms.X.rel * factor);
  m_RotY = Ogre::Degree(-ms.Y.rel * factor);
 }

 if(ms.Z.rel)///camera Zoom in/Zoom out
  m_pWorld->m_pCamera->moveRelative(Ogre::Vector3(0, 0,ms.Z.rel * -0.5f));

 return true;
}

bool Listener::mouseMoved (const OIS::MouseEvent &arg)
{
 return true;
}

bool Listener::mousePressed ( const OIS::MouseEvent &arg, OIS::MouseButtonID id )
{
 return true;
}

bool Listener::mouseReleased ( const OIS::MouseEvent &arg, OIS::MouseButtonID id )
{
 return true;
}

bool Listener::keyPressed ( const OIS::KeyEvent &arg )
{
 return true;
}

bool Listener::keyReleased ( const OIS::KeyEvent &arg )
{
 return true;
}

 

///FrameWorld.h

 

#ifndef _FRAME_WORLD_H__
#define _FRAME_WORLD_H__

#include <Ogre.h>

class Listener;

class World
{  
public:
 World();
 ~World();

 bool createWorld();
private:
 bool setupResources();
 bool chooseSceneManager();
 bool createCamera();
 bool createViewports();
 bool loadRes();
 bool createScene();
 bool createFrameListener();

public:
 Ogre::Root      *m_pRoot;
 Ogre::RenderWindow    *m_pWindow;
 Ogre::SceneManager    *m_pSceneMgr;
 Ogre::Camera     *m_pCamera;

 Listener         *m_pListener;
};

#endif  //_FRAME_WORLD_H__

 

///FrameWorld.cpp

 

#include "FrameWorld.h"
#include "FrameListener.h"


World::World():
m_pRoot(0),
m_pWindow(0),
m_pSceneMgr(0),
m_pCamera(0)
{

}

World::~World()
{
 if(m_pRoot != NULL)
 {
  delete m_pRoot;
  m_pRoot = NULL;
 }
}

bool World::createWorld()
{
 ///读取所需资源
 if(!setupResources())
  return false;

 ///创建场景管理器
 if(!chooseSceneManager())
  return false;

 //Ogre::TextureManager::getSingleton().setDefaultNumMipmaps(0);

 ///创建摄像机
 if(!createCamera())
  return false;

 ///创建视口
 if(!createViewports())
  return false;

 ///载入资源
 if(!loadRes())
  return false;

 ///创建场景
 if(!createScene())
  return false;

 ///创建监听器
 if(!createFrameListener())
  return false;

 return true;
}

bool World::setupResources()
{
 m_pRoot = new Ogre::Root("", "", "ogre.log");

 assert(NULL != m_pRoot);
 if(NULL == m_pRoot)
  return false;


#ifdef _DEBUG
 m_pRoot->loadPlugin("RenderSystem_Direct3D9_d");
#else
 m_pRoot->loadPlugin("RenderSystem_Direct3D9");
#endif

///////////////////////////////////////////////////////////////////
 Ogre::ConfigFile cf;
 cf.load("resources.cfg");

 Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator();
 Ogre::String secName, typeName, archName;
 while (seci.hasMoreElements())
 {
  secName = seci.peekNextKey();
  Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext();
  Ogre::ConfigFile::SettingsMultiMap::iterator i;
  for (i = settings->begin(); i != settings->end(); ++i)
  {
   typeName = i->first;
   archName = i->second;

   Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
    archName, typeName, secName);
  }
 }
//////////////////////////////////////////////////////////////////

 Ogre::RenderSystemList * rList = m_pRoot->getAvailableRenderers();

 Ogre::RenderSystemList::iterator it = rList->begin();

 Ogre::RenderSystem *rSys = 0;

 while(it != rList->end())
 {
  rSys = * (it++);
 }

 m_pRoot->setRenderSystem(rSys);

 m_pRoot->initialise(false);

 m_pWindow = m_pRoot->createRenderWindow("FrameWork", 800, 600, false,0);

 assert(NULL != m_pWindow);
 if(NULL == m_pWindow)
  return false;

 return true;
}


bool World::chooseSceneManager()
{
 m_pSceneMgr = m_pRoot->createSceneManager(Ogre::ST_GENERIC, "ExampleSMInstance");
 assert(NULL != m_pSceneMgr);
 if(NULL == m_pSceneMgr)
  return false;
 return true;
}

bool World::createCamera()
{
 m_pCamera = m_pSceneMgr->createCamera("mainCamera");
 assert(NULL != m_pCamera);
 if(NULL == m_pCamera)
  return false;
 m_pCamera->setPosition(Ogre::Vector3(0,600,800));
 m_pCamera->lookAt(Ogre::Vector3(0,0,0));
 m_pCamera->setNearClipDistance(5);
 return true;
}

bool World::createViewports()
{
 Ogre::Viewport* vp = m_pWindow->addViewport(m_pCamera);
 assert(NULL != vp);
 if(NULL == vp)
  return false;
 vp->setBackgroundColour(Ogre::ColourValue(0,0,0));
 return true;
}

bool World::createFrameListener()
{
 m_pListener = new Listener( this);
 assert(NULL != m_pListener);
 if(NULL == m_pListener)
  return false;

 m_pListener->initialise();
 m_pRoot->addFrameListener(m_pListener);
 return true;
}

bool World::loadRes()
{
 Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();

 return true;
}

bool World::createScene()
{
 /// Give it a little ambience with lights
 Ogre::Light* l;
 l = m_pSceneMgr->createLight("BlueLight");
 l->setType(Ogre::Light::LT_SPOTLIGHT);
 l->setPosition(0,5000,0);
 Ogre::Vector3 dir(-l->getPosition());
 dir.normalise();
 l->setDirection(dir);
 l->setDiffuseColour(0.5, 0.5, 1.0);

 Ogre::Entity * ogreHead = m_pSceneMgr->createEntity("ogreHead","ogrehead.mesh");
 Ogre::SceneNode * ogreNode = m_pSceneMgr->getRootSceneNode()->createChildSceneNode("ogreNode");
 ogreNode->attachObject(ogreHead);

 return true;
}

原创粉丝点击