SDL#3 Conflits animations regle (mutex) + a faire animations en fonction de taille d'un bloc + coordonnees dans matrice pour les Sprites animes
This commit is contained in:
parent
b0e5c93a23
commit
c6143c3ee1
|
@ -49,9 +49,18 @@ Sprite::Sprite(sdltl *m, const char *url){
|
||||||
_manager = m;
|
_manager = m;
|
||||||
_texture = NULL;
|
_texture = NULL;
|
||||||
|
|
||||||
/* (1) On cree la surface (avec image) */
|
/* (1) On cree la texture directement */
|
||||||
_texture = IMG_LoadTexture(_manager->renderer(), url);
|
_texture = IMG_LoadTexture(_manager->renderer(), url);
|
||||||
|
|
||||||
|
|
||||||
|
/* (1bis) On cree la surface puis on la copie dans la texture */
|
||||||
|
// SDL_Surface *surf = IMG_Load( url );
|
||||||
|
|
||||||
|
// _texture = SDL_CreateTextureFromSurface(_manager->renderer(), surf);
|
||||||
|
|
||||||
|
// SDL_FreeSurface(surf);
|
||||||
|
// surf = NULL;
|
||||||
|
|
||||||
// Gestion erreur
|
// Gestion erreur
|
||||||
if( _texture == NULL )
|
if( _texture == NULL )
|
||||||
return;
|
return;
|
||||||
|
@ -231,7 +240,7 @@ void SpriteGroupAnimation(SpriteGroup *sg, int t, int flags){
|
||||||
sg->get(i)->push();
|
sg->get(i)->push();
|
||||||
|
|
||||||
// On met a jour le rendu
|
// On met a jour le rendu
|
||||||
// sg->get(i)->update();
|
sg->get(i)->update();
|
||||||
|
|
||||||
|
|
||||||
if( SDL_GetTicks()-timer < t )
|
if( SDL_GetTicks()-timer < t )
|
||||||
|
|
|
@ -88,25 +88,25 @@ int main(int argc, char* argv[]) {
|
||||||
vector<thread*> animations(0);
|
vector<thread*> animations(0);
|
||||||
|
|
||||||
|
|
||||||
// animations.push_back( animated_mystery_bloc.animate(
|
animations.push_back( animated_mystery_bloc.animate(
|
||||||
// 200, // delai entre-frames
|
200, // delai entre-frames
|
||||||
// SPRITE_ANIM_INFINITE // FLAGS
|
SPRITE_ANIM_INFINITE // FLAGS
|
||||||
// ) );
|
) );
|
||||||
// delete animations[0]; // On le supprime ensuite
|
delete animations[0]; // On le supprime ensuite
|
||||||
|
|
||||||
|
|
||||||
// animations.push_back( animated_green_shell.animate(
|
animations.push_back( animated_green_shell.animate(
|
||||||
// 100, // delai entre-frames
|
100, // delai entre-frames
|
||||||
// SPRITE_ANIM_INFINITE // FLAGS
|
SPRITE_ANIM_INFINITE // FLAGS
|
||||||
// ) );
|
) );
|
||||||
// delete animations[1]; // On le supprime ensuite
|
delete animations[1]; // On le supprime ensuite
|
||||||
|
|
||||||
|
|
||||||
animations.push_back( animated_red_shell.animate(
|
animations.push_back( animated_red_shell.animate(
|
||||||
100, // delai entre-frames
|
100, // delai entre-frames
|
||||||
SPRITE_ANIM_INFINITE // FLAGS
|
SPRITE_ANIM_INFINITE // FLAGS
|
||||||
) );
|
) );
|
||||||
delete animations[0]; // On le supprime ensuite
|
delete animations[2]; // On le supprime ensuite
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
BIN
SDL#3/main.o
BIN
SDL#3/main.o
Binary file not shown.
|
@ -86,15 +86,24 @@ bool sdltl::setImage(const char *url){
|
||||||
/* [PUSH] Ajoute une texture au rendu principal
|
/* [PUSH] Ajoute une texture au rendu principal
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
void sdltl::push(SDL_Texture *t, SDL_Rect *src, SDL_Rect *dst){
|
void sdltl::push(SDL_Texture *t, SDL_Rect *src, SDL_Rect *dst){
|
||||||
|
// On bloque l'acces inter-thread
|
||||||
|
_mutex.lock();
|
||||||
|
|
||||||
_sprites.push_back( t );
|
_sprites.push_back( t );
|
||||||
_src.push_back( src );
|
_src.push_back( src );
|
||||||
_dst.push_back( dst );
|
_dst.push_back( dst );
|
||||||
|
|
||||||
|
// On debloque l'acces
|
||||||
|
_mutex.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* [PULL] Retire une texture du rendu principal
|
/* [PULL] Retire une texture du rendu principal
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
void sdltl::pull(SDL_Texture *t){
|
void sdltl::pull(SDL_Texture *t){
|
||||||
|
// On bloque l'acces inter-thread
|
||||||
|
_mutex.lock();
|
||||||
|
|
||||||
// On cherche l'indice de la texture
|
// On cherche l'indice de la texture
|
||||||
int index = -1;
|
int index = -1;
|
||||||
|
|
||||||
|
@ -109,6 +118,9 @@ void sdltl::pull(SDL_Texture *t){
|
||||||
_sprites.erase( _sprites.begin() + index );
|
_sprites.erase( _sprites.begin() + index );
|
||||||
_src.erase( _src.begin() + index );
|
_src.erase( _src.begin() + index );
|
||||||
_dst.erase( _dst.begin() + index );
|
_dst.erase( _dst.begin() + index );
|
||||||
|
|
||||||
|
// On debloque l'acces
|
||||||
|
_mutex.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -116,6 +128,7 @@ void sdltl::pull(SDL_Texture *t){
|
||||||
/* [UPDATE] Mise a jour du rendu
|
/* [UPDATE] Mise a jour du rendu
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
void sdltl::update(){
|
void sdltl::update(){
|
||||||
|
// cout << "Update MAIN SPRITE +" << _sprites.size() << " added sprites.." << endl;
|
||||||
|
|
||||||
|
|
||||||
/* (1) On efface le rendu */
|
/* (1) On efface le rendu */
|
||||||
|
@ -134,7 +147,6 @@ void sdltl::update(){
|
||||||
|
|
||||||
|
|
||||||
/* (4) On ajoute toutes les textures pushees */
|
/* (4) On ajoute toutes les textures pushees */
|
||||||
cout << "Update MAIN SPRITE +" << _sprites.size() << " added sprites.." << endl;
|
|
||||||
for( int i = 0 ; i < _sprites.size() ; i++ )
|
for( int i = 0 ; i < _sprites.size() ; i++ )
|
||||||
SDL_RenderCopy(_renderer, _sprites[i], _src[i], _dst[i]);
|
SDL_RenderCopy(_renderer, _sprites[i], _src[i], _dst[i]);
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
#include "SDL.h"
|
#include "SDL.h"
|
||||||
#include "SDL_image.h"
|
#include "SDL_image.h"
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <mutex>
|
||||||
|
|
||||||
/* [LIBS] Externes
|
/* [LIBS] Externes
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
|
@ -49,10 +50,15 @@
|
||||||
SDL_Renderer *_renderer;
|
SDL_Renderer *_renderer;
|
||||||
SDL_Texture *_texture;
|
SDL_Texture *_texture;
|
||||||
|
|
||||||
|
// Gestion des textures
|
||||||
vector<SDL_Texture*> _sprites;
|
vector<SDL_Texture*> _sprites;
|
||||||
vector<SDL_Rect*> _src;
|
vector<SDL_Rect*> _src;
|
||||||
vector<SDL_Rect*> _dst;
|
vector<SDL_Rect*> _dst;
|
||||||
|
|
||||||
|
// Protection thread-safe
|
||||||
|
mutex _mutex;
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue