Cube Creation Help

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
KarneeKarnay
Posts: 8
Joined: Mon Dec 19, 2011 3:48 pm

Cube Creation Help

Post by KarneeKarnay »

I've just started using Irrlicht and I'm trying to build a class the streamlines the creation of shapes. For some reason the shapes I make aren't being shown, with no error output. Code below.

main.cpp

Code: Select all

#include <irrlicht.h>
#include "shapes.h"
#include <IGUISkin.h>
using namespace irr;
 
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
 
 
#ifdef _IRR_WINDOWS_
#pragma comment(lib, "Irrlicht.lib")
#pragma comment(linker, "/subsystem:windows /ENTRY:mainCRTStartup")
#endif
 
int main()
{
    IrrlichtDevice *device =
        createDevice( video::EDT_OPENGL, dimension2d<u32>(1440, 900), 16,
            false, false, false, 0);
 
    if (!device)
        return 1;
 
    device->setWindowCaption(L"Game v0.1");
 
    IVideoDriver* driver = device->getVideoDriver();
    ISceneManager* smgr = device->getSceneManager();
    IGUIEnvironment* guienv = device->getGUIEnvironment();
    IGUISkin* skin = guienv->getSkin();
 
    shapes shapeMngr;
    scene::ISceneNode *n;
    shapeMngr.createSquare(NULL, vector3df(0,0,0), 0,vector3df(0,0,0), vector3df(1,1,1), driver, smgr, n );
 
    smgr->addCameraSceneNode(0, vector3df(0,30,-40), vector3df(0,5,0));
 
    while(device->run())
    {[code]
        driver->beginScene(true, true, SColor(255,100,101,140));
 
        smgr->drawAll();
        guienv->drawAll();
 
        driver->endScene();
    }
 
    device->drop();
 
    return 0;
}[/code]

shapes.h

Code: Select all

#pragma once
#include <irrlicht.h>
#include <IGUISkin.h>
#include <iostream>
using namespace std;
using namespace irr;
 
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
 
class shapes
{
public:
    shapes(void);
    void createSquare(float size, vector3df position, int id, vector3df rotation, vector3df scale, IVideoDriver* urDriver, ISceneManager* urSceneManger, ISceneNode* n);
 
};
 
 
shapes.cpp

Code: Select all

#include "shapes.h"
 
 
shapes::shapes(void)
{
}
 
void createSquare(float size, vector3df position, int id, vector3df rotation, vector3df scale, IVideoDriver* urDriver, ISceneManager* urSceneManger, ISceneNode* n)
{
    if(n)
    n = urSceneManger->addCubeSceneNode();
    n->setMaterialTexture(0, urDriver->getTexture("Textures/default.jpg"));
    n->setPosition(position);
    n->setScale(scale);
    n->setRotation(rotation);
    n->setID = id;
    n->setMaterialFlag(video::EMF_LIGHTING, false);
 
}
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Re: Cube Creation Help

Post by Seven »

If ( n) create?

If (! N)
{
Addscenenode()
Set variables
}

Unless n is the parent?
christianclavet
Posts: 1638
Joined: Mon Apr 30, 2007 3:24 am
Location: Montreal, CANADA
Contact:

Re: Cube Creation Help

Post by christianclavet »

Look like you don't know how to instantiate a class. You keep all the data inside your application and try to send the data to function inside a class.

I would recommend to have your class have a "setup" function and you copy the data (scene manager, video driver, etc) to it and use simple parameters when you call the class.

For this to work, you must create an instance of your class and manage it pointer.

Something like this:

Code: Select all

shapes shapeMngr;
Would be like this:

Code: Select all

shapes* shapeMngr = new shapes();
And when you call "shapeMngr" it would change from this:

Code: Select all

shapeMngr.createSquare(NULL, vector3df(0,0,0), 0,vector3df(0,0,0), vector3df(1,1,1), driver, smgr, n );
Note: "n" is created but not initialized. If you create it in the application instead, set it at least to a NULL. Like this: n=NULL;

to this:

Code: Select all

shapeMngr->createSquare(NULL, vector3df(0,0,0), 0,vector3df(0,0,0), vector3df(1,1,1), driver, smgr, n );
So now , since the class is instantiated, you could "store" data inside this class by adding variables in your class.

If they are of the "public" type you could set them up like this: (if urDriver is defined in the class as Public type)

Code: Select all

shapeMngr->urDriver = device->getVideoDriver();
I would also recommend to change this:

Code: Select all

void createSquare(float size, vector3df position, int id, vector3df rotation, vector3df scale, IVideoDriver* urDriver, ISceneManager* urSceneManger, ISceneNode* n)
{
    if(n)
    n = urSceneManger->addCubeSceneNode();
to this:

Code: Select all

ISceneNode* createSquare(float size, vector3df position, int id, vector3df rotation, vector3df scale, IVideoDriver* urDriver, ISceneManager* urSceneManger)
{
    
    ISceneNode* n = urSceneManger->addCubeSceneNode();
    ...
    ...
    return n;
This would return the pointer of the new created node so you could manipulate it later...

Also at the end of your application if you don't need your class you should remove it from memory like this:

Code: Select all

delete shapeMngr;
Post Reply