EventReciever in another file?
EventReciever in another file?
This seems like it would be a pretty common problem but I have my event receivers in another file away from the data it needs to manipulate. I'm wondering how I should get the data I need into the event receiver.
Or you could have your EventReceiver class something like this :
Of course this can be done in a much better way, but i think you got the idea.
Code: Select all
// Event Receiver.h
#include <irrlicht.h>
struct mouse {
bool left;
};
struct keyboard {
bool escape;
};
class EventReceiver : public irr::IEventReceiver
{
mouse Mouse;
keyboard Keyboard;
virtual bool OnEvent(irr::SEvent event);
public:
EventReceiver();
~EventReceiver();
bool isKeyDown (irr::core::stringc key);
bool isMouseDown (irr::core::stringc button);
};
// Event receiver.cpp
#include "EventReceiver.h"
EventReceiver::EventReceiver ()
{
Keyboard.escape = false;
Mouse.left = false;
}
EventReceiver::~EventReceiver()
{
}
bool EventReceiver::OnEvent(irr::SEvent event)
{
if (event.EventType == irr::EET_KEY_INPUT_EVENT)
{
switch(event.KeyInput.Key)
{
case irr::KEY_ESCAPE: Keyboard.escape = event.KeyInput.PressedDown;
}
}
if (event.EventType == irr::EET_MOUSE_INPUT_EVENT)
{
switch(event.MouseInput.Event)
{
case irr::EMIE_LMOUSE_PRESSED_DOWN: Mouse.left = true; break;
case irr::EMIE_LMOUSE_LEFT_UP: Mouse.left = false; break;
}
}
return false;
}
bool EventReceiver::isKeyDown (irr::core::stringc key)
{
if (key == "ESCAPE") return Keyboard.escape;
return false;
}
bool EventReceiver::isMouseDown (irr::core::stringc button)
{
if (button == "LEFT") return Mouse.left;
return false;
}
// And in the main.cpp
EventReceiver *receiver = new EventReceiver();
if (receiver->isKeyDown("ESCAPE")) break;
But you can have another function to pass them to...
THEN in a cpp file
THEN in main.cpp
NOW, the suggestion spopo made is actually better, if I understand it correctly.
This way we are creating copies of all the data. We want to use as little memory as possible so we don't want to copy them if we can.
so, we do this in our header
THEN in main.cpp file
I hope this helps
Code: Select all
#ifndef EVENTRECEIVER_H
#define EVENTRECEIVER_H
class EventReceiver : public IEventReceiver
{
public:
void initialize(); // make this function accept the variables you need
virtual bool OnEvent(SEvent event)
{
//code here
return false;
}
private:
// declare variables you need here
};
#endif
Code: Select all
#include "eventreceiver.h"
void GameEventReceiver::initialize()
{
//write those variables you accepted to your private data here
}
Code: Select all
#include "eventreceiver.h"
int main
{
eventreceiver receiver;
eventreceiver.initialize();
device->setEventReceiver(&receiver);
}
This way we are creating copies of all the data. We want to use as little memory as possible so we don't want to copy them if we can.
so, we do this in our header
Code: Select all
#ifndef EVENTRECEIVER_H
#define EVENTRECEIVER_H
class EventReceiver : public IEventReceiver
{
public:
void initialize(); // this will be only to initialize your bool keys
void handle(); // this will be in your main cpp file
virtual bool OnEvent(SEvent event)
{
//code here
//We WILL NOT do any event handling here, we simply store
//which buttons are being hit privately (use bool keys)
return false;
}
private:
// your bool keys will be defined here
};
#endif
Code: Select all
#include "eventreceiver.h"
int main
{
eventreceiver receiver;
receiver.initialize();
device->setEventReceiver(&receiver);
receiver.handle(); //THIS will go in your game loop
}
void EventReceiver::handle()
{
//THIS is where you will handle events
}
Sorry to bump this 10 days old thread, but I have a doubt on how to optimize that code.
Instead of using if (key == "ESCAPE") return Keyboard.escape;
A way so all the keys are already defined, something like:
But it didn't like much the key[Key], help?
Instead of using if (key == "ESCAPE") return Keyboard.escape;
A way so all the keys are already defined, something like:
Code: Select all
bool eventsReceiver::OnEvent(irr::SEvent event)
{
bool value = false;
if (event.EventType == irr::EET_KEY_INPUT_EVENT)
{
key[event.KeyInput.Key] = event.KeyInput.PressedDown;
value = true;
}
if (event.EventType == irr::EET_MOUSE_INPUT_EVENT)
{
switch(event.MouseInput.Event)
{
case EMIE_LMOUSE_PRESSED_DOWN: mouse.left = true; break;
case EMIE_RMOUSE_PRESSED_DOWN: mouse.right = true; break;
case EMIE_MMOUSE_PRESSED_DOWN: mouse.middle = true; break;
case EMIE_LMOUSE_LEFT_UP: mouse.left = false;break;
case EMIE_RMOUSE_LEFT_UP: mouse.right = false; break;
case EMIE_MMOUSE_LEFT_UP: mouse.middle = false; break;
case EMIE_MOUSE_MOVED: mouse.X = event.MouseInput.X; mouse.Y = event.MouseInput.Y; break;
case EMIE_MOUSE_WHEEL: mouse.wheel = mouse.wheel + event.MouseInput.Wheel; break;
}
value = true;
}
return value;
};
bool eventsReceiver::isKeyDown (irr::core::stringc Key)
{
return key[Key];
}