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_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();
}greetings,
Halan
edit: i thin the static event receiver is a bit senseless