From 2cbf3bc91b76fbaf3c6ceacd20bc96ccfd320fa8 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Wed, 6 Nov 2019 22:47:11 +0100 Subject: [PATCH] Simplify push/pull by reference, no more string identifier --- xSDL/xApplication.cpp | 30 +++++++----------------------- xSDL/xApplication.h | 12 ++++-------- 2 files changed, 11 insertions(+), 31 deletions(-) diff --git a/xSDL/xApplication.cpp b/xSDL/xApplication.cpp index 9e9d862..260ce2a 100644 --- a/xSDL/xApplication.cpp +++ b/xSDL/xApplication.cpp @@ -244,33 +244,17 @@ bool xApplication::setImage(const char *url){ -/** adds new sprite to draw (with its identifier) */ -void xApplication::push(string id, xElement* sprite){ +/** adds new sprite to draw */ +void xApplication::push(xElement* sprite){ _mutex_draw.try_lock(); - _sprites[id] = sprite; + _sprites.insert(sprite); _mutex_draw.unlock(); } -/** removes a sprite to draw (from its identifier) */ -void xApplication::pull(string id){ - _mutex_draw.try_lock(); - _sprites.erase(id); - _mutex_draw.unlock(); -} - - /** removes a sprite to draw (from its address) */ void xApplication::pull(xElement* sprite){ _mutex_draw.try_lock(); - - map::iterator it; - for( it = _sprites.begin() ; it != _sprites.end() ; it++ ){ - if( sprite == it->second ){ - _sprites.erase(it); - break; - } - } - + _sprites.erase(sprite); _mutex_draw.unlock(); } @@ -297,9 +281,9 @@ void xApplication::render(){ /* 4. draw every sprite */ - map::iterator it = _sprites.begin(); - for( ; it != _sprites.end() ; it++ ){ - xElement* sprite = it->second; + set::iterator it; + for( it = _sprites.begin() ; it != _sprites.end() ; it++ ){ + xElement* sprite = *it; sprite->draw(_renderer); } diff --git a/xSDL/xApplication.h b/xSDL/xApplication.h index beaf794..2070623 100644 --- a/xSDL/xApplication.h +++ b/xSDL/xApplication.h @@ -42,8 +42,7 @@ // bool hit(string current, int movex=0, int movey=0); // Gestion des collisions /** scene */ - void push(string id, xElement* sprite); - void pull(string id); + void push(xElement* sprite); void pull(xElement *sprite); void clear(); @@ -68,6 +67,9 @@ // event management xController _controller; + // sprites + set _sprites; + // sdl objects SDL_Window *_window; SDL_Rect _winrect; @@ -75,12 +77,6 @@ SDL_Renderer *_renderer; SDL_Texture *_texture; - // sprites - map _sprites; - - // shared threads - map _pool; - // thread safety mutex _mutex_draw; };