From ab2f170def614356b13dc0415fb4ab3ad1562659 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sun, 2 Feb 2020 16:17:52 +0100 Subject: [PATCH] replace xElement by xDrawable also fix mutexes --- include/xSDL.h | 2 +- xSDL/xApplication.cpp | 26 +++++++++++++------------- xSDL/xApplication.h | 8 ++++---- xSDL/xDrawable.cpp | 1 + xSDL/{xElement.h => xDrawable.h} | 2 +- xSDL/xElement.cpp | 1 - 6 files changed, 20 insertions(+), 20 deletions(-) create mode 100644 xSDL/xDrawable.cpp rename xSDL/{xElement.h => xDrawable.h} (82%) delete mode 100644 xSDL/xElement.cpp diff --git a/include/xSDL.h b/include/xSDL.h index 78e9e0d..2f3de5e 100644 --- a/include/xSDL.h +++ b/include/xSDL.h @@ -4,7 +4,7 @@ #include "../xSDL/xOrchestrable.h" #include "../xSDL/xApplication.h" #include "../xSDL/xController.h" - #include "../xSDL/xElement.h" + #include "../xSDL/xDrawable.h" #include "../xSDL/xSprite.h" #endif \ No newline at end of file diff --git a/xSDL/xApplication.cpp b/xSDL/xApplication.cpp index bd6f6b5..25a5045 100644 --- a/xSDL/xApplication.cpp +++ b/xSDL/xApplication.cpp @@ -139,7 +139,7 @@ bool xApplication::setImage(const char *url){ // // Anti conflit inter-thread -// _mutex_hit.try_lock(); +// _mutex_hit.lock(); // /* (1) On recupere le SDL_Rect destination du sprite courant */ // int xIndex = -1; @@ -224,7 +224,7 @@ bool xApplication::setImage(const char *url){ // bool xApplication::hit(string current, int movex, int movey){ // if( !this->status() ) return true; -// _mutex_hit.try_lock(); +// _mutex_hit.lock(); // /* (1) On recupere le SDL_Rect destination du sprite courant */ // xSprite *sprite = NULL; @@ -245,29 +245,29 @@ bool xApplication::setImage(const char *url){ /** adds new sprite to draw */ -void xApplication::push(xElement* sprite){ - _mutex_draw.try_lock(); +void xApplication::push(xDrawable* sprite){ + _mutex_draw.lock(); _sprites.insert(sprite); _mutex_draw.unlock(); } /** removes a sprite to draw (from its address) */ -void xApplication::pull(xElement* sprite){ - _mutex_draw.try_lock(); +void xApplication::pull(xDrawable* sprite){ + _mutex_draw.lock(); _sprites.erase(sprite); _mutex_draw.unlock(); } /** clears the scene */ void xApplication::clear(){ - _mutex_draw.try_lock(); + _mutex_draw.lock(); _sprites.clear(); _mutex_draw.unlock(); } /** Update the scene */ void xApplication::render(){ - _mutex_draw.try_lock(); + _mutex_draw.lock(); /* 1. clear scene */ SDL_RenderClear(_renderer); @@ -281,9 +281,9 @@ void xApplication::render(){ /* 4. draw every sprite */ - set::iterator it; + set::iterator it; for( it = _sprites.begin() ; it != _sprites.end() ; it++ ){ - xElement* sprite = *it; + xDrawable* sprite = *it; sprite->draw(_renderer); } @@ -308,7 +308,7 @@ void xApplication::schedule(){ const uint32_t ticks = SDL_GetTicks(); // trigger tick() on registered orchestrables - _mutex_orchestrate.try_lock(); + _mutex_orchestrate.lock(); set::iterator it; for( it = _orchestrables.begin() ; it != _orchestrables.end() ; it++ ){ xOrchestrable* orchestrable = *it; @@ -324,13 +324,13 @@ void xApplication::schedule(){ } void xApplication::addOrchestrable(xOrchestrable* o){ - _mutex_orchestrate.try_lock(); + _mutex_orchestrate.lock(); _orchestrables.insert(o); _mutex_orchestrate.unlock(); } void xApplication::removeOrchestrable(xOrchestrable* o){ - _mutex_orchestrate.try_lock(); + _mutex_orchestrate.lock(); _orchestrables.erase(o); _mutex_orchestrate.unlock(); } diff --git a/xSDL/xApplication.h b/xSDL/xApplication.h index bf47dd2..bf0b409 100644 --- a/xSDL/xApplication.h +++ b/xSDL/xApplication.h @@ -9,7 +9,7 @@ #include #include #include - #include "xElement.h" + #include "xDrawable.h" #include "xController.h" #include "xOrchestrable.h" using namespace std; @@ -41,8 +41,8 @@ // bool hit(string current, int movex=0, int movey=0); // Gestion des collisions /** scene */ - void push(xElement* sprite); - void pull(xElement *sprite); + void push(xDrawable* sprite); + void pull(xDrawable *sprite); void clear(); void render(); @@ -71,7 +71,7 @@ xController _controller; // sprites - set _sprites; + set _sprites; // execution pool set _orchestrables; diff --git a/xSDL/xDrawable.cpp b/xSDL/xDrawable.cpp new file mode 100644 index 0000000..aa8604c --- /dev/null +++ b/xSDL/xDrawable.cpp @@ -0,0 +1 @@ +#include "xDrawable.h" \ No newline at end of file diff --git a/xSDL/xElement.h b/xSDL/xDrawable.h similarity index 82% rename from xSDL/xElement.h rename to xSDL/xDrawable.h index 76eaadf..cd53932 100644 --- a/xSDL/xElement.h +++ b/xSDL/xDrawable.h @@ -3,7 +3,7 @@ #include "SDL.h" - struct xElement { + struct xDrawable { virtual void draw(SDL_Renderer* renderer) = 0; }; diff --git a/xSDL/xElement.cpp b/xSDL/xElement.cpp deleted file mode 100644 index 0748c12..0000000 --- a/xSDL/xElement.cpp +++ /dev/null @@ -1 +0,0 @@ -#include "xElement.h" \ No newline at end of file