Works fine for me. You are probably making the classic mistake of naming a global variable the same as a local variable, like this...
Code: Select all
IrrlichtDevice* device = 0;
int main()
{
IrrlichtDevice* device = createDevice(...);
//...
If you do this, the global
device will not get initialized. To avoid problems like this, most people use special notation to indicate the scope of the variable. As an example, you could prepend a
g_ to your global variable names. Any variable that is global would have a
g_ at the beginning, and anything that is local would not.
Also, this has been mentioned many times before, but you should avoid globals if you can. The following would be a better way to do the same thing without using a global device pointer.
Code: Select all
class MyEventReceiver : public IEventReceiver
{
public:
MyEventReceiver()
: Device(0)
{
}
virtual bool OnEvent(SEvent event)
{
if (event.EventType == irr::EET_KEY_INPUT_EVENT &&
event.KeyInput.PressedDown)
{
switch(event.KeyInput.Key)
{
case KEY_KEY_Q:
if (Device)
Device->closeDevice();
Device = 0;
return true;
default:
return false;
}
}
return false;
}
void setDevice(IrrlichtDevice* device)
{
if (Device)
Device->setEventReceiver(0);
Device = device;
if (Device)
Device->setEventReceiver(this);
}
private:
IrrlichtDevice* Device;
};
// in main...
IrrlichtDevice* device =
createDevice(driverType, core::dimension2d<s32>(1024, 768));
if (device == 0)
return 1; // could not create selected driver.
MyEventReceiver receiver;
receiver.setDevice(device);
Travis