Compare commits

...

3 Commits

5 changed files with 36 additions and 25 deletions

View File

@ -64,8 +64,8 @@
void syncFps();
// fps management
uint32_t _lasttick;
uint32_t _fpstime;
uint32_t _lasttick { 0 };
uint32_t _fpstime { (int) 1000.0/60 };
// event management
xController _controller;
@ -77,11 +77,11 @@
set<xOrchestrable*> _orchestrables;
// sdl objects
SDL_Window *_window;
SDL_Rect _winrect;
SDL_Window *_window { NULL };
SDL_Rect _winrect { 0, 0, 0, 0 };
SDL_Renderer *_renderer;
SDL_Texture *_texture;
SDL_Renderer *_renderer { NULL };
SDL_Texture *_texture { NULL };
// thread safety
mutex _mutex_draw;

View File

@ -12,29 +12,24 @@ xController::~xController(){
void xController::handleEvent(SDL_Event* event){
_mutex.lock();
map<SDL_EventType, EventHandler>::iterator found = _handlers.find( (SDL_EventType) event->type );
// ignore no handler found
if( found == _handlers.end() ){
_mutex.unlock();
return;
for( set<xEventHandler*>::iterator it = _handlers.begin() ; it != _handlers.end() ; it++ ){
xEventHandler* handler = (*it);
handler->handle(event);
}
(*found->second)(event);
_mutex.unlock();
}
// bind a new handler
void xController::attachEvent(SDL_EventType t, EventHandlerArg){
void xController::attachEvent(xEventHandler* handler){
_mutex.lock();
_handlers[ t ] = handler;
_handlers.insert(handler);
_mutex.unlock();
}
// removes an existing handler
void xController::detachEvent(SDL_EventType t){
void xController::detachEvent(xEventHandler* handler){
_mutex.lock();
_handlers.erase(t);
_handlers.erase(handler);
_mutex.unlock();
}

View File

@ -3,11 +3,10 @@
#include "SDL.h"
#include <mutex>
#include <map>
using namespace std;
#include <set>
#include "xEventHandler.h"
#define EventHandlerArg void(* handler)(SDL_Event*)
#define EventHandler void(*)(SDL_Event*)
using namespace std;
class xController{
@ -19,12 +18,12 @@
void handleEvent(SDL_Event* event);
// manage handlers
void attachEvent(SDL_EventType t, EventHandlerArg);
void detachEvent(SDL_EventType t);
void attachEvent(xEventHandler* handler);
void detachEvent(xEventHandler* handler);
private:
// event handlers
map<SDL_EventType, EventHandler> _handlers;
set<xEventHandler*> _handlers;
// thread safety
mutex _mutex;

1
xSDL/xEventHandler.cpp Normal file
View File

@ -0,0 +1 @@
#include "xEventHandler.h"

16
xSDL/xEventHandler.h Normal file
View File

@ -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) = 0;
};
#endif