最简单的Ogre系列之一——Ogre框架程序(不使用ExampleListener/Application)
2011-04-14 20:01
399 查看
本文参考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,其内容为
下面是SimpleListener.h的内容。
下面是SimpleApplication.h的内容。
下面是main.cpp的内容。
(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 (xiaoyang.zhu@ia.ac.cn) 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 (xiaoyang.zhu@ia.ac.cn) 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
相关文章推荐
- 最简单的Ogre系列之一——Ogre框架程序(不使用ExampleListener/Application)
- [渣译文] 使用 MVC 5 的 EF6 Code First 入门 系列:MVC程序中实体框架的Code First迁移和部署
- 最简单的Ogre系列之三——添加鼠标(不使用任何GUI)
- ASP.NET 使用application与session对象写的简单聊天室程序
- [05] 使用 MVC 5 的 EF6 Code First 入门 系列:MVC程序中实体框架的Code First迁移和部署
- 最简单的Ogre系列之三——添加鼠标(不使用任何GUI)
- [04] 使用 MVC 5 的 EF6 Code First 入门 系列:MVC程序中实体框架的连接恢复和命令拦截
- SBT(Scala Build Tool)系列教程-使用sbt构建第一个简单的HelloWorld程序
- 使用WTL+OGRE编写3D程序(1) - 基本框架
- 网络采集软件核心技术剖析系列(7)---如何使用C#语言搭建程序框架(经典Winform界面,顶部菜单栏,工具栏,左边树形列表,右边多Tab界面)
- 使用 acl_cpp 的 HttpServlet 类及服务器框架编写WEB服务器程序(系列文章)
- ASP.NET 使用application和session对象写的简单聊天室程序
- SpringMVC之ApplicationListener简单使用
- Mogre学习系列(5)如何使用Ogre建立游戏程序
- ASP.NET 使用application和session对象写的简单聊天室程序 推荐
- Flex快速入门系列之三:Parsley框架简单使用(IOC以及message机制)
- JAVA系列课程讲座二:使用Socket通信实现简单聊天通信程序(UDP方式)
- 树莓派学习系列5——使用python编写简单的游戏程序
- ASP.NET 使用application与session对象写的简单聊天室程序
- 简单的使用CreateDialog的Win32程序框架