create proper handler interface for xController : xEventHandler
This commit is contained in:
parent
5266b5d115
commit
414fbf6404
|
@ -12,29 +12,24 @@ xController::~xController(){
|
||||||
void xController::handleEvent(SDL_Event* event){
|
void xController::handleEvent(SDL_Event* event){
|
||||||
_mutex.lock();
|
_mutex.lock();
|
||||||
|
|
||||||
map<SDL_EventType, EventHandler>::iterator found = _handlers.find( (SDL_EventType) event->type );
|
for( set<xEventHandler*>::iterator it = _handlers.begin() ; it != _handlers.end() ; it++ ){
|
||||||
|
xEventHandler* handler = (*it);
|
||||||
// ignore no handler found
|
handler->handle(event);
|
||||||
if( found == _handlers.end() ){
|
|
||||||
_mutex.unlock();
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
(*found->second)(event);
|
|
||||||
|
|
||||||
_mutex.unlock();
|
_mutex.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
// bind a new handler
|
// bind a new handler
|
||||||
void xController::attachEvent(SDL_EventType t, EventHandlerArg){
|
void xController::attachEvent(xEventHandler* handler){
|
||||||
_mutex.lock();
|
_mutex.lock();
|
||||||
_handlers[ t ] = handler;
|
_handlers.insert(handler);
|
||||||
_mutex.unlock();
|
_mutex.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
// removes an existing handler
|
// removes an existing handler
|
||||||
void xController::detachEvent(SDL_EventType t){
|
void xController::detachEvent(xEventHandler* handler){
|
||||||
_mutex.lock();
|
_mutex.lock();
|
||||||
_handlers.erase(t);
|
_handlers.erase(handler);
|
||||||
_mutex.unlock();
|
_mutex.unlock();
|
||||||
}
|
}
|
|
@ -3,11 +3,10 @@
|
||||||
|
|
||||||
#include "SDL.h"
|
#include "SDL.h"
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <map>
|
#include <set>
|
||||||
using namespace std;
|
#include "xEventHandler.h"
|
||||||
|
|
||||||
#define EventHandlerArg void(* handler)(SDL_Event*)
|
using namespace std;
|
||||||
#define EventHandler void(*)(SDL_Event*)
|
|
||||||
|
|
||||||
class xController{
|
class xController{
|
||||||
|
|
||||||
|
@ -19,12 +18,12 @@
|
||||||
void handleEvent(SDL_Event* event);
|
void handleEvent(SDL_Event* event);
|
||||||
|
|
||||||
// manage handlers
|
// manage handlers
|
||||||
void attachEvent(SDL_EventType t, EventHandlerArg);
|
void attachEvent(xEventHandler* handler);
|
||||||
void detachEvent(SDL_EventType t);
|
void detachEvent(xEventHandler* handler);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// event handlers
|
// event handlers
|
||||||
map<SDL_EventType, EventHandler> _handlers;
|
set<xEventHandler*> _handlers;
|
||||||
|
|
||||||
// thread safety
|
// thread safety
|
||||||
mutex _mutex;
|
mutex _mutex;
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
#include "xEventHandler.h"
|
|
@ -0,0 +1,16 @@
|
||||||
|
#ifndef DEF_XEVENT_HANDLER_H
|
||||||
|
#define DEF_XEVENT_HANDLER_H
|
||||||
|
|
||||||
|
#include "SDL.h"
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
class xEventHandler {
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
// handles the event
|
||||||
|
virtual void handle(SDL_Event* event) const = 0;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
Loading…
Reference in New Issue