Compare commits

..

No commits in common. "4ca031247996253f7597445e36f48d8a7687f11f" and "2b81a725427fd4bf388c7b525adb4df205c0a7ae" have entirely different histories.

5 changed files with 25 additions and 36 deletions

View File

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

View File

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

View File

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

View File

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

View File

@ -1,16 +0,0 @@
#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