need some help with classes...

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
janw
Posts: 13
Joined: Wed Aug 11, 2010 2:23 pm

need some help with classes...

Post by janw »

Hi :D
I need some help with my code.. Basically its breaks at the run time.. It is hello world program which works fine in one cpp file but when I started mess around with classes this nasty thing came up..

The error:

Unhandled exception at 0x00141359 in irrGame_ver_0.4.exe: 0xC0000005: Access violation reading location 0xbaadf00d.

The code:


Game.h

Code: Select all

#include "irrlicht.h"
#include "sydney.h"

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

#ifdef _IRR_WINDOWS_
#pragma comment(lib, "Irrlicht.lib")
#pragma comment(linker, "/subsystem:windows /ENTRY:mainCRTStartup")
#endif





class Game
{
private:
	IrrlichtDevice *device;
	scene::ISceneManager *smgr;
	video::IVideoDriver *driver;
	gui::IGUIEnvironment *guienv;
	scene::ICameraSceneNode *camera;
public:

	Game();
	~Game();
};

Game.cpp

Code: Select all

#include "Game.h"


Game::Game()
{
	{

	device = createDevice(video::EDT_DIRECT3D9,core::dimension2d<u32>(800,600),16,false,false,false,0);
	driver = device->getVideoDriver();
	smgr = device->getSceneManager();
	guienv = device->getGUIEnvironment();
	camera = smgr->addCameraSceneNodeFPS();
	
	sydney* Sydney = new sydney;
	
	while(device->run())
		{
		driver->beginScene();
		smgr->drawAll();
		guienv->drawAll();
		driver->endScene();
		}

	device->drop();	
	}
}
Game::~Game()
{
}

Sydney.h

Code: Select all

#include "irrlicht.h"
using namespace irr;
class sydney
{

private:
	IrrlichtDevice *device;
	sc[code]ene::ISceneManager *smgr;
	video::IVideoDriver *driver;
	scene::IAnimatedMesh *mesh;
	scene::IAnimatedMes
hSceneNode *node;

public:

sydney();
~sydney();


};
[/code]
Sydney.cpp

Code: Select all

#include "sydney.h"
sydney::sydney()
{
	
	mesh = smgr->getMesh("../../../media/sydney.md2");
	if (!mesh)
	{
		device->drop();
		
	}

	node = smgr->addAnimatedMeshSceneNode(mesh);
	if (node)
	{
		node->setMaterialTexture(0,driver->getTexture("../../../media/sydney.bmp"));

		node->setMaterialFlag(video::EMF_LIGHTING,false);
	}
	
}
sydney::~sydney()
{
	delete mesh;
	mesh = 0;
}
mainThread.cpp

Code: Select all

#include "Game.h"

int main()
{
	Game *game = new Game;
	return 0;
}
The program breaks in Sydney.h at this point

Code: Select all

mesh = smgr->getMesh("../../../media/sydney.md2");
What am I missing?? Can some one point me in the right direction please?
[/code]
Lonesome Ducky
Competition winner
Posts: 1123
Joined: Sun Jun 10, 2007 11:14 pm

Post by Lonesome Ducky »

I'm guessing that smgr was never defined to the class. I'm seeing the pointer declaration for the SceneManager, but I'm never seeing it set to anything.
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Post by Seven »

recommendation :

Code: Select all


class engine
{
public:
    engine() 
     {
         m_Valid = false;
         m_Manager = 0;
         m_Driver = 0;
         // etc ........
      }
   
    ~engine()
     {
         if (m_Valid) destroy();
      }

    bool create(screensize, drivername, bpp, etc...)
   {
       driver = createDriver(parameters);
       if (!driver) { printf("error creating driver\n"); return false; } 
       m_Manager = createManager(parameters);
       if (!m_Manager) { printf("error creating manager\n"); return false; } 
       m_Valid = true;
    }

   bool destroy()
   {
       m_Valid = false;
       if (m_Manager) m_Manager-<drop(); m_Manager = 0;
       if (m_Driver) m_Driver->drop(); m_Driver = 0;
    }

   bool isValid() { return m_Valid; }
   ISceneManger* getManager() { return m_Manager; }
   IDriver* getDriver() { return m_Driver; }
    // etc.......


private:
   bool m_Valid; 
   ISceneManager* m_Smgr;
   IDriver*  m_Driver;
   // etc...
}

Code: Select all

class sydney
{
    public :
             sydney() 
              { 
                 m_Mesh = 0;
                 m_Node = 0; 
                 m_Engine = 0; 
              }
             ~sydney() 
             {
                 if (m_Node) m_Node->drop(); //release? i cant remember 
                 if (m_Mesh) m_Mesh->drop(); //release? i cant remember 
                 m_Engine = 0; 
              }          

             bool create(Engine* engine) 
             {
                     m_Engine = engine;

                     if (!m_Engine) 
                        { printf("Engine variable not valid!"); return false; } 
                     if (!m_Engine->isValid) 
                        { printf("Engine variable not valid!"); return false; } 
                    
                     m_Mesh = m_Engine->m_Manager->createmeshSomehow();
                     m_Node = m_Engine->m_Manager->createNodeSomehow(); 
            }

private:
      Engine* m_Engine;
      IMEsh* m_Mesh;
      ISceneNode* m_Node;
}

Code: Select all

int main()
{
    Engine* engine = new Engine();
    if (!engine->create(640x480,"D3DDriver",etc.......)
    {
         printf("unable to create engine\n");
         return -1;
    }
    
    sydney* syd = new sydney();
    if (!syd->create(engine)
    {
         printf("unable to create sydney\n");
         return -1;
    }

    engine->destroy();
    delete(engine);
    engine = 0;
    return 0;
}
Last edited by Seven on Thu Oct 14, 2010 10:11 pm, edited 1 time in total.
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Post by Seven »

it wont compile of course, but just giving an idea. your issue is that the Game class is an all in one game obejct. that is hard to do. use the main() function to create each item individually.

create the engine and verify that it created successfully
now create the sydney, passing the engine to it so that it has access to the driver, smgr, gui etc.... that the engine created.
check that the sydney was created successfully.


use printf() staements all over the place so tha tyou can see things being created, failed, successful. this way you can see what is happening and why things fail. for example, in your code, if you had a line that said

if (!smgr) printf("smgr is not valid");

then you would see that your code is missing something critical (which is, as said above, that you arent setting the smgr variable inside the sydney class to anything).

looking at your code, I dont think that you are seeing that the smgr in the Engine class is not the same variable as the smgr in the sydney class. the easiest method (for me) is to 'pass' the Game class reference to the sydney class (and store it) so that sydney can then access the engine variables (namely the smgr and driver) like this.....

m_Engine->smgr->createSomething(params);

i hope that this makes sense, because you really should get this down before you try too much more coding. if you dont, it will be difficult to undo the mistakes that are being made.

last suggestion..... dont use the Game() class as a simple, call only one constructor and it creates the entire game for me" class. it will come back to bite you later. instead, create each step and make sure to see if that step failed before moving to the next step.

Seven
janw
Posts: 13
Joined: Wed Aug 11, 2010 2:23 pm

Post by janw »

Thank you Seven. I will try write it properly this time:)
Post Reply