74 lines
1.2 KiB
C++
74 lines
1.2 KiB
C++
#include "sprite.h"
|
|
|
|
// Constructor
|
|
Sprite::Sprite(Texture *texture)
|
|
{
|
|
this->texture = texture;
|
|
pos = {0, 0, 0, 0};
|
|
clip = {0, 0, 0, 0};
|
|
frame = 0;
|
|
numFrames = NUM_FRAMES;
|
|
animationSpeed = 20;
|
|
animationCounter = 0;
|
|
}
|
|
|
|
// Destructor
|
|
Sprite::~Sprite()
|
|
{
|
|
}
|
|
|
|
// Establece la posición del sprite
|
|
void Sprite::setPos(SDL_Point pos)
|
|
{
|
|
this->pos.x = pos.x;
|
|
this->pos.y = pos.y;
|
|
}
|
|
|
|
// Pinta el sprite
|
|
void Sprite::render()
|
|
{
|
|
texture->render(&clip, &pos);
|
|
}
|
|
|
|
// Actualiza la lógica de la clase
|
|
void Sprite::update()
|
|
{
|
|
animationCounter++;
|
|
if (animationCounter % animationSpeed == 0)
|
|
{
|
|
animate();
|
|
}
|
|
}
|
|
|
|
// Establece el rectangulo de la textura que se va a pintar
|
|
void Sprite::setClip(SDL_Rect clip)
|
|
{
|
|
this->clip = clip;
|
|
}
|
|
|
|
// Establece el tamaño del sprite
|
|
void Sprite::setSize(int w, int h)
|
|
{
|
|
this->pos.w = w;
|
|
this->pos.h = h;
|
|
}
|
|
|
|
// Anima el sprite
|
|
void Sprite::animate()
|
|
{
|
|
frame = (frame + 1) % numFrames;
|
|
clip.x = frame * pos.w;
|
|
}
|
|
|
|
// Modulación de color
|
|
void Sprite::setColor(int r, int g, int b)
|
|
{
|
|
texture->setColor(r, g, b);
|
|
}
|
|
|
|
// Cambia la velocidad de la animación
|
|
void Sprite::setAnimationSpeed(int value)
|
|
{
|
|
animationSpeed = value;
|
|
animationCounter = 0;
|
|
} |