AppFrame.h
/* ===================================================== */
//
// ◢█████◣ ◢██████◣
// ◢◤ ◥◣ ◢◤ ◥◣
// ◤ ◥◣ ◢◤ █
// ▎ ◢█◣ ◥◣◢◤ ◢█◣ █
// ◣ ◢◤ ◥◣ ◢◣ ◥◣ ◢
// ◥██◤ ◢◤ ◥◣ ◥█◤
// █ ● ● █
// █ 〃 ▄ 〃 █
// ◥◣ ╚╩╝ ◢◤
// ◥█▅▃▃ ▃▃▅█◤
// ◢◤ ◥◣
// █ █
// ◢◤▕ ▎◥◣
// ▕▃◣◢▅▅▅◣◢▃▎
//
// Author : Wangxu
// Create Date :
// Description :
//
/* ===================================================== */
#ifndef __APPFRAME_H__
#define __APPFRAME_H__
#include <Ogre/Ogre.h>
#include <OIS/OIS.h>
#pragma comment(lib,"OgreMain_d.lib")
#pragma comment(lib,"OIS_d.lib")
namespace GC
{
class AppFrame : public Ogre::FrameListener,
public OIS::KeyListener,
public OIS::MouseListener
{
public:
AppFrame(const char* pszPlugins = "plugins.cfg",const char* pszConfig = "ogre.cfg",const char* pszLog = "Ogre.log");
//AppFrame(const std::vector& plugins);
bool initializeFrame(bool bShowDialog = true);
void loadResource(const char* pszResource);
void setup();
void run();
virtual void postSetup() = 0;
/************************************************************************/
/* Implement Interface in FrameListener */
/************************************************************************/
virtual bool frameStarted(const Ogre::FrameEvent& evt);
virtual bool frameRenderingQueued(const Ogre::FrameEvent& evt);
virtual bool frameEnded(const Ogre::FrameEvent& evt);
/************************************************************************/
/* Implement Interface in Key & MouseListener */
/************************************************************************/
virtual bool keyPressed(const OIS::KeyEvent &arg);
virtual bool keyReleased(const OIS::KeyEvent &arg);
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 );
protected:
Ogre::Root* mRoot;
Ogre::RenderWindow* mWindow;
Ogre::SceneManager* mSceneMgr;
Ogre::Camera* mCamera;
Ogre::Viewport* mViewport;
OIS::Keyboard* mKeyboard;
};
}
#endif
/* ===================================================== */
//
// ◢█████◣ ◢██████◣
// ◢◤ ◥◣ ◢◤ ◥◣
// ◤ ◥◣ ◢◤ █
// ▎ ◢█◣ ◥◣◢◤ ◢█◣ █
// ◣ ◢◤ ◥◣ ◢◣ ◥◣ ◢
// ◥██◤ ◢◤ ◥◣ ◥█◤
// █ ● ● █
// █ 〃 ▄ 〃 █
// ◥◣ ╚╩╝ ◢◤
// ◥█▅▃▃ ▃▃▅█◤
// ◢◤ ◥◣
// █ █
// ◢◤▕ ▎◥◣
// ▕▃◣◢▅▅▅◣◢▃▎
//
// Author : Wangxu
// Create Date :
// Description :
//
/* ===================================================== */
#include "AppFrame.h"
using namespace Ogre;
GC::AppFrame::AppFrame( const char* pszPlugins /*= "plugins.cfg"*/,const char* pszConfig /*= "ogre.cfg"*/,const char* pszLog /*= "Ogre.log"*/ )
{
mRoot = OGRE_NEW Ogre::Root(pszPlugins,pszConfig,pszLog);
}
bool GC::AppFrame::initializeFrame( bool bShowDialog /*= true*/ )
{
if(true)
{
return mRoot->showConfigDialog();
}
else
{
return mRoot->restoreConfig();
}
}
void GC::AppFrame::loadResource( const char* pszResource )
{
Ogre::ConfigFile cf;
cf.load(pszResource);
Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator();
Ogre::String sec, type, arch;
// go through all specified resource groups
while (seci.hasMoreElements())
{
sec = seci.peekNextKey();
Ogre::ConfigFile::SettingsMultiMap* settings = seci.getNext();
Ogre::ConfigFile::SettingsMultiMap::iterator i;
// go through all resource paths
for (i = settings->begin(); i != settings->end(); i++)
{
type = i->first;
arch = i->second;
Ogre::ResourceGroupManager::getSingleton().addResourceLocation(arch, type, sec);
}
}
}
void GC::AppFrame::setup()
{
mWindow = mRoot->initialise(true);
Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
//Ogre::ResourceGroupManager::getSingleton().initialiseResourceGroup("Popular");
mSceneMgr = mRoot->createSceneManager(Ogre::ST_GENERIC);
mCamera = mSceneMgr->createCamera("MainCamera");
mViewport = mWindow->addViewport(mCamera);
OIS::ParamList pl;
size_t winHandle = 0;
std::ostringstream winHandleStr;
mWindow->getCustomAttribute("WINDOW", &winHandle);
winHandleStr << winHandle;
pl.insert(std::make_pair("WINDOW", winHandleStr.str()));
OIS::InputManager* mInputMgr = OIS::InputManager::createInputSystem(pl);
mKeyboard = static_cast<OIS::Keyboard*>(mInputMgr->createInputObject(OIS::OISKeyboard, true));
/************************************************************************/
/* 注册回调事件 */
/************************************************************************/
mKeyboard->setEventCallback(this);
mRoot->addFrameListener(this);
postSetup();
}
void GC::AppFrame::run()
{
mRoot->startRendering();
}
bool GC::AppFrame::frameStarted( const Ogre::FrameEvent& evt )
{
// whether main window(the only one) closed,if so,quit
if(mWindow->isClosed())
{
return false;
}
mKeyboard->capture();
return true;
}
bool GC::AppFrame::keyPressed( const OIS::KeyEvent &arg )
{
return true;
}
bool GC::AppFrame::keyReleased( const OIS::KeyEvent &arg )
{
return true;
}
bool GC::AppFrame::mouseMoved( const OIS::MouseEvent &arg )
{
return true;
}
bool GC::AppFrame::mousePressed( const OIS::MouseEvent &arg, OIS::MouseButtonID id )
{
return true;
}
bool GC::AppFrame::mouseReleased( const OIS::MouseEvent &arg, OIS::MouseButtonID id )
{
return true;
}
bool GC::AppFrame::frameEnded( const Ogre::FrameEvent& evt )
{
return true;
}
bool GC::AppFrame::frameRenderingQueued( const Ogre::FrameEvent& evt )
{
return true;
}
用法,继承AppFrame类,并实现一个
postSetup
方法
比如:
TextureFXFrame.h
/* ===================================================== */
//
// ◢█████◣ ◢██████◣
// ◢◤ ◥◣ ◢◤ ◥◣
// ◤ ◥◣ ◢◤ █
// ▎ ◢█◣ ◥◣◢◤ ◢█◣ █
// ◣ ◢◤ ◥◣ ◢◣ ◥◣ ◢
// ◥██◤ ◢◤ ◥◣ ◥█◤
// █ ● ● █
// █ 〃 ▄ 〃 █
// ◥◣ ╚╩╝ ◢◤
// ◥█▅▃▃ ▃▃▅█◤
// ◢◤ ◥◣
// █ █
// ◢◤▕ ▎◥◣
// ▕▃◣◢▅▅▅◣◢▃▎
//
// Author : Wangxu
// Create Date :
// Description :
//
/* ===================================================== */
#include "AppFrame.h"
using namespace Ogre;
namespace GC
{
class TextureFXFrame : public AppFrame
{
public:
TextureFXFrame(const char* pszPlugins = "plugins.cfg",const char* pszConfig = "ogre.cfg",const char* pszLog = "Ogre.log")
: AppFrame(pszPlugins,pszConfig,pszLog)
{
}
virtual void postSetup()
{
/************************************************************************/
/* TextureFxFrame */
/************************************************************************/
mCamera->setAspectRatio((Ogre::Real)mViewport->getActualWidth() / (Ogre::Real)mViewport->getActualHeight());
mCamera->setNearClipDistance(5);
mCamera->setPosition(Vector3(0,38,144));
mCamera->setOrientation(Quaternion(0.991,-0.13,0,0));
mSceneMgr->setSkyBox(true, "Examples/TrippySkyBox");
// the names of the four materials we will use
String matNames[] = {"Examples/OgreDance", "Examples/OgreParade", "Examples/OgreSpin", "Examples/OgreWobble"};
for (unsigned int i = 0; i < 4; i++)
{
// create a standard plane entity
Entity* ent = mSceneMgr->createEntity("Plane" + StringConverter::toString(i + 1), SceneManager::PT_PLANE);
// attach it to a node, scale it, and position appropriately
SceneNode* node = mSceneMgr->getRootSceneNode()->createChildSceneNode();
node->setPosition(i % 2 ? 25 : -25, i / 2 ? -25 : 25, 0);
node->setScale(0.25, 0.25, 0.25);
node->attachObject(ent);
ent->setMaterialName(matNames[i]); // give it the material we prepared
}
}
};
}
main.cpp
/* ===================================================== */
//
// ◢█████◣ ◢██████◣
// ◢◤ ◥◣ ◢◤ ◥◣
// ◤ ◥◣ ◢◤ █
// ▎ ◢█◣ ◥◣◢◤ ◢█◣ █
// ◣ ◢◤ ◥◣ ◢◣ ◥◣ ◢
// ◥██◤ ◢◤ ◥◣ ◥█◤
// █ ● ● █
// █ 〃 ▄ 〃 █
// ◥◣ ╚╩╝ ◢◤
// ◥█▅▃▃ ▃▃▅█◤
// ◢◤ ◥◣
// █ █
// ◢◤▕ ▎◥◣
// ▕▃◣◢▅▅▅◣◢▃▎
//
// Author : Wangxu
// Create Date :
// Description :
//
/* ===================================================== */
#pragma warning(disable: 4251)
#pragma warning(disable: 4193)
#pragma warning(disable: 4275)
#include "TextureFXFrame.h"
int WINAPI WinMain( __in HINSTANCE hInstance, __in_opt HINSTANCE hPrevInstance, __in LPSTR lpCmdLine, __in int nShowCmd )
{
GC::AppFrame* app = new GC::TextureFXFrame();
if(app->initializeFrame())
{
app->loadResource("resources.cfg");
app->setup();
app->run();
}
return 0;
}