Trabajando en la clase animatedsprite
This commit is contained in:
@@ -7,8 +7,6 @@ AnimatedSprite::AnimatedSprite(LTexture *texture, SDL_Renderer *renderer)
|
|||||||
// Copia los punteros
|
// Copia los punteros
|
||||||
setTexture(texture);
|
setTexture(texture);
|
||||||
setRenderer(renderer);
|
setRenderer(renderer);
|
||||||
|
|
||||||
init();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Destructor
|
// Destructor
|
||||||
@@ -16,66 +14,61 @@ AnimatedSprite::~AnimatedSprite()
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
// Iniciador
|
// Obtiene el indice de la animación a partir del nombre
|
||||||
void AnimatedSprite::init()
|
int AnimatedSprite::getIndex(std::string name)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < 20; i++)
|
int result = -1;
|
||||||
|
for (int i = 0; i < animation.size(), i++)
|
||||||
{
|
{
|
||||||
mAnimation[i].numFrames = 0;
|
if (animation[i].name == name)
|
||||||
mAnimation[i].speed = 0;
|
|
||||||
mAnimation[i].loop = true;
|
|
||||||
mAnimation[i].completed = false;
|
|
||||||
for (int j = 0; i < 20; i++)
|
|
||||||
{
|
{
|
||||||
mAnimation[i].frames[j].x = 0;
|
result = i;
|
||||||
mAnimation[i].frames[j].y = 0;
|
|
||||||
mAnimation[i].frames[j].w = 0;
|
|
||||||
mAnimation[i].frames[j].h = 0;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
mCurrentFrame = 0;
|
return result;
|
||||||
mAnimationCounter = 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(std::string name)
|
||||||
{
|
{
|
||||||
if (mEnabled)
|
if (mEnabled)
|
||||||
{
|
{
|
||||||
// Calculamos el frame actual a partir del contador
|
const int index = getIndex(name);
|
||||||
mCurrentFrame = mAnimationCounter / mAnimation[index].speed;
|
|
||||||
|
|
||||||
// Si alcanzamos el final de la animación, reiniciamos el contador de la animación
|
// Calcula el frame actual a partir del contador
|
||||||
|
animation[index].currentFrame = animation[index].counter/animation[index].speed;
|
||||||
|
|
||||||
|
// Si alcanza el final de la animación, reinicia el contador de la animación
|
||||||
// en función de la variable loop
|
// en función de la variable loop
|
||||||
if (mCurrentFrame >= mAnimation[index].numFrames)
|
if (animation[index].currentFrame >= animation[index].frames.size())
|
||||||
{
|
{
|
||||||
if (mAnimation[index].loop)
|
if (animation[index].loop)
|
||||||
mAnimationCounter = 0;
|
animation[index].counter = 0;
|
||||||
else
|
else
|
||||||
mCurrentFrame = mAnimation[index].numFrames;
|
animation[index].currentFrame = animation[index].frames.size();
|
||||||
}
|
}
|
||||||
// En caso contrario
|
// En caso contrario
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Escogemos el frame correspondiente de la animación
|
// Escogemos el frame correspondiente de la animación
|
||||||
setSpriteClip(mAnimation[index].frames[mCurrentFrame]);
|
setSpriteClip(animation[index].frames[animation[index].currentFrame]);
|
||||||
|
|
||||||
// Incrementamos el contador de la animacion
|
// Incrementamos el contador de la animacion
|
||||||
mAnimationCounter++;
|
animation[index].counter++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Establece el frame actual de la animación
|
// Establece el frame actual de la animación
|
||||||
void AnimatedSprite::setCurrentFrame(Uint8 num)
|
void AnimatedSprite::setCurrentFrame(std::string name, int num)
|
||||||
{
|
{
|
||||||
mCurrentFrame = num;
|
animation[getIndex(name)].currentFrame = num;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Establece el valor del contador
|
// Establece el valor del contador
|
||||||
void AnimatedSprite::setAnimationCounter(Uint16 num)
|
void AnimatedSprite::setAnimationCounter(std::string name, int num)
|
||||||
{
|
{
|
||||||
mAnimationCounter = num;
|
animation[getIndex(name)].counter = num;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Establece el rectangulo para un frame de una animación
|
// Establece el rectangulo para un frame de una animación
|
||||||
@@ -88,8 +81,9 @@ void AnimatedSprite::setAnimationFrames(Uint8 index_animation, Uint8 index_frame
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Establece la velocidad de una animación
|
// Establece la velocidad de una animación
|
||||||
void AnimatedSprite::setAnimationSpeed(Uint8 index, Uint8 speed)
|
void AnimatedSprite::setAnimationSpeed(std::string name, int speed)
|
||||||
{
|
{
|
||||||
|
animation[getIndex(name)].counter = num;
|
||||||
mAnimation[index].speed = speed;
|
mAnimation[index].speed = speed;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <SDL2/SDL.h>
|
#include <SDL2/SDL.h>
|
||||||
#include "movingsprite.h"
|
#include "movingsprite.h"
|
||||||
|
#include <vector>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
#ifndef ANIMATEDSPRITE_H
|
#ifndef ANIMATEDSPRITE_H
|
||||||
#define ANIMATEDSPRITE_H
|
#define ANIMATEDSPRITE_H
|
||||||
@@ -9,18 +11,17 @@
|
|||||||
class AnimatedSprite : public MovingSprite
|
class AnimatedSprite : public MovingSprite
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
struct sAnimation
|
struct t_animation
|
||||||
{
|
{
|
||||||
SDL_Rect frames[MAX_FRAMES]; // Cada uno de los frames que componen la animación
|
std::string name; // Nombre de la animacion
|
||||||
Uint8 numFrames; // Numero de frames que componen la animación
|
std::vector<SDL_Rect> frames; // Cada uno de los frames que componen la animación
|
||||||
Uint8 speed; // Velocidad de la animación
|
int speed; // Velocidad de la animación
|
||||||
bool loop; // Indica si la animación se reproduce en bucle
|
bool loop; // Indica si la animación se reproduce en bucle
|
||||||
bool completed; // Indica si ha finalizado la animación
|
bool completed; // Indica si ha finalizado la animación
|
||||||
|
int currentFrame; // Frame actual
|
||||||
|
int counter; // Contador para las animaciones
|
||||||
};
|
};
|
||||||
sAnimation mAnimation[MAX_ANIMATIONS]; // Vector con las diferentes animaciones
|
std::vector<t_animation> animation; // Vector con las diferentes animaciones
|
||||||
|
|
||||||
Uint8 mCurrentFrame; // Frame actual
|
|
||||||
Uint16 mAnimationCounter; // Contador para las animaciones
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
// Constructor
|
// Constructor
|
||||||
@@ -29,23 +30,20 @@ public:
|
|||||||
// Destructor
|
// Destructor
|
||||||
~AnimatedSprite();
|
~AnimatedSprite();
|
||||||
|
|
||||||
// Iniciador
|
|
||||||
void init();
|
|
||||||
|
|
||||||
// Calcula el frame correspondiente a la animación
|
// Calcula el frame correspondiente a la animación
|
||||||
void animate(int index);
|
void animate(std::string name);
|
||||||
|
|
||||||
// Establece el frame actual de la animación
|
// Establece el frame actual de la animación
|
||||||
void setCurrentFrame(Uint8 num);
|
void setCurrentFrame(std::string name, int num);
|
||||||
|
|
||||||
// Establece el valor del contador
|
// Establece el valor del contador
|
||||||
void setAnimationCounter(Uint16 num);
|
void setAnimationCounter(std::string name, int 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_animation, Uint8 index_frame, int x, int y, int w, int h);
|
||||||
|
|
||||||
// Establece la velocidad de una animación
|
// Establece la velocidad de una animación
|
||||||
void setAnimationSpeed(Uint8 index, Uint8 speed);
|
void setAnimationSpeed(std::string name, int speed);
|
||||||
|
|
||||||
// Establece el numero de frames de una animación
|
// Establece el numero de frames de una animación
|
||||||
void setAnimationNumFrames(Uint8 index, Uint8 num);
|
void setAnimationNumFrames(Uint8 index, Uint8 num);
|
||||||
@@ -61,6 +59,9 @@ public:
|
|||||||
|
|
||||||
// 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_animation, Uint8 index_frame);
|
||||||
|
|
||||||
|
// Obtiene el indice de la animación a partir del nombre
|
||||||
|
int getIndex(std::string name);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
Reference in New Issue
Block a user