updated animatedsprite.cpp
This commit is contained in:
@@ -1,16 +1,18 @@
|
|||||||
#include "const.h"
|
//#include "const.h"
|
||||||
#include "animatedsprite.h"
|
#include "animatedsprite.h"
|
||||||
#include <iostream>
|
//#include <iostream>
|
||||||
|
|
||||||
// Constructor
|
// Constructor
|
||||||
AnimatedSprite::AnimatedSprite()
|
AnimatedSprite::AnimatedSprite()
|
||||||
{
|
{
|
||||||
|
MovingSprite::init(0, 0, 0, 0, 0, 0, 0, 0, nullptr, nullptr);
|
||||||
init(nullptr, nullptr);
|
init(nullptr, nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Destructor
|
// Destructor
|
||||||
AnimatedSprite::~AnimatedSprite()
|
AnimatedSprite::~AnimatedSprite()
|
||||||
{
|
{
|
||||||
|
MovingSprite::init(0, 0, 0, 0, 0, 0, 0, 0, nullptr, nullptr);
|
||||||
init(nullptr, nullptr);
|
init(nullptr, nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -19,72 +21,90 @@ void AnimatedSprite::init(LTexture *texture, SDL_Renderer *renderer)
|
|||||||
{
|
{
|
||||||
mRenderer = renderer;
|
mRenderer = renderer;
|
||||||
mTexture = texture;
|
mTexture = texture;
|
||||||
|
|
||||||
for (Uint8 i = 0; i < 20; i++)
|
for (Uint8 i = 0; i < 20; i++)
|
||||||
{
|
{
|
||||||
mAnimation[i].numFrames = 0;
|
mAnimation[i].numFrames = 0;
|
||||||
mAnimation[i].speed = 0;
|
mAnimation[i].speed = 0;
|
||||||
mAnimation[i].loop = true;
|
mAnimation[i].loop = true;
|
||||||
for (Uint8 j = 0; i < 20; i++)
|
mAnimation[i].currentFrame = 0;
|
||||||
|
mAnimation[i].counter = 0;
|
||||||
|
for (Uint8 j = 0; i < 50; i++)
|
||||||
{
|
{
|
||||||
mAnimation[i].frames[j].x = 0;
|
mAnimation[i].frame[j].x = 0;
|
||||||
mAnimation[i].frames[j].y = 0;
|
mAnimation[i].frame[j].y = 0;
|
||||||
mAnimation[i].frames[j].w = 0;
|
mAnimation[i].frame[j].w = 0;
|
||||||
mAnimation[i].frames[j].h = 0;
|
mAnimation[i].frame[j].h = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
mCurrentFrame = 0;
|
|
||||||
mAnimationCounter = 0;
|
mCurrentAnimation = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calcula el frame correspondiente a la animación
|
// Calcula el frame correspondiente a la animación
|
||||||
void AnimatedSprite::animate(int index)
|
void AnimatedSprite::animate(int index)
|
||||||
{
|
{
|
||||||
// Calculamos el frame actual a partir del contador
|
// Calculamos el frame actual a partir del contador
|
||||||
mCurrentFrame = mAnimationCounter / mAnimation[index].speed;
|
mAnimation[index].currentFrame = mAnimation[index].counter / mAnimation[index].speed;
|
||||||
|
|
||||||
// Si alcanzamos el final de la animación, reiniciamos el contador de la animación
|
// Final de la animación
|
||||||
// en función de la variable loop
|
if (mAnimation[index].currentFrame >= mAnimation[index].numFrames)
|
||||||
if (mCurrentFrame >= mAnimation[index].numFrames)
|
|
||||||
{
|
{
|
||||||
|
// Si se reproduce en bucle
|
||||||
if (mAnimation[index].loop)
|
if (mAnimation[index].loop)
|
||||||
{
|
{
|
||||||
mAnimationCounter = 0;
|
// Reiniciamos el contador de la animación
|
||||||
|
mAnimation[index].counter = 0;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
mCurrentFrame = mAnimation[index].numFrames;
|
// Mantenemos el ultimo frame
|
||||||
|
mAnimation[index].currentFrame = mAnimation[index].numFrames;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// En caso contrario
|
// La animación no ha llegado a su fin
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Escogemos el frame correspondiente de la animación
|
// Establece el frame correspondiente de la animación
|
||||||
setSpriteClip(mAnimation[index].frames[mCurrentFrame]);
|
setSpriteClip(mAnimation[index].frame[mAnimation[index].currentFrame]);
|
||||||
|
|
||||||
// Incrementamos el contador de la animacion
|
// Incrementa el contador de la animacion
|
||||||
++mAnimationCounter;
|
mAnimation[index].counter++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Actualiza todas las variables del objeto: posición, velocidad y animación
|
||||||
|
void AnimatedSprite::update()
|
||||||
|
{
|
||||||
|
MovingSprite::update();
|
||||||
|
animate(mCurrentAnimation);
|
||||||
|
}
|
||||||
|
|
||||||
// Establece el frame actual de la animación
|
// Establece el frame actual de la animación
|
||||||
void AnimatedSprite::setCurrentFrame(Uint8 num)
|
void AnimatedSprite::setCurrentFrame(Uint8 index, Uint8 num)
|
||||||
{
|
{
|
||||||
mCurrentFrame = num;
|
mAnimation[index].currentFrame = num;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Establece el valor del contador
|
// Establece el valor del contador
|
||||||
void AnimatedSprite::setAnimationCounter(Uint16 num)
|
void AnimatedSprite::setAnimationCounter(Uint8 index, Uint16 num)
|
||||||
{
|
{
|
||||||
mAnimationCounter = num;
|
mAnimation[index].counter = num;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Establece el rectangulo para un frame de una animación
|
// Establece el rectangulo para un frame de una animación
|
||||||
void AnimatedSprite::setAnimationFrames(Uint8 index_animation, Uint8 index_frame, int x, int y, int w, int h)
|
void AnimatedSprite::setAnimationFrames(Uint8 index, Uint8 frame, int x, int y, int w, int h)
|
||||||
{
|
{
|
||||||
mAnimation[index_animation].frames[index_frame].x = x;
|
mAnimation[index].frame[frame].y = y;
|
||||||
mAnimation[index_animation].frames[index_frame].y = y;
|
mAnimation[index].frame[frame].w = w;
|
||||||
mAnimation[index_animation].frames[index_frame].w = w;
|
mAnimation[index].frame[frame].h = h;
|
||||||
mAnimation[index_animation].frames[index_frame].h = h;
|
mAnimation[index].frame[frame].x = x;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece el rectangulo para un frame de una animación
|
||||||
|
void AnimatedSprite::setAnimationFrames(Uint8 index, Uint8 frame, SDL_Rect rect)
|
||||||
|
{
|
||||||
|
mAnimation[index].frame[frame] = rect;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Establece la velocidad de una animación
|
// Establece la velocidad de una animación
|
||||||
@@ -96,7 +116,8 @@ void AnimatedSprite::setAnimationSpeed(Uint8 index, Uint8 speed)
|
|||||||
// Establece el numero de frames de una animación
|
// Establece el numero de frames de una animación
|
||||||
void AnimatedSprite::setAnimationNumFrames(Uint8 index, Uint8 num)
|
void AnimatedSprite::setAnimationNumFrames(Uint8 index, Uint8 num)
|
||||||
{
|
{
|
||||||
mAnimation[index].numFrames = num;
|
if (num < MAX_FRAMES)
|
||||||
|
mAnimation[index].numFrames = num;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Establece si la animación se reproduce en bucle
|
// Establece si la animación se reproduce en bucle
|
||||||
@@ -106,7 +127,20 @@ void AnimatedSprite::setAnimationLoop(Uint8 index, bool loop)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Devuelve el rectangulo de una animación y frame concreto
|
// Devuelve el rectangulo de una animación y frame concreto
|
||||||
SDL_Rect AnimatedSprite::getAnimationClip(Uint8 index_animation, Uint8 index_frame)
|
SDL_Rect AnimatedSprite::getAnimationClip(Uint8 index, Uint8 frame)
|
||||||
{
|
{
|
||||||
return mAnimation[index_animation].frames[index_frame];
|
return mAnimation[index].frame[frame];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece la animación actual
|
||||||
|
void AnimatedSprite::setCurrentAnimation(Uint8 index)
|
||||||
|
{
|
||||||
|
if (index < MAX_ANIMATIONS)
|
||||||
|
mCurrentAnimation = index;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Obtiene la animación actual
|
||||||
|
Uint8 AnimatedSprite::getCurrentAnimation()
|
||||||
|
{
|
||||||
|
return mCurrentAnimation;
|
||||||
}
|
}
|
||||||
@@ -5,6 +5,9 @@
|
|||||||
#ifndef ANIMATEDSPRITE_H
|
#ifndef ANIMATEDSPRITE_H
|
||||||
#define ANIMATEDSPRITE_H
|
#define ANIMATEDSPRITE_H
|
||||||
|
|
||||||
|
#define MAX_FRAMES 50
|
||||||
|
#define MAX_ANIMATIONS 20
|
||||||
|
|
||||||
// Clase AnimatedSprite
|
// Clase AnimatedSprite
|
||||||
class AnimatedSprite : public MovingSprite
|
class AnimatedSprite : public MovingSprite
|
||||||
{
|
{
|
||||||
@@ -21,14 +24,20 @@ public:
|
|||||||
// Calcula el frame correspondiente a la animación
|
// Calcula el frame correspondiente a la animación
|
||||||
void animate(int index);
|
void animate(int index);
|
||||||
|
|
||||||
|
// Actualiza todas las variables del objeto: posición, velocidad y animación
|
||||||
|
void update();
|
||||||
|
|
||||||
// Establece el frame actual de la animación
|
// Establece el frame actual de la animación
|
||||||
void setCurrentFrame(Uint8 num);
|
void setCurrentFrame(Uint8 index, Uint8 num);
|
||||||
|
|
||||||
// Establece el valor del contador
|
// Establece el valor del contador
|
||||||
void setAnimationCounter(Uint16 num);
|
void setAnimationCounter(Uint8 index, Uint16 num);
|
||||||
|
|
||||||
// Establece el rectangulo para un frame de una animación
|
// Establece el rectangulo para un frame de una animación
|
||||||
void setAnimationFrames(Uint8 index_animation, Uint8 index_frame, int x, int y, int w, int h);
|
void setAnimationFrames(Uint8 index, Uint8 frame, int x, int y, int w, int h);
|
||||||
|
|
||||||
|
// Establece el rectangulo para un frame de una animación
|
||||||
|
void setAnimationFrames(Uint8 index, Uint8 frame, SDL_Rect rect);
|
||||||
|
|
||||||
// Establece la velocidad de una animación
|
// Establece la velocidad de una animación
|
||||||
void setAnimationSpeed(Uint8 index, Uint8 speed);
|
void setAnimationSpeed(Uint8 index, Uint8 speed);
|
||||||
@@ -40,25 +49,27 @@ public:
|
|||||||
void setAnimationLoop(Uint8 index, bool loop);
|
void setAnimationLoop(Uint8 index, bool loop);
|
||||||
|
|
||||||
// Devuelve el rectangulo de una animación y frame concreto
|
// Devuelve el rectangulo de una animación y frame concreto
|
||||||
SDL_Rect getAnimationClip(Uint8 index_animation, Uint8 index_frame);
|
SDL_Rect getAnimationClip(Uint8 index, Uint8 frame);
|
||||||
|
|
||||||
|
// Establece la animación actual
|
||||||
|
void AnimatedSprite::setCurrentAnimation(Uint8 index);
|
||||||
|
|
||||||
|
// Obtiene la animación actual
|
||||||
|
Uint8 AnimatedSprite::getCurrentAnimation();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
struct sAnimation
|
struct sAnimation
|
||||||
{
|
{
|
||||||
SDL_Rect frames[20]; // Hasta 20 frames
|
SDL_Rect frame[MAX_FRAMES]; // Vector con los rectangulos de cada frame de la animación
|
||||||
Uint8 numFrames;
|
Uint8 numFrames; // Cantidad de frames de la animacion
|
||||||
Uint8 speed;
|
Uint8 speed; // Velocidad de la animación
|
||||||
bool loop;
|
Uint8 currentFrame; // Frame actual
|
||||||
|
Uint16 counter; // Contador
|
||||||
|
bool loop; // Indica si la animación se reproduce en bucle
|
||||||
};
|
};
|
||||||
|
|
||||||
// Vector con las diferentes animaciones y los diferentes frames de cada animación
|
|
||||||
sAnimation mAnimation[20]; // Hasta 20 animaciones
|
|
||||||
|
|
||||||
// Frame actual
|
sAnimation mAnimation[MAX_ANIMATIONS]; // Vector con las animaciones
|
||||||
Uint8 mCurrentFrame;
|
Uint8 mCurrentAnimation; // Animación actual;
|
||||||
|
|
||||||
// Contador para las animaciones
|
|
||||||
Uint16 mAnimationCounter;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user