Crash when setting parameters of node made from class method

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
todor943
Posts: 3
Joined: Sat Sep 12, 2009 4:03 pm

Crash when setting parameters of node made from class method

Post by todor943 »

Hello,
I decided to go OOP on my project and started to rewrite it from scratch to see how it goes. Here is my problem : when I try to set an MD2 animation to my node everything crashes. Here is my code below:
the cgame.h header:

Code: Select all

 
#ifndef CGAME_H_INCLUDED
#define CGAME_H_INCLUDED
#include <irrlicht.h>
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
 
class cgame
{
    private:
        void setMap(stringw path)
        {
            IMesh * mapmesh = smgr->getMesh(path);
            IMeshSceneNode * map = smgr->addMeshSceneNode(mapmesh);
            map->setMaterialFlag(EMF_LIGHTING, false);
        }
        SIrrlichtCreationParameters setParams()
        {
            SIrrlichtCreationParameters params;
            params.WindowSize = dimension2d<u32>(800,600);
            params.DriverType = EDT_OPENGL;
            return params;
        }
        ICameraSceneNode * makeCamera()
        {
            camera = smgr->addCameraSceneNodeFPS(0);
        }
    public:
        IrrlichtDevice *device = createDeviceEx(setParams());
        IVideoDriver* driver = device->getVideoDriver();
        ISceneManager* smgr = device->getSceneManager();
        ICameraSceneNode * camera = makeCamera();
        cgame(stringw path)
        {
            setMap(path);
        }
 
        // MAKING THE NODE HERE <--- 
        IAnimatedMeshSceneNode * setChar(stringw path, stringw texture_path)
        {
            IAnimatedMeshSceneNode * charnode = smgr->addAnimatedMeshSceneNode(smgr->getMesh(path));
            charnode->setMaterialTexture(0, driver->getTexture(texture_path));
            charnode->setMaterialFlag(EMF_LIGHTING, false);
        }
};
#endif
 
and the main.cpp

Code: Select all

 
#include <irrlicht.h>
#include "cgame.h"
 
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
 
int main()
{
    cgame game("map2.3ds");
    IAnimatedMeshSceneNode *character = game.setChar("sydney.md2", "sydney.bmp");
 
    // crash when setting animation
    character->setMD2Animation(EMAT_STAND);
 
    while(game.device->run())
    {
        game.driver->beginScene(true, true, SColor(0,200,200,200));
        game.smgr->drawAll();
        game.driver->endScene();
    }
    game.device->drop();
 
    return 0;
}
 
This is the first time I'm doing anything object oriented :P
Cheers!
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: Crash when setting parameters of node made from class me

Post by hendu »

Your function returns nothing, and *character is then undefined.

Your debugger would have told you so...
jaworekplay
Posts: 36
Joined: Mon Jan 23, 2012 11:14 pm
Location: Lancs, UK

Re: Crash when setting parameters of node made from class me

Post by jaworekplay »

+1 to hendu's reply it should be:

Code: Select all

IAnimatedMeshSceneNode * setChar(stringw path, stringw texture_path)
        {
            IAnimatedMeshSceneNode * charnode = smgr->addAnimatedMeshSceneNode(smgr->getMesh(path));
            charnode->setMaterialTexture(0, driver->getTexture(texture_path));
            charnode->setMaterialFlag(EMF_LIGHTING, false);
            return charnode;
        }
 
Post Reply