University of Texas at Austin CS 378 – Game Technology Don Fussell CS 378: Computer Game Technology Ogre Overview Spring 2012Initialize Configuration University of Texas at Austin CS 378 – Game Technology Don Fussell 2 !mRoot = new Ogre::Root(mPluginsCfg);!!// Load resource paths from config file!!Ogre::ConfigFile cf;!!cf.load(mResourcesCfg);!!// Parse all sections & settings in the file!!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);!!!}!!}!!// Show the configuration dialog and initialize the system!!if((mRoot->restoreConfig() || mRoot->showConfigDialog())) { !!!//If initialized, create a render window!!!mWindow = mRoot->initialise(true, ”Demo App");!!}!!else { return false; }!Initialize Resource Mgr and Scene University of Texas at Austin CS 378 – Game Technology Don Fussell 3 !// Set default mipmap level (note: some APIs ignore this)!!Ogre::TextureManager::getSingleton().setDefaultNumMipmaps(5);!!// initialise all resource groups!!Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();!!!// Create the SceneManager, in this case a generic one!!mSceneMgr = mRoot->createSceneManager("DefaultSceneManager");!!!// Create a scene!!mSceneMgr->setSkyBox(true, "Examples/MorningSkyBox", 5000, false);!!PlayingField* bCourt = new PlayingField(mSceneMgr);!!ball = new Ball(mSceneMgr);!!bCourt->addChild(ball->getNode());!!ball->setPlayingField(bCourt);!Setting up a Camera University of Texas at Austin CS 378 – Game Technology Don Fussell 4 // Create the camera!mCamera = mSceneMgr->createCamera("PlayerCam");!// Position it near front of the room!mCamera->setPosition(Ogre::Vector3(0.0f, 0.0f, 0.4f * bCourt->getLength()));!// Look at the origin!mCamera->lookAt(Ogre::Vector3(0,0,0));!mCamera->setNearClipDistance(5);!// create a default camera controller!mCameraMan = new OgreBites::SdkCameraMan(mCamera);!// Create one viewport, entire window!Ogre::Viewport* vp = mWindow->addViewport(mCamera);!vp->setBackgroundColour(Ogre::ColourValue(0,0,0));!// Alter the camera aspect ratio to match the viewport!mCamera->setAspectRatio(!Ogre::Real(vp->getActualWidth()) / Ogre::Real(vp->getActualHeight()));!Mouse and Keyboard Input University of Texas at Austin CS 378 – Game Technology Don Fussell 5 #include <OISEvents.h>!#include <OISInputManager.h>!#include <OISKeyboard.h>!#include <OISMouse.h>!#include <OgreRenderWindow.h>!#include <SdkTrays.h>!#include <SdkCameraMan.h>!class ControlListener : public OIS::KeyListener, public OIS::MouseListener {!protected:!!OIS::InputManager* inputManager;!!OIS::Mouse* mouse;!!OIS::Keyboard* keyboard;!!!Ogre::RenderWindow* window;!!Ogre::Camera* camera;!!OgreBites::SdkTrayManager* trayManager;!!OgreBites::SdkCameraMan* cameraManager;!!OgreBites::ParamsPanel* detailsPanel;!!bool exitCmd;!Mouse and Keyboard Input University of Texas at Austin CS 378 – Game Technology Don Fussell 6 protected:! // OIS::KeyListener! virtual bool keyPressed( const OIS::KeyEvent &arg );! virtual bool keyReleased( const OIS::KeyEvent &arg );! // OIS::MouseListener! 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 ); !!public:!!ControlListener(Ogre::RenderWindow* window);!!~ControlListener();!!void detach();!!void update();!!void resizeWindow(unsigned int width, unsigned int height);!!void setCamera(Ogre::Camera* cam);!!void setTrayManager(OgreBites::SdkTrayManager* tM);!!void setCameraManager(OgreBites::SdkCameraMan* cM);!!void setDetailsPanel(OgreBites::ParamsPanel* pp);!!OIS::Mouse* getMouse() { return mouse; }!!bool exit() { return exitCmd; }!};!Buffered Input Callbacks University of Texas at Austin CS 378 – Game Technology Don Fussell 7 bool ControlListener::keyReleased( const OIS::KeyEvent &arg ) {! cameraManager->injectKeyUp(arg);! return true;!}!bool ControlListener::mouseMoved( const OIS::MouseEvent &arg ) {! if (trayManager->injectMouseMove(arg)) return true;! cameraManager->injectMouseMove(arg);! return true;!}!bool ControlListener::mousePressed( const OIS::MouseEvent &arg, OIS::MouseButtonID id ) {! if (trayManager->injectMouseDown(arg, id)) return true;! cameraManager->injectMouseDown(arg, id);! return true;!}!bool ControlListener::mouseReleased( const OIS::MouseEvent &arg, OIS::MouseButtonID id ) {! if (trayManager->injectMouseUp(arg, id)) return true;! cameraManager->injectMouseUp(arg, id);! return true;!}!Buffered Input Callbacks University of Texas at Austin CS 378 – Game Technology Don Fussell 8 bool ControlListener::keyPressed( const OIS::KeyEvent &arg ) {! if (trayManager->isDialogVisible()) return true; // don't process any more keys if dialog is up! if (arg.key == OIS::KC_F) { // toggle visibility of advanced frame stats!!trayManager->toggleAdvancedFrameStats(); }! else if (arg.key == OIS::KC_G) { // toggle visibility of even rarer debugging details!!if (detailsPanel->getTrayLocation() == OgreBites::TL_NONE) {! trayManager->moveWidgetToTray(detailsPanel, OgreBites::TL_TOPRIGHT, 0);! detailsPanel->show();! } else {! trayManager->removeWidgetFromTray(detailsPanel);! detailsPanel->hide();! }! }!….! else if(arg.key == OIS::KC_F5) { // refresh all textures!!Ogre::TextureManager::getSingleton().reloadAll(); }! else if (arg.key == OIS::KC_SYSRQ) { // take a screenshot!!window->writeContentsToTimestampedFile("screenshot", ".jpg"); }! else if
View Full Document