replace xElement by xDrawable

also fix mutexes
This commit is contained in:
Adrien Marquès 2020-02-02 16:17:52 +01:00
parent 44de8cb3ea
commit ab2f170def
6 changed files with 20 additions and 20 deletions

View File

@ -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

View File

@ -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<xElement*>::iterator it;
set<xDrawable*>::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<xOrchestrable*>::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();
}

View File

@ -9,7 +9,7 @@
#include <mutex>
#include <thread>
#include <stdexcept>
#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<xElement*> _sprites;
set<xDrawable*> _sprites;
// execution pool
set<xOrchestrable*> _orchestrables;

1
xSDL/xDrawable.cpp Normal file
View File

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

View File

@ -3,7 +3,7 @@
#include "SDL.h"
struct xElement {
struct xDrawable {
virtual void draw(SDL_Renderer* renderer) = 0;
};

View File

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