- [x] Erreur plus lent vers la droite
- [x] Creation des briques - [x] Refactor xAnimation extends xSprite - [x] Erreur modification de frames d'une xAnimation, restent tjs les anciennes -> creer methode dans xAnimation
This commit is contained in:
parent
48df701ef5
commit
7921efa3fc
|
@ -36,11 +36,11 @@ int main(int argc, char* argv[]) {
|
|||
btmleft.push("bottom-left");
|
||||
|
||||
// On cree un bout du terrain
|
||||
xMarioGrass btmcenter(mgr, (SDL_Rect){13, 20-3, 10, 3} );
|
||||
xMarioGrass btmcenter(mgr, (SDL_Rect){12, 20-2, 10, 3} );
|
||||
btmcenter.push("bottom-center");
|
||||
|
||||
|
||||
xMarioGrass floattcenter(mgr, (SDL_Rect){10, 10, 5, 5} );
|
||||
xMarioGrass floattcenter(mgr, (SDL_Rect){5, 2, 5, 5} );
|
||||
floattcenter.push("float-center");
|
||||
|
||||
|
||||
|
@ -51,11 +51,24 @@ int main(int argc, char* argv[]) {
|
|||
// xMarioGreenShell gs(mgr, 5, 20-3);
|
||||
// gs.start("green-sheel", 100, SPRITE_ANIM_INFINITE);
|
||||
|
||||
// On cree une brique
|
||||
xMarioBrick mbr1(mgr, 4, 20-5);
|
||||
mbr1.push("brick1");
|
||||
|
||||
// On cree un bloc mystere
|
||||
xMarioMysteryBloc mb(mgr, 5, 20-5);
|
||||
mb.push("mystery-bloc");
|
||||
mb.start(150, SPRITE_ANIM_INFINITE);
|
||||
|
||||
// On cree une brique
|
||||
xMarioBrick mbr2(mgr, 6, 20-5);
|
||||
mbr2.push("brick2");
|
||||
|
||||
|
||||
xMarioMysteryBloc mb1(mgr, 15, 20-5);
|
||||
mb1.push("mystery-bloc2");
|
||||
mb1.start(150, SPRITE_ANIM_INFINITE);
|
||||
|
||||
// On cree un bloc normal
|
||||
// xMarioBloc bl(mgr, (SDL_Rect){0, 20-2, 10, 3});
|
||||
// bl.push("bloc-bottom-left");
|
||||
|
@ -205,8 +218,6 @@ void keydownEventHandler(SDL_Event *e){
|
|||
*
|
||||
*/
|
||||
void keyupEventHandler(SDL_Event *e){
|
||||
SDL_Rect *mRect = mario->viewport();
|
||||
|
||||
|
||||
switch( (*e).key.keysym.sym ){
|
||||
case SDLK_UP:
|
||||
|
|
BIN
SDL#4/main.o
BIN
SDL#4/main.o
Binary file not shown.
|
@ -1,19 +1,25 @@
|
|||
A FAIRE
|
||||
=======
|
||||
- [ ] Ajout d'objets et non uniquement de SDL_Textures au xManager
|
||||
- [ ] Optimisation de update() pas dans boucle infinie, juste dans push/pull avec limitation FPS
|
||||
- [x][ ] Gestion du saut unique ou double (limitation)
|
||||
- [ ] Gestion de l'acceleration
|
||||
- [ ] Gestion de la gravite
|
||||
|
||||
- [ ] Erreur de hit()
|
||||
|
||||
EN COURS
|
||||
========
|
||||
- [ ] Erreur modification de frames d'une xAnimation, restent tjs les anciennes
|
||||
-> creer methode dans xAnimation
|
||||
- [ ] Liberation memoire car lag
|
||||
- [ ] Gestion sprites de mario en fonction mouvement
|
||||
- [...] Gestion velocite pour deplacement
|
||||
|
||||
FAIT
|
||||
====
|
||||
- [x] Erreur plus lent vers la droite
|
||||
- [x] Creation des briques
|
||||
- [x] Refactor xAnimation extends xSprite
|
||||
- [x] Erreur modification de frames d'une xAnimation, restent tjs les anciennes
|
||||
-> creer methode dans xAnimation
|
||||
- [x] Gestion de la gravite
|
||||
- [x] Erreur a corriger pour xSpriteAnimation on doit faire start() mais push() puis start() (qui est equivalent) ne marche pas
|
||||
- [x] Gestion des collisions
|
||||
|
|
|
@ -16,16 +16,20 @@
|
|||
#include "xMario/xMarioMario.h"
|
||||
#include "xMario/xMarioGrass.h"
|
||||
#include "xMario/xMarioGreenShell.h"
|
||||
#include "xMario/xMarioMysteryBloc.h"
|
||||
#include "xMario/xMarioBloc.h"
|
||||
|
||||
#include "xMario/BreakableBloc/xMarioMysteryBloc.h"
|
||||
#include "xMario/BreakableBloc/xMarioBrick.h"
|
||||
|
||||
|
||||
/* [BODIES] Inclusion des .cpp des sous-libs
|
||||
=========================================================*/
|
||||
#include "xMario/xMarioMario.cpp"
|
||||
#include "xMario/xMarioGrass.cpp"
|
||||
#include "xMario/xMarioGreenShell.cpp"
|
||||
#include "xMario/xMarioMysteryBloc.cpp"
|
||||
#include "xMario/xMarioBloc.cpp"
|
||||
|
||||
#include "xMario/BreakableBloc/xMarioMysteryBloc.cpp"
|
||||
#include "xMario/BreakableBloc/xMarioBrick.cpp"
|
||||
|
||||
#endif
|
|
@ -0,0 +1,20 @@
|
|||
/* [CONSTRUCTOR] Construction d'un xMarioBrick
|
||||
=========================================================*/
|
||||
xMarioBrick::xMarioBrick(xManager *m, int x, int y)
|
||||
: xSprite(
|
||||
m,
|
||||
"src/blocs.png"
|
||||
){
|
||||
_manager = m;
|
||||
|
||||
// Note: le rect correspond a un nombre de bloc
|
||||
// On convertit le tout en blocs reels
|
||||
int xReal = x * BLOC_SIZE;
|
||||
int yReal = y * BLOC_SIZE;
|
||||
|
||||
this->dimensions(
|
||||
(SDL_Rect){xReal, yReal, BLOC_SIZE, BLOC_SIZE}, // On definit le viewport
|
||||
(SDL_Rect){136, 0, 16, 16} // On definit le clip
|
||||
);
|
||||
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
#ifndef DEF_XMARIOBRICK_H
|
||||
|
||||
#define DEF_XMARIOBRICK_H
|
||||
|
||||
/* [DEF] Definition de la classe
|
||||
=========================================================*/
|
||||
class xMarioBrick : public xSprite{
|
||||
|
||||
public:
|
||||
xMarioBrick(xManager *m, int x, int y);
|
||||
|
||||
private:
|
||||
xManager *_manager;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
|
@ -13,7 +13,7 @@ xMarioMysteryBloc::xMarioMysteryBloc(xManager *m, int x, int y)
|
|||
this->addFrame( (SDL_Rect){0, 32, 16, 16} );
|
||||
this->addFrame( (SDL_Rect){0, 48, 16, 16} );
|
||||
|
||||
_defaultrect = _viewport;
|
||||
_defaultrect = _dst;
|
||||
_lastjump = SDL_GetTicks();
|
||||
_jumps = 0;
|
||||
_active = true;
|
||||
|
@ -29,14 +29,14 @@ void xMarioMysteryBloc::jump(){
|
|||
if( !this->active() )
|
||||
return;
|
||||
|
||||
if( _defaultrect.y == _viewport.y && SDL_GetTicks()-_lastjump > 300 ){
|
||||
if( _defaultrect.y == _dst.y && SDL_GetTicks()-_lastjump > 300 ){
|
||||
|
||||
_viewport.x = _defaultrect.x - _defaultrect.w*.1/2;
|
||||
_viewport.y = _defaultrect.y-10 - _defaultrect.h*.1/2;
|
||||
_viewport.w = _defaultrect.w * 1.1;
|
||||
_viewport.h = _defaultrect.h * 1.1;
|
||||
_dst.x = _defaultrect.x - _defaultrect.w*.1/2;
|
||||
_dst.y = _defaultrect.y-10 - _defaultrect.h*.1/2;
|
||||
_dst.w = _defaultrect.w * 1.1;
|
||||
_dst.h = _defaultrect.h * 1.1;
|
||||
|
||||
this->move(_viewport);
|
||||
this->move(_dst);
|
||||
|
||||
_lastjump = SDL_GetTicks();
|
||||
|
||||
|
@ -51,14 +51,14 @@ void xMarioMysteryBloc::jump(){
|
|||
/* [UNJUMP] Animation d'activation (quand saut)
|
||||
=========================================================*/
|
||||
void xMarioMysteryBloc::unjump(){
|
||||
if( _defaultrect.y != _viewport.y && SDL_GetTicks()-_lastjump > 100 ){
|
||||
if( _defaultrect.y != _dst.y && SDL_GetTicks()-_lastjump > 100 ){
|
||||
|
||||
_viewport.x = _defaultrect.x;
|
||||
_viewport.y = _defaultrect.y;
|
||||
_viewport.w = _defaultrect.w;
|
||||
_viewport.h = _defaultrect.h;
|
||||
_dst.x = _defaultrect.x;
|
||||
_dst.y = _defaultrect.y;
|
||||
_dst.w = _defaultrect.w;
|
||||
_dst.h = _defaultrect.h;
|
||||
|
||||
this->move(_viewport);
|
||||
this->move(_dst);
|
||||
|
||||
_lastjump = SDL_GetTicks();
|
||||
}
|
||||
|
@ -83,10 +83,8 @@ void xMarioMysteryBloc::active(bool active){
|
|||
if( active ){
|
||||
|
||||
|
||||
_frames.clear();
|
||||
// for( int i = 0 ; i < _frames.size() ; i++ )
|
||||
// _frames.erase(_frames.begin()+i);
|
||||
this->pull();
|
||||
this->clear();
|
||||
|
||||
|
||||
this->addFrame( (SDL_Rect){0, 0, 16, 16} );
|
||||
|
@ -99,10 +97,8 @@ void xMarioMysteryBloc::active(bool active){
|
|||
|
||||
}else{
|
||||
|
||||
_frames.clear();
|
||||
// for( int i = 0 ; i < _frames.size() ; i++ )
|
||||
// _frames.erase(_frames.begin()+i);
|
||||
this->pull();
|
||||
this->clear();
|
||||
|
||||
this->addFrame( (SDL_Rect){0, 64, 16, 16} );
|
||||
this->addFrame( (SDL_Rect){0, 64, 16, 16} );
|
|
@ -18,12 +18,15 @@ xMarioMario::xMarioMario(xManager *m, int x, int y)
|
|||
_down = false;
|
||||
_jumps = 0;
|
||||
|
||||
// Position frame par defaut
|
||||
_pos = "NR"; // bottom-center
|
||||
|
||||
_gravity = 13;
|
||||
|
||||
// Constantes de velocite sur X
|
||||
_velocity[0] = 0.0;
|
||||
_mult[0] = 10;
|
||||
_dec[0] = 0.9;
|
||||
_dec[0] = 1;
|
||||
_acc[0] = 5;
|
||||
_min_vel[0] = 0.1;
|
||||
_max_vel[0] = 40;
|
||||
|
@ -76,26 +79,27 @@ void xMarioMario::moveFromVelocity(){
|
|||
this->velocity(0, -1);
|
||||
_jumps++;
|
||||
}
|
||||
}else if( _down )
|
||||
this->velocity(0, 1);
|
||||
else
|
||||
}else
|
||||
_jumps = 0;
|
||||
|
||||
|
||||
/* (2) Si aucune collision, on deplace */
|
||||
this->move(_velocity[0], _velocity[1]);
|
||||
|
||||
/* (3) Modification du sprite en fonction du mouvement */
|
||||
this->turn();
|
||||
|
||||
/* (3) On diminue la velocite (decceleration) */
|
||||
|
||||
/* (4) On diminue la velocite (decceleration) */
|
||||
_velocity[0] *= ( 1 - _dec[0] );
|
||||
_velocity[1] *= ( 1 - _dec[1] );
|
||||
|
||||
|
||||
/* (4) Gestion de la gravite */
|
||||
/* (5) Gestion de la gravite */
|
||||
if( !this->onFloor() )
|
||||
this->move(0, 13);
|
||||
|
||||
/* (5) Si velocite sous borne min */
|
||||
/* (6) Si velocite sous borne min */
|
||||
if( _velocity[0] < _min_vel[0] ) // sur x
|
||||
_velocity[0] = 0;
|
||||
|
||||
|
@ -103,13 +107,22 @@ void xMarioMario::moveFromVelocity(){
|
|||
_velocity[1] = 0;
|
||||
|
||||
|
||||
/* (6) Gestion du temps */
|
||||
// if( _velocity[0]*_velocity[1] != 0 )
|
||||
// cout << _velocity[0] << " - " << _velocity[1] << endl;
|
||||
/* (7) Gestion du temps */
|
||||
if( abs(_velocity[0]) > 0 )
|
||||
cout << "x -> " << _velocity[0] << endl;
|
||||
|
||||
if( abs(_velocity[1]) > 0 )
|
||||
cout << "y -> " << _velocity[1] << endl;
|
||||
|
||||
usleep(1);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/* [VELOCITY] Retourne un pointeur sur la velocite
|
||||
=========================================================*/
|
||||
double xMarioMario::velocity(bool way){
|
||||
|
@ -118,6 +131,11 @@ double xMarioMario::velocity(bool way){
|
|||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/* [VELOCITY] Modifie la velocite
|
||||
=========================================================*/
|
||||
void xMarioMario::velocity(double x, double y){
|
||||
|
@ -126,14 +144,8 @@ void xMarioMario::velocity(double x, double y){
|
|||
/* (1) Gestion velocite axe X */
|
||||
if( last[0]*x > 0 ) // Si meme sens, on accelere
|
||||
_velocity[0] *= _acc[0];
|
||||
else{
|
||||
_velocity[0] += x * _mult[0];
|
||||
|
||||
if( _velocity[0] > 0 ) // vers droite
|
||||
this->turnRight();
|
||||
else
|
||||
this->turnLeft();
|
||||
}
|
||||
_velocity[0] += x * _mult[0];
|
||||
|
||||
|
||||
/* (2) Gestion velocite axe Y */
|
||||
|
@ -142,6 +154,7 @@ void xMarioMario::velocity(double x, double y){
|
|||
else
|
||||
_velocity[1] += y * _mult[1];
|
||||
|
||||
|
||||
/* (3) On borne la velocite aux max */
|
||||
if( abs(_velocity[0]) > _max_vel[0] ) // Si max x
|
||||
_velocity[0] = _max_vel[0] * (_velocity[0] / abs(_velocity[0]) );
|
||||
|
@ -154,43 +167,162 @@ void xMarioMario::velocity(double x, double y){
|
|||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
bool xMarioMario::onFloor(){
|
||||
return _manager->hit(_texture, 0, 1);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* [TURNLEFT] Charge le sprite vers la gauche
|
||||
|
||||
|
||||
|
||||
/* [TURN] Gestion des sprites en fonction du mouvement
|
||||
=========================================================*/
|
||||
void xMarioMario::turnLeft(){
|
||||
this->stop();
|
||||
void xMarioMario::turn(){
|
||||
/* (0) Variables utiles */
|
||||
bool left = _velocity[0] < 0; // si vers la gauche
|
||||
bool up = _velocity[1] < 0; // si vers le haut
|
||||
|
||||
bool centerx = _velocity[0] == 0; // centre sur x
|
||||
bool centery = _velocity[1] == 0; // centre sur y
|
||||
|
||||
|
||||
|
||||
/* (1) Vers le haut */
|
||||
if( up ){
|
||||
|
||||
/* (1.2) HAUT - CENTRE */
|
||||
if( centerx ){ // tc
|
||||
if( _pos != "TC" ){
|
||||
this->pull();
|
||||
this->clear();
|
||||
|
||||
this->addFrame( (SDL_Rect){2, 0, 19, 29} );
|
||||
this->addFrame( (SDL_Rect){93, 0, 19, 29} );
|
||||
|
||||
// On ajoute au rendu
|
||||
this->addFrame( (SDL_Rect){300, 0, 19, 29} );
|
||||
this->addFrame( (SDL_Rect){300, 0, 19, 29} );
|
||||
this->push(_index);
|
||||
this->start(_timeout, _flags);
|
||||
|
||||
_pos = "TC";
|
||||
return;
|
||||
}
|
||||
|
||||
/* (1.1) HAUT - GAUCHE */
|
||||
}else if( left ){ // tl
|
||||
if( _pos != "TL" ){
|
||||
this->pull();
|
||||
this->clear();
|
||||
this->addFrame( (SDL_Rect){31, 0, 19, 29} );
|
||||
this->addFrame( (SDL_Rect){31, 0, 19, 29} );
|
||||
this->push(_index);
|
||||
|
||||
_pos = "TL";
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* [TURNRIGHT] Charge le sprite vers la droite
|
||||
=========================================================*/
|
||||
void xMarioMario::turnRight(){
|
||||
this->stop();
|
||||
/* (1.2) HAUT - DROITE */
|
||||
}else{ // tr
|
||||
if( _pos != "TR" ){
|
||||
this->pull();
|
||||
this->clear();
|
||||
this->addFrame( (SDL_Rect){300, 0, 19, 29} );
|
||||
this->addFrame( (SDL_Rect){300, 0, 19, 29} );
|
||||
this->push(_index);
|
||||
|
||||
_pos = "TR";
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/* (2) Vers le bas */
|
||||
}else if( !centery ){
|
||||
|
||||
|
||||
|
||||
|
||||
/* (2.2) BAS - CENTRE */
|
||||
if( centerx ){ // bc
|
||||
if( _pos != "BC" ){
|
||||
this->pull();
|
||||
this->clear();
|
||||
this->addFrame( (SDL_Rect){122, 160, 19, 29} );
|
||||
this->addFrame( (SDL_Rect){122, 160, 19, 29} );
|
||||
this->push(_index);
|
||||
|
||||
_pos = "BC";
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
/* (2.1) BAS - GAUCHE */
|
||||
}else if( left ){ // bl
|
||||
if( _pos != "BL" ){
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* (2.2) BAS - DROITE */
|
||||
}else{ // br
|
||||
if( _pos != "BR" ){
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/* (3) Droit */
|
||||
}else{
|
||||
|
||||
/* (3.2) NORMAL - CENTRE */
|
||||
if( centerx ){ // nc
|
||||
if( _pos != "NR" ){
|
||||
this->pull();
|
||||
this->clear();
|
||||
this->addFrame( (SDL_Rect){238, 0, 19, 29} );
|
||||
this->addFrame( (SDL_Rect){329, 0, 19, 29} );
|
||||
|
||||
|
||||
// On ajoute au rendu
|
||||
this->push(_index);
|
||||
this->start(_timeout, _flags);
|
||||
|
||||
_pos = "NR";
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
/* (3.1) NORMAL - GAUCHE */
|
||||
}else if( left ){ // nl
|
||||
if( _pos != "NL" ){
|
||||
this->pull();
|
||||
this->clear();
|
||||
this->addFrame( (SDL_Rect){2, 0, 19, 29} );
|
||||
this->addFrame( (SDL_Rect){93, 0, 19, 29} );
|
||||
this->push(_index);
|
||||
|
||||
_pos = "NL";
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* (3.2) NORMAL - DROITE */
|
||||
}else{ // nr
|
||||
if( _pos != "NR" ){
|
||||
this->pull();
|
||||
this->clear();
|
||||
this->addFrame( (SDL_Rect){238, 0, 19, 29} );
|
||||
this->addFrame( (SDL_Rect){329, 0, 19, 29} );
|
||||
this->push(_index);
|
||||
|
||||
_pos = "NR";
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
|
@ -17,9 +17,8 @@
|
|||
|
||||
void moveFromVelocity();
|
||||
|
||||
// Changement de frames
|
||||
void turnLeft();
|
||||
void turnRight();
|
||||
// Changement de frames en fonction du mouvement
|
||||
void turn();
|
||||
|
||||
// GETTER
|
||||
double velocity(bool way=XMARIO_VEL_HOR); // Recupere velocite
|
||||
|
@ -37,6 +36,9 @@
|
|||
|
||||
int _jumps;
|
||||
|
||||
// Etat physique
|
||||
string _pos;
|
||||
|
||||
|
||||
private:
|
||||
Uint32 _lastmove;
|
||||
|
|
|
@ -362,6 +362,7 @@ void xManager::manageFps(const int fps){
|
|||
/* [UPDATE] Mise a jour du rendu
|
||||
=========================================================*/
|
||||
void xManager::update(){
|
||||
|
||||
// cout << "Update MAIN SPRITE +" << _sprites.size() << " added sprites.." << endl;
|
||||
// On bloque l'acces inter-thread
|
||||
_mutex_update.lock();
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
xSprite::~xSprite(){
|
||||
SDL_DestroyTexture( _texture );
|
||||
_manager = NULL;
|
||||
_manager = NULL;
|
||||
}
|
||||
|
||||
|
||||
|
@ -10,6 +11,8 @@ xSprite::~xSprite(){
|
|||
=========================================================*/
|
||||
xSprite::xSprite(xManager *m){
|
||||
_manager = m;
|
||||
|
||||
SDL_DestroyTexture(_texture);
|
||||
_texture = NULL;
|
||||
}
|
||||
|
||||
|
@ -66,6 +69,68 @@ xSprite::xSprite(xManager *m, SDL_Texture *t){
|
|||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/* [MOVE] Modification de la position/taille du sprite
|
||||
=========================================================*/
|
||||
void xSprite::move(SDL_Rect newpos){
|
||||
|
||||
if( !_manager->hit(_texture, newpos.x, newpos.y) ){
|
||||
|
||||
if( newpos.x != 0 )
|
||||
_dst.x = newpos.x;
|
||||
|
||||
if( newpos.y != 0 )
|
||||
_dst.y = newpos.y;
|
||||
|
||||
if( newpos.w != 0 )
|
||||
_dst.w = newpos.w;
|
||||
|
||||
if( newpos.h != 0)
|
||||
_dst.h = newpos.h;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* [MOVE] Deplacement de la position/taille du sprite
|
||||
=========================================================*/
|
||||
void xSprite::move(int x, int y){
|
||||
/* (1) Variables utiles */
|
||||
int incrx = x;
|
||||
int incry = y;
|
||||
bool moveY = true;
|
||||
|
||||
int signofx = (x==0) ? 0 : x / abs(x);
|
||||
int signofy = (y==0) ? 0 : y / abs(y);
|
||||
|
||||
/* (2) Tant qu'on n'a pas bouge */
|
||||
while( incrx != 0 || incry != 0 ){
|
||||
|
||||
/* (3) Si on peut aller a la destination */
|
||||
if( !_manager->hit(_texture, incrx, incry) ){
|
||||
_dst.x += incrx;
|
||||
_dst.y += incry;
|
||||
return;
|
||||
}
|
||||
|
||||
/* (4) Sinon, on decremente les deplacements alternativement */
|
||||
if( moveY )
|
||||
incry -= signofy;
|
||||
else
|
||||
incrx -= signofx;
|
||||
|
||||
moveY = !moveY;
|
||||
}
|
||||
|
||||
// Mise a jour
|
||||
_manager->update();
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/* [DIMENSIONS] Definition des dimensions par defaut
|
||||
=========================================================*/
|
||||
void xSprite::dimensions(){
|
||||
|
@ -104,12 +169,16 @@ void xSprite::dimensions(SDL_Rect r, SDL_Rect clip){
|
|||
/* [PUSH] Ajoute le xSprite au rendu
|
||||
=========================================================*/
|
||||
void xSprite::push(string index){
|
||||
_index = index;
|
||||
|
||||
_manager->push(index, _texture, &_src, &_dst);
|
||||
}
|
||||
|
||||
/* [PULL] Retire une sprite du rendu
|
||||
=========================================================*/
|
||||
void xSprite::pull(string index){
|
||||
_index = index;
|
||||
|
||||
_manager->pull( index );
|
||||
}
|
||||
|
||||
|
|
|
@ -11,11 +11,15 @@
|
|||
xSprite(xManager *m, SDL_Texture *t); // Sprite texture
|
||||
~xSprite();
|
||||
|
||||
|
||||
void move(SDL_Rect newpos); // Deplace le sprite
|
||||
void move(int x, int y); // Deplace le sprite
|
||||
|
||||
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(string index); // Ajoute a l'affichage
|
||||
virtual void push(string index); // Ajoute a l'affichage
|
||||
void pull(string index); // Retire de l'affichage
|
||||
void pull(); // Retire de l'affichage
|
||||
|
||||
|
@ -28,13 +32,16 @@
|
|||
SDL_Rect *dst();
|
||||
SDL_Rect *src();
|
||||
|
||||
private:
|
||||
protected:
|
||||
xManager *_manager;
|
||||
SDL_Texture *_texture;
|
||||
|
||||
SDL_Rect _dst;
|
||||
SDL_Rect _src;
|
||||
|
||||
// Enregistre le dernier index utilise
|
||||
string _index;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
|
@ -1,105 +1,36 @@
|
|||
/* [DESTRUCTUR] Destruction de l'animation
|
||||
=========================================================*/
|
||||
xSpriteAnimation::~xSpriteAnimation(){
|
||||
|
||||
SDL_DestroyTexture( _texture );
|
||||
_manager = NULL;
|
||||
_texture = NULL;
|
||||
_manager = NULL;
|
||||
|
||||
// On supprime les frames
|
||||
_frames.erase( _frames.begin(), _frames.end() );
|
||||
}
|
||||
|
||||
|
||||
/* [CONSTRUCTOR] Construction de l'animation (chargement)
|
||||
=========================================================*/
|
||||
xSpriteAnimation::xSpriteAnimation(xManager *manager, const char *url, SDL_Rect viewport){
|
||||
/* (1) Definition des attributs */
|
||||
_manager = manager;
|
||||
_texture = NULL;
|
||||
xSpriteAnimation::xSpriteAnimation(xManager *manager, const char *url, SDL_Rect dest)
|
||||
: xSprite( manager, url ){
|
||||
|
||||
_viewport = viewport;
|
||||
|
||||
|
||||
/* (2) On charge le spritesheet */
|
||||
_texture = IMG_LoadTexture( _manager->renderer(), url );
|
||||
|
||||
// Gestion erreur
|
||||
if( _texture == NULL )
|
||||
return;
|
||||
// On definit le viewport
|
||||
this->dimensions(dest);
|
||||
}
|
||||
|
||||
|
||||
/* [CONSTRUCTOR] Construction de l'animation a partir d'une texture existante
|
||||
=========================================================*/
|
||||
xSpriteAnimation::xSpriteAnimation(xManager *manager, SDL_Texture *t, SDL_Rect viewport){
|
||||
/* (1) Definition des attributs */
|
||||
_manager = manager;
|
||||
_texture = NULL;
|
||||
xSpriteAnimation::xSpriteAnimation(xManager *manager, SDL_Texture *t, SDL_Rect dest)
|
||||
: xSprite( manager, t) {
|
||||
|
||||
_viewport = viewport;
|
||||
|
||||
|
||||
/* (2) On charge le spritesheet */
|
||||
_texture = t;
|
||||
|
||||
// Gestion erreur
|
||||
if( _texture == NULL )
|
||||
return;
|
||||
// On definit le viewport
|
||||
this->dimensions(dest);
|
||||
}
|
||||
|
||||
|
||||
/* [MOVE] Modification de la position/taille du sprite
|
||||
=========================================================*/
|
||||
void xSpriteAnimation::move(SDL_Rect newpos){
|
||||
|
||||
if( !_manager->hit(_texture, newpos.x, newpos.y) ){
|
||||
|
||||
if( newpos.x != 0 )
|
||||
_viewport.x = newpos.x;
|
||||
|
||||
if( newpos.y != 0 )
|
||||
_viewport.y = newpos.y;
|
||||
|
||||
if( newpos.w != 0 )
|
||||
_viewport.w = newpos.w;
|
||||
|
||||
if( newpos.h != 0)
|
||||
_viewport.h = newpos.h;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* [MOVE] Deplacement de la position/taille du sprite
|
||||
=========================================================*/
|
||||
void xSpriteAnimation::move(int x, int y){
|
||||
/* (1) Variables utiles */
|
||||
int incrx = x;
|
||||
int incry = y;
|
||||
bool moveY = true;
|
||||
|
||||
int signofx = (x==0) ? 0 : x / abs(x);
|
||||
int signofy = (y==0) ? 0 : y / abs(y);
|
||||
|
||||
/* (2) Tant qu'on n'a pas bouge */
|
||||
while( incrx != 0 || incry != 0 ){
|
||||
|
||||
/* (3) Si on peut aller a la destination */
|
||||
if( !_manager->hit(_texture, incrx, incry) ){
|
||||
_viewport.x += incrx;
|
||||
_viewport.y += incry;
|
||||
return;
|
||||
}
|
||||
|
||||
/* (4) Sinon, on decremente les deplacements alternativement */
|
||||
if( moveY )
|
||||
incry -= signofy;
|
||||
else
|
||||
incrx -= signofx;
|
||||
|
||||
moveY = !moveY;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* [ADDFRAME] Ajout d'une frame d'animation
|
||||
=========================================================*/
|
||||
void xSpriteAnimation::addFrame(SDL_Rect clip){
|
||||
|
@ -119,27 +50,10 @@ void xSpriteAnimation::addFrame(SDL_Rect clip){
|
|||
/* [CLEAR] Supprime toutes les frames
|
||||
=========================================================*/
|
||||
void xSpriteAnimation::clear(){
|
||||
|
||||
for( int i = 0 ; i < _frames.size() ; i++ )
|
||||
_frames.erase(_frames.begin() + i);
|
||||
|
||||
cout << "NB: " << _frames.size() << endl;
|
||||
_frames.erase(_frames.begin(), _frames.end());
|
||||
}
|
||||
|
||||
|
||||
/* [MANAGER] Retourne le manager
|
||||
=========================================================*/
|
||||
xManager *xSpriteAnimation::manager(){ return _manager; }
|
||||
|
||||
|
||||
/* [VIEWPORT] Retourne le viewport
|
||||
=========================================================*/
|
||||
SDL_Rect *xSpriteAnimation::viewport(){ return &_viewport; }
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -153,32 +67,13 @@ void xSpriteAnimation::push(string index){
|
|||
if( _frames.size() == 0 )
|
||||
return;
|
||||
|
||||
_frame = _frames[0];
|
||||
_src = _frames[0];
|
||||
|
||||
/* (1) On ajoute le sprite au rendu */
|
||||
_manager->push(index, _texture, &_frame, &_viewport);
|
||||
_manager->push(index, _texture, &_src, &_dst);
|
||||
}
|
||||
|
||||
|
||||
/* [PULL] Retire du rendu
|
||||
=========================================================*/
|
||||
void xSpriteAnimation::pull(string index){
|
||||
_index = index;
|
||||
|
||||
/* (2) On retire le sprite du rendu */
|
||||
_manager->pull(index);
|
||||
}
|
||||
|
||||
|
||||
/* [PULL] Retire du rendu
|
||||
=========================================================*/
|
||||
void xSpriteAnimation::pull(){
|
||||
/* (2) On retire le sprite du rendu */
|
||||
_manager->pull(_texture);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -199,7 +94,7 @@ void xSpriteAnimationProcess(xSpriteAnimation *xSA, int t, int flags){
|
|||
|
||||
|
||||
// On met a jour la frame
|
||||
xSA->_frame = xSA->_frames[i];
|
||||
xSA->_src = xSA->_frames.at(i);
|
||||
|
||||
xSA->manager()->update();
|
||||
|
||||
|
@ -248,4 +143,7 @@ void xSpriteAnimation::start(int t, int flags){
|
|||
void xSpriteAnimation::stop(){
|
||||
/* (1) On arrete l'animation */
|
||||
delete _animation;
|
||||
|
||||
// On nettoie le pointeur
|
||||
_animation = NULL;
|
||||
}
|
|
@ -2,27 +2,17 @@
|
|||
|
||||
#define DEF_XSPRITEANIMATION_H
|
||||
|
||||
class xSpriteAnimation{
|
||||
class xSpriteAnimation : public xSprite{
|
||||
|
||||
public:
|
||||
xSpriteAnimation(xManager *manager, const char *url, SDL_Rect viewport); // Spritesheet avec taille de chaque sprite
|
||||
xSpriteAnimation(xManager *manager, SDL_Texture *t, SDL_Rect viewport); // Spritesheet avec taille de chaque sprite
|
||||
xSpriteAnimation(xManager *manager, const char *url, SDL_Rect dest); // Spritesheet avec taille de chaque sprite
|
||||
xSpriteAnimation(xManager *manager, SDL_Texture *t, SDL_Rect dest); // Spritesheet avec taille de chaque sprite
|
||||
~xSpriteAnimation();
|
||||
|
||||
void move(SDL_Rect newpos); // Deplace l'animation
|
||||
void move(int x, int y); // Deplace l'animation
|
||||
|
||||
void addFrame(SDL_Rect clip); // Ajoute une frame en fonction des coordonnees
|
||||
void clear(); // Supprime les frames
|
||||
|
||||
// GETTER
|
||||
xManager *manager();
|
||||
SDL_Rect *viewport();
|
||||
|
||||
// Gestion de l'ajout au rendu
|
||||
void push(string index); // Ajout au rendu
|
||||
void pull(string index); // Retrait du rendu
|
||||
void pull(); // Retrait du rendu
|
||||
void push(string index); // Ajoute au rendu
|
||||
|
||||
// Gestion de l'animation
|
||||
void start(int t, int flags=SPRITE_ANIM_ONCE);
|
||||
|
@ -32,18 +22,10 @@
|
|||
|
||||
|
||||
protected:
|
||||
xManager *_manager;
|
||||
SDL_Texture *_texture;
|
||||
|
||||
// Position de l'animation
|
||||
SDL_Rect _viewport;
|
||||
|
||||
// Contiendra les frames
|
||||
vector<SDL_Rect> _frames;
|
||||
SDL_Rect _frame; // Frame courante
|
||||
|
||||
// Nom
|
||||
string _index;
|
||||
// Pour rappel animation
|
||||
int _timeout;
|
||||
int _flags;
|
||||
|
||||
|
|
|
@ -6,9 +6,15 @@ xSpriteGroup::xSpriteGroup(){
|
|||
/* [DESTRUCTOR] Efface la liste de xSprite
|
||||
=========================================================*/
|
||||
xSpriteGroup::~xSpriteGroup(){
|
||||
for( int i = _sprites.size()-1 ; i >= 0 ; i-- )
|
||||
// Lors de la suppression, on libere toutes les textures
|
||||
for( int i = _sprites.size()-1 ; i >= 0 ; i-- ){
|
||||
|
||||
delete _sprites[i];
|
||||
_sprites[i] = 0;
|
||||
|
||||
_sprites.erase( _sprites.begin() + i );
|
||||
}
|
||||
}
|
||||
|
||||
/* [MOVE] Deplace toutes les sprites
|
||||
=========================================================*/
|
||||
|
|
Loading…
Reference in New Issue