基于GLUT的CEGUI DEMO

30 篇文章 1 订阅
  1. #include <GL/glut.h>
  2. #include <CEGUI.h>
  3. #include <CEGUIDefaultResourceProvider.h>
  4. #include <RendererModules/OpenGLGUIRenderer/openglrenderer.h>
  5. using namespace CEGUI;
  6. //#include "UIAnimationManager.h"
  7. // GLUT callback prototypes
  8. void drawFrame(void);
  9. void mouseMotion(int x, int y);
  10. void mouseButton(int button, int state, int x, int y);
  11. void keyChar(unsigned char key, int x, int y);
  12. // super global vars!
  13. bool G_quitFlag = false;
  14. CEGUI::OpenGLRenderer* G_renderer = 0;
  15. //uiae::UIAnimationManager* G_animMgr = 0;
  16. int G_lastFrameTime = 0;
  17. // app entry point
  18. int main(int argc, char* argv[])
  19. {
  20.         // Do GLUT init
  21.         glutInit(&argc, argv);
  22.         glutInitDisplayMode(GLUT_DEPTH|GLUT_DOUBLE|GLUT_RGBA);
  23.         glutInitWindowSize(800, 600);
  24.         glutInitWindowPosition(100, 100);
  25.         glutCreateWindow("CEGUI / UIAE Test");
  26.         glutSetCursor(GLUT_CURSOR_NONE);
  27.         // register callbacks
  28.         glutDisplayFunc(drawFrame);
  29.         glutMotionFunc(mouseMotion);
  30.         glutPassiveMotionFunc(mouseMotion);
  31.         glutMouseFunc(mouseButton);
  32.         glutKeyboardFunc(keyChar);
  33.         // initialise basic CEGUI system
  34.         G_renderer = new OpenGLRenderer(1024);
  35.         new System(G_renderer);
  36.         // initialise the required dirs for the DefaultResourceProvider
  37.         DefaultResourceProvider* rp = static_cast<DefaultResourceProvider*>
  38.                 (System::getSingleton().getResourceProvider());
  39.         //注意:这里要修改为下载的CEGUI界面文件对应的目录
  40.         rp->setResourceGroupDirectory("schemes", "../GUI/schemes/");
  41.         rp->setResourceGroupDirectory("imagesets", "../GUI/imagesets/");
  42.         rp->setResourceGroupDirectory("fonts", "../GUI/fonts/");
  43.         rp->setResourceGroupDirectory("layouts", "../GUI/layouts/");
  44.         rp->setResourceGroupDirectory("looknfeels", "../GUI/looknfeel/");
  45.         rp->setResourceGroupDirectory("lua_scripts", "../GUI/lua_scripts/");
  46.         // set the default resource groups to be used
  47.         Imageset::setDefaultResourceGroup("imagesets");
  48.         Font::setDefaultResourceGroup("fonts");
  49.         Scheme::setDefaultResourceGroup("schemes");
  50.         WidgetLookManager::setDefaultResourceGroup("looknfeels");
  51.         WindowManager::setDefaultResourceGroup("layouts");
  52.         ScriptModule::setDefaultResourceGroup("lua_scripts");
  53.         // we will make use of the WindowManager.
  54.         WindowManager& winMgr = WindowManager::getSingleton();
  55.         // load scheme and set up defaults
  56.         SchemeManager::getSingleton().loadScheme("TaharezLook.scheme");
  57.         System::getSingleton().setDefaultMouseCursor("TaharezLook", "MouseArrow");
  58.         if(!FontManager::getSingleton().isFontPresent("Commonwealth-10"))
  59.                 FontManager::getSingleton().createFont("Commonwealth-10.font");
  60.         // create root window
  61.         DefaultWindow* root = (DefaultWindow*)winMgr.createWindow("DefaultWindow", "Root");
  62.         // set root as the active GUI sheet
  63.         System::getSingleton().setGUISheet(root);
  64.         // create a FrameWindow for the test
  65.         FrameWindow* wnd = (FrameWindow*)winMgr.createWindow("TaharezLook/FrameWindow", "TestWindow");
  66.         // add the frame window to the root
  67.         root->addChildWindow(wnd);
  68.         // set window initial position and size
  69.         wnd->setPosition(UVector2(cegui_reldim(0.25f), cegui_reldim( 0.25f)));
  70.         wnd->setSize(UVector2(cegui_reldim(0.5f), cegui_reldim( 0.5f)));
  71.         // set window text
  72.         wnd->setText("UIAE Demo Window");
  73.         initialise the uiae system
  74.         *** Step 1 from the artice ***
  75.         //G_animMgr = uiae::UIAnimationManager::instance();
  76.         load the sample animations that came with uiae
  77.         *** Step 2 from the artice ***
  78.         //if (G_animMgr->loadAnimationsFromFile("./sample.xml"))
  79.         //{
  80.         //        // add the FrameWindow to be managed by uiae, using anims from the "misc" list
  81.         //        // *** Step 3 from the artice ***
  82.         //        if (G_animMgr->addWindow(wnd, "1", "misc"))
  83.         //        {
  84.         //                // set window ID "1" to use the "bounce" animation (from the "misc" list).
  85.         //                // *** Step 4 from the artice ***
  86.         //                G_animMgr->setAnimationState("1", "bounce");
  87.         //        }
  88.         //}
  89.         // start loop
  90.         glutMainLoop();
  91.         return 0;
  92. }
  93. void drawFrame(void)
  94. {
  95.         // do time based updates
  96.         int thisTime = glutGet(GLUT_ELAPSED_TIME);
  97.         float elapsed = static_cast<float>(thisTime - G_lastFrameTime);
  98.         G_lastFrameTime = thisTime;
  99.         // inject the time pulse (CEGUI and UIAE)
  100.         CEGUI::System::getSingleton().injectTimePulse(elapsed / 1000.0f);
  101.         // *** Step 5 from the artice ***
  102.         //G_animMgr->update(elapsed / 1000.f);
  103.         // do rendering for this frame.
  104.         glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  105.         glEnable(GL_DEPTH_TEST);
  106.         glMatrixMode(GL_MODELVIEW);
  107.         glLoadIdentity();
  108.         gluLookAt(0.0, 0.0, 12, 0.0, 0.0, -100, 0.0, 1.0, 0.0);
  109.         CEGUI::System::getSingleton().renderGUI();
  110.         glFlush();
  111.         glutPostRedisplay();
  112.         glutSwapBuffers();
  113.         // handle quit conditiion
  114.         if (G_quitFlag)
  115.         {
  116.                 //G_animMgr->cleanup();
  117.                 // cleanup cegui system
  118.                 delete CEGUI::System::getSingletonPtr();
  119.                 delete G_renderer;
  120.                 // exit
  121.                 exit(0);
  122.         }
  123. }
  124. void mouseMotion(int x, int y)
  125. {
  126.         CEGUI::System::getSingleton().injectMousePosition(x, y);
  127. }
  128. void mouseButton(int button, int state, int x, int y)
  129. {
  130.         switch(button)
  131.         {
  132.         case  GLUT_LEFT_BUTTON:
  133.                 if (state == GLUT_UP)
  134.                 {
  135.                         CEGUI::System::getSingleton().injectMouseButtonUp(CEGUI::LeftButton);
  136.                 }
  137.                 else
  138.                 {
  139.                         CEGUI::System::getSingleton().injectMouseButtonDown(CEGUI::LeftButton);
  140.                 }
  141.                 break;
  142.         case GLUT_RIGHT_BUTTON:
  143.                 if (state == GLUT_UP)
  144.                 {
  145.                         CEGUI::System::getSingleton().injectMouseButtonUp(CEGUI::RightButton);
  146.                 }
  147.                 else
  148.                 {
  149.                         CEGUI::System::getSingleton().injectMouseButtonDown(CEGUI::RightButton);
  150.                 }
  151.                 break;
  152.         case GLUT_MIDDLE_BUTTON:
  153.                 if (state == GLUT_UP)
  154.                 {
  155.                         CEGUI::System::getSingleton().injectMouseButtonUp(CEGUI::MiddleButton);
  156.                 }
  157.                 else
  158.                 {
  159.                         CEGUI::System::getSingleton().injectMouseButtonDown(CEGUI::MiddleButton);
  160.                 }
  161.                 break;
  162.         }
  163. }
  164. void keyChar(unsigned char key, int x, int y)
  165. {
  166.         // extract some keys may be handled via key code and generate those too
  167.         switch (key)
  168.         {
  169.         case 0x08:  // backspace
  170.                 CEGUI::System::getSingleton().injectKeyDown(CEGUI::Key::Backspace);
  171.                 break;
  172.         case 0x7F:  // delete
  173.                 CEGUI::System::getSingleton().injectKeyDown(CEGUI::Key::Delete);
  174.                 break;
  175.         case 0x1B:  // Escape
  176.                 G_quitFlag = true;
  177.                 break;
  178.         case 0x0D:  // CR (Return)
  179.                 CEGUI::System::getSingleton().injectKeyDown(CEGUI::Key::Return);
  180.                 break;
  181.         default:
  182.                 // inject Character code
  183.                 CEGUI::System::getSingleton().injectChar(static_cast<CEGUI::utf32>(key));
  184.                 break;
  185.         }
  186. }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值