Multiple event listeners

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
renegadeandy
Posts: 122
Joined: Sun May 25, 2008 11:14 pm
Location: Scotland
Contact:

Multiple event listeners

Post by renegadeandy »

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
CuteAlien
Admin
Posts: 9933
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

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
renegadeandy
Posts: 122
Joined: Sun May 25, 2008 11:14 pm
Location: Scotland
Contact:

Post by renegadeandy »

Ok.

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;
   }
};

I want this to be in its own CPP file, thus made the following header for it:

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);
  
};
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?
CuteAlien
Admin
Posts: 9933
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

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.
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
renegadeandy
Posts: 122
Joined: Sun May 25, 2008 11:14 pm
Location: Scotland
Contact:

Post by renegadeandy »

I am not entirely sure I folllow - for a start, that code doesnt compile, I think the headers wrong - can you tell me what is wrong? I need a practicle example of this in use to get an idea of whats going on.
radiant
Posts: 112
Joined: Fri Feb 22, 2008 8:04 pm
Location: Mexico

Post by radiant »

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:

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;
}; 
then a .cpp file for that class

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;
   }
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:

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
best regards
Post Reply