Hi!
I am using the RTSCamera found in code snippets - works good, will be tweaking it soon.
Anyway, it obviously has an event handler in it, with the onEvent which is where any mouse movements, key press etc will be handeled.
Does this mean all interaction with the user events needs to be handeled within the RTSCamera code, or can I somehow have multiple EventReceivers?
It doesnt make much sense to have code dealing with enemy units inside the camera code!
Thanks in advance,
Andy
Multiple event listeners
-
renegadeandy
- Posts: 122
- Joined: Sun May 25, 2008 11:14 pm
- Location: Scotland
- Contact:
Check here (easy) and here (advanced).
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
-
renegadeandy
- Posts: 122
- Joined: Sun May 25, 2008 11:14 pm
- Location: Scotland
- Contact:
Ok.
Settled for this :
I want this to be in its own CPP file, thus made the following header for it:
I dont know if that is correct or not (probably not, please correct if i am wrong).
When adding a receiever, do I pass it a pointer to a function?
Settled for this :
Code: Select all
#include <stdlib.h>
#include <irrlicht.h>
#include "MainEventReceiver.h"
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
#ifdef _IRR_WINDOWS_
#pragma comment(lib, "Irrlicht.lib")
#endif
class MainEventReceiver : public irr::IEventReceiver
{
public:
virtual bool OnEvent(const irr::SEvent& event_)
{
for ( unsigned int i = 0; i < mEventReceivers.size(); ++i )
{
if ( mEventReceivers[i]->OnEvent(event_) )
return true;
}
return false;
}
void AddEventReceiver(irr::IEventReceiver * receiver_)
{
mEventReceivers.push_back(receiver_);
}
bool RemoveEventReceiver(irr::IEventReceiver * receiver_)
{
for ( unsigned int i=0; i<mEventReceivers.size(); ++i )
{
if ( mEventReceivers[i] == receiver_ )
{
mEventReceivers.erase(i);
return true;
}
}
return false;
}
private:
irr::core::array<irr::IEventReceiver*> mEventReceivers;
};
// example for a custom receiver
class HandleMouseEvents : public irr::IEventReceiver
{
public:
virtual bool OnEvent(const irr::SEvent& event)
{
if (event.EventType != EET_MOUSE_INPUT_EVENT )
return false;
// do some stuff with mouse events, this is just an example
printf("mouse event happened\n");
return false;
}
};
// another example for a custom receiver
class HandleKeyEvents : public irr::IEventReceiver
{
public:
virtual bool OnEvent(const irr::SEvent& event)
{
if (event.EventType != EET_KEY_INPUT_EVENT )
return false;
// do some stuff with key events, this is just an example
printf("key event happened\n");
return false;
}
};
Code: Select all
class MainEventReceiver : public irr::IEventReceiver{
public:
virtual bool OnEvent(const SEvent& event_);
void AddEventReceiver(irr::IEventReceiver * receiver_);
bool RemoveEventReceiver(irr::IEventReceiver * receiver_);
private:
irr::core::array<irr::IEventReceiver*> mEventReceivers;
};
class HandleMouseEvents : public irr::IEventReceiver
{
public:
virtual bool OnEvent(const irr::SEvent& event);
};
class HandleKeyEvents : public irr::IEventReceiver
{
public:
virtual bool OnEvent(const irr::SEvent& event);
};
When adding a receiever, do I pass it a pointer to a function?
The HandleKeyEvents and HandleMouseEvents are just two example classes to show you how to add own EventReceivers.
You always have to pass the pointer to a class object as parameter. And make sure the pointer will be valid, that means the object must be within a scope which will stay valid as long as you need that pointer.
You always have to pass the pointer to a class object as parameter. And make sure the pointer will be valid, that means the object must be within a scope which will stay valid as long as you need that pointer.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
-
renegadeandy
- Posts: 122
- Joined: Sun May 25, 2008 11:14 pm
- Location: Scotland
- Contact:
ok... i usually make 2 files for most classes, a .h file and a .cpp file(like many people here i guess)
the .h file only contains the class/class members declarations, something like this:
then a .cpp file for that class
nowto use this u need one or more classes also derivated from irr::IEventReceiver with its own OnEvent method. these can also have its own .cpp and .h files or can just be in a single file... or in the main file... or whatever
now in the main application file u need to do something like this:
best regards
the .h file only contains the class/class members declarations, something like this:
Code: Select all
//MainEventReceiver.h
#include <irrlicht.h>
using namespace irr;
class MainEventReceiver : public irr::IEventReceiver{
public:
virtual bool OnEvent(const SEvent& event_);
void AddEventReceiver(irr::IEventReceiver * receiver_);
bool RemoveEventReceiver(irr::IEventReceiver * receiver_);
private:
irr::core::array<irr::IEventReceiver*> mEventReceivers;
};
Code: Select all
//MainEventReceiver.cpp
#include "MainEventReceiver.h"
bool MainEventReceiver::OnEvent(const irr::SEvent& event_)
{
for ( unsigned int i = 0; i < mEventReceivers.size(); ++i )
{
if ( mEventReceivers[i]->OnEvent(event_) )
return true;
}
return false;
}
void MainEventReceiver::void AddEventReceiver(irr::IEventReceiver * receiver_)
{
mEventReceivers.push_back(receiver_);
}
bool MainEventReceiver::RemoveEventReceiver(irr::IEventReceiver * receiver_)
{
for ( unsigned int i=0; i<mEventReceivers.size(); ++i )
{
if ( mEventReceivers[i] == receiver_ )
{
mEventReceivers.erase(i);
return true;
}
}
return false;
}
now in the main application file u need to do something like this:
Code: Select all
//main.cpp
#include "MainEventReceiver.h"
//include also all other .h necesary files
/*initializing the device and all that stuff*/
//create the mainEventHandler
MainEventReceiver ME_receiver;
//create secundary event receivers like:
HandleMouseEvents MouseE_receiver;
HandleKeyEvents KeyEvent_Receiver;
//add all those event receivers to the main one:
ME_receiver.AddEventReceiver(& MouseE_receiver);
ME_receiver.AddEventReceiver(& KeyEvent_receiver);
all the rest... main loop and stuff