renombrades les clases SSprite a SurfaceSprite

This commit is contained in:
2025-10-26 14:56:56 +01:00
parent fdea094e26
commit 342177a751
34 changed files with 225 additions and 221 deletions

View File

@@ -31,8 +31,8 @@ Animations loadAnimationsFromFile(const std::string& file_path) {
}
// Constructor
SAnimatedSprite::SAnimatedSprite(std::shared_ptr<Surface> surface, const std::string& file_path)
: SMovingSprite(surface) {
SurfaceAnimatedSprite::SurfaceAnimatedSprite(std::shared_ptr<Surface> surface, const std::string& file_path)
: SurfaceMovingSprite(surface) {
// Carga las animaciones
if (!file_path.empty()) {
Animations v = loadAnimationsFromFile(file_path);
@@ -41,15 +41,15 @@ SAnimatedSprite::SAnimatedSprite(std::shared_ptr<Surface> surface, const std::st
}
// Constructor
SAnimatedSprite::SAnimatedSprite(std::shared_ptr<Surface> surface, const Animations& animations)
: SMovingSprite(surface) {
SurfaceAnimatedSprite::SurfaceAnimatedSprite(std::shared_ptr<Surface> surface, const Animations& animations)
: SurfaceMovingSprite(surface) {
if (!animations.empty()) {
setAnimations(animations);
}
}
// Obtiene el indice de la animación a partir del nombre
int SAnimatedSprite::getIndex(const std::string& name) {
int SurfaceAnimatedSprite::getIndex(const std::string& name) {
auto index = -1;
for (const auto& a : animations_) {
@@ -63,7 +63,7 @@ int SAnimatedSprite::getIndex(const std::string& name) {
}
// Calcula el frame correspondiente a la animación
void SAnimatedSprite::animate() {
void SurfaceAnimatedSprite::animate() {
if (animations_[current_animation_].speed == 0) {
return;
}
@@ -93,12 +93,12 @@ void SAnimatedSprite::animate() {
}
// Comprueba si ha terminado la animación
bool SAnimatedSprite::animationIsCompleted() {
bool SurfaceAnimatedSprite::animationIsCompleted() {
return animations_[current_animation_].completed;
}
// Establece la animacion actual
void SAnimatedSprite::setCurrentAnimation(const std::string& name) {
void SurfaceAnimatedSprite::setCurrentAnimation(const std::string& name) {
const auto new_animation = getIndex(name);
if (current_animation_ != new_animation) {
current_animation_ = new_animation;
@@ -110,7 +110,7 @@ void SAnimatedSprite::setCurrentAnimation(const std::string& name) {
}
// Establece la animacion actual
void SAnimatedSprite::setCurrentAnimation(int index) {
void SurfaceAnimatedSprite::setCurrentAnimation(int index) {
const auto new_animation = index;
if (current_animation_ != new_animation) {
current_animation_ = new_animation;
@@ -122,20 +122,20 @@ void SAnimatedSprite::setCurrentAnimation(int index) {
}
// Actualiza las variables del objeto
void SAnimatedSprite::update() {
void SurfaceAnimatedSprite::update() {
animate();
SMovingSprite::update();
SurfaceMovingSprite::update();
}
// Reinicia la animación
void SAnimatedSprite::resetAnimation() {
void SurfaceAnimatedSprite::resetAnimation() {
animations_[current_animation_].current_frame = 0;
animations_[current_animation_].counter = 0;
animations_[current_animation_].completed = false;
}
// Carga la animación desde un vector de cadenas
void SAnimatedSprite::setAnimations(const Animations& animations) {
void SurfaceAnimatedSprite::setAnimations(const Animations& animations) {
float frame_width = 1.0F;
float frame_height = 1.0F;
int frames_per_row = 1;
@@ -221,7 +221,7 @@ void SAnimatedSprite::setAnimations(const Animations& animations) {
}
// Establece el frame actual de la animación
void SAnimatedSprite::setCurrentAnimationFrame(int num) {
void SurfaceAnimatedSprite::setCurrentAnimationFrame(int num) {
// Descarta valores fuera de rango
if (num < 0 || num >= static_cast<int>(animations_[current_animation_].frames.size())) {
num = 0;

View File

@@ -32,7 +32,7 @@ using Animations = std::vector<std::string>;
// Carga las animaciones en un vector(Animations) desde un fichero
Animations loadAnimationsFromFile(const std::string& file_path);
class SAnimatedSprite : public SMovingSprite {
class SurfaceAnimatedSprite : public SurfaceMovingSprite {
protected:
// Variables
std::vector<AnimationData> animations_; // Vector con las diferentes animaciones
@@ -46,13 +46,13 @@ class SAnimatedSprite : public SMovingSprite {
public:
// Constructor
SAnimatedSprite(std::shared_ptr<Surface> surface, const std::string& file_path);
SAnimatedSprite(std::shared_ptr<Surface> surface, const Animations& animations);
explicit SAnimatedSprite(std::shared_ptr<Surface> surface)
: SMovingSprite(surface) {}
SurfaceAnimatedSprite(std::shared_ptr<Surface> surface, const std::string& file_path);
SurfaceAnimatedSprite(std::shared_ptr<Surface> surface, const Animations& animations);
explicit SurfaceAnimatedSprite(std::shared_ptr<Surface> surface)
: SurfaceMovingSprite(surface) {}
// Destructor
virtual ~SAnimatedSprite() override = default;
virtual ~SurfaceAnimatedSprite() override = default;
// Actualiza las variables del objeto
void update() override;

View File

@@ -3,26 +3,26 @@
#include "core/rendering/surface.hpp" // Para Surface
// Constructor
SMovingSprite::SMovingSprite(std::shared_ptr<Surface> surface, SDL_FRect pos, SDL_FlipMode flip)
: SSprite(surface, pos),
SurfaceMovingSprite::SurfaceMovingSprite(std::shared_ptr<Surface> surface, SDL_FRect pos, SDL_FlipMode flip)
: SurfaceSprite(surface, pos),
x_(pos.x),
y_(pos.y),
flip_(flip) { SSprite::pos_ = pos; }
flip_(flip) { SurfaceSprite::pos_ = pos; }
SMovingSprite::SMovingSprite(std::shared_ptr<Surface> surface, SDL_FRect pos)
: SSprite(surface, pos),
SurfaceMovingSprite::SurfaceMovingSprite(std::shared_ptr<Surface> surface, SDL_FRect pos)
: SurfaceSprite(surface, pos),
x_(pos.x),
y_(pos.y),
flip_(SDL_FLIP_NONE) { SSprite::pos_ = pos; }
flip_(SDL_FLIP_NONE) { SurfaceSprite::pos_ = pos; }
SMovingSprite::SMovingSprite(std::shared_ptr<Surface> surface)
: SSprite(surface),
SurfaceMovingSprite::SurfaceMovingSprite(std::shared_ptr<Surface> surface)
: SurfaceSprite(surface),
x_(0.0f),
y_(0.0f),
flip_(SDL_FLIP_NONE) { SSprite::clear(); }
flip_(SDL_FLIP_NONE) { SurfaceSprite::clear(); }
// Reinicia todas las variables
void SMovingSprite::clear() {
void SurfaceMovingSprite::clear() {
x_ = 0.0f; // Posición en el eje X
y_ = 0.0f; // Posición en el eje Y
@@ -34,11 +34,11 @@ void SMovingSprite::clear() {
flip_ = SDL_FLIP_NONE; // Establece como se ha de voltear el sprite
SSprite::clear();
SurfaceSprite::clear();
}
// Mueve el sprite
void SMovingSprite::move() {
void SurfaceMovingSprite::move() {
x_ += vx_;
y_ += vy_;
@@ -50,22 +50,22 @@ void SMovingSprite::move() {
}
// Actualiza las variables internas del objeto
void SMovingSprite::update() {
void SurfaceMovingSprite::update() {
move();
}
// Muestra el sprite por pantalla
void SMovingSprite::render() {
void SurfaceMovingSprite::render() {
surface_->render(pos_.x, pos_.y, &clip_, flip_);
}
// Muestra el sprite por pantalla
void SMovingSprite::render(Uint8 source_color, Uint8 target_color) {
void SurfaceMovingSprite::render(Uint8 source_color, Uint8 target_color) {
surface_->renderWithColorReplace(pos_.x, pos_.y, source_color, target_color, &clip_, flip_);
}
// Establece la posición y_ el tamaño del objeto
void SMovingSprite::setPos(SDL_FRect rect) {
void SurfaceMovingSprite::setPos(SDL_FRect rect) {
x_ = static_cast<float>(rect.x);
y_ = static_cast<float>(rect.y);
@@ -73,7 +73,7 @@ void SMovingSprite::setPos(SDL_FRect rect) {
}
// Establece el valor de las variables
void SMovingSprite::setPos(float x, float y) {
void SurfaceMovingSprite::setPos(float x, float y) {
x_ = x;
y_ = y;
@@ -82,13 +82,13 @@ void SMovingSprite::setPos(float x, float y) {
}
// Establece el valor de la variable
void SMovingSprite::setPosX(float value) {
void SurfaceMovingSprite::setPosX(float value) {
x_ = value;
pos_.x = static_cast<int>(x_);
}
// Establece el valor de la variable
void SMovingSprite::setPosY(float value) {
void SurfaceMovingSprite::setPosY(float value) {
y_ = value;
pos_.y = static_cast<int>(y_);
}

View File

@@ -8,7 +8,7 @@
class Surface; // lines 8-8
// Clase SMovingSprite. Añade movimiento y flip al sprite
class SMovingSprite : public SSprite {
class SurfaceMovingSprite : public SurfaceSprite {
public:
protected:
float x_; // Posición en el eje X
@@ -27,12 +27,12 @@ class SMovingSprite : public SSprite {
public:
// Constructor
SMovingSprite(std::shared_ptr<Surface> surface, SDL_FRect pos, SDL_FlipMode flip);
SMovingSprite(std::shared_ptr<Surface> surface, SDL_FRect pos);
explicit SMovingSprite(std::shared_ptr<Surface> surface);
SurfaceMovingSprite(std::shared_ptr<Surface> surface, SDL_FRect pos, SDL_FlipMode flip);
SurfaceMovingSprite(std::shared_ptr<Surface> surface, SDL_FRect pos);
explicit SurfaceMovingSprite(std::shared_ptr<Surface> surface);
// Destructor
virtual ~SMovingSprite() override = default;
virtual ~SurfaceMovingSprite() override = default;
// Actualiza las variables internas del objeto
virtual void update();

View File

@@ -3,44 +3,44 @@
#include "core/rendering/surface.hpp" // Para Surface
// Constructor
SSprite::SSprite(std::shared_ptr<Surface> surface, float x, float y, float w, float h)
SurfaceSprite::SurfaceSprite(std::shared_ptr<Surface> surface, float x, float y, float w, float h)
: surface_(surface),
pos_((SDL_FRect){x, y, w, h}),
clip_((SDL_FRect){0.0F, 0.0F, pos_.w, pos_.h}) {}
SSprite::SSprite(std::shared_ptr<Surface> surface, SDL_FRect rect)
SurfaceSprite::SurfaceSprite(std::shared_ptr<Surface> surface, SDL_FRect rect)
: surface_(surface),
pos_(rect),
clip_((SDL_FRect){0, 0, pos_.w, pos_.h}) {}
SSprite::SSprite(std::shared_ptr<Surface> surface)
SurfaceSprite::SurfaceSprite(std::shared_ptr<Surface> surface)
: surface_(surface),
pos_((SDL_FRect){0.0F, 0.0F, surface_->getWidth(), surface_->getHeight()}),
clip_(pos_) {}
// Muestra el sprite por pantalla
void SSprite::render() {
void SurfaceSprite::render() {
surface_->render(pos_.x, pos_.y, &clip_);
}
void SSprite::render(Uint8 source_color, Uint8 target_color) {
void SurfaceSprite::render(Uint8 source_color, Uint8 target_color) {
surface_->renderWithColorReplace(pos_.x, pos_.y, source_color, target_color, &clip_);
}
// Establece la posición del objeto
void SSprite::setPosition(float x, float y) {
void SurfaceSprite::setPosition(float x, float y) {
pos_.x = x;
pos_.y = y;
}
// Establece la posición del objeto
void SSprite::setPosition(SDL_FPoint p) {
void SurfaceSprite::setPosition(SDL_FPoint p) {
pos_.x = p.x;
pos_.y = p.y;
}
// Reinicia las variables a cero
void SSprite::clear() {
void SurfaceSprite::clear() {
pos_ = {0, 0, 0, 0};
clip_ = {0, 0, 0, 0};
}

View File

@@ -5,8 +5,8 @@
#include <memory> // Para shared_ptr
class Surface; // lines 5-5
// Clase SSprite
class SSprite {
// Clase SurfaceSprite
class SurfaceSprite {
protected:
// Variables
std::shared_ptr<Surface> surface_; // Surface donde estan todos los dibujos del sprite
@@ -15,12 +15,12 @@ class SSprite {
public:
// Constructor
SSprite(std::shared_ptr<Surface>, float x, float y, float w, float h);
SSprite(std::shared_ptr<Surface>, SDL_FRect rect);
explicit SSprite(std::shared_ptr<Surface>);
SurfaceSprite(std::shared_ptr<Surface>, float x, float y, float w, float h);
SurfaceSprite(std::shared_ptr<Surface>, SDL_FRect rect);
explicit SurfaceSprite(std::shared_ptr<Surface>);
// Destructor
virtual ~SSprite() = default;
virtual ~SurfaceSprite() = default;
// Muestra el sprite por pantalla
virtual void render();

View File

@@ -88,7 +88,7 @@ Text::Text(std::shared_ptr<Surface> surface, const std::string& text_file) {
}
// Crea los objetos
sprite_ = std::make_unique<SSprite>(surface, (SDL_FRect){0.0F, 0.0F, static_cast<float>(box_width_), static_cast<float>(box_height_)});
sprite_ = std::make_unique<SurfaceSprite>(surface, (SDL_FRect){0.0F, 0.0F, static_cast<float>(box_width_), static_cast<float>(box_height_)});
// Inicializa variables
fixed_width_ = false;
@@ -106,7 +106,7 @@ Text::Text(std::shared_ptr<Surface> surface, std::shared_ptr<TextFile> text_file
}
// Crea los objetos
sprite_ = std::make_unique<SSprite>(surface, (SDL_FRect){0.0F, 0.0F, static_cast<float>(box_width_), static_cast<float>(box_height_)});
sprite_ = std::make_unique<SurfaceSprite>(surface, (SDL_FRect){0.0F, 0.0F, static_cast<float>(box_width_), static_cast<float>(box_height_)});
// Inicializa variables
fixed_width_ = false;

View File

@@ -30,7 +30,7 @@ std::shared_ptr<TextFile> loadTextFile(const std::string& file_path);
class Text {
private:
// Objetos y punteros
std::unique_ptr<SSprite> sprite_ = nullptr; // Objeto con los graficos para el texto
std::unique_ptr<SurfaceSprite> sprite_ = nullptr; // Objeto con los graficos para el texto
// Variables
int box_width_ = 0; // Anchura de la caja de cada caracter en el png