Irrlicht: Hello World!

Introduction

In this tutorial, we will learn how to create an empty window using Irrlicht and C++ programming language. Let’s get started!

Header & Namespaces

1. The first thing we need to do is to add the Irrlicht header file to our main.cpp source code:

#include <irrlich.h>

2. Then, add the following namespaces to the top of the main() function:

using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;

int main()
{
    return 0;
}

This way, we don’t have to repeatedly write the namespaces before calling its functions. For example, we can directly call createDevice() instead of irr::createDevice(). This can reduce a lot of typing, but might cause conflicts if you have multiple matching function names but under different namespaces. We are good for now as our code for this example project will be very simple and short.

Creating Irrlicht Device

3. Next, we must write the most important line of code in every Irrlicht application – creating the Irrlicht device:

int main()
{
    IrrlichtDevice *device = createDevice(video::EDT_SOFTWARE, dimension2d<u32>(640, 480), 16, false, false, false, 0);

    return 0;
}

The preceding code initializes Irrlicht engine and passes the configuration settings to the engine through irr::createDevice() function. The function will return a pointer to an IrrlichtDevice object upon successful initialization.

The parameters for createDevice() are explained below, in ascending order:

  • deviceType – The type of device that renders the graphics to screen. Could be EDT_SOFTWARE, EDT_BURNINGSVIDEO, EDT_NULL, EDT_DIRECT3D8, EDT_DIRECT3D9, or EDT_OPENGL
  • windowSize – The size of the window being created
  • bits – Amount of color bits per pixel. Could be 16 or 32, but generally ignored if not run in fullscreen
  • fullscreen – Set it to true if we want our program to run in full screen mode. Otherwise, set it to false.
  • stencilbuffer – Specifies if we want to enable stencil buffer for drawing shadows and other stuff
  • vsync – Enable vsync only in full screen mode.
  • eventReceiver – Set this to 0 as we are not using any event receiver in this example project.

4. We must check whether the device has been successfully created. If not, close the program immediately:

if (!device)
    return 1;

5. Set the window title to “Hello World!” if the device has been successfully created:

device->setWindowCaption(L"Hello World!");

Miscellaneous

6. After that, create the video driver, scene manager and GUI environment objects, like so:

IVideoDriver *driver = device->getVideoDriver();
ISceneManager *smgr = device->getSceneManager();
IGUIEnvironment *guienv = device->getGUIEnvironment();

7. Then, create a while loop and check if the device if still running. If it does, call drawAll() on both the scene manager and GUI environment. It must be called between beginScene() and endScene() functions:

while (device->run())
{
    driver->beginScene(true, true, SColor(255, 100, 100, 100));

    smgr->drawAll();
    guienv->drawAll();

    driver->endScene();
}

8. Lastly, drop the Irrlicht device before shutting down the program:

device->drop();

9. Build and run the project by going to Build 🡒 Build and run. Once the compilation is successful, a window that looks like this should be popping out on the screen:

Phew, what an accomplishment!

Source Code

The full source code looks something like this:

#include <irrlicht.h>

using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;

int main()
{
    IrrlichtDevice *device = createDevice(video::EDT_SOFTWARE, dimension2d<u32>(640, 480), 16, false, false, false, 0);

    if (!device)
        return 1;

    device->setWindowCaption(L"Hello World!");

    IVideoDriver *driver = device->getVideoDriver();
    ISceneManager *smgr = device->getSceneManager();
    IGUIEnvironment *guienv = device->getGUIEnvironment();

    while (device->run())
    {
        driver->beginScene(true, true, SColor(255, 100, 100, 100));

        smgr->drawAll();
        guienv->drawAll();

        driver->endScene();
    }
    device->drop();

    return 0;
}

In the next tutorial (coming soon), we will learn how to import 3D models to Irrlicht and display it on the screen. Please click on the link below to proceed.

Leave a Reply