Guest User

CEGUI

a guest
Jan 23rd, 2014
115
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "stdafx.h"
  2.  
  3. #pragma managed(push, off)
  4. #include "Ogre.h"
  5. #include "OgreRoot.h"
  6. #include "OgreRenderWindow.h"
  7. #include "OgreSceneManager.h"
  8. #include "OgreCamera.h"
  9. #include "OgreViewport.h"
  10. #include "OgreRenderSystem.h"
  11. #include "OgreFrameListener.h"
  12. #include "OIS.h"
  13. #include "OISListeners.h"
  14. #include "CEGUI.h"
  15. #include "CEGUI\RendererModules\Ogre\Renderer.h"
  16. #pragma managed(pop)
  17.  
  18. #define RENDERSYSTEM            "Direct3D9 Rendering Subsystem"
  19. #define WINDOWNAME          "MyGUI Test"
  20. #define VIDEOMODE           "800x600 @ 32-bit colour"
  21. #define UI_RESOURCESUBFOLDER        "ui"
  22. #define UI_FILE_LAYOUT          "empty.layout"
  23. #define UI_FILE_SCHEME          "TaharezLook.scheme"
  24. #define DEFAULTARROW            "TaharezLook/MouseArrow"
  25. #define DEFAULTTOOLTIP          "TaharezLook/Tooltip"
  26. #define RESGROUP_IMAGESETS      "ImageSets"
  27. #define RESGROUP_FONTS          "Fonts"
  28. #define RESGROUP_SCHEMES        "Schemes"
  29. #define RESGROUP_LOOKNFEEL      "LookNFeel"
  30. #define RESGROUP_LAYOUTS        "Layouts"
  31.  
  32. /* OGRE */
  33. Ogre::Root* root;
  34. Ogre::RenderSystem* renderSystem;
  35. Ogre::RenderWindow* renderWindow;
  36. Ogre::SceneManager* sceneManager;
  37. Ogre::Camera* camera;
  38. Ogre::Viewport* viewport;
  39. Ogre::ResourceGroupManager* resMan;
  40.  
  41. /* OIS */
  42. OIS::InputManager* inputManager;
  43. OIS::Keyboard* oisKeyboard;
  44. OIS::Mouse* oisMouse;
  45.  
  46. /* CEGUI */
  47. CEGUI::OgreRenderer* renderer;
  48. CEGUI::System* ceguiSystem;
  49. CEGUI::GUIContext* guiContext;
  50. CEGUI::Scheme* scheme;
  51. CEGUI::Window* guiRoot;
  52.  
  53. /// <summary>
  54. /// FrameListener class for Ogre calling injectTimePulse
  55. /// </summary>  
  56. public class MyFrameListener : public ::Ogre::FrameListener
  57. {
  58. public:
  59.     MyFrameListener() { }
  60.  
  61.     virtual bool frameRenderingQueued (const Ogre::FrameEvent &evt) override
  62.     {
  63.         // inject timepulse
  64.         CEGUI::System::getSingleton().injectTimePulse(evt.timeSinceLastFrame);
  65.         return true;
  66.     };
  67. };
  68.  
  69. /// <summary>
  70. /// APPLICATION MAIN METHOD
  71. /// </summary>  
  72. int main(array<::System::String ^> ^args)
  73. {  
  74.     /****************************************
  75.             1. INIT OGRE
  76.     *****************************************/
  77.  
  78.     // init the ogre root object
  79.     root = OGRE_NEW Ogre::Root();
  80.  
  81.     // init the rendersystem
  82.     renderSystem = root->getRenderSystemByName(RENDERSYSTEM);
  83.        
  84.     // set values on renderSystem and attach it to root
  85.     renderSystem->setConfigOption("Full Screen", "No");
  86.     renderSystem->setConfigOption("Video Mode", VIDEOMODE);
  87.     root->setRenderSystem(renderSystem);
  88.          
  89.     // initialise the root
  90.     root->initialise(false, WINDOWNAME);
  91.  
  92.     // some more settings for the window
  93.     Ogre::NameValuePairList misc;
  94.     misc["FSAA"] = "4";
  95.     misc["vsync"] = "off";
  96.     misc["border"] = "fixed";
  97.  
  98.     // create the renderwindow
  99.     renderWindow = root->createRenderWindow(
  100.         WINDOWNAME,
  101.         800,
  102.         600,
  103.         false,
  104.         &misc);
  105.  
  106.     // create SceneManager
  107.     sceneManager = root->createSceneManager(Ogre::SceneType::ST_GENERIC);
  108.     sceneManager->setCameraRelativeRendering(true);
  109.     sceneManager->setAmbientLight(Ogre::ColourValue::White);
  110.  
  111.     // get resourcemanager singleton
  112.     resMan = Ogre::ResourceGroupManager::getSingletonPtr();
  113.    
  114.     // create camera
  115.     camera = sceneManager->createCamera("Camera1");
  116.     camera->setPosition(*new ::Ogre::Vector3(0, 0, 0));
  117.     camera->setNearClipDistance(1.0f);
  118.  
  119.     // create ViewPort
  120.     viewport = renderWindow->addViewport(camera, 0);  
  121.  
  122.     // attach framelistener to inject timepulses
  123.     root->addFrameListener(new MyFrameListener());
  124.  
  125.     /****************************************
  126.             2. INIT OIS
  127.     *****************************************/
  128.  
  129.     size_t windowHnd = 0;
  130.     renderWindow->getCustomAttribute("WINDOW", &windowHnd);
  131.        
  132.     std::ostringstream windowHndStr;
  133.     windowHndStr << windowHnd;
  134.  
  135.     OIS::ParamList oisParameters = OIS::ParamList();   
  136.     oisParameters.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str()));
  137.     oisParameters.insert(std::make_pair(std::string("w32_mouse"), "DISCL_NONEXCLUSIVE"));
  138.     oisParameters.insert(std::make_pair(std::string("w32_mouse"), "DISCL_FOREGROUND"));
  139.            
  140.     inputManager = OIS::InputManager::createInputSystem(oisParameters);
  141.     oisKeyboard = (OIS::Keyboard*)inputManager->createInputObject(OIS::Type::OISKeyboard, true);
  142.     oisMouse = (OIS::Mouse*)inputManager->createInputObject(OIS::Type::OISMouse, true);
  143.        
  144.     const OIS::MouseState &mouseState = oisMouse->getMouseState();
  145.     mouseState.width = viewport->getActualWidth();
  146.     mouseState.height = viewport->getActualHeight();
  147.            
  148.     OISKeyListener* keylistener = new OISKeyListener();
  149.     OISMouseListener* mouselistener = new OISMouseListener();
  150.        
  151.     oisKeyboard->setEventCallback(keylistener);
  152.     oisMouse->setEventCallback(mouselistener);
  153.    
  154.     /****************************************
  155.             3. INIT CEGUI
  156.     *****************************************/
  157.  
  158.     renderer = &CEGUI::OgreRenderer::bootstrapSystem(*((::Ogre::RenderTarget*)renderWindow));
  159.     ceguiSystem = ::CEGUI::System::getSingletonPtr();
  160.     guiContext = &ceguiSystem->getDefaultGUIContext();
  161.  
  162.     // load resource to ogre
  163.     resMan->createResourceGroup(RESGROUP_IMAGESETS);
  164.     resMan->createResourceGroup(RESGROUP_FONTS);
  165.     resMan->createResourceGroup(RESGROUP_SCHEMES);
  166.     resMan->createResourceGroup(RESGROUP_LOOKNFEEL);
  167.     resMan->createResourceGroup(RESGROUP_LAYOUTS);
  168.  
  169.     resMan->addResourceLocation("./ui/"RESGROUP_IMAGESETS, "FileSystem", RESGROUP_IMAGESETS);
  170.     resMan->addResourceLocation("./ui/"RESGROUP_FONTS, "FileSystem", RESGROUP_FONTS);
  171.     resMan->addResourceLocation("./ui/"RESGROUP_SCHEMES, "FileSystem", RESGROUP_SCHEMES);
  172.     resMan->addResourceLocation("./ui/"RESGROUP_LOOKNFEEL, "FileSystem", RESGROUP_LOOKNFEEL);
  173.     resMan->addResourceLocation("./ui/"RESGROUP_LAYOUTS, "FileSystem", RESGROUP_LAYOUTS);
  174.  
  175.     resMan->initialiseResourceGroup(RESGROUP_IMAGESETS);
  176.     resMan->initialiseResourceGroup(RESGROUP_FONTS);
  177.     resMan->initialiseResourceGroup(RESGROUP_SCHEMES);
  178.     resMan->initialiseResourceGroup(RESGROUP_LOOKNFEEL);
  179.     resMan->initialiseResourceGroup(RESGROUP_LAYOUTS);
  180.                
  181.     resMan->loadResourceGroup(RESGROUP_IMAGESETS);
  182.     resMan->loadResourceGroup(RESGROUP_FONTS);
  183.     resMan->loadResourceGroup(RESGROUP_SCHEMES);
  184.     resMan->loadResourceGroup(RESGROUP_LOOKNFEEL);
  185.     resMan->loadResourceGroup(RESGROUP_LAYOUTS);
  186.  
  187.     // set UI resourcegroups
  188.     CEGUI::ImageManager::setImagesetDefaultResourceGroup(RESGROUP_IMAGESETS);
  189.     CEGUI::Font::setDefaultResourceGroup(RESGROUP_FONTS);
  190.     CEGUI::Scheme::setDefaultResourceGroup(RESGROUP_SCHEMES);
  191.     CEGUI::WidgetLookManager::setDefaultResourceGroup(RESGROUP_LOOKNFEEL);
  192.     CEGUI::WindowManager::setDefaultResourceGroup(RESGROUP_LAYOUTS);
  193.        
  194.     // load scheme
  195.     scheme = &CEGUI::SchemeManager::getSingleton().createFromFile(UI_FILE_SCHEME);
  196.        
  197.     guiContext->getMouseCursor().setDefaultImage(DEFAULTARROW);    
  198.     guiContext->setDefaultTooltipType(DEFAULTTOOLTIP);
  199.                
  200.     // get windowmanager
  201.     CEGUI::WindowManager* wndMgr = CEGUI::WindowManager::getSingletonPtr();
  202.  
  203.     /****************************************
  204.             4. SETUP CEGUI TESTSTUFF
  205.     *****************************************/
  206.  
  207.     // load layout/rootelement
  208.     guiRoot = wndMgr->loadLayoutFromFile(UI_FILE_LAYOUT);
  209.        
  210.     // set rootwindow
  211.     guiContext->setRootWindow(guiRoot);
  212.  
  213.     // create layoutcontainer
  214.     CEGUI::HorizontalLayoutContainer* layout = (CEGUI::HorizontalLayoutContainer*)wndMgr->createWindow(
  215.         "HorizontalLayoutContainer", "testlayout");
  216.        
  217.      /*
  218.      layout->setArea(
  219.        CEGUI::UDim(0.0f, 0.0f),
  220.        CEGUI::UDim(0.0f, 0.0f),
  221.        CEGUI::UDim(1.0f, 0),
  222.        CEGUI::UDim(1.0f, 0));
  223.  
  224.      layout->setSize(CEGUI::USize(
  225.        CEGUI::UDim(1.0f, 0.0f),
  226.        CEGUI::UDim(1.0f, 0.0f)));
  227.      */
  228.        
  229.     // create widgets
  230.     CEGUI::Window* widget1 = (CEGUI::Window*)wndMgr->createWindow(
  231.        "TaharezLook/StaticImage", "widget1");
  232.        
  233.     CEGUI::Window* widget2 = (CEGUI::Window*)wndMgr->createWindow(
  234.        "TaharezLook/Label", "widget2");
  235.  
  236.     CEGUI::Window* widget3 = (CEGUI::Window*)wndMgr->createWindow(
  237.        "TaharezLook/Label", "widget3");
  238.        
  239.     // set sizes
  240.     widget1->setSize(CEGUI::USize(CEGUI::UDim(0, 16), CEGUI::UDim(0, 16)));
  241.     widget2->setSize(CEGUI::USize(CEGUI::UDim(0, 16), CEGUI::UDim(0, 16)));
  242.     widget3->setSize(CEGUI::USize(CEGUI::UDim(0, 16), CEGUI::UDim(0, 16)));
  243.        
  244.     widget1->setMaxSize(CEGUI::USize(CEGUI::UDim(0, 16), CEGUI::UDim(0, 16)));
  245.     widget2->setMaxSize(CEGUI::USize(CEGUI::UDim(0, 16), CEGUI::UDim(0, 16)));
  246.     widget3->setMaxSize(CEGUI::USize(CEGUI::UDim(0, 16), CEGUI::UDim(0, 16)));
  247.  
  248.     widget1->setMinSize(CEGUI::USize(CEGUI::UDim(0, 16), CEGUI::UDim(0, 16)));
  249.     widget2->setMinSize(CEGUI::USize(CEGUI::UDim(0, 16), CEGUI::UDim(0, 16)));
  250.     widget3->setMinSize(CEGUI::USize(CEGUI::UDim(0, 16), CEGUI::UDim(0, 16)));
  251.  
  252.     // set textvalues
  253.     widget2->setText("--");
  254.     widget3->setText("00");
  255.  
  256.     // add to layout
  257.     layout->addChild(widget1);
  258.     layout->addChild(widget2);
  259.     layout->addChild(widget3);
  260.  
  261.     // add layout to rootelement
  262.     guiRoot->addChild(layout);
  263.  
  264.     /****************************************
  265.             4. APP MAINLOOP
  266.     *****************************************/
  267.  
  268.     while(!renderWindow->isClosed())
  269.     {
  270.         // capture input
  271.         oisMouse->capture();
  272.     oisKeyboard->capture();
  273.  
  274.     // render one frame
  275.     root->renderOneFrame();
  276.  
  277.     // generic WM_MESSAGE pump
  278.     ::Ogre::WindowEventUtilities::messagePump();
  279.     }
  280.    
  281.     /****************************************
  282.             5. SHUTDOWN
  283.     *****************************************/
  284.  
  285.     renderSystem->destroyRenderWindow(renderWindow->getName());            
  286.     root->shutdown();
  287.    
  288.     return 0;
  289. };
RAW Paste Data