原文:http://blog.csdn.net/zhuxiaoyang2000/article/details/6324080
本文參考Ogre官網上的MinimalApplication寫了一個最簡單的Ogre程序,不包含任何資源文件,當然了,也沒有任何功能: )。本文寫作的初衷是:
(1)ExampleListener/Application包含的內容過於豐富,不便於在此基礎上寫出簡單的程序應用,如2D程序。它需要的資源文件和相應的配置文件對於簡單的程序顯得臃腫。
(2)現有的MinimalApplication混淆了Listener和Application,而且將所有文件寫到了一個程序裏面,文檔結構不清晰。
(3)更重要的是通過自己寫這樣一個程序,能夠了解Ogre建立的詳細過程,有助於理解Ogre的框架。
閒言少敘,進入正題。建立一個Win32程序,程序包括三個文件,分別是SimpleListener.h,SimpleApplication.h和main.cpp。該程序使用的是Ogre 1.6.5版本,1.7.x版本可能需要修改相應的include文件。
程序需要包含庫OgreMain_d.lib、OIS_d.lib(Debug模式),或OgreMain.lib、OIS.lib(Release模式);需要包含dll文件OgreMain_d.dll、OIS_d.dll、RenderSystem_Direct3D9_d.dll(Debug模式),或OgreMain.dll、OIS.dll、RenderSystem_Direct3D9.dll(Release模式)。
程序需要建立一個空文件resources.cfg。
程序需要建立一個文件Plugins.cfg,其內容爲
- # Defines plugins to load
- # Define plugin folder
- PluginFolder=.
- # Define plugins
- Plugin=RenderSystem_Direct3D9_d
下面是SimpleListener.h的內容。
- /*
- ======================================================================
- SimpleListener.h --- protoype to show off the simple ogre listener.
- ----------------------------------------------------------------------
- Author : Zhu Xiaoyang ([email protected])
- Creation Date : Apr 14 2011
- Description:
- This is a mini Ogre Listener, including SimpleFrameListener,
- SimpleKeyListener and SimpleMouseListener.
- =======================================================================
- */
- #ifndef __SimpleListener_H__
- #define __SimpleListener_H__
- #include "Ogre.h"
- #include "OgreFrameListener.h"
- #include <OIS/OIS.h>
- using namespace Ogre;
- /**
- ----------------------------------------------------------------------
- class SimpleFrameListener
- ----------------------------------------------------------------------
- */
- class SimpleFrameListener : public FrameListener
- {
- public:
- SimpleFrameListener(OIS::Keyboard* keyboard, OIS::Mouse* mouse)
- {
- mKeyboard = keyboard;
- mMouse = mouse;
- }
- //This gets called before the next frame is being rendered.
- bool frameStarted(const FrameEvent& evt)
- {
- //update the input devices
- mKeyboard->capture();
- mMouse->capture();
- //exit if key KC_ESCAPE pressed
- if( mKeyboard->isKeyDown( OIS::KC_ESCAPE ) )
- return false;
- else
- return true;
- }
- //This gets called at the end of a frame
- bool frameEnded(const FrameEvent& evt)
- {
- return true;
- }
- private:
- OIS::Keyboard* mKeyboard;
- OIS::Mouse* mMouse;
- };
- /**
- ----------------------------------------------------------------------
- class SimpleKeyListener
- ----------------------------------------------------------------------
- */
- class SimpleKeyListener : public OIS::KeyListener
- {
- public:
- bool keyPressed (const OIS::KeyEvent& e) { return true; }
- bool keyReleased (const OIS::KeyEvent& e) { return true; }
- };
- /**
- ----------------------------------------------------------------------
- class SimpleMouseListener
- ----------------------------------------------------------------------
- */
- class SimpleMouseListener : public OIS::MouseListener
- {
- public:
- bool mouseMoved (const OIS::MouseEvent& e) { return true; }
- bool mousePressed (const OIS::MouseEvent& e, OIS::MouseButtonID id) { return true; }
- bool mouseReleased (const OIS::MouseEvent& e, OIS::MouseButtonID id) { return true; }
- };
- #endif //__SimpleListener_H__
下面是SimpleApplication.h的內容。
- /*
- ==========================================================================
- SimpleApplication.h --- protoype to show off the simple ogre application.
- --------------------------------------------------------------------------
- Author : Zhu Xiaoyang ([email protected])
- Creation Date : Apr 14 2011
- Description:
- This is a mini Ogre Application. It does noting.
- ==========================================================================
- */
- #ifndef __SimpleApplication_H__
- #define __SimpleApplication_H__
- #include "Ogre.h"
- #include "SimpleListener.h"
- using namespace Ogre;
- /**
- ----------------------------------------------------------------------
- class SimpleApplication
- ----------------------------------------------------------------------
- */
- class SimpleApplication
- {
- public:
- SimpleApplication()
- {
- mRoot = 0;
- }
- ~SimpleApplication()
- {
- if(mRoot)
- OGRE_DELETE mRoot;
- }
- //start example
- void go()
- {
- if( !setup() )
- return;
- /**
- ----------------------------------------------------------------------
- 8 start rendering
- @ blocks until a frame listener returns false.
- eg. from pressing escape in this example.
- ----------------------------------------------------------------------
- */
- mRoot->startRendering();
- /**
- ----------------------------------------------------------------------
- 9 clean up
- ----------------------------------------------------------------------
- */
- destroyScene();
- }
- //setup Ogre
- bool setup()
- {
- /**
- ----------------------------------------------------------------------
- 1 Enter ogre
- ----------------------------------------------------------------------
- */
- mRoot = new Root;
- /**
- ----------------------------------------------------------------------
- 2 Configure resource paths
- @ Load resource paths from config file
- File format is:
- [ResourceGroupName]
- ArchiveType=Path
- .. repeat
- For example:
- [General]
- FileSystem=media/
- zip=packages/level1.zip
- ----------------------------------------------------------------------
- */
- ConfigFile cf;
- cf.load("resources.cfg");
- //Go through all sections & settings in the file
- ConfigFile::SectionIterator seci = cf.getSectionIterator();
- String secName, typeName, archName;
- while ( seci.hasMoreElements() )
- {
- secName = seci.peekNextKey();
- ConfigFile::SettingsMultiMap *settings = seci.getNext();
- ConfigFile::SettingsMultiMap::iterator i;
- for (i = settings->begin(); i != settings->end(); ++i)
- {
- typeName = i->first;
- archName = i->second;
- ResourceGroupManager::getSingleton().addResourceLocation(
- archName, typeName, secName);
- }
- }
- /**
- ----------------------------------------------------------------------
- 3 Configures the application and creates the window
- ----------------------------------------------------------------------
- */
- if ( !mRoot->showConfigDialog() )
- {
- //Ogre
- delete mRoot;
- return false; //Exit the application on cancel
- }
- mWindow = mRoot->initialise(true, "Simple Ogre App");
- ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
- /**
- ----------------------------------------------------------------------
- 4 Create the SceneManager
- @ SceneManager Type
- ST_GENERIC = octree
- ST_EXTERIOR_CLOSE = simple terrain
- ST_EXTERIOR_FAR = nature terrain (depreciated)
- ST_EXTERIOR_REAL_FAR = paging landscape
- ST_INTERIOR = Quake3 BSP
- ----------------------------------------------------------------------
- */
- mSceneMgr = mRoot->createSceneManager(ST_GENERIC);
- /**
- ----------------------------------------------------------------------
- 5 Create the camera
- ----------------------------------------------------------------------
- */
- mCamera = mSceneMgr->createCamera("SimpleCamera");
- /**
- ----------------------------------------------------------------------
- 6 Create one viewport, entire window
- ----------------------------------------------------------------------
- */
- Viewport* viewport = mWindow->addViewport(mCamera);
- /**
- ----------------------------------------------------------------------
- 7 Add OIS input handling
- ----------------------------------------------------------------------
- */
- OIS::ParamList pl;
- size_t windowHnd = 0;
- std::ostringstream windowHndStr;
- //tell OIS about the Ogre window
- mWindow->getCustomAttribute("WINDOW", &windowHnd);
- windowHndStr<<windowHnd;
- pl.insert( std::make_pair( std::string("WINDOW"), windowHndStr.str() ) );
- //setup the manager, keyboard and mouse to handle input
- inputManager = OIS::InputManager::createInputSystem( pl );
- keyboard = static_cast<OIS::Keyboard*>(inputManager->createInputObject( OIS::OISKeyboard, true ) );
- mouse = static_cast<OIS::Mouse*>(inputManager->createInputObject( OIS::OISMouse, true ) );
- //tell OIS about the window's dimensions
- unsigned int width, height, depth;
- int top, left;
- mWindow->getMetrics(width, height, depth, left, top);
- const OIS::MouseState &ms = mouse->getMouseState();
- ms.width = width;
- ms.height = height;
- //everything is set up, now we listen for input and frames (replaces while loops)
- //key events
- keyListener = new SimpleKeyListener();
- keyboard->setEventCallback(keyListener);
- //mouse events
- mouseListener = new SimpleMouseListener();
- mouse->setEventCallback(mouseListener);
- //render events
- mFrameListener = new SimpleFrameListener(keyboard, mouse);
- mRoot->addFrameListener(mFrameListener);
- return true;
- }
- //clean Ogre
- void destroyScene()
- {
- //OIS
- inputManager->destroyInputObject(mouse); mouse = 0;
- inputManager->destroyInputObject(keyboard); keyboard = 0;
- OIS::InputManager::destroyInputSystem(inputManager); inputManager = 0;
- //listeners
- delete mFrameListener;
- delete mouseListener;
- delete keyListener;
- }
- private:
- Root* mRoot;
- Camera* mCamera;
- SceneManager* mSceneMgr;
- RenderWindow* mWindow;
- //OIS
- OIS::InputManager* inputManager;
- OIS::Keyboard* keyboard;
- OIS::Mouse* mouse;
- //Listener
- SimpleKeyListener* keyListener;
- SimpleMouseListener* mouseListener;
- SimpleFrameListener* mFrameListener;
- };
- #endif //__SimpleApplication_H__
下面是main.cpp的內容。
- #include "SimpleApplication.h"
- #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
- {
- // Create application object
- SimpleApplication app;
- srand(time(0));
- try {
- app.go();
- } catch( Exception& e ) {
- #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
- MessageBoxA( NULL, e.getFullDescription().c_str(), "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL);
- #else
- std::cerr << "An exception has occured: " << e.getFullDescription();
- #endif
- }
- return 0;
- }
- #ifdef __cplusplus
- }
- #endif