Animation par SDL_Rect ok + lib faite
|
@ -24,4 +24,4 @@ Lab de prog. en C++.
|
|||
|
||||
[SDL#3] Passage de SDL_Surface a SDL_Renderer
|
||||
|
||||
|
||||
[SDL#4] Gestion des spritesheet
|
||||
|
|
|
@ -26,6 +26,14 @@ int main(int argc, char* argv[]) {
|
|||
// mgr->setImage( "src/1.bmp" );
|
||||
|
||||
|
||||
/* [3] Gestion des animations
|
||||
=========================================================*/
|
||||
// Definition du spritesheet
|
||||
// SpriteSheet mario("url", unit_width, unit_height);
|
||||
|
||||
// Creation des animations a partir du spritesheet
|
||||
// SpriteAnimation mario_mystery_block(mario, x, y);
|
||||
|
||||
|
||||
/* [4] On ajoute Une animation
|
||||
=========================================================*/
|
||||
|
|
BIN
SDL#3/main.o
|
@ -0,0 +1,92 @@
|
|||
#include "main.h"
|
||||
|
||||
// On cree un sdl-toplevel statique
|
||||
static xManager *mgr = NULL;
|
||||
static bool running = true;
|
||||
|
||||
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
srand(time(0));
|
||||
|
||||
/* [0] Initialisation du manager + Creation de la fenetre
|
||||
=========================================================*/
|
||||
mgr = new xManager("Ma fenetre SDL", WIN_WIDTH, WIN_HEIGHT);
|
||||
|
||||
// Gestion erreur
|
||||
if( !mgr->status() ){
|
||||
cout << "[INIT] -> " << SDL_GetError() << endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
/* [2] On definit le background color
|
||||
=========================================================*/
|
||||
mgr->setBackground(255, 0, 255, 150);
|
||||
// mgr->setImage( "src/1.bmp" );
|
||||
|
||||
|
||||
/* [3] Gestion des animations
|
||||
=========================================================*/
|
||||
xSpriteAnimation mario_mystery_block(
|
||||
mgr,
|
||||
"src/mario_crop.png",
|
||||
(SDL_Rect){0, 0, 20, 20}
|
||||
);
|
||||
mario_mystery_block.addFrame(4, 2);
|
||||
mario_mystery_block.addFrame(5, 2);
|
||||
mario_mystery_block.addFrame(6, 2);
|
||||
mario_mystery_block.addFrame(7, 2);
|
||||
|
||||
mario_mystery_block.start(200, SPRITE_ANIM_INFINITE);
|
||||
|
||||
|
||||
xSpriteAnimation mario_green_shell(
|
||||
mgr,
|
||||
"src/mario_crop.png",
|
||||
(SDL_Rect){50, 50, 20, 20}
|
||||
);
|
||||
mario_green_shell.addFrame(4, 11);
|
||||
mario_green_shell.addFrame(5, 11);
|
||||
mario_green_shell.addFrame(6, 11);
|
||||
mario_green_shell.addFrame(7, 11);
|
||||
|
||||
mario_green_shell.start(200, SPRITE_ANIM_INFINITE);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/* [n-1] Boucle infinie
|
||||
=========================================================*/
|
||||
mgr->update();
|
||||
mgr->manageFps(FPS);
|
||||
running = true;
|
||||
while(running){
|
||||
|
||||
mgr->waitEvent(SDL_QUIT, &quitEventHandler);
|
||||
|
||||
// cout << "Main loop" << endl;
|
||||
mgr->update();
|
||||
|
||||
// Gestion des FPS (vitesse de la boucle)
|
||||
mgr->manageFps();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/* [n] Fin d'execution
|
||||
=========================================================*/
|
||||
delete mgr;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
void quitEventHandler(SDL_Event *e){
|
||||
cout << "Ferme" << endl;
|
||||
running = false;
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
#ifndef DEF_MAIN_H
|
||||
|
||||
#define DEF_MAIN_H
|
||||
|
||||
/* [LIB] Internes
|
||||
=========================================================*/
|
||||
#include <iostream>
|
||||
#include <ctime>
|
||||
#include <thread>
|
||||
|
||||
/* [LIB] Externes
|
||||
=========================================================*/
|
||||
#include "xSDL.h" // Librairie perso
|
||||
|
||||
/* [NS] Namespace
|
||||
=========================================================*/
|
||||
using namespace std;
|
||||
|
||||
/* [CONST] Constantes et enumerations
|
||||
=========================================================*/
|
||||
#define WIN_WIDTH 1200
|
||||
#define WIN_HEIGHT 800
|
||||
|
||||
#define FPS 60
|
||||
|
||||
/* [FONCTIONS] Fonctions du corps
|
||||
=========================================================*/
|
||||
void quitEventHandler(SDL_Event *e);
|
||||
|
||||
#endif
|
|
@ -0,0 +1,41 @@
|
|||
.PHONY: init, clean, mrproper
|
||||
CC=g++
|
||||
FLAGS=-pthread -std=c++11 `pkg-config sdl2 --cflags --libs` -l SDL2_image
|
||||
|
||||
# INIT > STRUCTURE DE FICHIERS POUR LES EXECUTABLES
|
||||
init: clean
|
||||
mkdir dep.o
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# EXECUTABLE > DEPENDANCES DE L'EXECUTABLE
|
||||
all: init main.o clean
|
||||
rm -r dep.o
|
||||
$(CC) main.o -o exe $(FLAGS)
|
||||
|
||||
# AMORCE > PROGRAMME PRINCIPAL
|
||||
main.o: main.cpp
|
||||
$(CC) -c $< -o main.o $(FLAGS)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# RESET > SUPPRESSION DES FICHIERS
|
||||
clean:
|
||||
touch init.o
|
||||
rm -r *.o
|
||||
|
||||
# RESET FOR REBUILD > SUPPRESSION DE L'EXECUTABLE
|
||||
mrproper:
|
||||
rm exe
|
After Width: | Height: | Size: 8.5 MiB |
After Width: | Height: | Size: 257 KiB |
After Width: | Height: | Size: 13 KiB |
After Width: | Height: | Size: 32 KiB |
After Width: | Height: | Size: 194 KiB |
After Width: | Height: | Size: 33 KiB |
After Width: | Height: | Size: 142 KiB |
After Width: | Height: | Size: 53 KiB |
|
@ -0,0 +1,39 @@
|
|||
#ifndef DEF_XSDL_H
|
||||
|
||||
#define DEF_XSDL_H
|
||||
|
||||
/* [LIBS] Internes
|
||||
=========================================================*/
|
||||
#include "SDL.h"
|
||||
#include "SDL_image.h"
|
||||
#include <vector>
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
#include <unistd.h>
|
||||
|
||||
/* [DEF] Definitions basiques
|
||||
=========================================================*/
|
||||
#define SPRITE_ANIM_ONCE 0x1
|
||||
#define SPRITE_ANIM_INFINITE 0x10
|
||||
#define SPRITE_ANIM_REVERSE 0x100
|
||||
|
||||
/* [NS] Namespace
|
||||
=========================================================*/
|
||||
using namespace std;
|
||||
|
||||
/* [HEADERS] Inclusion des .h des sous-libs
|
||||
=========================================================*/
|
||||
#include "xSDL/xManager.h"
|
||||
#include "xSDL/xSprite.h"
|
||||
#include "xSDL/xSpriteGroup.h"
|
||||
#include "xSDL/xSpriteAnimation.h"
|
||||
|
||||
|
||||
/* [BODIES] Inclusion des .cpp des sous-libs
|
||||
=========================================================*/
|
||||
#include "xSDL/xManager.cpp"
|
||||
#include "xSDL/xSprite.cpp"
|
||||
#include "xSDL/xSpriteGroup.cpp"
|
||||
#include "xSDL/xSpriteAnimation.cpp"
|
||||
|
||||
#endif
|
|
@ -0,0 +1,197 @@
|
|||
/* [CONSTRUCTOR] Constructeur de la classe
|
||||
=========================================================*/
|
||||
xManager::xManager(const char *t, int w, int h){
|
||||
// default values
|
||||
_lasttick = 0;
|
||||
_fpstime = 1000/60;
|
||||
_window = NULL;
|
||||
_renderer = NULL;
|
||||
_texture = NULL;
|
||||
|
||||
|
||||
// Initialisation des sous-sys. SDL
|
||||
SDL_Init( SDL_INIT_EVERYTHING );
|
||||
|
||||
|
||||
// Creation de la fenetre
|
||||
_window = SDL_CreateWindow(
|
||||
t,
|
||||
SDL_WINDOWPOS_CENTERED,
|
||||
SDL_WINDOWPOS_CENTERED,
|
||||
w,
|
||||
h,
|
||||
SDL_WINDOW_SHOWN
|
||||
);
|
||||
|
||||
// Gestion erreur
|
||||
if( _window == NULL )
|
||||
return;
|
||||
|
||||
|
||||
// Creation du renderer
|
||||
_renderer = SDL_CreateRenderer(
|
||||
_window,
|
||||
-1,
|
||||
SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
/* [DESTROYER] Destructeur de la classe
|
||||
=========================================================*/
|
||||
xManager::~xManager(){
|
||||
SDL_DestroyTexture(_texture);
|
||||
SDL_DestroyRenderer(_renderer);
|
||||
SDL_DestroyWindow( _window );
|
||||
SDL_Quit();
|
||||
}
|
||||
|
||||
/* [STATUS] Retourne le status
|
||||
=========================================================*/
|
||||
bool xManager::status(){
|
||||
return _window != NULL && _renderer != NULL;
|
||||
}
|
||||
|
||||
|
||||
/* [WINDOW] Retourne la fenetre
|
||||
=========================================================*/
|
||||
SDL_Window* xManager::window(){ return _window; }
|
||||
|
||||
|
||||
/* [SCREEN] Retourne la fenetre
|
||||
=========================================================*/
|
||||
SDL_Renderer* xManager::renderer(){ return _renderer; }
|
||||
|
||||
|
||||
/* [SETBACKGROUND] Modifie la couleur de fond
|
||||
=========================================================*/
|
||||
bool xManager::setBackground(Uint8 r, Uint8 g, Uint8 b, Uint8 a){
|
||||
|
||||
SDL_SetRenderDrawColor( _renderer, r, g, b, a );
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/* [SETIMAGE] Met une image en fond
|
||||
=========================================================*/
|
||||
bool xManager::setImage(const char *url){
|
||||
|
||||
// On cree la texture associee
|
||||
_texture = IMG_LoadTexture( _renderer, url );
|
||||
|
||||
return _texture != NULL;
|
||||
}
|
||||
|
||||
|
||||
/* [PUSH] Ajoute une texture au rendu principal
|
||||
=========================================================*/
|
||||
void xManager::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 xManager::pull(SDL_Texture *t){
|
||||
// On bloque l'acces inter-thread
|
||||
_mutex.lock();
|
||||
|
||||
// On cherche l'indice de la texture
|
||||
int index = -1;
|
||||
|
||||
for( int i = 0 ; i < _sprites.size() ; i++ )
|
||||
if( _sprites[i] == t ) index = i;
|
||||
|
||||
// Si on a rien trouve
|
||||
if( index == -1 )
|
||||
return;
|
||||
|
||||
// On supprime la texture et ses dimensions
|
||||
_sprites.erase( _sprites.begin() + index );
|
||||
_src.erase( _src.begin() + index );
|
||||
_dst.erase( _dst.begin() + index );
|
||||
|
||||
// On debloque l'acces
|
||||
_mutex.unlock();
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* [UPDATE] Mise a jour du rendu
|
||||
=========================================================*/
|
||||
void xManager::update(){
|
||||
// cout << "Update MAIN SPRITE +" << _sprites.size() << " added sprites.." << endl;
|
||||
|
||||
|
||||
/* (1) On efface le rendu */
|
||||
SDL_RenderClear(_renderer);
|
||||
|
||||
|
||||
/* (2) On applique la couleur de fond */
|
||||
SDL_Rect dRect; // On recupere les dimensions de la fenetre
|
||||
SDL_GetWindowSize(_window, &dRect.w, &dRect.h);
|
||||
SDL_RenderDrawRect(_renderer, &dRect);
|
||||
|
||||
|
||||
/* (3) On ajoute le rendu principal (si existe) */
|
||||
if( _texture != NULL)
|
||||
SDL_RenderCopy(_renderer, _texture, NULL, NULL);
|
||||
|
||||
|
||||
/* (4) On ajoute toutes les textures pushees */
|
||||
for( int i = 0 ; i < _sprites.size() ; i++ )
|
||||
SDL_RenderCopy(_renderer, _sprites[i], _src[i], _dst[i]);
|
||||
|
||||
|
||||
/* (n) On affiche le resultat */
|
||||
SDL_RenderPresent(_renderer);
|
||||
|
||||
}
|
||||
|
||||
/* [WAITEVENT] Attends un evenement pour executer
|
||||
=========================================================*/
|
||||
void xManager::waitEvent(SDL_EventType t, void(*handler)(SDL_Event*)){
|
||||
SDL_Event event;
|
||||
|
||||
// On attends un evenement
|
||||
while( SDL_PollEvent(&event) ){
|
||||
|
||||
// quand evenement, si type = t
|
||||
if( event.type == t )
|
||||
(*handler)(&event);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/* [MANAGEFTP] Gestion de la vitesse de boucle
|
||||
=========================================================*/
|
||||
void xManager::manageFps(const int fps){
|
||||
/* (1) Definition de fps */
|
||||
if( fps != 0 ) _fpstime = 1000/fps;
|
||||
|
||||
/* (2) Initialisation timer */
|
||||
if( _lasttick == 0 )
|
||||
_lasttick = SDL_GetTicks();
|
||||
|
||||
/* (3) Utilisation en fin de WHILE() */
|
||||
|
||||
// 1 > Si trop rapide, on attends + definit _lasttick
|
||||
else if( SDL_GetTicks()-_lasttick < _fpstime ){
|
||||
SDL_Delay( _fpstime - (SDL_GetTicks()-_lasttick) );
|
||||
|
||||
_lasttick = SDL_GetTicks() + _fpstime - (SDL_GetTicks()-_lasttick);
|
||||
|
||||
// 2 > Temps ok
|
||||
}else
|
||||
_lasttick = SDL_GetTicks();
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,47 @@
|
|||
#ifndef DEF_XMANAGER_H
|
||||
|
||||
#define DEF_XMANAGER_H
|
||||
class xManager{
|
||||
|
||||
public:
|
||||
xManager(const char *t, int w, int h);
|
||||
~xManager();
|
||||
SDL_Window *window();
|
||||
SDL_Renderer *renderer();
|
||||
bool status();
|
||||
bool setBackground(Uint8 r=0xff, Uint8 g=0xff, Uint8 b=0xff, Uint8 a=0xff);
|
||||
bool setImage(const char *url);
|
||||
void waitEvent(SDL_EventType t, void(*handler)(SDL_Event*) );
|
||||
|
||||
void push(SDL_Texture *t, SDL_Rect *origin, SDL_Rect *dest);
|
||||
void pull(SDL_Texture *t);
|
||||
|
||||
void update();
|
||||
|
||||
void manageFps(const int fps=0);
|
||||
|
||||
private:
|
||||
// gestion FPS
|
||||
Uint32 _lasttick;
|
||||
Uint32 _fpstime;
|
||||
|
||||
// status de l'initialisation
|
||||
bool _status;
|
||||
|
||||
// Elements utiles
|
||||
SDL_Window *_window;
|
||||
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;
|
||||
|
||||
|
||||
};
|
||||
|
||||
#endif
|
|
@ -0,0 +1,136 @@
|
|||
/* [DESTRUCTOR] Destruction de la surface
|
||||
=========================================================*/
|
||||
xSprite::~xSprite(){
|
||||
SDL_DestroyTexture( _texture );
|
||||
_manager = NULL;
|
||||
}
|
||||
|
||||
|
||||
/* [CONSTRUCTOR] Construction de la surface vide
|
||||
=========================================================*/
|
||||
xSprite::xSprite(xManager *m){
|
||||
_manager = m;
|
||||
_texture = NULL;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* [CONSTRUCTOR] Construction de la surface avec couleur
|
||||
=========================================================*/
|
||||
xSprite::xSprite(xManager *m, const int rgb[]){
|
||||
_manager = m;
|
||||
_texture = NULL;
|
||||
|
||||
|
||||
cout << "a: [" << (sizeof(rgb)/sizeof(*rgb)) << "]" << endl;
|
||||
|
||||
SDL_Surface *surf = SDL_CreateRGBSurface(0, 0, 0, 32, 0, 0, 0, rgb[3]);
|
||||
|
||||
// On recupere la couleur
|
||||
Uint32 color = SDL_MapRGBA( surf->format, rgb[0], rgb[1], rgb[2], rgb[3]);
|
||||
|
||||
|
||||
// On cree la texture a partir de la surface
|
||||
_texture = SDL_CreateTextureFromSurface(_manager->renderer(), surf);
|
||||
|
||||
|
||||
// On libere la surface inutile maintenant
|
||||
SDL_FreeSurface( surf );
|
||||
surf = NULL;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* [CONSTRUCTOR] Construction de la surface avec image
|
||||
=========================================================*/
|
||||
xSprite::xSprite(xManager *m, const char *url){
|
||||
|
||||
_manager = m;
|
||||
_texture = NULL;
|
||||
|
||||
/* (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;
|
||||
}
|
||||
|
||||
|
||||
/* [DIMENSIONS] Definition des dimensions par defaut
|
||||
=========================================================*/
|
||||
void xSprite::dimensions(){
|
||||
cout <<"A"<<endl;
|
||||
|
||||
/* (1) On recupere les informations de la texture */
|
||||
int w, h;
|
||||
SDL_QueryTexture(_texture, NULL, NULL, &w, &h);
|
||||
|
||||
|
||||
/* (2) On definit les dimensions par defaut */
|
||||
_dst = (SDL_Rect){0, 0, w, h};
|
||||
_src = (SDL_Rect){0, 0, w, h};
|
||||
}
|
||||
|
||||
|
||||
/* [DIMENSIONS] Definition des dimensions de la texture
|
||||
=========================================================*/
|
||||
void xSprite::dimensions(SDL_Rect r){
|
||||
cout <<"B"<<endl;
|
||||
|
||||
/* (1) On recupere les informations de la texture */
|
||||
int w, h;
|
||||
SDL_QueryTexture(_texture, NULL, NULL, &w, &h);
|
||||
|
||||
|
||||
/* (2) On definit les dimensions */
|
||||
_dst = (SDL_Rect){r.x, r.y, r.w, r.h};
|
||||
_src = (SDL_Rect){0, 0, w, h};
|
||||
}
|
||||
|
||||
/* [DIMENSIONS] Definition des dimensions de la texture+clip
|
||||
=========================================================*/
|
||||
void xSprite::dimensions(SDL_Rect r, SDL_Rect clip){
|
||||
cout <<"C"<<endl;
|
||||
|
||||
/* (1) On definit les dimensions */
|
||||
_dst = (SDL_Rect){r.x, r.y, r.w, r.h};
|
||||
_src = (SDL_Rect){clip.x, clip.y, clip.w, clip.h};
|
||||
}
|
||||
|
||||
/* [PUSH] Ajoute le xSprite au rendu
|
||||
=========================================================*/
|
||||
void xSprite::push(){
|
||||
_manager->push(_texture, &_src, &_dst);
|
||||
}
|
||||
|
||||
/* [PULL] Retire une sprite du rendu
|
||||
=========================================================*/
|
||||
void xSprite::pull(){
|
||||
_manager->pull( _texture );
|
||||
}
|
||||
|
||||
/* [UPDATE] Mise a jour du rendu
|
||||
=========================================================*/
|
||||
void xSprite::update(){
|
||||
_manager->update();
|
||||
}
|
||||
|
||||
|
||||
/* [TEXTURE] Retourne la texture
|
||||
=========================================================*/
|
||||
SDL_Texture *xSprite::texture(){ return _texture; }
|
||||
|
||||
/* [MANAGER] Retourne le manager
|
||||
=========================================================*/
|
||||
xManager *xSprite::manager(){ return _manager; }
|
|
@ -0,0 +1,36 @@
|
|||
#ifndef DEF_XSPRITE_H
|
||||
|
||||
#define DEF_XSPRITE_H
|
||||
|
||||
class xSprite{
|
||||
|
||||
public:
|
||||
xSprite(xManager *m); // Sprite vide
|
||||
xSprite(xManager *m, const int rgb[]); // Sprite couleur
|
||||
xSprite(xManager *m, const char *url); // Sprite image
|
||||
~xSprite();
|
||||
|
||||
void dimensions(); // Dimensions par defaut
|
||||
void dimensions(SDL_Rect r); // Dimensions sortie
|
||||
void dimensions(SDL_Rect r, SDL_Rect clip); // Dimensions in/out
|
||||
|
||||
void push(); // Ajoute a l'affichage
|
||||
void pull(); // Retire de l'affichage
|
||||
|
||||
void update(); // Fait renmonter la mise a jour du manager
|
||||
|
||||
// GETTERS
|
||||
SDL_Texture *texture();
|
||||
xManager *manager();
|
||||
|
||||
|
||||
private:
|
||||
xManager *_manager;
|
||||
SDL_Texture *_texture;
|
||||
|
||||
SDL_Rect _dst;
|
||||
SDL_Rect _src;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
|
@ -0,0 +1,111 @@
|
|||
/* [DESTRUCTUR] Destruction de l'animation
|
||||
=========================================================*/
|
||||
xSpriteAnimation::~xSpriteAnimation(){
|
||||
SDL_DestroyTexture( _texture );
|
||||
_manager = NULL;
|
||||
_texture = NULL;
|
||||
}
|
||||
|
||||
/* [CONSTRUCTOR] Construction de l'animation (chargement)
|
||||
=========================================================*/
|
||||
xSpriteAnimation::xSpriteAnimation(xManager *manager, const char *url, SDL_Rect viewport){
|
||||
/* (1) Definition des attributs */
|
||||
_manager = manager;
|
||||
_texture = NULL;
|
||||
|
||||
_clip = viewport;
|
||||
|
||||
|
||||
/* (2) On charge le spritesheet */
|
||||
_texture = IMG_LoadTexture( _manager->renderer(), url );
|
||||
|
||||
// Gestion erreur
|
||||
if( _texture == NULL )
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
/* [ADDFRAME] Ajout d'une frame d'animation
|
||||
=========================================================*/
|
||||
void xSpriteAnimation::addFrame(float x, float y){
|
||||
|
||||
// On ajoute une frame
|
||||
_frames.push_back( (SDL_Rect){
|
||||
(int) x*_clip.w,
|
||||
(int) y*_clip.h,
|
||||
(int) _clip.w,
|
||||
(int) _clip.h
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/* [TRHEAD] Process de l'animation
|
||||
=========================================================*/
|
||||
void xSpriteAnimationProcess(xSpriteAnimation *xSA, int t, int flags){
|
||||
int length = xSA->_frames.size();
|
||||
int timer = 0;
|
||||
int step = 1;
|
||||
int start = 0;
|
||||
int stop = length;
|
||||
|
||||
while( flags&SPRITE_ANIM_INFINITE ){
|
||||
|
||||
/* (1) Pour chaque sprite */
|
||||
for( int i = start ; i != stop ; i+=step ){
|
||||
timer = SDL_GetTicks();
|
||||
|
||||
|
||||
// On met a jour la frame
|
||||
xSA->_frame = xSA->_frames[i];
|
||||
|
||||
|
||||
if( SDL_GetTicks()-timer < t )
|
||||
SDL_Delay( t - (SDL_GetTicks()-timer) );
|
||||
}
|
||||
|
||||
/* (2) Gestion des flags */
|
||||
|
||||
// SPRITE_ANIM_REVERSE
|
||||
if( flags&SPRITE_ANIM_REVERSE ){
|
||||
step *= -1;
|
||||
start = (step==1) ? 0 : length-1;
|
||||
stop = (step==1) ? length-1 : 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/* (n) On termine le thread */
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/* [START] Ajoute l'animation au rendu
|
||||
=========================================================*/
|
||||
void xSpriteAnimation::start(int t, int flags){
|
||||
/* (1) On ajoute le sprite au rendu */
|
||||
_manager->push(_texture, &_frame, &_clip);
|
||||
|
||||
/* (2) On lance l'animation */
|
||||
_animation = new thread(xSpriteAnimationProcess, this, t, flags);
|
||||
|
||||
// On attends pas le thread
|
||||
_animation->detach();
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* [STOP] Arrete l'animation
|
||||
=========================================================*/
|
||||
void xSpriteAnimation::stop(){
|
||||
delete _animation;
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
#ifndef DEF_XSPRITEANIMATION_H
|
||||
|
||||
#define DEF_XSPRITEANIMATION_H
|
||||
|
||||
class xSpriteAnimation{
|
||||
|
||||
public:
|
||||
xSpriteAnimation(xManager *manager, const char *url, SDL_Rect viewport); // Spritesheet avec taille de chaque sprite
|
||||
~xSpriteAnimation();
|
||||
|
||||
void addFrame(float x=0.0, float y=0.0); // Ajoute une frame en fonction des coordonnees
|
||||
|
||||
void start(int t, int flags=SPRITE_ANIM_ONCE);
|
||||
void stop();
|
||||
|
||||
|
||||
private:
|
||||
xManager *_manager;
|
||||
SDL_Texture *_texture;
|
||||
|
||||
// Taille unitaire d'une sprite
|
||||
SDL_Rect _clip;
|
||||
|
||||
// Contiendra les frames
|
||||
vector<SDL_Rect> _frames;
|
||||
SDL_Rect _frame; // Frame courante
|
||||
|
||||
// Contiendra le thread de l'animation
|
||||
thread *_animation;
|
||||
friend void xSpriteAnimationProcess(xSpriteAnimation *xSA, int t, int flags );
|
||||
|
||||
};
|
||||
|
||||
#endif
|
|
@ -0,0 +1,58 @@
|
|||
/* [CONSTRUCTOR] Initialisation de la liste de xSprite
|
||||
=========================================================*/
|
||||
xSpriteGroup::xSpriteGroup(){
|
||||
// _sprites = new vector<xSprite*>(0);
|
||||
}
|
||||
|
||||
/* [ADD] Ajoute un xSprite au groupe
|
||||
=========================================================*/
|
||||
void xSpriteGroup::add(xSprite *s){
|
||||
_sprites.push_back( s );
|
||||
}
|
||||
|
||||
/* [REMOVE] Suppression d'un xSprite du groupe
|
||||
=========================================================*/
|
||||
void xSpriteGroup::remove(xSprite *s){
|
||||
int index = -1; // on cherche l'indice du sprite
|
||||
|
||||
// On parcours la liste pour trouver l'indice
|
||||
for( int i = 0 ; i < _sprites.size() ; i++ )
|
||||
if( _sprites[i] == s ) index = i;
|
||||
|
||||
// Si on a pas trouve l'indice
|
||||
if( index == -1 ) return;
|
||||
|
||||
// On supprime le sprite de la liste
|
||||
_sprites.erase(_sprites.begin() + index );
|
||||
|
||||
}
|
||||
|
||||
/* [PUSH] Ajoute tous les xSprite du groupe a une surface parente
|
||||
=========================================================*/
|
||||
void xSpriteGroup::push(){
|
||||
for( int i = 0 ; i < _sprites.size() ; i++ )
|
||||
_sprites[i]->push();
|
||||
}
|
||||
|
||||
/* [PULL] Retire une sprite de la surface parents
|
||||
=========================================================*/
|
||||
void xSpriteGroup::pull(){
|
||||
for( int i = 0 ; i < _sprites.size() ; i++ )
|
||||
_sprites[i]->pull();
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* [UPDATE] Mise a jour du rendu
|
||||
=========================================================*/
|
||||
void xSpriteGroup::update(){
|
||||
if( _sprites.size() > 0 )
|
||||
_sprites[0]->update();
|
||||
}
|
||||
|
||||
|
||||
/* [GET] Retourne le xSprite d'index donne
|
||||
=========================================================*/
|
||||
xSprite* xSpriteGroup::get(int i){
|
||||
return _sprites[i];
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
#ifndef DEF_XSPRITEGROUP_H
|
||||
|
||||
#define DEF_XSPRITEGROUP_H
|
||||
|
||||
|
||||
class xSpriteGroup{
|
||||
|
||||
public:
|
||||
xSpriteGroup();
|
||||
void add(xSprite *s);
|
||||
void remove(xSprite *s);
|
||||
xSprite* get(int i);
|
||||
|
||||
void push(); // Ajoute les sprites a l'affichage
|
||||
void pull(); // Retire les sprites de l'affichage
|
||||
|
||||
void update(); // Fait renmonter la mise a jour du manager
|
||||
|
||||
private:
|
||||
vector<xSprite*> _sprites;
|
||||
|
||||
};
|
||||
|
||||
|
||||
#endif
|