Using Irrlicht with multiple Header files?

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
Stauricus
Posts: 20
Joined: Mon Jun 25, 2012 10:12 pm
Location: Uberlandia, Brazil

Using Irrlicht with multiple Header files?

Post by Stauricus »

hello everybody

i'm completely newbie both to programming and Irrlicht, so maybe this is a stupid and simple C++ related question :mrgreen:
i'm trying to make a little game with the main file ".cpp" and a header files ".h", to make it more readable. however, i'm having a little problem.

so far, what i did...

File main.cpp

Code: Select all

#include "title.h"
 
int main(){
    IrrlichtDevice *device = createDevice(EDT_OPENGL, dimension2d<u32>(800, 600), 16, false, false, false, 0);
    if (!device){
        return 1;
    }
 
    device->setWindowCaption(L"Test Game");
    IVideoDriver* driver = device->getVideoDriver();
    ISceneManager* smgr = device->getSceneManager();
    IGUIEnvironment* guienv = device->getGUIEnvironment();
 
    Title();
}
File title.h

Code: Select all

#ifndef TITLE_H_INCLUDED
#define TITLE_H_INCLUDED
 
#include <Irrlicht.h>
 
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
 
int Title(){
        while(device->run())
        {
                driver->beginScene(true, true, SColor(255,100,101,140));
                smgr->drawAll();
                guienv->drawAll();
                driver->endScene();
        }
    return 0;
}
 
#endif // TITLE_H_INCLUDED
the title.h is where i'd like to make a simple title screen...

as you can see, the main.cpp starts the basics of the engine, and then makes a call to the function Title, from title.h.
the error i get says that smgr, guienv, and driver "is not declared on this scope". well, i know that, as it's declared only on funcion main. as i can't declare it as global (or can i?) do i have do create these pointers in every function that will make use of Irrlicht??

thanks in advance :D
randomMesh
Posts: 1186
Joined: Fri Dec 29, 2006 12:04 am

Re: Using Irrlicht with multiple Header files?

Post by randomMesh »

Never, ever, use globals.

Instead, do something like this:

main.cpp

Code: Select all

 
#include "Game.h"
 
int main()
{
     Game game;
      if (game.init())
            game.run();
 
return 0;
}
 
Game.h

Code: Select all

 
#include <irrlicht>
 
class Game : public irr::IEventReceiver
{
 
public:
 
//constructor
Game() :
device(0) //always initialize your pointers!
{
 
} 
 
//destructor
~Game()
{
     if (device)
         device->drop(); //drop the device, otherwise memory leaks will occur!
}
 
bool init()
{
    //Irrlicht setup and other init stuff
    device = irr::createDecvice...
 
    //...
 
    device->setEventReceiver(this);
    return true;
}
 
void run()
{
    while (device->run())
    {
         //gamelogic and renderloop dependent on game state
    }
}
 
// must override
virtual bool OnEvent(const irr::SEvent& event)
{
  //if irrlicht sends events, you can access them here
  return false;
}
 
inline IrrlichtDevice* getDevice() const { return device; }
 
private:
 
IrrlichtDevice* device;
 
//Other members
 
};
 
All encapsulated in the game class, there's no need for globals. Just pass an object of the game class around if you need access to any of its members.

And don't use the using namespace directive in headers, it will pollute your namespace and can lead to name clashes!
Last edited by randomMesh on Wed Jun 27, 2012 12:55 pm, edited 6 times in total.
"Whoops..."
Stauricus
Posts: 20
Joined: Mon Jun 25, 2012 10:12 pm
Location: Uberlandia, Brazil

Re: Using Irrlicht with multiple Header files?

Post by Stauricus »

thanks for the answer! :mrgreen:
ok, i think i'm starting to understand this. so, i should make a class, and inside it, the funcions, etc.?
got it, no more using namespace in .h files...

could you just explain me this?

Code: Select all

public irr::IEventReceiver
and this?

Code: Select all

inline IrrlichtDevice* getDevice() const { return device; }

one more thing, declaring a global isn't the same as declaring something inside a class as public?

thanks!
Mel
Competition winner
Posts: 2292
Joined: Wed May 07, 2008 11:40 am
Location: Granada, Spain

Re: Using Irrlicht with multiple Header files?

Post by Mel »

No. A global is a variable that is known program-wide, and so, it can be accesed from anywhere, while it is useful on its own, it is never a recomendable practice because of all the potential side effects, so it SHOULD BE AVOIDED at all costs.

A public atribute, on the other hand, is an atribute which is freely accesable an modifiable from any other code, the diference is that each instance of that class has its own copy of the atribute, so modifying one won't affect any other. besides, there are the friend classes which can access private members from other classes, so the access to the data can be more controlled and, normally, if there is any type of restriction, the classes use to implement also the getter/setter methods to modify and access those attributes marked as private, while this is a good practice, it is sometimes too annoying, or somewhat ineficient to call a function to modify a simple variable which has no restrictions on its own, so, they are simply marked public, and can be modified easier.

Read about C++,class and structure definitions for more stuff regarding this
"There is nothing truly useless, it always serves as a bad example". Arthur A. Schmitt
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: Using Irrlicht with multiple Header files?

Post by hendu »

Eh, globals are just like goto. Not the plague, but use only with taste.
Stauricus
Posts: 20
Joined: Mon Jun 25, 2012 10:12 pm
Location: Uberlandia, Brazil

Re: Using Irrlicht with multiple Header files?

Post by Stauricus »

oh, i see. thanks for the clarifiation.

one more thing, this line

Code: Select all

inline IrrlichtDevice* getDevice() const { return device; }
is for check if Irrlicht is still running?
not easy for me to understand all this at once...

thanks :mrgreen:
randomMesh
Posts: 1186
Joined: Fri Dec 29, 2006 12:04 am

Re: Using Irrlicht with multiple Header files?

Post by randomMesh »

No. This is a so called getter method, which allows access to the Irrlicht device like this

Code: Select all

 
Game game;
game.init();
 
irr::IrrlichtDevice* device = game.getDevice();
 
could you just explain me this? public irr::IEventReceiver
The Game class inherits Irrlichts IEventReceiver class. Read up on inheritance, it's important.
Basically, your Game class IS an EventReceiver too.

Oh, and i added a constructor and a destructor to the Game class, please review it. It's important to properly cleanup stuff.
"Whoops..."
Stauricus
Posts: 20
Joined: Mon Jun 25, 2012 10:12 pm
Location: Uberlandia, Brazil

Re: Using Irrlicht with multiple Header files?

Post by Stauricus »

ok, got it. :)
and if I want to make another header, let's say a title.h and a game.h, i'd have to start the creation of devices and everything over again?

thanks!
randomMesh
Posts: 1186
Joined: Fri Dec 29, 2006 12:04 am

Re: Using Irrlicht with multiple Header files?

Post by randomMesh »

No, you only need one device. Just pass your game object around. I'd do it like this:

Title.h

Code: Select all

 
class Title
{
 
public:
 
Title(Game* game) :
game(game)
{
     irr::IrrlichtDevice = game->getDevice();
}
 
}
 
private:
 
Game* game;
};
 
Game.cpp

Code: Select all

 
 
bool Game::init()
{
      //setup irrlicht
       device = irr::createDevice...
 
      //setup YOUR classes
     title = new Title(this);
 
     return true;
}
 
"Whoops..."
Mel
Competition winner
Posts: 2292
Joined: Wed May 07, 2008 11:40 am
Location: Granada, Spain

Re: Using Irrlicht with multiple Header files?

Post by Mel »

Another thing i'd say. inline functions actually aren't compiled into real functions, so, i'd advise to use them on small pieces of code only. They are translated directly into the instructions they contain, instead of creating the code for a function call. While the advantage is that their code runs a bit faster because there isn't a function call involved, they may make the program unnecesarily big.

Just in case you didn't know already :)
"There is nothing truly useless, it always serves as a bad example". Arthur A. Schmitt
Stauricus
Posts: 20
Joined: Mon Jun 25, 2012 10:12 pm
Location: Uberlandia, Brazil

Re: Using Irrlicht with multiple Header files?

Post by Stauricus »

great! thanks guys! you can bet i didn't knew about inlines too :/

i feel a bit lost, but i'll play around with you showed me up to now.
i've followed some c++ tutorials for a while, but i feel like i still know almost nothing, hahaha.


just for curiosity: how you guys usually split code in headers and source files when making games with Irrlicht? because now it seems that what i'm trying to do with headers is kinda nonsense...
any suggestion is appreciated! thanks again! :D
serengeor
Posts: 1712
Joined: Tue Jan 13, 2009 7:34 pm
Location: Lithuania

Re: Using Irrlicht with multiple Header files?

Post by serengeor »

Stauricus wrote:just for curiosity: how you guys usually split code in headers and source files when making games with Irrlicht? because now it seems that what i'm trying to do with headers is kinda nonsense...
any suggestion is appreciated! thanks again! :D
There are a lot of opensource games, just look at those to see how other people do it :)
Working on game: Marrbles (Currently stopped).
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Re: Using Irrlicht with multiple Header files?

Post by hybrid »

You usually put a class per file. The layout of the whole system is that art of programming. There are some rules of thumb, but usually more than you can use in the next ten projects. You need to find the right one, and apply them properly. You will get a good feeling after doing enough larger projects. But as it seems you should do many small examples first. As you have to know the basics, i.e. syntax first.
Rusk
Posts: 7
Joined: Fri Feb 03, 2012 1:51 am

Re: Using Irrlicht with multiple Header files?

Post by Rusk »

cotton wrote:as you can see, the main.cpp starts the basics of the engine, and then makes a call to the function Title, from title.h.
the error i get says that smgr, guienv, and driver "is not declared on this scope". well, i know that, as it's declared only on funcion main. as i can't declare it as global (or can i?) do i have do create these pointers in every function that will make use of Irrlicht??
You can do what randomMesh said above:
randomMesh wrote: All encapsulated in the game class, there's no need for globals. Just pass an object of the game class around if you need access to any of its members.

And don't use the using namespace directive in headers, it will pollute your namespace and can lead to name clashes! return device;
So you would call title(game), and in title use game->smgr.
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Using Irrlicht with multiple Header files?

Post by CuteAlien »

@Rusk: In case you wonder why Cotton is gone. He was just just a spammer copy-pasting random forum-messages from other threads.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Post Reply