This annoyed me cause i didn't want an enormous onEvent code. So instead i created a EventReceiverManger Class, so i figured i'd share that code with you guys.
This way you can create a event, then register it with the manager, and it will parse all events registered with it. It stores these events as a linked list.
This code is also useful if you want to activate different sets of on_events at different times, so you could declare one manager for your intro screen, one for a general gaming mode, one for a alternate gaming mode, etc.
Hell it can be used to set up all kinds of hierchial input so you can use abstraction to its fullest to simplify code.
example use:
Code: Select all
//initializes the onevent controller
GEventController eventintro(device);
//registers arbitrary classes that inherits iEventReceiver
eventdefault.AddEvent(&node1);
eventdefault.AddEvent(&MyEvent2);
eventdefault.AddEvent(&class3);
GEventController eventgaming();
eventgaming.AddEvent(&class4);
eventgaming.AddEvent(&class5);
if(intro == done && gamingmode == started)
device->setEventReceiver( eventgaming );
Code: Select all
gEventController player1();
gEventController player2();
/... some assignments of event to both players .../
gEventController allPlayers(device);
if (player1active == TRUE) allPlayers.AddEvent(player1);
if (player2active == TRUE) allPlayers.AddEvent(player2);
Code: Select all
#include <irrlicht.h>
#include <iostream>
struct GEventNode
{
GEventNode* Next;
irr::IEventReceiver* Event;
};
using namespace irr;
#pragma comment(lib, "Irrlicht.lib")
class GEventController : public IEventReceiver
{
public:
//Constructor, initalizes controller without activating
GEventController();
//Constructor, initializes controller and activates it on the passed device
GEventController(IrrlichtDevice* device);
// Adds an onEvent Node to the linked list
bool AddEvent(IEventReceiver* IEvent);
// Parses through all registers on_Events when primary On_event is called
bool OnEvent(SEvent event);
private:
GEventNode* leadnode;
};
Code: Select all
#include "geventcontroller.h"
//Constructor, initalizes controller without activating
GEventController::GEventController()
{
leadnode = 0; //Sets Point to zero.
}
//Constructor, initializes controller and activates it on the passed device
GEventController::GEventController(IrrlichtDevice* device)
{
//sets GEventController as primary Controller
device->setEventReceiver( this );
leadnode = 0;
}
// adds a new event to the linked list
bool GEventController::AddEvent(IEventReceiver* IEvent)
{
GEventNode* newnode = new GEventNode; //creates a new node
newnode->Event = IEvent; // Sets the object to point to the event
newnode->Next = leadnode; //Points to the previous node
leadnode = newnode; // Sets head of the list to the newest node
return true;
}
// Parses through all registers Events and executes them.
bool GEventController::OnEvent(SEvent event)
{
GEventNode* node = leadnode; //first event to call.
while (node != 0) //if node points to an event, run OnEvent
{
node->Event->OnEvent(event);
node = node->Next; //Goes to next node.
}
}