Skip to content

Example 1: Hello World

ZahlGraf edited this page Nov 4, 2015 · 8 revisions

IrrIMGUI Examples

Example 1: Hello World

The HelloWorld example builds up a simple scene with Irrlicht (I expect that you have some experience with Irrlicht. If not, please look first at the Irrlicht Tutorial). Furthermore it draws a simple IMGUI window with some text and an Exit-button. The full source code can be found in the file examples/01.HelloWorld/main.cpp. In this description, I will simply highlight the IMGUI specific code.

  • Include the IrrIMGUI main header: Include the file IrrIMGUI/IrrIMGUI.h to your project.
#include <IrrIMGUI/IrrIMGUI.h>
#include <IrrIMGUI/IncludeIrrlicht.h>
  • Setup the Irrlicht device: When you setup the Irrlicht device, you can use the build in IrrIMGUI EventReceiver class to pass your Mouse and Keyboard events directly to the GUI.
  using namespace irr;
  using namespace IrrIMGUI;

  // Irrlicht OpenGL Settings
  SIrrlichtCreationParameters IrrlichtParams;
  IrrlichtParams.DriverType    = video::EDT_OPENGL;
  IrrlichtParams.WindowSize    = core::dimension2d<u32>(1024, 800);
  IrrlichtParams.Bits          = 32;
  IrrlichtParams.Fullscreen    = false;
  IrrlichtParams.Stencilbuffer = true;
  IrrlichtParams.AntiAlias     = 16;
  IrrlichtParams.Vsync         = false;
  
  // Create standard event receiver for the IrrIMGUI
  CIMGUIEventReceiver EventReceiver;  
  IrrlichtParams.EventReceiver = &EventReceiver;

  // Create Irrlicht device
  IrrlichtDevice * const pDevice = createDeviceEx(IrrlichtParams);
  • Create a GUI object: Before you can use the GUI system, you need to create a IMGUI handle object: The corresponding factory function needs a pointer to the Irrlicht device and to the event receiver, to be able to pass events to the GUI.
  // Create GUI object
  IIMGUIHandle * const pGUI = createIMGUI(pDevice, &EventReceiver);
  • Draw GUI elements to the screen: You can create the GUI elements inside the main-loop after calling pGUI->startGUI();. This function prepares the GUI for the next frame and passes the state of Mouse and Keyboard to IMGUI. Afterwards you can use the GUI element functions in the namespace ImGui to create the elements you need. You must create them inside the main-loop, otherwise you will not see them (see the concept of an immediate mode GUI). Before rendering the GUI elements, you first need to render your Irrlicht scene. Otherwise the GUI is behind your scene. Afterwards you can render the GUI with pGUI->drawAll();.
 scene::ISceneManager * const pSceneManager = pDevice->getSceneManager();
 video::IVideoDriver * const pDriver = pDevice->getVideoDriver();

 // TODO: build your Irrlicht scene here ...
 
 // add camera to the scene
 pSceneManager->addCameraSceneNode(0, core::vector3df(0, 0, 0), core::vector3df(0,0,5));
  
  // Start main loop
 while(pDevice->run())
 {
   pDriver->beginScene(true, true, irr::video::SColor(255,100,101,140));

   // create the GUI elements
   pGUI->startGUI();
   ImGui::Begin("My first Window");
   ImGui::Text("Hello World!");
   if (ImGui::Button("Exit", ImVec2(40, 20)))
   {
     pDevice->closeDevice();
   }
   ImGui::End();

   // render your scene
   pSceneManager->drawAll();
   
   // render the GUI
   pGUI->drawAll();

   pDriver->endScene();
 }

 // free up memory
 pDevice->drop();
 pGUI->drop();
  • Compile and look at your first GUI element
    Hello World
Clone this wiki locally