EventListener Issue

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
charl3s7
Posts: 11
Joined: Sat Oct 03, 2009 3:17 am

EventListener Issue

Post by charl3s7 »

When I add the event listener, App->run() breaks.

Code:

main.cpp:

Code: Select all

#include "Device.h"

int main()
{
	Device * D = new Device(dimension2du(800,600), L"My Render Window", EDT_DIRECT3D9, 4U);

	if(D)
		D->BeginLoop();
	return 0;
}
Device.h:

Code: Select all

#pragma once
#include <irrlicht.h>

#include <Windows.h>

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

#pragma comment(lib, "Irrlicht.lib")

class Device
{
private:
	IrrlichtDevice * App;
	bool Running;
public:
	void Stop();
	void BeginLoop();
	Device(dimension2du WinSize, wchar_t *, E_DRIVER_TYPE DriverType, unsigned int AntiAliasing);
};

/* Event Handler */

class EventReceiver : public IEventReceiver
{
public:
	EventReceiver(Device * d) : Dev(d){};
	virtual bool OnEvent(const SEvent & event)
	{
		switch(event.EventType)
		{
		case EET_KEY_INPUT_EVENT:
			switch(event.KeyInput.Key)
			{
			case KEY_ESCAPE:
				Dev->Stop();
				break;
			default:
				break;
			}
			break;
		default:
			break;
		}
		return false;
	}
private:
	Device * Dev;
};
Device.cpp:

Code: Select all

#include "Device.h"

Device::Device(dimension2du WinSize = dimension2du(800,600), wchar_t * WindowTitle = L"REngine Window", E_DRIVER_TYPE DriverType = EDT_DIRECT3D9, unsigned int AntiAliasing = 4U)
{
	EventReceiver Events(this);
	SIrrlichtCreationParameters Params;
	Params.AntiAlias = AntiAliasing;
	Params.DriverType = DriverType;
	Params.WindowSize = WinSize;
	Params.Bits = 16U;
	Params.LoggingLevel = ELL_INFORMATION;
	Params.EventReceiver = &Events;

	App = createDeviceEx(Params);

	if(App)
		Running = true;
}

void Device::Stop()
{
	Running = false;
}

void Device::BeginLoop()
{
	while(App->run())
	{
		App->getVideoDriver()->beginScene();
		App->getVideoDriver()->endScene();
	}
}
//Charles
randomMesh
Posts: 1186
Joined: Fri Dec 29, 2006 12:04 am

Re: EventListener Issue

Post by randomMesh »

It's because your event receiver goes out of scope after the constructor's done. And you got several memory leaks.
"Whoops..."
charl3s7
Posts: 11
Joined: Sat Oct 03, 2009 3:17 am

Post by charl3s7 »

So how could I potentially go about fixing it?
//Charles
randomMesh
Posts: 1186
Joined: Fri Dec 29, 2006 12:04 am

Post by randomMesh »

charl3s7 wrote:So how could I potentially go about fixing it?
Create it on the heap rather than on the stack using new. And please drop the Irrlicht device in the destructor and delete your own Device at the end to get rid of the memory leaks. This code is cruel, my eyes hurt. :wink:

PS: Don't use the using namespace directive in headers.
"Whoops..."
Post Reply