最近几天把CELayouteditor编译了,也按照日志“Ogre1.7.1下使用CEGUI0.7.1”里把CEGUI和OGRE重新配置了一番,并且终于看完了OgreCN上的基础教程,还提取了天龙八部2里的所有资源,哈哈,感觉收获颇多。
中文Ogre wiki上的教程已经有些老了,从基础教程6开始就代码不能正常跑了。这里贴下能正常运行的代码,复制就OK了。关于教程结尾那个两个界面间的切换,我捣鼓了一阵子,差不多弄出个样子,但效果还是很差,啥透明都没,所以代码就不贴了。
012 | #include "ExampleApplication.h" |
014 | CEGUI::MouseButton convertOgreButtonToCegui( int buttonID) |
019 | return CEGUI::LeftButton; |
021 | return CEGUI::RightButton; |
023 | return CEGUI::MiddleButton; |
025 | return CEGUI::LeftButton; |
029 | class GuiFrameListener: public ExampleFrameListener, public OIS::MouseListener, public OIS::KeyListener |
032 | CEGUI::Renderer *mGUIRenderer; |
033 | bool mShoutdownRequested; |
035 | GuiFrameListener(RenderWindow* win,Camera* cam,CEGUI::Renderer* renderer) |
036 | :ExampleFrameListener(win,cam, true , true ),mGUIRenderer(renderer) |
037 | ,mShoutdownRequested( false ) |
039 | mMouse->setEventCallback( this ); |
040 | mKeyboard->setEventCallback( this ); |
042 | CEGUI::WindowManager* wmgr=CEGUI::WindowManager::getSingletonPtr(); |
043 | CEGUI::Window* quitBtn=wmgr->getWindow((CEGUI::utf8*) "Quit" ); |
044 | quitBtn->subscribeEvent(CEGUI::PushButton::EventClicked,CEGUI::Event::Subscriber(&GuiFrameListener::quit, this )); |
047 | bool quit( const CEGUI::EventArgs &evt) |
049 | mShoutdownRequested= true ; |
053 | bool frameStarted( const FrameEvent& evt) |
058 | mKeyboard->capture(); |
060 | return ExampleFrameListener::frameStarted(evt); |
063 | bool frameEnded( const FrameEvent& evt) |
065 | if (mShoutdownRequested) |
068 | return ExampleFrameListener::frameEnded(evt); |
071 | bool mouseMoved( const OIS::MouseEvent &evt) |
073 | CEGUI::System::getSingleton().injectMouseMove(evt.state.X.rel,evt.state.Y.rel); |
077 | bool mousePressed( const OIS::MouseEvent &evt, OIS::MouseButtonID id) |
079 | CEGUI::System::getSingleton().injectMouseButtonDown(convertOgreButtonToCegui(id)); |
083 | bool mouseReleased( const OIS::MouseEvent &evt, OIS::MouseButtonID id ) |
085 | CEGUI::System::getSingleton().injectMouseButtonUp(convertOgreButtonToCegui(id)); |
089 | bool keyPressed( const OIS::KeyEvent &evt ) |
091 | if (evt.key==OIS::KC_ESCAPE) |
093 | mShoutdownRequested= true ; |
097 | CEGUI::System::getSingleton().injectKeyDown(evt.key); |
099 | CEGUI::System::getSingleton().injectChar(evt.text); |
103 | bool keyReleased( const OIS::KeyEvent &evt ) |
105 | CEGUI::System::getSingleton().injectKeyUp(evt.key); |
110 | class TutorialApplication: public ExampleApplication |
113 | CEGUI::OgreRenderer* mGUIRenderer; |
114 | CEGUI::Window* mEditorGuiSheet; |
117 | TutorialApplication() |
118 | :mGUIRenderer(NULL),mEditorGuiSheet(NULL) |
122 | ~TutorialApplication() |
131 | void createScene( void ) |
133 | mGUIRenderer=&CEGUI::OgreRenderer::bootstrapSystem(); |
136 | CEGUI::Logger::getSingleton().setLoggingLevel(CEGUI::LoggingLevel::Informative); |
139 | CEGUI::SchemeManager::getSingleton().create( "TaharezLook.scheme" ); |
140 | CEGUI::System::getSingleton().setDefaultMouseCursor( "TaharezLook" , "MouseArrow" ); |
141 | CEGUI::MouseCursor::getSingleton().setImage( "TaharezLook" , "MouseMoveCursor" ); |
142 | CEGUI::System::getSingleton().setDefaultFont( "DejaVuSans-10" ); |
143 | mEditorGuiSheet=(CEGUI::Window*)CEGUI::WindowManager::getSingleton().createWindow( "DefaultWindow" , "Sheet" ); |
144 | CEGUI::System::getSingleton().setGUISheet(mEditorGuiSheet); |
147 | CEGUI::PushButton* quitBtn=(CEGUI::PushButton*)CEGUI::WindowManager::getSingleton().createWindow( "TaharezLook/Button" ,(CEGUI::utf8*) "Quit" ); |
148 | quitBtn->setPosition(CEGUI::UVector2(cegui_reldim(.35f), cegui_reldim(.45f))); |
149 | quitBtn->setSize(CEGUI::UVector2(cegui_reldim(.15f), cegui_reldim(.05f))); |
150 | quitBtn->setText((CEGUI::utf8*) "OK" ); |
151 | mEditorGuiSheet->addChildWindow(quitBtn); |
158 | mSceneMgr->setAmbientLight(ColourValue(1, 1, 1)); |
159 | mSceneMgr->setSkyDome( true , "Examples/CloudySky" , 5, 8); |
162 | void createFrameListener( void ) |
164 | mFrameListener = new GuiFrameListener(mWindow, mCamera, mGUIRenderer); |
165 | mRoot->addFrameListener(mFrameListener); |
169 | #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32 |
170 | #define WIN32_LEAN_AND_MEAN |
172 | INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE , LPSTR strCmdLine, INT ) |
174 | int main( int argc, char **argv) |
178 | TutorialApplication app; |
181 | } catch ( Exception& e ) { |
182 | #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32 |
183 | MessageBox( NULL, e.getFullDescription().c_str(), "An exception has occured!" , MB_OK | MB_ICONERROR | MB_TASKMODAL); |
185 | fprintf (stderr, "An exception has occured: %s/n" ,e.getFullDescription().c_str()); |
![beyondc](http://www.beyondc.cn/wp-content/uploads/2010/09/ocegui1.jpg)
![beyondc](http://www.beyondc.cn/wp-content/uploads/2010/09/ocegui2.jpg)
原文地址:http://www.beyondc.cn/ogre-in-the-interface-using-cegui-do.html