SDL#4 Gestion de update a optimiser (uniquement si qqch a faire et pas en continu

This commit is contained in:
xdrm-brackets 2016-03-13 10:08:07 +01:00
parent 8910e63b2c
commit 83fa7db424
6 changed files with 39 additions and 24 deletions

BIN
SDL#4/exe

Binary file not shown.

View File

@ -31,12 +31,12 @@ int main(int argc, char* argv[]) {
xSpriteAnimation mario_mystery_block(
mgr,
"src/mario_crop.png",
(SDL_Rect){0, 0, 20, 20}
(SDL_Rect){0, 0, 50, 50}
);
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.addFrame( (SDL_Rect){4*19, 2*20, 20, 20} );
mario_mystery_block.addFrame( (SDL_Rect){5*19, 2*20, 20, 20} );
mario_mystery_block.addFrame( (SDL_Rect){6*19, 2*20, 20, 20} );
mario_mystery_block.addFrame( (SDL_Rect){7*19, 2*20, 20, 20} );
mario_mystery_block.start(200, SPRITE_ANIM_INFINITE);
@ -44,12 +44,12 @@ int main(int argc, char* argv[]) {
xSpriteAnimation mario_green_shell(
mgr,
"src/mario_crop.png",
(SDL_Rect){50, 50, 20, 20}
(SDL_Rect){50, 0, 50, 50}
);
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.addFrame( (SDL_Rect){4*19, 210, 20, 20} );
mario_green_shell.addFrame( (SDL_Rect){5*19, 210, 20, 20} );
mario_green_shell.addFrame( (SDL_Rect){6*19, 210, 20, 20} );
mario_green_shell.addFrame( (SDL_Rect){7*19, 210, 20, 20} );
mario_green_shell.start(200, SPRITE_ANIM_INFINITE);
@ -64,13 +64,14 @@ int main(int argc, char* argv[]) {
running = true;
while(running){
// Gestion d'un evenement
mgr->waitEvent(SDL_QUIT, &quitEventHandler);
// cout << "Main loop" << endl;
mgr->update();
// Gestion des FPS (vitesse de la boucle)
mgr->manageFps();
mgr->manageFps();
mgr->update(); // Mise a jour du rendu
}
@ -87,6 +88,6 @@ int main(int argc, char* argv[]) {
void quitEventHandler(SDL_Event *e){
cout << "Ferme" << endl;
cout << "Exiting program" << endl;
running = false;
}

Binary file not shown.

BIN
SDL#4/mario_ground.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 63 KiB

View File

@ -13,7 +13,7 @@ xSpriteAnimation::xSpriteAnimation(xManager *manager, const char *url, SDL_Rect
_manager = manager;
_texture = NULL;
_clip = viewport;
_viewport = viewport;
/* (2) On charge le spritesheet */
@ -27,19 +27,22 @@ xSpriteAnimation::xSpriteAnimation(xManager *manager, const char *url, SDL_Rect
/* [ADDFRAME] Ajout d'une frame d'animation
=========================================================*/
void xSpriteAnimation::addFrame(float x, float y){
void xSpriteAnimation::addFrame(SDL_Rect clip){
// On ajoute une frame
_frames.push_back( (SDL_Rect){
(int) x*_clip.w,
(int) y*_clip.h,
(int) _clip.w,
(int) _clip.h
});
clip.x,
clip.y,
clip.w,
clip.h
} );
}
/* [MANAGER] Retourne le manager
=========================================================*/
xManager *xSpriteAnimation::manager(){ return _manager; }
@ -62,6 +65,8 @@ void xSpriteAnimationProcess(xSpriteAnimation *xSA, int t, int flags){
// On met a jour la frame
xSA->_frame = xSA->_frames[i];
// xSA->manager()->update();
if( SDL_GetTicks()-timer < t )
SDL_Delay( t - (SDL_GetTicks()-timer) );
@ -93,7 +98,7 @@ void xSpriteAnimationProcess(xSpriteAnimation *xSA, int t, int flags){
=========================================================*/
void xSpriteAnimation::start(int t, int flags){
/* (1) On ajoute le sprite au rendu */
_manager->push(_texture, &_frame, &_clip);
_manager->push(_texture, &_frame, &_viewport);
/* (2) On lance l'animation */
_animation = new thread(xSpriteAnimationProcess, this, t, flags);
@ -107,5 +112,9 @@ void xSpriteAnimation::start(int t, int flags){
/* [STOP] Arrete l'animation
=========================================================*/
void xSpriteAnimation::stop(){
/* (1) On arrete l'animation */
delete _animation;
/* (2) On retire le sprite du rendu */
_manager->pull(_texture);
}

View File

@ -8,18 +8,23 @@
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 addFrame(SDL_Rect clip); // Ajoute une frame en fonction des coordonnees
// GETTER
xManager *manager();
// Gestion de l'animation
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;
// Position de l'animation
SDL_Rect _viewport;
// Contiendra les frames
vector<SDL_Rect> _frames;