Having the IrrlichtDevice in a main game class

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Tanuva
Posts: 54
Joined: Tue Oct 10, 2006 6:49 pm
Location: 200 metres behind the moon
Contact:

Having the IrrlichtDevice in a main game class

Post by Tanuva »

I should return to c#... I simply want to have the IrrlichtDevice, IVideoDriver and ISceneManager collected in a central class CGame. Trying to do it like this:

Extract from the header (constructor):

Code: Select all

IrrlichtDevice* device;
video::IVideoDriver* driver;
scene::ISceneManager* smgr;
And the corresponding implementation extract:

Code: Select all

CGame::device = createDevice(driverType, res, bpp, fullscreen, stencilbuffer, vsync, CGame::eventrec);
CGame::driver = CGame::device->getVideoDriver();
CGame::smgr = CGame::device->getSceneManager();
Now g++ gives me such errors for each of the classes:
CGame.h:22: error: ISO C++ forbids initialization of member ‘device’
CGame.h:22: error: making ‘device’ static
CGame.h:22: error: invalid in-class initialization of static data member of non-integral type ‘irr::IrrlichtDevice*’
Can anyone tell me how I get my IrrlichtDevice constructed in the CGame class...?
Tanuva
JonLT
Posts: 152
Joined: Thu Mar 15, 2007 5:47 pm
Location: Denmark

Post by JonLT »

I don't really understand your errors but here is what I'd do:

Code: Select all

class CGame
{
public:
    //constructor
    CGame(driverType, blah, blah blah)
    { 
        device = createDevice(driverType, blah, blah blah);
        smgr = device->getSceneManager();
        driver = device->getVideoDriver();
    }
private:
    IrrlichtDevice *device;
    scene::ISceneManager *smgr;
    video::IVideoDriver *driver;
};
I haven't tested it, but it should work
Tanuva
Posts: 54
Joined: Tue Oct 10, 2006 6:49 pm
Location: 200 metres behind the moon
Contact:

Post by Tanuva »

That seems to be nearly what I had before, with the difference that I had the constructor implementation in the cpp-file, too.... Gonna test it tomorrow, hope it works! :)
Tanuva
JonLT
Posts: 152
Joined: Thu Mar 15, 2007 5:47 pm
Location: Denmark

Post by JonLT »

if you want the constructor in a different file try this:
CGame.h

Code: Select all

class CGame 
{ 
public: 
    //constructor 
    CGame(..arguments..)
private: 
    IrrlichtDevice *device; 
    scene::ISceneManager *smgr; 
    video::IVideoDriver *driver; 
}; 
CGame.cpp

Code: Select all

#include "CGame.h"
CGame::CGame(..arguments..)
{
    device = createDevice(...arguments...); 
    smgr = device->getSceneManager(); 
    driver = device->getVideoDriver();
}
Dances
Posts: 454
Joined: Sat Jul 02, 2005 1:45 am
Location: Canada
Contact:

Post by Dances »

I have a bit more... complex version of your code in my current game;

Code: Select all

#ifndef GLOBALS_H
#define GLOBALS_H

#include "irrlicht.h"
#include "irrKlang.h"

using namespace irr;

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

class globals
{
      public:
         int getGameState(); 
         bool isGameRunning(), setGameState(int), getGameOver();
         IrrlichtDevice* getDevice();
         IVideoDriver* getDriver();
         ISoundEngine* getSound();
         IGUIEnvironment* getGUIEnv();
         void setGameRunning(bool), setDevice(IrrlichtDevice*), initialize(), setupIrrlicht(),
            setGameOver(bool), setSound(ISoundEngine*);
      private:
         IrrlichtDevice *device;
         ISoundEngine *sound;
         int gamestate;
         bool gamerunning, gameover;
};

#endif

Code: Select all

#include "irrlicht.h"
#include "irrKlang.h"
#include "globals.h"

using namespace irr;

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

void globals::initialize()
{
     device = 0;
     gamerunning = true;
     gamestate = 1;
}

int globals::getGameState()
{
    return gamestate;
}

bool globals::isGameRunning()
{
    return gamerunning;
}

IrrlichtDevice* globals::getDevice()
{
   return device;
}

ISoundEngine* globals::getSound()
{
   return sound;
}

bool globals::setGameState(int newvalue)
{
     if(newvalue  > 0 && newvalue < 3)
        gamestate = newvalue;
     else
        return false;
     return true;
}

void globals::setGameRunning(bool newvalue)
{
     gamerunning = newvalue;
}

void globals::setDevice(IrrlichtDevice* newvalue)
{
     device = newvalue;
}

bool globals::getGameOver()
{
    return gameover;
}

void globals::setGameOver(bool newvalue)
{
     gameover = newvalue;
}

void globals::setSound(ISoundEngine* newvalue)
{
     sound = newvalue;
}
I create my device with

Code: Select all

globals gamevariables.setDevice(createDeviceEx(param));
perhaps reading all this will give you an idea of what is wrong...
kompromis
Posts: 98
Joined: Mon Sep 11, 2006 2:36 pm
Location: sweden/stockholm

Post by kompromis »

you cant' set the value in the class only initialize it
Tanuva
Posts: 54
Joined: Tue Oct 10, 2006 6:49 pm
Location: 200 metres behind the moon
Contact:

Post by Tanuva »

Hmpfgrml... In a fresh environment the class-in-class works.. But in my "real" code it doesnt. Seems like I restart off from scratch, Dances' example was a quite like mine. My code looks like yours, but it wont work. C++ is [very bad word] hard... ;)
Tanuva
Dances
Posts: 454
Joined: Sat Jul 02, 2005 1:45 am
Location: Canada
Contact:

Post by Dances »

I know how you feel. I'm having an issue with my mouse pointer with the same effect; it works in a new project but not in my majorly developed one -.-
swesoulc
Posts: 17
Joined: Tue Apr 03, 2007 10:05 am

Post by swesoulc »

a sidetrack to this thread

if I structure my code as ...
Single .cpp file
class IEventReceiver

int main ()
/* everything down here */
... then everything works fine, gui , 3d everything

But If I structure it as this thread implies, and as I have done before in other projects ( non irrlicht )
Single .h file
class game : public IEventreceiver
/* globals, variables, devices, functions , declarations*/

and then the obvious .cpp file where : device setEventreceiver ( this )
now I know I am not showing much of my code
but it is the structure that messes it up somehow I am sure

The way it messes it up is, all my GUI becomes non functional.. broken... busted... takes no commands what so ever... buttons dun work, editboxes and so on and so on

Does irrlicht have any know issues about having problems with it's IGUIEnvironment* being stored in a class

yes I know this sounds ridicules
Post Reply