Creating a "layer" between Irrlicht and custom OO

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
d3lay
Posts: 8
Joined: Thu Jan 15, 2009 10:20 pm

Creating a "layer" between Irrlicht and custom OO

Post by d3lay »

Hi there.
Is there any option for programming irrlicht in a custom Oriented Object code ?

let me explain.

i know that irrlicht has a lot of functions for make anything i want, but in some moments the engine takes so much code that i lost myself.

I´m just wondering, it´s healthy for the code standards use the structure like i´m doing below ?

Code: Select all

#ifndef APP_H
#define APP_H

#include <irrlicht.h>
using namespace irr;
using namespace core;
using namespace video;


class app{

  protected:
    IrrlichtDevice *m_dispositivo;

  private:
    bool m_stencil;
    bool m_vsync;



  public:
    /**largura,altura,pbits,fullscreen?*/
    app(int,int,int,bool);
    ~app();

    bool run();
    IrrlichtDevice *dispositivo();


};

#endif

Code: Select all

#include "app.h"
using namespace irr;


app::app(int l_largura, int l_altura, int l_pbits, bool l_fullscreen){


  this->m_stencil       = false;
  this->m_vsync         = false;

  this->m_dispositivo   = createDevice(EDT_OPENGL,
                        dimension2d<s32>(l_largura,l_altura),
                        l_pbits,
                        l_fullscreen,
                        this->m_stencil,
                        this->m_vsync,
                        0
                        );

}




app::~app(){

 delete(this->m_dispositivo);
}




IrrlichtDevice *app::dispositivo(){
  return m_dispositivo;
}

bool app::run(){
 return this->m_dispositivo->run();
}


there is a snag of my device class implementation. i´m thinking to split the most important functions of irrlicht each one in on custom class that i made.

i have another for video

Code: Select all

#ifndef VIDEO_H
#define VIDEO_H

#include <irrlicht.h>
#include "app.h"
using namespace irr;
using namespace video;



class Cvideo {

  protected:
    IVideoDriver   *m_video;
  private:
    bool m_backbuffer;
    bool m_zbuffer;



  public:
    Cvideo();
    void clear(int,int,int,int);
    void render();


};

#endif

Code: Select all

#include "video.h"


Cvideo::Cvideo(){
 this->m_backbuffer = true;
 this->m_zbuffer = true;

}

void Cvideo::clear(int l_r, int l_g, int l_b, int l_a){
     this->m_video->beginScene(
                    m_backbuffer,
                    m_zbuffer,
                    SColor(l_a,l_r,l_g,l_b),0,0);
}

void Cvideo::render(){
     this->m_video->endScene();
another for a class called WORLD that stores all that a world would need, like camera, terrain, skybox and etc...

Code: Select all

#ifndef WORLD_H
#define WORLD_H

#include <irrlicht.h>
using namespace irr;
using namespace core;
using namespace video;
using namespace gui;
using namespace io;
using namespace scene;

class world {

    private:
        ISceneManager       *m_cena;
        IVideoDriver        *m_video;
        ICameraSceneNode    *m_camera;
        s32 cameraId;


    public:
        ISceneManager *cena();
        world(IrrlichtDevice*);
        void addCamera(vector3df,vector3df);
        void SkyBox(char*,char*,char*,char*,char*,char*);


};

#endif

Code: Select all

#include "world.h"

class game;

world::world(IrrlichtDevice* l_dispositivo) {

 this->m_cena   = l_dispositivo->getSceneManager();
 this->m_video  = l_dispositivo->getVideoDriver();
 cameraId = 0;


}

ISceneManager *world::cena(){
return this->m_cena;
}


void world::addCamera(vector3df l_posicao, vector3df l_target){
this->m_camera = m_cena->addCameraSceneNode(0,l_posicao,l_target,cameraId);
cameraId++;
}

void world::SkyBox(char *l_up,char * l_down,char * l_left,char * l_right,char * l_front ,char * l_back){

    m_cena->addSkyBoxSceneNode(
                m_video->getTexture(l_up),
                m_video->getTexture(l_down),
                m_video->getTexture(l_left),
                m_video->getTexture(l_right),
                m_video->getTexture(l_front),
                m_video->getTexture(l_back));



}
and finally i have a MASTER CLASS calle JOGO (game in portuguese).

Code: Select all

#ifndef GAME_H
#define GAME_H

#include <irrlicht.h>
using namespace irr;
using namespace core;
using namespace video;
using namespace gui;
using namespace io;

#include "app.h"
#include "video.h"
#include "world.h"

    class game : public app, public Cvideo {

        private:
            int  m_largura;
            int  m_altura;
            int  m_pbits;
            bool m_fullscreen;

        public:
            game(int,int);
            ~game();


    };

#endif

Code: Select all

#include "game.h"

game::game(int l_largura, int l_altura)
: app(l_largura,l_altura,32,false),
 Cvideo() {

    this->m_largura     = l_largura;
    this->m_altura      = l_altura;
    this->m_pbits       = 32;
    this->m_fullscreen  = false;

    this->m_video = dispositivo()->getVideoDriver();



}


game::~game(){
 delete(m_video);
 delete(m_dispositivo);

}
note that i use and inheritance of video and app for game.

Now, i have de MAIN.CC that keep the things more cleaner.

Code: Select all

#include "game.h"

int main() {
    game *jogo = new game(800,600);

    world *mundo = new world(jogo->dispositivo());
    mundo->SkyBox("1.jpg","1.jpg","1.jpg","1.jpg","1.jpg","1.jpg");
    mundo->addCamera(vector3df(0,0,0),vector3df(0,0,100));




    while(jogo->run()){
        jogo->clear(150,150,150,255); //Limpa os buffers
        mundo->cena()->drawAll();

        jogo->render(); //Renderiza
    }


    return 0;
}
I´m doing the right thing ? Note that the only purpose for this, is to get the main program a little more cleaner. If you think, i have a bunch of classes, but the only thing that matters is the main program for a newbie programmer, where he just use game *jogo = new game(800,600); and with var jogo he can call all the rest without concerning with IscenesNode, IcameraNode, I***Nodes and another I´s.

Am i getting clear in my doubt ? I hope you guys can help-me, i´m just a regular programmer trying to make the best way to non-pros or newbies understand irrlicht.

Thanx a lot :D

U rock!
Cloudef
Posts: 123
Joined: Wed Dec 03, 2008 9:02 pm
Location: Finland

Post by Cloudef »

Check out this thing i wrote, i guess its about same subject :lol:
d3lay
Posts: 8
Joined: Thu Jan 15, 2009 10:20 pm

Post by d3lay »

Cloudef wrote:Check out this thing i wrote, i guess its about same subject :lol:
yeah right, but my question is if is healthy to create one class for each device.

like your code but splitting one class for device, another class for video, another for gui, another for scenes and so on...
Kalango
Posts: 157
Joined: Thu Apr 26, 2007 12:46 am

Post by Kalango »

IMO its more than healthy :P, its a good way to avoid those structured/OO hybrid code....The only things you must look out for is the memory allocation and disposal and to go carefully when making devices and more devices....drivers, etc. I like to make them(device pointers and stuff you need constantly) static variables or something realy easy to point at any part of the code...
d3lay
Posts: 8
Joined: Thu Jan 15, 2009 10:20 pm

Hei

Post by d3lay »

Hei man, thanx.
i think that you guys should understand more my problem viewing the code below;


this is a fragment of a class WORLD that in this exact momento has a SCENEMANAGER and TERRAINNODE.

The first question: If i assume that world must have at least a NODE FOR CAMERA, A NODE FOR TERRAIN, a NODE FOR SKYBOX, i´m getting to the right way ?

i mean, look at this fragment.

Code: Select all

world *my_world = new world(my_game->device());

//adding a skybox
world->Skybox("1.jpg","2.jpg","3.jpg","4.jpg","5.jpg","6.jpg");

//adding a camera
world->addcamera() // this is for the moment an IRRLICHT FPS CAMERA

//adding a terrain
world->addTerrain("heightmap.jpg");
//material light ?
world->setTerrainMaterialLight(false);
//yeah, cos i wanna texture
world->setTerrainTexture("texture.jpg");
//and a detail map
world->setTerrainDetail("detail.jpg");
The code above, works fine a lot, but the problem comes when a try to implement and method for scaling, positioning or rotating my terrain after i create it... Assuming that ITerrainSceneNode doesn´t have a method to get the rendered terrain and scale it (or another geometric function), when i call

Code: Select all

world->setTerrainScale(10,10,10);
i have a second terrain rendered across the first terrain. Is there any function or any way to get the rendered terrain and work specially on this ?

the way that i´m writing setTerrainScale is to get these 3 values and put on a vector3df m_terrainscale and after i call again an addTerrainSceneNode().

Ok, this is horrible, but, is there any option to UpdateTerrainSceneNode()" or something like that ?
d3lay
Posts: 8
Joined: Thu Jan 15, 2009 10:20 pm

Post by d3lay »

UP!
Post Reply