Qt wiki will be updated on October 12th 2023 starting at 11:30 AM (EEST) and the maintenance will last around 2-3 hours. During the maintenance the site will be unavailable.

Using Irrlicht with QtCreator

From Qt Wiki
Jump to navigation Jump to search

Introduction

Irrlicht is a C++ API for creating 3D applications or games, preferrably first person shooters. According to the wiki Irrlicht (pronounced [ˈʔɪɐ̯lɪçt] in German) is an open source game engine written in C++. It is cross-platform, officially running on Windows, OS X, Linux and Windows CE and due to its open nature ports to other systems are available, including FreeBSD, Xbox (up to 1.8.1), PlayStation Portable,[1] Raspberry Pi, Symbian,[2] iPhone[3] and Google Native Client.

Create a new Project

In order to get code completion working within a project not using Qt libraries we still need to have qmake control the build process. This is also the case with Irrlicht. In order to achieve this we create a plain C++ project. It is assumed that you have Irrlicht itself already installed in a system standard location.

Qtcreator-create-new-project.png

Select Plain C++ Application and proceed with Choose....

On the next wizard page give the project name and location. Followed by the build system which we set to qmake as mentioned earlier.

Then select select your Kit and finally on the last page the versioning system if required.

This will result in the following project file tree:

Qtcreator-file-tree.png

In order to make qmake find Irrlicht headers and link to it, alter the project file as following:

TEMPLATE = app
CONFIG = console c++11 silent

INCLUDEPATH += /usr/include/irrlicht
LIBS += -l Irrlicht

SOURCES += main.cpp

The important changes are to add the Irrlicht include path to the qmake variable INCLUDEPATH. Furthermore we need the Irrlicht.so dynamic link library to be added to the LIBS variable so the library is found in the linking stage.

Now in the main.cpp file we can include the main header irrlicht.h and use the namespace irr for the main classes like IrrlichtDevice.

#include <iostream>
#include <irrlicht.h>

using namespace irr;

int main(int argc, char *argv[])
{
  IrrlichtDevice *d = createDevice(
        video::EDT_OPENGL,
        core::dimension2d<u32>(800,600)
        );

  if ( d == nullptr ) {
    std::cerr << "Unable to create device" << std::endl;
    return -1;
  }

  std::cout << "Device created: " << d << std::endl;

  video::IVideoDriver *driver = d->getVideoDriver();
  scene::ISceneManager *manager = d->getSceneManager();

  d->setWindowCaption(L"Hello world!");

  scene::ISceneNode *n = manager->addAnimatedMeshSceneNode(
        manager->getMesh("/usr/share/irrlicht/media/sydney.md2"));

  if ( n == nullptr ) {
    std::cerr << "Unable to load requested mesh" << std::endl;
    return -1;
  }

  n->setMaterialTexture(0,
    driver->getTexture("/usr/share/irrlicht/media/sydney.bmp")
                        );
  n->setMaterialFlag( video::EMF_LIGHTING, false );
  manager->addCameraSceneNodeFPS();

  d->getFileSystem()->addFileArchive(
        "/usr/share/irrlicht/media/map-20kdm2.pk3"
        );
  manager->addOctreeSceneNode(manager->getMesh("maps/20kdm2.bsp"));

  while( d->run() && driver ) {
    driver->beginScene(true,
                       true,
                       video::SColor(255,100,0,255)
                       );
    manager->drawAll();
    driver->endScene();
  }

  d->drop();

  std::cout << "Done" << std::endl;
  return 0;
}