Run time error

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.
Debeli
Posts: 31
Joined: Thu Jan 07, 2010 2:32 pm

Run time error

Post by Debeli »

I have a problem when trying to load a mesh....well actually i split my project into seperate files i mada a file called Player.cpp a nd Player.h and i wrote an class called Player which i intended to be just a very simple class with basic functions
Heres Player.cpp

Code: Select all

#include<irrlicht.h>
#include"Player.h"

using namespace irr;
using namespace video;
using namespace scene;



scene::ISceneManager* smgr = NULL;
video::IVideoDriver* driver = NULL;


void Player::loadPlayer()
{
IAnimatedMesh* meshcar=smgr->getMesh("../../vozila/ninja.b3d");
IAnimatedMeshSceneNode* car=smgr->addAnimatedMeshSceneNode(meshcar);
car->setPosition(core::vector3df(0,0,0));
}
And Player.h

Code: Select all

#ifndef PLAYER_H_INCLUDED
#define PLAYER_H_INCLUDED


class Player
{

public:
void loadPlayer();
};
#endif // PLAYER_H_INCLUDED
so i wrote in my main() Player object;object.loadPlayer();
and it all complied fine but when i run the program when it comes to the part where i call the function i get and error and its sttops runing.

Its very likely that i made some terible mistake in the code because its the first time i used OOP in my project and i always put everything in one file so excuse me if thats the case :oops:
ent1ty
Competition winner
Posts: 1106
Joined: Sun Nov 08, 2009 11:09 am

Post by ent1ty »

I bet it's because you do scene::ISceneManager* smgr = NULL;
you must get smgr from device, like shown in the tutorials.
irrRenderer 1.0
Height2Normal v. 2.1 - convert height maps to normal maps

Step back! I have a void pointer, and I'm not afraid to use it!
Debeli
Posts: 31
Joined: Thu Jan 07, 2010 2:32 pm

Post by Debeli »

Did you mean something like this :?:
Player.cpp

Code: Select all

#include<irrlicht.h>
#include"Player.h"

using namespace irr;
using namespace video;
using namespace scene;


IrrlichtDevice *device;
 ISceneManager* smgr = device->getSceneManager();



void Player::loadPlayer()
{
IAnimatedMesh* meshcar=smgr->getMesh("../../vozila/ninja.b3d");
IAnimatedMeshSceneNode* car=smgr->addAnimatedMeshSceneNode(meshcar);
car->setPosition(core::vector3df(0,0,0));
}
I tryed it but it dosent work
ent1ty
Competition winner
Posts: 1106
Joined: Sun Nov 08, 2009 11:09 am

Post by ent1ty »

You must also create device like shown in tutorials :wink:
irrRenderer 1.0
Height2Normal v. 2.1 - convert height maps to normal maps

Step back! I have a void pointer, and I'm not afraid to use it!
Debeli
Posts: 31
Joined: Thu Jan 07, 2010 2:32 pm

Post by Debeli »

What?But i already declared the decice in my main
Lonesome Ducky
Competition winner
Posts: 1123
Joined: Sun Jun 10, 2007 11:14 pm

Post by Lonesome Ducky »

The device has to be created, not just declared. Look at the tutorials if you need help.
Debeli
Posts: 31
Joined: Thu Jan 07, 2010 2:32 pm

Post by Debeli »

OU i didnt said it right i did just like in the tut..i instalized the device in my main()
ent1ty
Competition winner
Posts: 1106
Joined: Sun Nov 08, 2009 11:09 am

Post by ent1ty »

Then why are you declaring another one?
Mate, you need to polish your C++ skills :)
irrRenderer 1.0
Height2Normal v. 2.1 - convert height maps to normal maps

Step back! I have a void pointer, and I'm not afraid to use it!
randomMesh
Posts: 1186
Joined: Fri Dec 29, 2006 12:04 am

Post by randomMesh »

I was bored. I made a basic Hello-OOP example for you. But please promise that if you don't understand a single line, grab a book on C++. ;)

main.cpp

Code: Select all

#include "Application.h"

int main()
{
	Application app;

	if (app.init())
		app.run();

	return 0;
}

Application.h

Code: Select all

#ifndef APPLICATION_H_
#define APPLICATION_H_

#include <IEventReceiver.h>

namespace irr
{
	class IrrlichtDevice;
}

class Player;

class Application : public irr::IEventReceiver
{

public:

	Application();

	~Application();

	virtual bool OnEvent(const irr::SEvent& event);

	bool init();

	void run() const;

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

private:

	irr::IrrlichtDevice* device;

	Player* player;
};

#endif /* APPLICATION_H_ */

Application.cpp

Code: Select all

#include "Application.h"
#include "Player.h"
#include <irrlicht.h>

Application::Application() :
	device(0), player(0)
{

}

Application::~Application()
{
	delete player;

	if (device)
		device->drop();
}

bool Application::init()
{
	device = irr::createDevice();
	if (!device)
		return false;

	device->setEventReceiver(this);
	device->setWindowCaption(L"OOP test");

	player = new Player(this);
	player->load();

	irr::scene::ISceneManager* const smgr = device->getSceneManager();
	smgr->addCameraSceneNode();

	return true;
}

void Application::run() const
{
	irr::scene::ISceneManager* const smgr = device->getSceneManager();
	irr::video::IVideoDriver* const driver = device->getVideoDriver();

	while (device->run())
	{
		if (device->isWindowActive())
		{
			driver->beginScene();
			smgr->drawAll();
			driver->endScene();
		}
	}
}

bool Application::OnEvent(const irr::SEvent& event)
{
	//check keyboard events
	if (event.EventType == irr::EET_KEY_INPUT_EVENT)
	{
		if (!event.KeyInput.PressedDown)
		{
			switch (event.KeyInput.Key)
			{
			case irr::KEY_ESCAPE: device->closeDevice(); return true;
			default:
				return false;
			}
		}
	}

	return false;
}

Player.h

Code: Select all

#ifndef PLAYER_H_
#define PLAYER_H_

namespace irr
{
	namespace scene
	{
		class IAnimatedMeshSceneNode;
	}
}

class Application;

class Player
{

public:

	Player(Application* const app);

	~Player();

	void load();

private:

	Application* const app;

	irr::scene::IAnimatedMeshSceneNode* car;
};

#endif /* PLAYER_H_ */

Player.cpp

Code: Select all

#include "Player.h"
#include "Application.h"
#include <IrrlichtDevice.h>
#include <ISceneManager.h>
#include <IAnimatedMeshSceneNode.h>

Player::Player(Application* const app) :
	app(app), car(0)
{

}

Player::~Player()
{
	if (car)
		car->remove();
}

void Player::load()
{
	irr::scene::ISceneManager* const smgr = app->getDevice()->getSceneManager();

	irr::scene::IAnimatedMesh* const meshcar = smgr->getMesh("../../vozila/ninja.b3d");

	if (meshcar)
	{
		car = smgr->addAnimatedMeshSceneNode(meshcar);
		car->setPosition(irr::core::vector3df(0, 0, 15));
	}
}

"Whoops..."
Debeli
Posts: 31
Joined: Thu Jan 07, 2010 2:32 pm

Post by Debeli »

@entety-I dont understant you i am not declaring another one just when i use smgr and get it out of device i need to declare the device in that file soo the complier could understand what device means(but thats most likely not the way its done so i get an error)anyway thanks for your help..And you re right i need to polish my C++ skilles

@RandomMesh-Thanks a lot :D ...And i do understand more then one line of code acctually four lines(Just kidding :lol: )Like i said i m still in the procces of learning and i read and did some basic exeples on OOP and i thing i understood some basic consteps and now i m trying to do some more practise with Irrlicht..So thanks once again(were usefull)

Ou yeah and just one other thing what does inline mean?
Last edited by Debeli on Mon Apr 12, 2010 5:24 pm, edited 1 time in total.
ent1ty
Competition winner
Posts: 1106
Joined: Sun Nov 08, 2009 11:09 am

Post by ent1ty »

If you want to use device in other funcs then the main, then make it global(yes, I know, globals are evil, but what is the chance that he will declare another variable with name device?)

To make it global, just declare it before main like IrllichtDevice* device and then do that createDevice stuff somewhere in the beginning of main
irrRenderer 1.0
Height2Normal v. 2.1 - convert height maps to normal maps

Step back! I have a void pointer, and I'm not afraid to use it!
randomMesh
Posts: 1186
Joined: Fri Dec 29, 2006 12:04 am

Post by randomMesh »

Never, ever use globals! Do it like i showed in my example.
"Whoops..."
Debeli
Posts: 31
Joined: Thu Jan 07, 2010 2:32 pm

Post by Debeli »

@entity reading your post i just figure out how stupid i really am about a week a go im reading about globals and that they can be used throught diffrent filles in my project and why there are eveil, and then in this stupid case i frogot all about it
randomMesh
Posts: 1186
Joined: Fri Dec 29, 2006 12:04 am

Post by randomMesh »

"Whoops..."
Debeli
Posts: 31
Joined: Thu Jan 07, 2010 2:32 pm

Post by Debeli »

I compliede the program and i get again run time error it all complies fine and bemm it shuts down
Post Reply