Compare commits
No commits in common. "7a6022e69410e37a1c76422bdffd01f70c3e121c" and "44de8cb3ea7db3c2c3b0df5466afa72427ff9b6d" have entirely different histories.
7a6022e694
...
44de8cb3ea
|
@ -4,8 +4,7 @@
|
||||||
#include "../xSDL/xOrchestrable.h"
|
#include "../xSDL/xOrchestrable.h"
|
||||||
#include "../xSDL/xApplication.h"
|
#include "../xSDL/xApplication.h"
|
||||||
#include "../xSDL/xController.h"
|
#include "../xSDL/xController.h"
|
||||||
#include "../xSDL/xDrawable.h"
|
#include "../xSDL/xElement.h"
|
||||||
#include "../xSDL/xSprite.h"
|
#include "../xSDL/xSprite.h"
|
||||||
#include "../xSDL/xSpriteGroup.h"
|
|
||||||
|
|
||||||
#endif
|
#endif
|
|
@ -139,7 +139,7 @@ bool xApplication::setImage(const char *url){
|
||||||
|
|
||||||
|
|
||||||
// // Anti conflit inter-thread
|
// // Anti conflit inter-thread
|
||||||
// _mutex_hit.lock();
|
// _mutex_hit.try_lock();
|
||||||
|
|
||||||
// /* (1) On recupere le SDL_Rect destination du sprite courant */
|
// /* (1) On recupere le SDL_Rect destination du sprite courant */
|
||||||
// int xIndex = -1;
|
// int xIndex = -1;
|
||||||
|
@ -224,7 +224,7 @@ bool xApplication::setImage(const char *url){
|
||||||
// bool xApplication::hit(string current, int movex, int movey){
|
// bool xApplication::hit(string current, int movex, int movey){
|
||||||
// if( !this->status() ) return true;
|
// if( !this->status() ) return true;
|
||||||
|
|
||||||
// _mutex_hit.lock();
|
// _mutex_hit.try_lock();
|
||||||
|
|
||||||
// /* (1) On recupere le SDL_Rect destination du sprite courant */
|
// /* (1) On recupere le SDL_Rect destination du sprite courant */
|
||||||
// xSprite *sprite = NULL;
|
// xSprite *sprite = NULL;
|
||||||
|
@ -245,29 +245,29 @@ bool xApplication::setImage(const char *url){
|
||||||
|
|
||||||
|
|
||||||
/** adds new sprite to draw */
|
/** adds new sprite to draw */
|
||||||
void xApplication::push(xDrawable* sprite){
|
void xApplication::push(xElement* sprite){
|
||||||
_mutex_draw.lock();
|
_mutex_draw.try_lock();
|
||||||
_sprites.insert(sprite);
|
_sprites.insert(sprite);
|
||||||
_mutex_draw.unlock();
|
_mutex_draw.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** removes a sprite to draw (from its address) */
|
/** removes a sprite to draw (from its address) */
|
||||||
void xApplication::pull(xDrawable* sprite){
|
void xApplication::pull(xElement* sprite){
|
||||||
_mutex_draw.lock();
|
_mutex_draw.try_lock();
|
||||||
_sprites.erase(sprite);
|
_sprites.erase(sprite);
|
||||||
_mutex_draw.unlock();
|
_mutex_draw.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** clears the scene */
|
/** clears the scene */
|
||||||
void xApplication::clear(){
|
void xApplication::clear(){
|
||||||
_mutex_draw.lock();
|
_mutex_draw.try_lock();
|
||||||
_sprites.clear();
|
_sprites.clear();
|
||||||
_mutex_draw.unlock();
|
_mutex_draw.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Update the scene */
|
/** Update the scene */
|
||||||
void xApplication::render(){
|
void xApplication::render(){
|
||||||
_mutex_draw.lock();
|
_mutex_draw.try_lock();
|
||||||
|
|
||||||
/* 1. clear scene */
|
/* 1. clear scene */
|
||||||
SDL_RenderClear(_renderer);
|
SDL_RenderClear(_renderer);
|
||||||
|
@ -281,9 +281,9 @@ void xApplication::render(){
|
||||||
|
|
||||||
|
|
||||||
/* 4. draw every sprite */
|
/* 4. draw every sprite */
|
||||||
set<xDrawable*>::iterator it;
|
set<xElement*>::iterator it;
|
||||||
for( it = _sprites.begin() ; it != _sprites.end() ; it++ ){
|
for( it = _sprites.begin() ; it != _sprites.end() ; it++ ){
|
||||||
xDrawable* sprite = *it;
|
xElement* sprite = *it;
|
||||||
sprite->draw(_renderer);
|
sprite->draw(_renderer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -308,7 +308,7 @@ void xApplication::schedule(){
|
||||||
const uint32_t ticks = SDL_GetTicks();
|
const uint32_t ticks = SDL_GetTicks();
|
||||||
|
|
||||||
// trigger tick() on registered orchestrables
|
// trigger tick() on registered orchestrables
|
||||||
_mutex_orchestrate.lock();
|
_mutex_orchestrate.try_lock();
|
||||||
set<xOrchestrable*>::iterator it;
|
set<xOrchestrable*>::iterator it;
|
||||||
for( it = _orchestrables.begin() ; it != _orchestrables.end() ; it++ ){
|
for( it = _orchestrables.begin() ; it != _orchestrables.end() ; it++ ){
|
||||||
xOrchestrable* orchestrable = *it;
|
xOrchestrable* orchestrable = *it;
|
||||||
|
@ -324,13 +324,13 @@ void xApplication::schedule(){
|
||||||
}
|
}
|
||||||
|
|
||||||
void xApplication::addOrchestrable(xOrchestrable* o){
|
void xApplication::addOrchestrable(xOrchestrable* o){
|
||||||
_mutex_orchestrate.lock();
|
_mutex_orchestrate.try_lock();
|
||||||
_orchestrables.insert(o);
|
_orchestrables.insert(o);
|
||||||
_mutex_orchestrate.unlock();
|
_mutex_orchestrate.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
void xApplication::removeOrchestrable(xOrchestrable* o){
|
void xApplication::removeOrchestrable(xOrchestrable* o){
|
||||||
_mutex_orchestrate.lock();
|
_mutex_orchestrate.try_lock();
|
||||||
_orchestrables.erase(o);
|
_orchestrables.erase(o);
|
||||||
_mutex_orchestrate.unlock();
|
_mutex_orchestrate.unlock();
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,7 @@
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include "xDrawable.h"
|
#include "xElement.h"
|
||||||
#include "xController.h"
|
#include "xController.h"
|
||||||
#include "xOrchestrable.h"
|
#include "xOrchestrable.h"
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
@ -41,8 +41,8 @@
|
||||||
// bool hit(string current, int movex=0, int movey=0); // Gestion des collisions
|
// bool hit(string current, int movex=0, int movey=0); // Gestion des collisions
|
||||||
|
|
||||||
/** scene */
|
/** scene */
|
||||||
void push(xDrawable* sprite);
|
void push(xElement* sprite);
|
||||||
void pull(xDrawable *sprite);
|
void pull(xElement *sprite);
|
||||||
void clear();
|
void clear();
|
||||||
|
|
||||||
void render();
|
void render();
|
||||||
|
@ -71,7 +71,7 @@
|
||||||
xController _controller;
|
xController _controller;
|
||||||
|
|
||||||
// sprites
|
// sprites
|
||||||
set<xDrawable*> _sprites;
|
set<xElement*> _sprites;
|
||||||
|
|
||||||
// execution pool
|
// execution pool
|
||||||
set<xOrchestrable*> _orchestrables;
|
set<xOrchestrable*> _orchestrables;
|
||||||
|
|
|
@ -1 +0,0 @@
|
||||||
#include "xDrawable.h"
|
|
|
@ -0,0 +1 @@
|
||||||
|
#include "xElement.h"
|
|
@ -3,7 +3,7 @@
|
||||||
|
|
||||||
#include "SDL.h"
|
#include "SDL.h"
|
||||||
|
|
||||||
struct xDrawable {
|
struct xElement {
|
||||||
virtual void draw(SDL_Renderer* renderer) = 0;
|
virtual void draw(SDL_Renderer* renderer) = 0;
|
||||||
};
|
};
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
/** clean SDL objects */
|
/** clean SDL objects */
|
||||||
xSprite::~xSprite(){
|
xSprite::~xSprite(){
|
||||||
_mutex.lock();
|
_mutex.try_lock();
|
||||||
SDL_FreeSurface( _surface );
|
SDL_FreeSurface( _surface );
|
||||||
_mutex.unlock();
|
_mutex.unlock();
|
||||||
}
|
}
|
||||||
|
@ -24,7 +24,7 @@ xSprite::xSprite(const char *url, SDL_Rect clip){
|
||||||
|
|
||||||
/** update sprite to rhb color */
|
/** update sprite to rhb color */
|
||||||
void xSprite::setSurface(const int rgba[]){
|
void xSprite::setSurface(const int rgba[]){
|
||||||
_mutex.lock();
|
_mutex.try_lock();
|
||||||
if( _surface != NULL ){
|
if( _surface != NULL ){
|
||||||
SDL_FreeSurface( _surface );
|
SDL_FreeSurface( _surface );
|
||||||
_surface = NULL;
|
_surface = NULL;
|
||||||
|
@ -42,7 +42,7 @@ void xSprite::setSurface(const int rgba[]){
|
||||||
|
|
||||||
/** update sprite to image */
|
/** update sprite to image */
|
||||||
void xSprite::setSurface(const char *url){
|
void xSprite::setSurface(const char *url){
|
||||||
_mutex.lock();
|
_mutex.try_lock();
|
||||||
if( _surface != NULL ){
|
if( _surface != NULL ){
|
||||||
SDL_FreeSurface( _surface );
|
SDL_FreeSurface( _surface );
|
||||||
_surface = NULL;
|
_surface = NULL;
|
||||||
|
@ -58,7 +58,7 @@ void xSprite::setSurface(const char *url){
|
||||||
|
|
||||||
/** copies an existing surface */
|
/** copies an existing surface */
|
||||||
void xSprite::setSurface(SDL_Surface *s){
|
void xSprite::setSurface(SDL_Surface *s){
|
||||||
_mutex.lock();
|
_mutex.try_lock();
|
||||||
if( _surface != NULL ){
|
if( _surface != NULL ){
|
||||||
SDL_FreeSurface( _surface );
|
SDL_FreeSurface( _surface );
|
||||||
_surface = NULL;
|
_surface = NULL;
|
||||||
|
@ -73,29 +73,72 @@ void xSprite::setSurface(SDL_Surface *s){
|
||||||
_mutex.unlock();
|
_mutex.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** set sprite clip */
|
/** add new animation frame with new clip */
|
||||||
|
void xSprite::addFrame(SDL_Rect clip){
|
||||||
|
_mutex.try_lock();
|
||||||
|
_frames.push_back(clip);
|
||||||
|
_mutex.unlock();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** clear every animation frames */
|
||||||
|
void xSprite::clearFrames(){
|
||||||
|
_mutex.try_lock();
|
||||||
|
_frames.clear();
|
||||||
|
_mutex.unlock();
|
||||||
|
}
|
||||||
|
/** set sprite clip and clears animation frames */
|
||||||
void xSprite::setClip(SDL_Rect clip){
|
void xSprite::setClip(SDL_Rect clip){
|
||||||
_mutex.lock();
|
_mutex.try_lock();
|
||||||
_clip = clip;
|
_frames.clear();
|
||||||
|
_frames.push_back(clip);
|
||||||
_mutex.unlock();
|
_mutex.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** set sprite projection */
|
/** set sprite projection */
|
||||||
void xSprite::project(SDL_Rect dest){
|
void xSprite::project(SDL_Rect dest){
|
||||||
_mutex.lock();
|
_mutex.try_lock();
|
||||||
_projection = dest;
|
_projection = dest;
|
||||||
_mutex.unlock();
|
_mutex.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** draws to renderer */
|
/** draws to renderer */
|
||||||
void xSprite::draw(SDL_Renderer* renderer){
|
void xSprite::draw(SDL_Renderer* renderer){
|
||||||
_mutex.lock();
|
_mutex.try_lock();
|
||||||
|
|
||||||
|
// no clip -> use surface dimensions
|
||||||
|
if( _frames.size() <= 0 ){
|
||||||
|
setClip( (SDL_Rect){0, 0, _surface->w, _surface->h} );
|
||||||
|
}
|
||||||
|
|
||||||
|
// only 1 clip -> use it
|
||||||
|
if( _frames.size() == 1 ){
|
||||||
|
_active_clip = 0;
|
||||||
|
}
|
||||||
|
|
||||||
SDL_RenderCopy(
|
SDL_RenderCopy(
|
||||||
renderer,
|
renderer,
|
||||||
SDL_CreateTextureFromSurface(renderer, _surface),
|
SDL_CreateTextureFromSurface(renderer, _surface),
|
||||||
&_clip,
|
&_frames.at(_active_clip),
|
||||||
&_projection
|
&_projection
|
||||||
);
|
);
|
||||||
_mutex.unlock();
|
_mutex.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** animation process */
|
||||||
|
void xSprite::tick(const uint32_t ticks){
|
||||||
|
_mutex.try_lock();
|
||||||
|
uint32_t time_index = ticks / _animation_interval;
|
||||||
|
_active_clip = time_index % _frames.size();
|
||||||
|
_mutex.unlock();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** orchestrate the animation */
|
||||||
|
void xSprite::animate(uint32_t interval){
|
||||||
|
_animation_interval = interval;
|
||||||
|
xApplication::get()->addOrchestrable(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** stops orchestrating the animation */
|
||||||
|
void xSprite::freeze(){
|
||||||
|
xApplication::get()->removeOrchestrable(this);
|
||||||
|
}
|
|
@ -6,11 +6,12 @@
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include "SDL.h"
|
#include "SDL.h"
|
||||||
#include "SDL_image.h"
|
#include "SDL_image.h"
|
||||||
#include "xDrawable.h"
|
#include "xElement.h"
|
||||||
|
#include "xOrchestrable.h"
|
||||||
#include "xApplication.h"
|
#include "xApplication.h"
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
class xSprite : public xDrawable{
|
class xSprite : public xElement, public xOrchestrable {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
// color sprite
|
// color sprite
|
||||||
|
@ -26,19 +27,41 @@
|
||||||
void setSurface(const char *url); // image sprite
|
void setSurface(const char *url); // image sprite
|
||||||
void setSurface(SDL_Surface *t); // copy surface
|
void setSurface(SDL_Surface *t); // copy surface
|
||||||
|
|
||||||
|
// animation
|
||||||
|
void addFrame(SDL_Rect clip);
|
||||||
|
void clearFrames();
|
||||||
|
|
||||||
// sets the sprite clip
|
// sets the sprite clip
|
||||||
|
// + clears animation frames
|
||||||
void setClip(SDL_Rect clip);
|
void setClip(SDL_Rect clip);
|
||||||
|
|
||||||
// set projection into scene
|
// set projection into scene
|
||||||
void project(SDL_Rect dest);
|
void project(SDL_Rect dest);
|
||||||
|
|
||||||
// implement xDrawable
|
// implement xElement
|
||||||
virtual void draw(SDL_Renderer* renderer) override;
|
virtual void draw(SDL_Renderer* renderer) override;
|
||||||
private:
|
|
||||||
|
// orchestrable process
|
||||||
|
void tick(const uint32_t ticks);
|
||||||
|
|
||||||
|
// animation handles
|
||||||
|
void animate(uint32_t interval);
|
||||||
|
void freeze();
|
||||||
|
|
||||||
|
protected:
|
||||||
SDL_Surface* _surface = NULL;
|
SDL_Surface* _surface = NULL;
|
||||||
SDL_Rect _clip;
|
|
||||||
SDL_Rect _projection;
|
SDL_Rect _projection;
|
||||||
|
|
||||||
|
// surface clips
|
||||||
|
// [0] : no clip, use default surface dimensions
|
||||||
|
// [1] : global clip, use it every time
|
||||||
|
// [2+]: clips are used for animation
|
||||||
|
vector<SDL_Rect> _frames;
|
||||||
|
|
||||||
|
// active clip for animation ; equal to 0 if not an animated sprite
|
||||||
|
size_t _active_clip = 0;
|
||||||
|
uint32_t _animation_interval;
|
||||||
|
|
||||||
mutex _mutex;
|
mutex _mutex;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,64 +0,0 @@
|
||||||
#include "xSpriteGroup.h"
|
|
||||||
|
|
||||||
/** clean SDL objects */
|
|
||||||
xSpriteGroup::~xSpriteGroup(){
|
|
||||||
_mutex.lock();
|
|
||||||
_sprites.clear();
|
|
||||||
_mutex.unlock();
|
|
||||||
}
|
|
||||||
|
|
||||||
/** default constructor */
|
|
||||||
xSpriteGroup::xSpriteGroup(){
|
|
||||||
}
|
|
||||||
|
|
||||||
/** adds a sprite to the group */
|
|
||||||
void xSpriteGroup::add(xSprite* sprite){
|
|
||||||
_mutex.lock();
|
|
||||||
_sprites.insert(sprite);
|
|
||||||
_mutex.unlock();
|
|
||||||
}
|
|
||||||
|
|
||||||
/** removes a sprite from the group */
|
|
||||||
void xSpriteGroup::remove(xSprite* sprite){
|
|
||||||
_mutex.lock();
|
|
||||||
_sprites.erase(sprite);
|
|
||||||
_mutex.unlock();
|
|
||||||
}
|
|
||||||
|
|
||||||
/** set absolute move for each sprite */
|
|
||||||
void xSpriteGroup::moveTo(int x, int y){
|
|
||||||
_mutex.lock();
|
|
||||||
_move.x = x;
|
|
||||||
_move.y = y;
|
|
||||||
_mutex.unlock();
|
|
||||||
}
|
|
||||||
|
|
||||||
/** draws to renderer */
|
|
||||||
void xSpriteGroup::draw(SDL_Renderer* renderer){
|
|
||||||
_mutex.lock();
|
|
||||||
|
|
||||||
set<xSprite*>::iterator it;
|
|
||||||
for( it = _sprites.begin() ; it != _sprites.end() ; it++ ){
|
|
||||||
(*it)->draw(renderer);
|
|
||||||
}
|
|
||||||
_mutex.unlock();
|
|
||||||
}
|
|
||||||
|
|
||||||
/** animation process */
|
|
||||||
void xSpriteGroup::tick(const uint32_t ticks){
|
|
||||||
_mutex.lock();
|
|
||||||
uint32_t time_index = ticks / _animation_interval;
|
|
||||||
_active_sprite = time_index % _sprites.size();
|
|
||||||
_mutex.unlock();
|
|
||||||
}
|
|
||||||
|
|
||||||
/** orchestrate the animation */
|
|
||||||
void xSpriteGroup::animate(uint32_t interval){
|
|
||||||
_animation_interval = interval;
|
|
||||||
xApplication::get()->addOrchestrable(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** stops orchestrating the animation */
|
|
||||||
void xSpriteGroup::freeze(){
|
|
||||||
xApplication::get()->removeOrchestrable(this);
|
|
||||||
}
|
|
|
@ -1,52 +0,0 @@
|
||||||
#ifndef DEF_XSPRITEGROUP_H
|
|
||||||
#define DEF_XSPRITEGROUP_H
|
|
||||||
|
|
||||||
#include <string>
|
|
||||||
#include <vector>
|
|
||||||
#include <mutex>
|
|
||||||
#include "SDL.h"
|
|
||||||
#include "SDL_image.h"
|
|
||||||
#include "xDrawable.h"
|
|
||||||
#include "xOrchestrable.h"
|
|
||||||
#include "xApplication.h"
|
|
||||||
#include "xSprite.h"
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
class xSpriteGroup : public xDrawable, public xOrchestrable {
|
|
||||||
|
|
||||||
public:
|
|
||||||
xSpriteGroup();
|
|
||||||
virtual ~xSpriteGroup();
|
|
||||||
|
|
||||||
// manage sprite list
|
|
||||||
void add(xSprite* sprite);
|
|
||||||
void remove(xSprite* sprite);
|
|
||||||
void clear();
|
|
||||||
|
|
||||||
// set absolute move for each sprite
|
|
||||||
void moveTo(int x, int y);
|
|
||||||
|
|
||||||
// implement xDrawable
|
|
||||||
virtual void draw(SDL_Renderer* renderer) override;
|
|
||||||
|
|
||||||
// implement xOrchestrable
|
|
||||||
void tick(const uint32_t ticks);
|
|
||||||
|
|
||||||
// animation handles
|
|
||||||
void animate(uint32_t interval);
|
|
||||||
void freeze();
|
|
||||||
|
|
||||||
protected:
|
|
||||||
SDL_Rect _move;
|
|
||||||
|
|
||||||
set<xSprite*> _sprites;
|
|
||||||
|
|
||||||
// active sprite for animation ; equal to 0 if not an animated sprite
|
|
||||||
size_t _active_sprite = 0;
|
|
||||||
uint32_t _animation_interval;
|
|
||||||
|
|
||||||
mutex _mutex;
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
Loading…
Reference in New Issue