exiting the program with ESC key?

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
Valor
Posts: 40
Joined: Fri Jul 30, 2010 1:20 pm

exiting the program with ESC key?

Post by Valor »

Can someone please point me in the direction of putting in the code the option of exiting the program using the ESC key using Irrlicht api and without producing memory leaks.
greenya
Posts: 1012
Joined: Sun Jan 21, 2007 1:46 pm
Location: Ukraine
Contact:

Post by greenya »

Something like this:

Code: Select all

IrrlichtDevice* device = 0;

class MyEventReceiver : public IEventReceiver
{
public:
	...

	bool OnEvent(const SEvent& event)
	{
		if (event.EventType == irr::EET_KEY_INPUT_EVENT &&
			event.KeyInput.Key == irr::KEY_ESCAPE
			event.KeyInput.PressedDown)
		{
			device->closeDevice();
			return true;
		}
		...
	}

	...
}

void main()
{
	...

	device = createDevice();
	device->setEventReceiver(new MyEventReceiver());

	...

	while (device->run())
	{
		...
	}

	...
}
The main parts are:
- you check key irr::KEY_ESCAPE
- you call closeDevice()
DtD
Posts: 264
Joined: Mon Aug 11, 2008 7:05 am
Location: Kansas
Contact:

Post by DtD »

Don't forget that you need to call device->run() after closing it in order for it to close correctly.
serengeor
Posts: 1712
Joined: Tue Jan 13, 2009 7:34 pm
Location: Lithuania

Post by serengeor »

DtD wrote:Don't forget that you need to call device->run() after closing it in order for it to close correctly.
Is that true?
never seen that done in the tutorials :shock:
Working on game: Marrbles (Currently stopped).
greenya
Posts: 1012
Joined: Sun Jan 21, 2007 1:46 pm
Location: Ukraine
Contact:

Post by greenya »

I don't know if it is necessary to call device->run() after device->closeDevice(), BUT i can say that after you called device->closeDevice() each call of device->run() will return false, so your ordinary "while (device->run()) { ... }" cycle will correctly exit. And the "plus" of device->closeDevice() is that you can call it from any place of you program.
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

serengeor wrote:
DtD wrote:Don't forget that you need to call device->run() after closing it in order for it to close correctly.
Is that true?
in general yes, but it's only realy neccessary if you want the program to be still running, eg. when you want to change driver, resolution, etc. and you break the main loop for this (otherwise see further explanation below :lol: )...
greenya wrote:so your ordinary "while (device->run()) { ... }" cycle will correctly exit.
right, because you're still in the loop it will call device->run() a last time after the closeDevice() call, return false and exit, so no extra call is needed in this case...
(I know this is what you said, I just wanted to explain it with my words ;) )
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
Valor
Posts: 40
Joined: Fri Jul 30, 2010 1:20 pm

Post by Valor »

do you need to make device a global?
greenya
Posts: 1012
Joined: Sun Jan 21, 2007 1:46 pm
Location: Ukraine
Contact:

Post by greenya »

Valor wrote:do you need to make device a global?
No. you can pass device to EventReceiver constructor and save it internally for example. Any way, you need to call device->closeDevice() in order to close device properly.

You also can send WM_CLOSE message to your window. So you do not need the device, but this is only for Windows.
Valor
Posts: 40
Joined: Fri Jul 30, 2010 1:20 pm

Post by Valor »

Thanks!
Post Reply