69 lines
1.2 KiB
C++
69 lines
1.2 KiB
C++
#include "sprite.h"
|
|
|
|
// Constructor
|
|
Sprite::Sprite(Texture *texture)
|
|
{
|
|
texture_ = texture;
|
|
pos_ = {0, 0, 0, 0};
|
|
clip_ = {0, 0, 0, 0};
|
|
frame_ = 0;
|
|
num_frames_ = NUM_FRAMES;
|
|
animation_speed_ = 20;
|
|
animation_counter_ = 0;
|
|
}
|
|
|
|
// Establece la posición del sprite
|
|
void Sprite::setPos(SDL_Point pos)
|
|
{
|
|
pos_.x = pos.x;
|
|
pos_.y = pos.y;
|
|
}
|
|
|
|
// Pinta el sprite
|
|
void Sprite::render()
|
|
{
|
|
texture_->render(&clip_, &pos_);
|
|
}
|
|
|
|
// Actualiza la lógica de la clase
|
|
void Sprite::update()
|
|
{
|
|
animation_counter_++;
|
|
if (animation_counter_ % animation_speed_ == 0)
|
|
{
|
|
animate();
|
|
}
|
|
}
|
|
|
|
// Establece el rectangulo de la textura que se va a pintar
|
|
void Sprite::setClip(SDL_FRect clip)
|
|
{
|
|
clip_ = clip;
|
|
}
|
|
|
|
// Establece el tamaño del sprite
|
|
void Sprite::setSize(int w, int h)
|
|
{
|
|
pos_.w = w;
|
|
pos_.h = h;
|
|
}
|
|
|
|
// Anima el sprite
|
|
void Sprite::animate()
|
|
{
|
|
frame_ = (frame_ + 1) % num_frames_;
|
|
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)
|
|
{
|
|
animation_speed_ = value;
|
|
animation_counter_ = 0;
|
|
} |