Irrlicht as Singleton

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
Halan
Posts: 447
Joined: Tue Oct 04, 2005 8:17 pm
Location: Germany, Freak City
Contact:

Irrlicht as Singleton

Post by Halan »

Hey Guys,

for an old project i encapsulated Irrlicht into a singleton Class. Kinda useful if youre not in the mood to pass pointers around.

HEADER:

Code: Select all

#ifndef _INCLUDE_GRAPHICS_
#define _INCLUDE_GRAPHICS_

#include <irrlicht.h>
#include "..\Both\CLogger.h"

class CClient;

#define graphics CGraphics::Instance()
#define pIrrlicht CGraphics::Instance()->getIrrlicht()
#define pSmgr CGraphics::Instance()->getIrrlicht()->getSceneManager()
#define pGuienv CGraphics::Instance()->getIrrlicht()->getGUIEnvironment()
#define pFsystem CGraphics::Instance()->getIrrlicht()->getFileSystem()
#define pDriver CGraphics::Instance()->getIrrlicht()->getVideoDriver()

//Used so that i dont have to declare them every time
using namespace irr;
using namespace core;
using namespace video;
using namespace io;
using namespace scene;
using namespace gui;

class CGraphics
{
    private:
        IrrlichtDevice* irrlicht;

        //Singleton Pattern
        static CGraphics* instance;
        static IEventReceiver* receiver;
    public:
        CGraphics();
        ~CGraphics();

        void setEventReceiver(IEventReceiver* receiver_);
        bool isIrrlichtRunning();
        bool setupIrrlicht(SIrrlichtCreationParameters* params = 0);
        void removeIrrlicht();
        void renderAll(bool drawGUI = true);
        IrrlichtDevice* getIrrlicht();

        //Singleton Pattern
        static CGraphics* Instance();
        void destroy();
};

#endif //_INCLUDE_GRAPHICS_
CPP FILE:

Code: Select all

#include "CGraphics.h"

CGraphics* CGraphics::instance = 0;
IEventReceiver* CGraphics::receiver = 0;

CGraphics* CGraphics::Instance()
{
    if(!instance)
        instance = new CGraphics();

    return instance;
}

void CGraphics::setEventReceiver(IEventReceiver* receiver_)
{
    receiver = receiver_;
}

void CGraphics::destroy()
{
    if(instance)
        delete instance;

    instance = 0;
}

CGraphics::CGraphics()
{
    irrlicht = 0;
}

bool CGraphics::isIrrlichtRunning()
{
    if(irrlicht)
        return true;
    else
        return false;
}

bool CGraphics::setupIrrlicht(SIrrlichtCreationParameters* params)
{
    if(params)
    {
        params->EventReceiver = receiver;
        irrlicht = createDeviceEx(*params);
    }
    else
    {
        //Allternative Fallback
        irrlicht = createDevice(EDT_DIRECT3D9, dimension2d<s32>(1024,768), 32, false ,true, true, receiver);
    }

    if(!irrlicht)
    {
        logger->log(EMT_ERROR, "Couln't create render Device");
        return false;
    }

    logger->log(EMT_INFO, "Irrlicht initialized");
    return true;

}

void CGraphics::renderAll(bool drawGUI)
{
        pDriver->beginScene(true, true, 0);
        pSmgr->drawAll();        
        if(drawGUI)
            pGuienv->drawAll();
        pDriver->endScene();
}

IrrlichtDevice* CGraphics::getIrrlicht()
{
    return irrlicht;
}

void CGraphics::removeIrrlicht()
{
    if(!this->isIrrlichtRunning())
        return;

    //Stupid but this will end EVERYTHING
    irrlicht->closeDevice();
    irrlicht->run();
    irrlicht->drop();
    irrlicht = 0;

    logger->log(EMT_INFO, "IrrlichtDevice closed");
}

CGraphics::~CGraphics()
{
    this->removeIrrlicht();
}
if you want code for the logger too ill post it

greetings,
Halan

edit: i thin the static event receiver is a bit senseless ;)
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Post by MasterGod »

Thats Great for me ! thanks!
BlindSide
Admin
Posts: 2821
Joined: Thu Dec 08, 2005 9:09 am
Location: NZ!

Post by BlindSide »

Two little quirks

1. The event reciever should be inited to 0 in the contructor (Else if the fallback happens when creating the device it may crash if the event reciever has not been set.)

2. Also, if the Irrlicht device exists, then setting the event reciever should call the "setEventReciever" function on the current device.
ShadowMapping for Irrlicht!: Get it here
Need help? Come on the IRC!: #irrlicht on irc://irc.freenode.net
Post Reply