SHARE
TWEET

Ogre 1.9-2.0 CEGUI Multiple window test

a guest Feb 3rd, 2015 11 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <OgreRoot.h>
  2. #include <OgreCamera.h>
  3. #include <OgreViewport.h>
  4. #include <OgreSceneManager.h>
  5. #include <OgreRenderWindow.h>
  6. #include <OgreEntity.h>
  7. #include <OgreWindowEventUtilities.h>
  8. #include <OgreMeshManager.h>
  9. #include <OgreMesh.h>
  10. #include <OgreSubMesh.h>
  11. #include <OgreTechnique.h>
  12. #include <OgreMaterialManager.h>
  13. #include <OgreMaterial.h>
  14. #include <OgreQuaternion.h>
  15. #include <OgreManualObject.h>
  16. #include <OgreFrameListener.h>
  17. #include <OIS/OIS.h>
  18.  
  19.  
  20.  
  21. #ifdef OGRE20
  22.  
  23. #include <Compositor/OgreTextureDefinition.h>
  24. #include <Compositor/OgreCompositorManager2.h>
  25. #include <Compositor/OgreCompositorCommon.h>
  26. #include <Compositor/OgreCompositorWorkspaceDef.h>
  27. #include <Compositor/OgreCompositorWorkspace.h>
  28. #include <Compositor/OgreCompositorNodeDef.h>
  29. #include <Compositor/Pass/PassClear/OgreCompositorPassClear.h>
  30. #include <Compositor/Pass/PassScene/OgreCompositorPassScene.h>
  31.  
  32. #endif //ogre 20
  33.  
  34.  
  35. #include <cegui\CEGUI.h>
  36. #include <cegui\RendererModules/Ogre\Renderer.h>
  37. #include <cegui\RendererModules/Ogre\WindowTarget.h>
  38. #include <CEGUI\RendererModules\Ogre\ResourceProvider.h>
  39. #include <CEGUI\RendererModules\Ogre\ImageCodec.h>
  40.  
  41. #include <sstream>
  42.  
  43. #include <string>
  44. #include <fstream>
  45.  
  46. struct CEGUIOgreInterface : public Ogre::FrameListener, public OIS::KeyListener, public OIS::MouseListener
  47. {
  48. public:
  49.   CEGUIOgreInterface(Ogre::RenderWindow *window)
  50.   {
  51.     this->window = window;
  52.     ceguiOgreRenderer = &CEGUI::OgreRenderer::create(*window);
  53.     //ceguiOgreRenderer->setDefaultRootRenderTarget(*window);
  54.     if(!CEGUI::System::getSingletonPtr())
  55.     {
  56.       CEGUI::System *system = &CEGUI::System::create(*ceguiOgreRenderer, new CEGUI::OgreResourceProvider(), 0, new CEGUI::OgreImageCodec());
  57.       Ogre::ResourceGroupManager::getSingleton().createResourceGroup("Imagesets");
  58.       Ogre::ResourceGroupManager::getSingleton().addResourceLocation("../data/CeguiData/imagesets", "FileSystem", "Imagesets");
  59.       Ogre::ResourceGroupManager::getSingleton().createResourceGroup("Fonts");
  60.       Ogre::ResourceGroupManager::getSingleton().addResourceLocation("../data/CeguiData/fonts", "FileSystem", "Fonts");
  61.       Ogre::ResourceGroupManager::getSingleton().createResourceGroup("Schemes");
  62.       Ogre::ResourceGroupManager::getSingleton().addResourceLocation("../data/CeguiData/schemes", "FileSystem", "Schemes");
  63.       Ogre::ResourceGroupManager::getSingleton().createResourceGroup("LookNFeel");
  64.       Ogre::ResourceGroupManager::getSingleton().addResourceLocation("../data/CeguiData/looknfeel", "FileSystem", "LookNFeel");
  65.       Ogre::ResourceGroupManager::getSingleton().createResourceGroup("Layouts");
  66.       Ogre::ResourceGroupManager::getSingleton().addResourceLocation("../data/CeguiData/layouts", "FileSystem", "Layouts");
  67.       Ogre::ResourceGroupManager::getSingleton().addResourceLocation("../data", "FileSystem", "Layouts");
  68.  
  69.       CEGUI::ImageManager::setImagesetDefaultResourceGroup("Imagesets");
  70.       CEGUI::Font::setDefaultResourceGroup("Fonts");
  71.       CEGUI::Scheme::setDefaultResourceGroup("Schemes");
  72.       CEGUI::WidgetLookManager::setDefaultResourceGroup("LookNFeel");
  73.       CEGUI::WindowManager::setDefaultResourceGroup("Layouts");
  74.  
  75.       CEGUI::SchemeManager::getSingleton().createFromFile("AlfiskoSkin.scheme");
  76.       CEGUI::SchemeManager::getSingleton().createFromFile("TaharezLook.scheme");
  77.       CEGUI::SchemeManager::getSingleton().createFromFile("OgreTray.scheme");
  78.       CEGUI::SchemeManager::getSingleton().createFromFile("VanillaSkin.scheme");
  79.       CEGUI::SchemeManager::getSingleton().createFromFile("Generic.scheme");
  80.     }
  81.     /*for(
  82.       CEGUI::ImageManager::ImageIterator it = CEGUI::ImageManager::getSingleton().getIterator();
  83.       !it.isAtEnd();
  84.       it++)
  85.     {
  86.       printf("Img: %s\n", it.getCurrentValue().first->getName().c_str());
  87.     }*/
  88.  
  89.     renderTarget = new CEGUI::OgreWindowTarget(*ceguiOgreRenderer, *Ogre::Root::getSingleton().getRenderSystem(), *window);
  90.     guiContext = &CEGUI::System::getSingleton().createGUIContext(*renderTarget);
  91.     //guiContext->getCursor().setDefaultImage("OgreTrayImages/MouseArrow");
  92.     Ogre::Root::getSingleton().addFrameListener(this);
  93.  
  94.     ceguiOgreRenderer->setRenderingEnabled(false);
  95.     InitInput();
  96.   }
  97.  
  98.  
  99.   bool frameStarted(const Ogre::FrameEvent& evt)
  100.   {
  101.     return true;
  102.   }
  103.   bool frameEnded(const Ogre::FrameEvent& evt)
  104.   {
  105.     return true;
  106.   }
  107.  
  108.   virtual bool keyPressed(const OIS::KeyEvent& keyEvent)
  109.   {
  110.     inputReceiver->injectKeyDown((CEGUI::Key::Scan)keyEvent.key);
  111.     inputReceiver->injectChar((CEGUI::Key::Scan)keyEvent.text);
  112.     return true;
  113.   }
  114.  
  115.   virtual bool keyReleased(const OIS::KeyEvent& keyEvent)
  116.   {
  117.     inputReceiver->injectKeyUp((CEGUI::Key::Scan)keyEvent.key);
  118.     return true;
  119.   }
  120.  
  121.   virtual bool mousePressed(const OIS::MouseEvent& mouseEvent, OIS::MouseButtonID buttonId)
  122.   {
  123.     inputReceiver->injectMouseButtonDown((CEGUI::MouseButton)buttonId);
  124.     return true;
  125.   }
  126.  
  127.   virtual bool mouseReleased (const OIS::MouseEvent& mouseEvent, OIS::MouseButtonID buttonId)
  128.   {
  129.     inputReceiver->injectMouseButtonUp((CEGUI::MouseButton)buttonId);
  130.     return true;
  131.   }
  132.  
  133.   virtual bool mouseMoved(const OIS::MouseEvent& mouseEvent)
  134.   {
  135.     //we're not injecting mouse movement because it's broken with non-exclusive mouse
  136.     //inputReceiver->injectMouseMove(mouseEvent.state.X.rel, mouseEvent.state.Y.rel);
  137.     // Scroll wheel.
  138.     if (mouseEvent.state.Z.rel)
  139.       inputReceiver->injectMouseWheelChange(mouseEvent.state.Z.rel / 120.0f);
  140.     return true;
  141.   }
  142.  
  143.  
  144.  
  145.   bool frameRenderingQueued(const Ogre::FrameEvent& args)
  146.   {
  147.     if(window->isClosed())
  148.         return true;
  149.  
  150.     const OIS::MouseState state = mouseInput->getMouseState();
  151.     inputReceiver->injectMousePosition(state.X.abs,state.Y.abs);
  152.  
  153.     ceguiOgreRenderer->updateWorkspaceRenderTarget(*window);
  154.  
  155.      
  156.     CEGUI::System& guiSystem(CEGUI::System::getSingleton());
  157.  
  158.     float dt = 1e-2f;
  159.     keyboardInput->capture();
  160.     mouseInput->capture();
  161.  
  162.     unsigned int w, h, d;
  163.     int left, top;
  164.     window->getMetrics(w, h, d, left, top);
  165.     const OIS::MouseState &ms = mouseInput->getMouseState();
  166.     ms.width = w;
  167.     ms.height = h;
  168.  
  169.     ceguiOgreRenderer->initialiseRenderStateSettings(); //this line actually
  170.  
  171.     ceguiOgreRenderer->beginRendering();
  172.     CEGUI::System::getSingleton().injectTimePulse(1e-2f);
  173.     guiContext->draw();
  174.     ceguiOgreRenderer->endRendering();
  175.  
  176.     CEGUI::WindowManager::getSingleton().cleanDeadPool();
  177.  
  178.     return true;
  179.   }
  180.   CEGUI::GUIContext *GetCeguiContext()
  181.   {
  182.     return guiContext;
  183.   }
  184. private:
  185.   void InitInput()
  186.   {
  187.     OIS::ParamList parameterList;
  188.     size_t windowHnd = 0;
  189.     std::ostringstream windowHndStr;
  190.  
  191.     window->getCustomAttribute("WINDOW", &windowHnd);
  192.     windowHndStr << windowHnd;
  193.     parameterList.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str()));
  194.     parameterList.insert(std::make_pair(std::string("w32_mouse"), std::string("DISCL_FOREGROUND" )));
  195.     parameterList.insert(std::make_pair(std::string("w32_mouse"), std::string("DISCL_NONEXCLUSIVE")));
  196.     parameterList.insert(std::make_pair(std::string("w32_keyboard"), std::string("DISCL_FOREGROUND")));
  197.     parameterList.insert(std::make_pair(std::string("w32_keyboard"), std::string("DISCL_NONEXCLUSIVE")));
  198.  
  199.     inputManager = OIS::InputManager::createInputSystem(parameterList);
  200.  
  201.     keyboardInput = static_cast<OIS::Keyboard*>(inputManager->createInputObject( OIS::OISKeyboard, true ));
  202.     mouseInput = static_cast<OIS::Mouse*>(inputManager->createInputObject( OIS::OISMouse, true ));
  203.  
  204.     keyboardInput->setEventCallback(this);
  205.     mouseInput->setEventCallback(this);
  206.  
  207.     #ifdef OGRE20
  208.       inputReceiver = new CEGUI::InputAggregator(guiContext);
  209.     #else
  210.       inputReceiver = guiContext;
  211.     #endif
  212.  
  213.     //Register as a Window listener
  214.     //Ogre::WindowEventUtilities::addWindowEventListener(window, this);
  215.  
  216.     Ogre::Root::getSingleton().addFrameListener(this);
  217.   }
  218.   CEGUI::OgreRenderer *ceguiOgreRenderer;
  219.   CEGUI::RenderTarget *renderTarget;
  220.   CEGUI::GUIContext *guiContext;
  221.   Ogre::RenderWindow *window;
  222.   OIS::InputManager *inputManager;
  223.   OIS::Keyboard *keyboardInput;
  224.   OIS::Mouse *mouseInput;
  225.   #ifdef OGRE20
  226.   CEGUI::InputAggregator *inputReceiver;
  227.   #else
  228.   CEGUI::GUIContext *inputReceiver;
  229.   #endif
  230.  
  231. };
  232.  
  233. int main( int argc, char **argv )
  234. {
  235.   Ogre::String configSuffix = "";
  236.   #ifdef _DEBUG
  237.     configSuffix = "_d";
  238.   #endif
  239.  
  240.   Ogre::String pluginsFileName = "OgrePlugins" + configSuffix + ".cfg";
  241.   Ogre::String configFileName = "OgreConfig" + configSuffix + ".cfg";
  242.   Ogre::String logFileName = "OgreLog" + configSuffix + ".log";
  243.   Ogre::Root *root = new Ogre::Root(pluginsFileName, configFileName, logFileName);
  244.  
  245.   //Ogre::RenderSystem *renderSystem = root->getRenderSystemByName("Direct3D11 Rendering Subsystem");
  246.   //Ogre::RenderSystem *renderSystem = root->getRenderSystemByName("Direct3D9 Rendering Subsystem");
  247.   Ogre::RenderSystem *renderSystem = root->getRenderSystemByName("OpenGL Rendering Subsystem");
  248.   root->setRenderSystem(renderSystem);
  249.   renderSystem->setConfigOption("Full Screen", "No");
  250.   renderSystem->setConfigOption("Video Mode", "800 x 600 @ 32-bit colour");
  251.  
  252.   root->initialise(false);
  253.  
  254.  
  255.  
  256.   //Ogre::RenderWindow *window = root->getAutoCreatedWindow();
  257.   Ogre::RenderWindow *window = root->createRenderWindow("Awesome window", 800, 600, false);
  258.   Ogre::RenderWindow *window2 = root->createRenderWindow("Awesome window 2!", 800, 600, false);
  259.  
  260.   #ifdef OGRE20
  261.   Ogre::SceneManager* sceneManager =  root->createSceneManager(Ogre::ST_EXTERIOR_CLOSE, /*"DefaultSceneManager", */8, Ogre::INSTANCING_CULLING_THREADED);
  262.   Ogre::SceneManager* sceneManager2 = root->createSceneManager(Ogre::ST_EXTERIOR_CLOSE, /*"DefaultSceneManager", */8, Ogre::INSTANCING_CULLING_THREADED);
  263.   #else //OGRE20
  264.   Ogre::SceneManager* sceneManager =  root->createSceneManager(Ogre::ST_EXTERIOR_CLOSE, "SceneManager1");
  265.   Ogre::SceneManager* sceneManager2 = root->createSceneManager(Ogre::ST_EXTERIOR_CLOSE, "SceneManager2");
  266.   #endif //OGRE20
  267.  
  268.   /*
  269.   HWND hWnd = 0;  // Get the hWnd of the application!
  270.   NameValuePairList misc;
  271.   misc["externalWindowHandle"] = StringConverter::toString((int)hWnd);
  272.   Ogre::RenderWindow *window = root->createRenderWindow("Main RenderWindow", 800, 600, false, &misc);*/
  273.  
  274.   Ogre::ResourceGroupManager::getSingleton().createResourceGroup("Manual meshes");
  275.   Ogre::ResourceGroupManager::getSingleton().addResourceLocation("data", "FileSystem");
  276.   Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
  277.  
  278.   Ogre::Camera *camera = sceneManager->createCamera("Cool camera");
  279.   Ogre::Camera *camera2 = sceneManager->createCamera("Cool camera2");
  280.  
  281.   camera->setPosition(Ogre::Vector3(0.0f, 1500.0f, 1500.0f));
  282.   camera->lookAt(Ogre::Vector3(0.0f, 0.0f, 0.0f));
  283.   camera->setNearClipDistance(5.0f);
  284.   camera->setFarClipDistance(10000.0f);
  285.   Ogre::Radian fov = camera->getFOVy();
  286.  
  287.   #ifdef OGRE20
  288.   Ogre::CompositorManager2 *compositorManager = root->getCompositorManager2();
  289.  
  290.   const Ogre::IdString workspaceName("Awesome workspace1");
  291.   compositorManager->createBasicWorkspaceDef( workspaceName, Ogre::ColourValue(0.5f, 0.5f, 0.5f, 0.5f), Ogre::IdString());
  292.   compositorManager->addWorkspace( sceneManager, window, camera, workspaceName, true );
  293.  
  294.   Ogre::Viewport* viewport = window->getViewport(0);
  295.   camera->setAspectRatio(Ogre::Real(viewport->getActualWidth()) / Ogre::Real(viewport->getActualHeight()));
  296.  
  297.  
  298.   //experiments with not creating a workspace
  299.   /*const Ogre::IdString workspaceName2("Awesome workspace2");
  300.   compositorManager->createBasicWorkspaceDef( workspaceName2, Ogre::ColourValue(0.5f, 0.5f, 0.5f, 0.5f), Ogre::IdString());
  301.   compositorManager->addWorkspace( sceneManager2, window2, camera2, workspaceName2, true );
  302.  
  303.   Ogre::Viewport* viewport2 = window2->getViewport(0);
  304.   camera2->setAspectRatio(Ogre::Real(viewport2->getActualWidth()) / Ogre::Real(viewport2->getActualHeight()));*/
  305.   #else //OGRE20
  306.   Ogre::Viewport* viewport = window->addViewport(camera);
  307.   viewport->setBackgroundColour(Ogre::ColourValue(0.5f, 0.5f, 0.5f, 1.0f));
  308.  
  309.   Ogre::Viewport* viewport2 = window2->addViewport(camera2);
  310.   viewport2->setBackgroundColour(Ogre::ColourValue(0.5f, 0.5f, 0.5f, 1.0f));
  311.   #endif
  312.  
  313.   CEGUIOgreInterface ceguiInterface(window);
  314.   CEGUI::GUIContext *guiContext = ceguiInterface.GetCeguiContext();
  315.   CEGUI::Window *testWindow = CEGUI::WindowManager::getSingleton().loadLayoutFromFile("test.layout", "Layouts");
  316.   guiContext->setRootWindow(testWindow);
  317.  
  318.   CEGUIOgreInterface ceguiInterface2(window2);
  319.   CEGUI::GUIContext *guiContext2 = ceguiInterface2.GetCeguiContext();
  320.   CEGUI::Window *testWindow2 = CEGUI::WindowManager::getSingleton().loadLayoutFromFile("testAlfisko.layout", "Layouts");
  321.   guiContext2->setRootWindow(testWindow2);
  322.  
  323.  
  324.   bool running = 1;
  325.   float time = 0.0f;
  326.  
  327.   bool firstTime = 1;
  328.   while(running)
  329.   {
  330.     float dt = 1e-2f;
  331.     time += dt; //we don't need actual time, just a value increasing with time
  332.     Ogre::WindowEventUtilities::messagePump();
  333.  
  334.     CEGUI::System::getSingleton().injectTimePulse(dt);
  335.  
  336.     if(window->isClosed())
  337.       running = 0;
  338.     root->renderOneFrame();
  339.   }
  340. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top