Files
coffee_crisis_arcade_edition/source/sprite.cpp

54 lines
1.6 KiB
C++

#include "sprite.hpp"
#include <utility>
#include <vector> // Para vector
#include "texture.hpp" // Para Texture
// Constructor
Sprite::Sprite(std::shared_ptr<Texture> texture, float pos_x, float pos_y, float width, float height)
: textures_{std::move(texture)},
pos_((SDL_FRect){.x = pos_x, .y = pos_y, .w = width, .h = height}),
sprite_clip_((SDL_FRect){.x = 0, .y = 0, .w = pos_.w, .h = pos_.h}) {}
Sprite::Sprite(std::shared_ptr<Texture> texture, SDL_FRect rect)
: textures_{std::move(texture)},
pos_(rect),
sprite_clip_((SDL_FRect){.x = 0, .y = 0, .w = pos_.w, .h = pos_.h}) {}
Sprite::Sprite(std::shared_ptr<Texture> texture)
: textures_{std::move(texture)},
pos_(SDL_FRect{.x = 0, .y = 0, .w = static_cast<float>(textures_.at(texture_index_)->getWidth()), .h = static_cast<float>(textures_.at(texture_index_)->getHeight())}),
sprite_clip_(pos_) {}
// Muestra el sprite por pantalla
void Sprite::render() {
textures_.at(texture_index_)->render(pos_.x, pos_.y, &sprite_clip_, zoom_, zoom_);
}
// Establece la posición del objeto
void Sprite::setPosition(float pos_x, float pos_y) {
pos_.x = pos_x;
pos_.y = pos_y;
}
// Establece la posición del objeto
void Sprite::setPosition(SDL_FPoint point) {
pos_.x = point.x;
pos_.y = point.y;
}
// Reinicia las variables a cero
void Sprite::clear() {
pos_ = {.x = 0, .y = 0, .w = 0, .h = 0};
sprite_clip_ = {.x = 0, .y = 0, .w = 0, .h = 0};
}
// Cambia la textura activa por índice
auto Sprite::setActiveTexture(size_t index) -> bool {
if (index < textures_.size()) {
texture_index_ = index;
}
return index < textures_.size();
}