最簡單的Ogre系列之一——Ogre框架程序(不使用ExampleListener/Application)

原文: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,其內容爲

[c-sharp] view plaincopy
  1. # Defines plugins to load  
  2. # Define plugin folder  
  3. PluginFolder=.  
  4. # Define plugins  
  5. Plugin=RenderSystem_Direct3D9_d  

下面是SimpleListener.h的內容。

  1. /* 
  2.   ====================================================================== 
  3.    SimpleListener.h --- protoype to show off the simple ogre listener. 
  4.   ---------------------------------------------------------------------- 
  5.    Author : Zhu Xiaoyang ([email protected]) 
  6.    Creation Date : Apr 14 2011 
  7.    Description: 
  8.    This is a mini Ogre Listener, including SimpleFrameListener, 
  9.    SimpleKeyListener and SimpleMouseListener.  
  10.   ======================================================================= 
  11. */  
  12. #ifndef __SimpleListener_H__  
  13. #define __SimpleListener_H__  
  14. #include "Ogre.h"  
  15. #include "OgreFrameListener.h"  
  16. #include <OIS/OIS.h>  
  17. using namespace Ogre;  
  18. /** 
  19. ---------------------------------------------------------------------- 
  20.     class SimpleFrameListener 
  21. ---------------------------------------------------------------------- 
  22. */  
  23. class SimpleFrameListener : public FrameListener  
  24. {  
  25. public:  
  26.     SimpleFrameListener(OIS::Keyboard* keyboard, OIS::Mouse* mouse)  
  27.     {  
  28.         mKeyboard = keyboard;  
  29.         mMouse    = mouse;  
  30.     }  
  31.     //This gets called before the next frame is being rendered.  
  32.     bool frameStarted(const FrameEvent& evt)  
  33.     {  
  34.         //update the input devices  
  35.         mKeyboard->capture();  
  36.         mMouse->capture();  
  37.         //exit if key KC_ESCAPE pressed  
  38.         if( mKeyboard->isKeyDown( OIS::KC_ESCAPE ) )  
  39.             return false;  
  40.         else   
  41.             return true;  
  42.     }  
  43.     //This gets called at the end of a frame  
  44.     bool frameEnded(const FrameEvent& evt)  
  45.     {  
  46.         return true;  
  47.     }  
  48. private:  
  49.     OIS::Keyboard* mKeyboard;  
  50.     OIS::Mouse*    mMouse;  
  51. };  
  52. /** 
  53. ---------------------------------------------------------------------- 
  54.     class SimpleKeyListener 
  55. ---------------------------------------------------------------------- 
  56. */  
  57. class SimpleKeyListener : public OIS::KeyListener  
  58. {  
  59. public:  
  60.     bool keyPressed    (const OIS::KeyEvent& e)   { return true; }  
  61.     bool keyReleased   (const OIS::KeyEvent& e)   { return true; }  
  62. };  
  63. /** 
  64. ---------------------------------------------------------------------- 
  65.     class SimpleMouseListener 
  66. ---------------------------------------------------------------------- 
  67. */  
  68. class SimpleMouseListener : public OIS::MouseListener  
  69. {  
  70. public:  
  71.     bool mouseMoved    (const OIS::MouseEvent& e) { return true; }  
  72.     bool mousePressed  (const OIS::MouseEvent& e, OIS::MouseButtonID id) { return true; }  
  73.     bool mouseReleased (const OIS::MouseEvent& e, OIS::MouseButtonID id) { return true; }  
  74. };  
  75. #endif //__SimpleListener_H__   

下面是SimpleApplication.h的內容。

  1. /* 
  2.   ========================================================================== 
  3.    SimpleApplication.h --- protoype to show off the simple ogre application. 
  4.   -------------------------------------------------------------------------- 
  5.    Author : Zhu Xiaoyang ([email protected]) 
  6.    Creation Date : Apr 14 2011 
  7.    Description: 
  8.    This is a mini Ogre Application.  It does noting. 
  9.   ========================================================================== 
  10. */  
  11. #ifndef __SimpleApplication_H__  
  12. #define __SimpleApplication_H__  
  13. #include "Ogre.h"  
  14. #include "SimpleListener.h"  
  15. using namespace Ogre;  
  16. /** 
  17. ---------------------------------------------------------------------- 
  18.     class SimpleApplication 
  19. ---------------------------------------------------------------------- 
  20. */  
  21. class SimpleApplication  
  22. {  
  23. public:  
  24.     SimpleApplication()  
  25.     {  
  26.         mRoot = 0;  
  27.     }  
  28.     ~SimpleApplication()  
  29.     {  
  30.         if(mRoot)  
  31.             OGRE_DELETE mRoot;  
  32.     }  
  33.     //start example  
  34.     void go()   
  35.     {  
  36.         if( !setup() )  
  37.             return;  
  38.         /** 
  39.         ---------------------------------------------------------------------- 
  40.         8 start rendering 
  41.         @ blocks until a frame listener returns false.  
  42.           eg. from pressing escape in this example. 
  43.         ---------------------------------------------------------------------- 
  44.         */  
  45.         mRoot->startRendering();  
  46.         /** 
  47.         ---------------------------------------------------------------------- 
  48.         9 clean up 
  49.         ---------------------------------------------------------------------- 
  50.         */  
  51.         destroyScene();  
  52.     }  
  53.     //setup Ogre  
  54.     bool setup()  
  55.     {  
  56.         /** 
  57.         ---------------------------------------------------------------------- 
  58.         1 Enter ogre 
  59.         ---------------------------------------------------------------------- 
  60.         */  
  61.         mRoot = new Root;  
  62.         /** 
  63.         ---------------------------------------------------------------------- 
  64.         2 Configure resource paths 
  65.         @ Load resource paths from config file 
  66.           File format is: 
  67.           [ResourceGroupName] 
  68.           ArchiveType=Path 
  69.           .. repeat 
  70.           For example: 
  71.           [General] 
  72.           FileSystem=media/ 
  73.           zip=packages/level1.zip 
  74.         ---------------------------------------------------------------------- 
  75.         */  
  76.         ConfigFile cf;  
  77.         cf.load("resources.cfg");  
  78.         //Go through all sections & settings in the file  
  79.         ConfigFile::SectionIterator seci = cf.getSectionIterator();  
  80.         String secName, typeName, archName;  
  81.         while ( seci.hasMoreElements() )  
  82.         {  
  83.             secName = seci.peekNextKey();  
  84.             ConfigFile::SettingsMultiMap  *settings = seci.getNext();  
  85.             ConfigFile::SettingsMultiMap::iterator i;  
  86.             for (i = settings->begin(); i != settings->end(); ++i)  
  87.             {  
  88.                 typeName = i->first;  
  89.                 archName = i->second;  
  90.                 ResourceGroupManager::getSingleton().addResourceLocation(  
  91.                     archName, typeName, secName);  
  92.             }  
  93.         }  
  94.         /** 
  95.         ---------------------------------------------------------------------- 
  96.         3 Configures the application and creates the window 
  97.         ---------------------------------------------------------------------- 
  98.         */  
  99.         if ( !mRoot->showConfigDialog() )  
  100.         {  
  101.             //Ogre  
  102.             delete mRoot;  
  103.             return false//Exit the application on cancel  
  104.         }  
  105.         mWindow = mRoot->initialise(true"Simple Ogre App");  
  106.         ResourceGroupManager::getSingleton().initialiseAllResourceGroups();  
  107.         /** 
  108.         ---------------------------------------------------------------------- 
  109.         4 Create the SceneManager 
  110.         @ SceneManager Type 
  111.           ST_GENERIC = octree 
  112.           ST_EXTERIOR_CLOSE = simple terrain 
  113.           ST_EXTERIOR_FAR = nature terrain (depreciated) 
  114.           ST_EXTERIOR_REAL_FAR = paging landscape 
  115.           ST_INTERIOR = Quake3 BSP 
  116.         ---------------------------------------------------------------------- 
  117.         */  
  118.         mSceneMgr = mRoot->createSceneManager(ST_GENERIC);  
  119.         /** 
  120.         ---------------------------------------------------------------------- 
  121.         5 Create the camera 
  122.         ---------------------------------------------------------------------- 
  123.         */  
  124.         mCamera = mSceneMgr->createCamera("SimpleCamera");  
  125.         /** 
  126.         ---------------------------------------------------------------------- 
  127.         6 Create one viewport, entire window 
  128.         ---------------------------------------------------------------------- 
  129.         */  
  130.         Viewport* viewport = mWindow->addViewport(mCamera);  
  131.         /** 
  132.         ---------------------------------------------------------------------- 
  133.         7 Add OIS input handling 
  134.         ---------------------------------------------------------------------- 
  135.         */  
  136.         OIS::ParamList pl;  
  137.         size_t windowHnd = 0;  
  138.         std::ostringstream windowHndStr;  
  139.         //tell OIS about the Ogre window  
  140.         mWindow->getCustomAttribute("WINDOW", &windowHnd);  
  141.         windowHndStr<<windowHnd;  
  142.         pl.insert( std::make_pair( std::string("WINDOW"), windowHndStr.str() ) );  
  143.         //setup the manager, keyboard and mouse to handle input  
  144.         inputManager = OIS::InputManager::createInputSystem( pl );  
  145.         keyboard = static_cast<OIS::Keyboard*>(inputManager->createInputObject( OIS::OISKeyboard, true ) );  
  146.         mouse    = static_cast<OIS::Mouse*>(inputManager->createInputObject( OIS::OISMouse,    true ) );  
  147.         //tell OIS about the window's dimensions  
  148.         unsigned int width, height, depth;  
  149.         int top, left;  
  150.         mWindow->getMetrics(width, height, depth, left, top);  
  151.         const OIS::MouseState &ms = mouse->getMouseState();  
  152.         ms.width  = width;  
  153.         ms.height = height;  
  154.         //everything is set up, now we listen for input and frames (replaces while loops)  
  155.         //key events  
  156.         keyListener = new SimpleKeyListener();  
  157.         keyboard->setEventCallback(keyListener);  
  158.         //mouse events  
  159.         mouseListener = new SimpleMouseListener();  
  160.         mouse->setEventCallback(mouseListener);  
  161.         //render events  
  162.         mFrameListener = new SimpleFrameListener(keyboard, mouse);  
  163.         mRoot->addFrameListener(mFrameListener);  
  164.         return true;  
  165.     }  
  166.     //clean Ogre  
  167.     void destroyScene()  
  168.     {  
  169.         //OIS  
  170.         inputManager->destroyInputObject(mouse);             mouse = 0;  
  171.         inputManager->destroyInputObject(keyboard);          keyboard = 0;  
  172.         OIS::InputManager::destroyInputSystem(inputManager); inputManager = 0;  
  173.         //listeners  
  174.         delete mFrameListener;  
  175.         delete mouseListener;  
  176.         delete keyListener;  
  177.     }  
  178. private:  
  179.     Root* mRoot;  
  180.     Camera* mCamera;  
  181.     SceneManager* mSceneMgr;  
  182.     RenderWindow* mWindow;  
  183.     //OIS   
  184.     OIS::InputManager* inputManager;  
  185.     OIS::Keyboard* keyboard;  
  186.     OIS::Mouse* mouse;  
  187.     //Listener  
  188.     SimpleKeyListener* keyListener;  
  189.     SimpleMouseListener* mouseListener;  
  190.     SimpleFrameListener* mFrameListener;  
  191. };  
  192. #endif //__SimpleApplication_H__  

 

下面是main.cpp的內容。

 

  1. #include "SimpleApplication.h"  
  2. #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32  
  3. #define WIN32_LEAN_AND_MEAN  
  4. #include "windows.h"  
  5. #endif  
  6. #ifdef __cplusplus  
  7. extern "C" {  
  8. #endif  
  9. #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32  
  10. INT WINAPI WinMain( HINSTANCE hInst, HINSTANCELPSTR strCmdLine, INT )  
  11. #else  
  12. int main(int argc, char **argv)  
  13. #endif  
  14. {  
  15.     // Create application object  
  16.     SimpleApplication app;  
  17.     srand(time(0));  
  18.     try {  
  19.         app.go();  
  20.     } catch( Exception& e ) {  
  21. #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32  
  22.         MessageBoxA( NULL, e.getFullDescription().c_str(), "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL);  
  23. #else  
  24.         std::cerr << "An exception has occured: " << e.getFullDescription();  
  25. #endif  
  26.     }  
  27.     return 0;  
  28. }  
  29. #ifdef __cplusplus  
  30. }  
  31. #endif  

 

 

 

 

 

 

 

發佈了44 篇原創文章 · 獲贊 9 · 訪問量 25萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章