/*
* File: EventManager.cpp
* Author: wernerkroneman
*
* Created on 29 augustus 2013, 12:01
*/
#include "EventManager.h"
#include <iostream>
EventManager::EventManager() {
//Check if there's any joysticks
if( SDL_NumJoysticks() > 0 ) {
joystick = SDL_JoystickOpen( 0 ); //If there's a problem opening the joystick
//TODO error check if( stick == NULL ) { }
} //Open the joystick
}
void EventManager::update() {
SDL_Event event;
while (SDL_PollEvent(&event)) {
handleEvent(event);
}
}
void EventManager::registerListener(EventListener* listener,const SDL_EventType& type){
listeners[type].push_back(listener);
}
bool EventManager::handleEvent(const Event& event){
ListenersMapType::iterator itr;
if ((itr = listeners.find((SDL_EventType)(event.type))) != listeners.end()){
for (std::vector<EventListener*>::iterator listenerItr = (*itr).second.begin(); listenerItr != (*itr).second.end(); listenerItr++)
(*listenerItr)->eventDidOccur(event);
returntrue;
}
returnfalse;
}
EventManager::~EventManager() {
}
Basically, other objects can subclass "EventListener", implement "eventDidOccur" and then call "eventManager->registerListener(this, SDL_KEYDOWN)" to be notified whenever the specified type of event occurs.
It works just as intended, but could you please take a look at it, to see if any improvements could be made or if I'm making any mistakes?