TutorialApplication.h
#ifndef TUTORIALAPPLICATION_H
#define TUTORIALAPPLICATION_H
#include <OgreRoot.h>
#include <OgreWindowEventUtilities.h>
#include <OISEvents.h>
#include <OISInputManager.h>
#include <OISKeyboard.h>
#include <OISMouse.h>
class TutorialApplication
: public Ogre::WindowEventListener,
public Ogre::FrameListener
{
public:
TutorialApplication();
virtual ~TutorialApplication();
bool go();
private:
virtual bool frameRenderingQueued(const Ogre::FrameEvent& fe);
virtual void windowResized(Ogre::RenderWindow* rw);
virtual void windowClosed(Ogre::RenderWindow* rw);
Ogre::Root* mRoot;
Ogre::String mResourcesCfg;
Ogre::String mPluginsCfg;
Ogre::RenderWindow* mWindow;
Ogre::SceneManager* mSceneMgr;
Ogre::Camera* mCamera;
OIS::InputManager* mInputMgr;
OIS::Keyboard* mKeyboard;
OIS::Mouse* mMouse;
};
#endif
TutorialApplication.cpp
#include "TutorialApplication.h"
#include <OgreEntity.h>
#include <OgreCamera.h>
#include <OgreViewport.h>
#include <OgreSceneManager.h>
#include <OgreRenderWindow.h>
#include <OgreConfigFile.h>
#include <OgreException.h>
TutorialApplication::TutorialApplication()
: mRoot(0),
mResourcesCfg(Ogre::StringUtil::BLANK),
mPluginsCfg(Ogre::StringUtil::BLANK),
mWindow(0),
mSceneMgr(0),
mCamera(0),
mInputMgr(0),
mMouse(0),
mKeyboard(0)
{
}
TutorialApplication::~TutorialApplication()
{
Ogre::WindowEventUtilities::removeWindowEventListener(mWindow, this);
windowClosed(mWindow);
delete mRoot;
}
bool TutorialApplication::go()
{
#ifdef _DEBUG
mResourcesCfg = "resources_d.cfg";
mPluginsCfg = "plugins_d.cfg";
#else
mResourcesCfg = "resources.cfg";
mPluginsCfg = "plugins.cfg";
#endif
mRoot = new Ogre::Root(mPluginsCfg);
Ogre::ConfigFile cf;
cf.load(mResourcesCfg);
Ogre::String name, locType;
Ogre::ConfigFile::SectionIterator secIt = cf.getSectionIterator();
while (secIt.hasMoreElements())
{
Ogre::ConfigFile::SettingsMultiMap* settings = secIt.getNext();
Ogre::ConfigFile::SettingsMultiMap::iterator it;
for (it = settings->begin(); it != settings->end(); ++it)
{
locType = it->first;
name = it->second;
Ogre::ResourceGroupManager::getSingleton().addResourceLocation(name, locType);
}
}
if (!(mRoot->restoreConfig() || mRoot->showConfigDialog()))
return false;
mWindow = mRoot->initialise(true, "TutorialApplication Render Window");
Ogre::TextureManager::getSingleton().setDefaultNumMipmaps(5);
Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
mSceneMgr = mRoot->createSceneManager(Ogre::ST_GENERIC);
mCamera = mSceneMgr->createCamera("MainCam");
mCamera->setPosition(0, 0, 80);
mCamera->lookAt(0, 0, -300);
mCamera->setNearClipDistance(5);
Ogre::Viewport* vp = mWindow->addViewport(mCamera);
vp->setBackgroundColour(Ogre::ColourValue(0, 0, 0));
mCamera->setAspectRatio(
Ogre::Real(vp->getActualWidth()) /
Ogre::Real(vp->getActualHeight()));
// Create Scene
Ogre::Entity* ogreEntity = mSceneMgr->createEntity("ogrehead.mesh");
Ogre::SceneNode* ogreNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();
ogreNode->attachObject(ogreEntity);
mSceneMgr->setAmbientLight(Ogre::ColourValue(.5, .5, .5));
Ogre::Light* light = mSceneMgr->createLight("MainLight");
light->setPosition(20, 80, 50);
// OIS
Ogre::LogManager::getSingletonPtr()->logMessage("*** Initializing OIS ***");
OIS::ParamList pl;
size_t windowHandle = 0;
std::ostringstream windowHandleStr;
mWindow->getCustomAttribute("WINDOW", &windowHandle);
windowHandleStr << windowHandle;
pl.insert(std::make_pair(std::string("WINDOW"), windowHandleStr.str()));
mInputMgr = OIS::InputManager::createInputSystem(pl);
mKeyboard = static_cast<OIS::Keyboard*>(
mInputMgr->createInputObject(OIS::OISKeyboard, false));
mMouse = static_cast<OIS::Mouse*>(
mInputMgr->createInputObject(OIS::OISMouse, false));
windowResized(mWindow);
Ogre::WindowEventUtilities::addWindowEventListener(mWindow, this);
mRoot->addFrameListener(this);
mRoot->startRendering();
return true;
}
bool TutorialApplication::frameRenderingQueued(const Ogre::FrameEvent& fe)
{
if (mWindow->isClosed()) return false;
mKeyboard->capture();
mMouse->capture();
if (mKeyboard->isKeyDown(OIS::KC_ESCAPE)) return false;
return true;
}
void TutorialApplication::windowResized(Ogre::RenderWindow* rw)
{
int left, top;
unsigned int width, height, depth;
rw->getMetrics(width, height, depth, left, top);
const OIS::MouseState& ms = mMouse->getMouseState();
ms.width = width;
ms.height = height;
}
void TutorialApplication::windowClosed(Ogre::RenderWindow* rw)
{
if(rw == mWindow)
{
if(mInputMgr)
{
mInputMgr->destroyInputObject(mMouse);
mInputMgr->destroyInputObject(mKeyboard);
OIS::InputManager::destroyInputSystem(mInputMgr);
mInputMgr = 0;
}
}
}
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
#define WIN32_LEAN_AND_MEAN
#include "windows.h"
#endif
#ifdef __cplusplus
extern "C"
{
#endif
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )
#else
int main(int argc, char *argv[])
#endif
{
TutorialApplication app;
try
{
app.go();
}
catch(Ogre::Exception& e)
{
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
MessageBox(
NULL,
e.getFullDescription().c_str(),
"An exception has occured!",
MB_OK | MB_ICONERROR | MB_TASKMODAL);
#else
std::cerr << "An exception has occured: " <<
e.getFullDescription().c_str() << std::endl;
#endif
}
return 0;
}
#ifdef __cplusplus
}
#endif