static IrrlichtHelper class

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
odenter
Posts: 48
Joined: Thu Oct 21, 2004 10:35 am
Location: Bremen (Germany)

static IrrlichtHelper class

Post by odenter »

It's to avoid passing parameters like IrrlichtDevice*, ISceneManager* and so on.
Maybe this isn't new, but I'll post it. Could be easy extended.

irrlichthelper.h

Code: Select all

#pragma once

#include <irrlicht.h>

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

class IrrlichtHelper {
private:
  static IrrlichtDevice *_irrlichtDevice;
  static IVideoDriver *_videoDriver;
  static ISceneManager *_sceneManager;
  static s32 _width;
  static s32 _height;
  static s32 _bpp;
  static bool _fullscreen;

protected:
 

public:

  IrrlichtHelper(void);
  ~IrrlichtHelper(void);

  static bool Init(s32 width, s32 height, s32 bpp, bool fullscreen, IEventReceiver *eventReceiver) {
    IrrlichtHelper::_width = width;
    IrrlichtHelper::_height = height;
    IrrlichtHelper::_bpp = bpp;
    IrrlichtHelper::_fullscreen = fullscreen;

    if (Device(eventReceiver) == 0) {
      return false;
    }    

    if (Driver() == 0) {
      return false;
    }

    return true;
  }

  static IrrlichtDevice *Device() {
    return Device(0);
  }

  static IrrlichtDevice *Device(IEventReceiver *eventReceiver) {
    if (IrrlichtHelper::_irrlichtDevice == 0) {
      IrrlichtHelper::_irrlichtDevice = createDevice(video::EDT_OPENGL, dimension2d<u32>(_width, _height), _bpp, _fullscreen, false, false, eventReceiver);
    }
    return IrrlichtHelper::_irrlichtDevice;
  }

  static IVideoDriver *Driver() {
    if (IrrlichtHelper::_videoDriver == 0) {
      IrrlichtHelper::_videoDriver = IrrlichtHelper::Device()->getVideoDriver();
    }
    return IrrlichtHelper::_videoDriver;
  }

  static ISceneManager *Smgr() {
    if (IrrlichtHelper::_sceneManager == 0) {
      IrrlichtHelper::_sceneManager = IrrlichtHelper::Device()->getSceneManager();
    }
    return IrrlichtHelper::_sceneManager;
  }

  static int Width() {
    return IrrlichtHelper::_width;
  }

  static int Height() {
    return IrrlichtHelper::_height;
  }

  static int Bpp() {
    return IrrlichtHelper::_bpp;
  }

  static bool Fullscreen() {
    return IrrlichtHelper::_fullscreen;
  }

};
irrlichthelper.cpp

Code: Select all

#include "IrrlichtHelper.h"

IrrlichtDevice *IrrlichtHelper::_irrlichtDevice = 0;
IVideoDriver *IrrlichtHelper::_videoDriver = 0;
ISceneManager *IrrlichtHelper::_sceneManager = 0;
int IrrlichtHelper::_width = 0;
int IrrlichtHelper::_height = 0;
int IrrlichtHelper::_bpp = 0;
bool IrrlichtHelper::_fullscreen = false;

//! constructor
IrrlichtHelper::IrrlichtHelper(void) {
}

//! destructor
IrrlichtHelper::~IrrlichtHelper(void) {
}
usage

Code: Select all

 if (!IrrlichtHelper::Init(1024, 768, 16, false, this)) {
    return false;
  }
  while(IrrlichtHelper::Device()->run()) {
    if (IrrlichtHelper::Device()->isWindowActive()) {
      // render something
    } else {
      IrrlichtHelper::Device()->yield();
    }
  }
  IrrlichtHelper::Device()->drop();
REDDemon
Developer
Posts: 1044
Joined: Tue Aug 31, 2010 8:06 pm
Location: Genova (Italy)

Post by REDDemon »

nice work. what advantages there are instead of using a Context struct with all the pointers needed ?
Junior Irrlicht Developer.
Real value in social networks is not about "increasing" number of followers, but about getting in touch with Amazing people.
- by Me
vinjn
Posts: 27
Joined: Tue May 20, 2008 4:45 am

Post by vinjn »

I am afraid to say it's oo simple, you even don't have option to choose D3D.
More helper functions need to be added to make it really usable.
Post Reply