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;
|
||||
_texture = NULL;
|
||||
|
||||
/* (1) On cree la surface (avec image) */
|
||||
/* (1) On cree la texture directement */
|
||||
_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
|
||||
if( _texture == NULL )
|
||||
return;
|
||||
|
@ -231,7 +240,7 @@ void SpriteGroupAnimation(SpriteGroup *sg, int t, int flags){
|
|||
sg->get(i)->push();
|
||||
|
||||
// On met a jour le rendu
|
||||
// sg->get(i)->update();
|
||||
sg->get(i)->update();
|
||||
|
||||
|
||||
if( SDL_GetTicks()-timer < t )
|
||||
|
|
|
@ -87,26 +87,26 @@ int main(int argc, char* argv[]) {
|
|||
// =========================================================*/
|
||||
vector<thread*> animations(0);
|
||||
|
||||
|
||||
// animations.push_back( animated_mystery_bloc.animate(
|
||||
// 200, // delai entre-frames
|
||||
// SPRITE_ANIM_INFINITE // FLAGS
|
||||
// ) );
|
||||
// delete animations[0]; // On le supprime ensuite
|
||||
|
||||
animations.push_back( animated_mystery_bloc.animate(
|
||||
200, // delai entre-frames
|
||||
SPRITE_ANIM_INFINITE // FLAGS
|
||||
) );
|
||||
delete animations[0]; // On le supprime ensuite
|
||||
|
||||
|
||||
// animations.push_back( animated_green_shell.animate(
|
||||
// 100, // delai entre-frames
|
||||
// SPRITE_ANIM_INFINITE // FLAGS
|
||||
// ) );
|
||||
// delete animations[1]; // On le supprime ensuite
|
||||
animations.push_back( animated_green_shell.animate(
|
||||
100, // delai entre-frames
|
||||
SPRITE_ANIM_INFINITE // FLAGS
|
||||
) );
|
||||
delete animations[1]; // On le supprime ensuite
|
||||
|
||||
|
||||
animations.push_back( animated_red_shell.animate(
|
||||
100, // delai entre-frames
|
||||
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
|
||||
=========================================================*/
|
||||
void sdltl::push(SDL_Texture *t, SDL_Rect *src, SDL_Rect *dst){
|
||||
// On bloque l'acces inter-thread
|
||||
_mutex.lock();
|
||||
|
||||
_sprites.push_back( t );
|
||||
_src.push_back( src );
|
||||
_dst.push_back( dst );
|
||||
|
||||
// On debloque l'acces
|
||||
_mutex.unlock();
|
||||
}
|
||||
|
||||
|
||||
/* [PULL] Retire une texture du rendu principal
|
||||
=========================================================*/
|
||||
void sdltl::pull(SDL_Texture *t){
|
||||
// On bloque l'acces inter-thread
|
||||
_mutex.lock();
|
||||
|
||||
// On cherche l'indice de la texture
|
||||
int index = -1;
|
||||
|
||||
|
@ -109,6 +118,9 @@ void sdltl::pull(SDL_Texture *t){
|
|||
_sprites.erase( _sprites.begin() + index );
|
||||
_src.erase( _src.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
|
||||
=========================================================*/
|
||||
void sdltl::update(){
|
||||
// cout << "Update MAIN SPRITE +" << _sprites.size() << " added sprites.." << endl;
|
||||
|
||||
|
||||
/* (1) On efface le rendu */
|
||||
|
@ -134,7 +147,6 @@ void sdltl::update(){
|
|||
|
||||
|
||||
/* (4) On ajoute toutes les textures pushees */
|
||||
cout << "Update MAIN SPRITE +" << _sprites.size() << " added sprites.." << endl;
|
||||
for( int i = 0 ; i < _sprites.size() ; i++ )
|
||||
SDL_RenderCopy(_renderer, _sprites[i], _src[i], _dst[i]);
|
||||
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
#include "SDL.h"
|
||||
#include "SDL_image.h"
|
||||
#include <vector>
|
||||
#include <mutex>
|
||||
|
||||
/* [LIBS] Externes
|
||||
=========================================================*/
|
||||
|
@ -49,10 +50,15 @@
|
|||
SDL_Renderer *_renderer;
|
||||
SDL_Texture *_texture;
|
||||
|
||||
// Gestion des textures
|
||||
vector<SDL_Texture*> _sprites;
|
||||
vector<SDL_Rect*> _src;
|
||||
vector<SDL_Rect*> _dst;
|
||||
|
||||
// Protection thread-safe
|
||||
mutex _mutex;
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue