updated animatedsprite.cpp
This commit is contained in:
112
source/animatedsprite.cpp
Normal file
112
source/animatedsprite.cpp
Normal file
@@ -0,0 +1,112 @@
|
|||||||
|
#include "const.h"
|
||||||
|
#include "animatedsprite.h"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
// Constructor
|
||||||
|
AnimatedSprite::AnimatedSprite()
|
||||||
|
{
|
||||||
|
init(nullptr, nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Destructor
|
||||||
|
AnimatedSprite::~AnimatedSprite()
|
||||||
|
{
|
||||||
|
init(nullptr, nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Iniciador
|
||||||
|
void AnimatedSprite::init(LTexture *texture, SDL_Renderer *renderer)
|
||||||
|
{
|
||||||
|
mRenderer = renderer;
|
||||||
|
mTexture = texture;
|
||||||
|
for (Uint8 i = 0; i < 20; i++)
|
||||||
|
{
|
||||||
|
mAnimation[i].numFrames = 0;
|
||||||
|
mAnimation[i].speed = 0;
|
||||||
|
mAnimation[i].loop = true;
|
||||||
|
for (Uint8 j = 0; i < 20; i++)
|
||||||
|
{
|
||||||
|
mAnimation[i].frames[j].x = 0;
|
||||||
|
mAnimation[i].frames[j].y = 0;
|
||||||
|
mAnimation[i].frames[j].w = 0;
|
||||||
|
mAnimation[i].frames[j].h = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
mCurrentFrame = 0;
|
||||||
|
mAnimationCounter = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calcula el frame correspondiente a la animación
|
||||||
|
void AnimatedSprite::animate(int index)
|
||||||
|
{
|
||||||
|
// Calculamos el frame actual a partir del contador
|
||||||
|
mCurrentFrame = mAnimationCounter / mAnimation[index].speed;
|
||||||
|
|
||||||
|
// Si alcanzamos el final de la animación, reiniciamos el contador de la animación
|
||||||
|
// en función de la variable loop
|
||||||
|
if (mCurrentFrame >= mAnimation[index].numFrames)
|
||||||
|
{
|
||||||
|
if (mAnimation[index].loop)
|
||||||
|
{
|
||||||
|
mAnimationCounter = 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
mCurrentFrame = mAnimation[index].numFrames;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// En caso contrario
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Escogemos el frame correspondiente de la animación
|
||||||
|
setSpriteClip(mAnimation[index].frames[mCurrentFrame]);
|
||||||
|
|
||||||
|
// Incrementamos el contador de la animacion
|
||||||
|
++mAnimationCounter;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece el frame actual de la animación
|
||||||
|
void AnimatedSprite::setCurrentFrame(Uint8 num)
|
||||||
|
{
|
||||||
|
mCurrentFrame = num;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece el valor del contador
|
||||||
|
void AnimatedSprite::setAnimationCounter(Uint16 num)
|
||||||
|
{
|
||||||
|
mAnimationCounter = num;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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)
|
||||||
|
{
|
||||||
|
mAnimation[index_animation].frames[index_frame].x = x;
|
||||||
|
mAnimation[index_animation].frames[index_frame].y = y;
|
||||||
|
mAnimation[index_animation].frames[index_frame].w = w;
|
||||||
|
mAnimation[index_animation].frames[index_frame].h = h;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece la velocidad de una animación
|
||||||
|
void AnimatedSprite::setAnimationSpeed(Uint8 index, Uint8 speed)
|
||||||
|
{
|
||||||
|
mAnimation[index].speed = speed;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece el numero de frames de una animación
|
||||||
|
void AnimatedSprite::setAnimationNumFrames(Uint8 index, Uint8 num)
|
||||||
|
{
|
||||||
|
mAnimation[index].numFrames = num;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establece si la animación se reproduce en bucle
|
||||||
|
void AnimatedSprite::setAnimationLoop(Uint8 index, bool loop)
|
||||||
|
{
|
||||||
|
mAnimation[index].loop = loop;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Devuelve el rectangulo de una animación y frame concreto
|
||||||
|
SDL_Rect AnimatedSprite::getAnimationClip(Uint8 index_animation, Uint8 index_frame)
|
||||||
|
{
|
||||||
|
return mAnimation[index_animation].frames[index_frame];
|
||||||
|
}
|
||||||
64
source/animatedsprite.h
Normal file
64
source/animatedsprite.h
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
#pragma once
|
||||||
|
#include "ifdefs.h"
|
||||||
|
#include "movingsprite.h"
|
||||||
|
|
||||||
|
#ifndef ANIMATEDSPRITE_H
|
||||||
|
#define ANIMATEDSPRITE_H
|
||||||
|
|
||||||
|
// Clase AnimatedSprite
|
||||||
|
class AnimatedSprite : public MovingSprite
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
// Constructor
|
||||||
|
AnimatedSprite();
|
||||||
|
|
||||||
|
// Destructor
|
||||||
|
~AnimatedSprite();
|
||||||
|
|
||||||
|
// Iniciador
|
||||||
|
void init(LTexture *texture, SDL_Renderer *renderer);
|
||||||
|
|
||||||
|
// Calcula el frame correspondiente a la animación
|
||||||
|
void animate(int index);
|
||||||
|
|
||||||
|
// Establece el frame actual de la animación
|
||||||
|
void setCurrentFrame(Uint8 num);
|
||||||
|
|
||||||
|
// Establece el valor del contador
|
||||||
|
void setAnimationCounter(Uint16 num);
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
|
||||||
|
// Establece la velocidad de una animación
|
||||||
|
void setAnimationSpeed(Uint8 index, Uint8 speed);
|
||||||
|
|
||||||
|
// Establece el numero de frames de una animación
|
||||||
|
void setAnimationNumFrames(Uint8 index, Uint8 num);
|
||||||
|
|
||||||
|
// Establece si la animación se reproduce en bucle
|
||||||
|
void setAnimationLoop(Uint8 index, bool loop);
|
||||||
|
|
||||||
|
// Devuelve el rectangulo de una animación y frame concreto
|
||||||
|
SDL_Rect getAnimationClip(Uint8 index_animation, Uint8 index_frame);
|
||||||
|
|
||||||
|
private:
|
||||||
|
struct sAnimation
|
||||||
|
{
|
||||||
|
SDL_Rect frames[20]; // Hasta 20 frames
|
||||||
|
Uint8 numFrames;
|
||||||
|
Uint8 speed;
|
||||||
|
bool loop;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Vector con las diferentes animaciones y los diferentes frames de cada animación
|
||||||
|
sAnimation mAnimation[20]; // Hasta 20 animaciones
|
||||||
|
|
||||||
|
// Frame actual
|
||||||
|
Uint8 mCurrentFrame;
|
||||||
|
|
||||||
|
// Contador para las animaciones
|
||||||
|
Uint16 mAnimationCounter;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
Reference in New Issue
Block a user