exiting the program with ESC key?
exiting the program with ESC key?
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.
Something like this:
The main parts are:
- you check key irr::KEY_ESCAPE
- you call closeDevice()
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())
{
...
}
...
}
- you check key irr::KEY_ESCAPE
- you call closeDevice()
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.
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 )...serengeor wrote:Is that true?DtD wrote:Don't forget that you need to call device->run() after closing it in order for it to close correctly.
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...greenya wrote:so your ordinary "while (device->run()) { ... }" cycle will correctly exit.
(I know this is what you said, I just wanted to explain it with my words )
while(!asleep) sheep++;
IrrExtensions:
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
IrrExtensions:
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
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.Valor wrote:do you need to make device a global?
You also can send WM_CLOSE message to your window. So you do not need the device, but this is only for Windows.