treball en curs: correccions de tidy
This commit is contained in:
@@ -36,7 +36,7 @@ static auto parseAnimationStream(std::istream &file, Texture *texture, const std
|
||||
buffer.speed = 0;
|
||||
buffer.loop = -1;
|
||||
buffer.counter = 0;
|
||||
buffer.currentFrame = 0;
|
||||
buffer.current_frame = 0;
|
||||
buffer.completed = false;
|
||||
|
||||
do {
|
||||
@@ -98,9 +98,9 @@ static auto parseAnimationStream(std::istream &file, Texture *texture, const std
|
||||
}
|
||||
|
||||
// Carga la animación desde un fichero (vía ResourceHelper: pack si està inicialitzat, filesystem si no)
|
||||
auto loadAnimationFromFile(Texture *texture, const std::string &filePath, bool verbose) -> AnimatedSpriteData {
|
||||
const std::string filename = filePath.substr(filePath.find_last_of("\\/") + 1);
|
||||
auto bytes = ResourceHelper::loadFile(filePath);
|
||||
auto loadAnimationFromFile(Texture *texture, const std::string &file_path, bool verbose) -> AnimatedSpriteData {
|
||||
const std::string filename = file_path.substr(file_path.find_last_of("\\/") + 1);
|
||||
auto bytes = ResourceHelper::loadFile(file_path);
|
||||
if (bytes.empty()) {
|
||||
if (verbose) {
|
||||
std::cout << "Warning: Unable to open " << filename.c_str() << " file" << '\n';
|
||||
@@ -113,7 +113,7 @@ auto loadAnimationFromFile(Texture *texture, const std::string &filePath, bool v
|
||||
}
|
||||
|
||||
// Carga la animación desde bytes en memoria
|
||||
auto loadAnimationFromMemory(Texture *texture, const std::vector<uint8_t> &bytes, const std::string &nameForLogs, bool verbose) -> AnimatedSpriteData {
|
||||
auto loadAnimationFromMemory(Texture *texture, const std::vector<uint8_t> &bytes, const std::string &name_for_logs, bool verbose) -> AnimatedSpriteData {
|
||||
if (bytes.empty()) {
|
||||
AnimatedSpriteData as;
|
||||
as.texture = texture;
|
||||
@@ -121,12 +121,12 @@ auto loadAnimationFromMemory(Texture *texture, const std::vector<uint8_t> &bytes
|
||||
}
|
||||
std::string content(reinterpret_cast<const char *>(bytes.data()), bytes.size());
|
||||
std::stringstream ss(content);
|
||||
return parseAnimationStream(ss, texture, nameForLogs, verbose);
|
||||
return parseAnimationStream(ss, texture, name_for_logs, verbose);
|
||||
}
|
||||
|
||||
// Constructor
|
||||
AnimatedSprite::AnimatedSprite(Texture *texture, SDL_Renderer *renderer, const std::string &file, std::vector<std::string> *buffer)
|
||||
: currentAnimation(0) {
|
||||
: current_animation_(0) {
|
||||
// Copia los punteros
|
||||
setTexture(texture);
|
||||
setRenderer(renderer);
|
||||
@@ -136,7 +136,7 @@ AnimatedSprite::AnimatedSprite(Texture *texture, SDL_Renderer *renderer, const s
|
||||
AnimatedSpriteData as = loadAnimationFromFile(texture, file);
|
||||
|
||||
// Copia los datos de las animaciones
|
||||
animation.insert(animation.end(), as.animations.begin(), as.animations.end());
|
||||
animation_.insert(animation_.end(), as.animations.begin(), as.animations.end());
|
||||
}
|
||||
|
||||
else if (buffer != nullptr) {
|
||||
@@ -145,29 +145,29 @@ AnimatedSprite::AnimatedSprite(Texture *texture, SDL_Renderer *renderer, const s
|
||||
}
|
||||
|
||||
// Constructor
|
||||
AnimatedSprite::AnimatedSprite(SDL_Renderer *renderer, AnimatedSpriteData *animation)
|
||||
: currentAnimation(0) {
|
||||
AnimatedSprite::AnimatedSprite(SDL_Renderer *renderer, AnimatedSpriteData *data)
|
||||
: current_animation_(0) {
|
||||
// Copia los punteros
|
||||
setTexture(animation->texture);
|
||||
setTexture(data->texture);
|
||||
setRenderer(renderer);
|
||||
|
||||
// Copia los datos de las animaciones
|
||||
this->animation.insert(this->animation.end(), animation->animations.begin(), animation->animations.end());
|
||||
this->animation_.insert(this->animation_.end(), data->animations.begin(), data->animations.end());
|
||||
}
|
||||
|
||||
// Destructor
|
||||
AnimatedSprite::~AnimatedSprite() {
|
||||
for (auto &a : animation) {
|
||||
for (auto &a : animation_) {
|
||||
a.frames.clear();
|
||||
}
|
||||
animation.clear();
|
||||
animation_.clear();
|
||||
}
|
||||
|
||||
// Obtiene el indice de la animación a partir del nombre
|
||||
auto AnimatedSprite::getIndex(const std::string &name) -> int {
|
||||
int index = -1;
|
||||
|
||||
for (const auto &a : animation) {
|
||||
for (const auto &a : animation_) {
|
||||
index++;
|
||||
if (a.name == name) {
|
||||
return index;
|
||||
@@ -181,102 +181,102 @@ auto AnimatedSprite::getIndex(const std::string &name) -> int {
|
||||
|
||||
// Calcula el frame correspondiente a la animación
|
||||
void AnimatedSprite::animate() {
|
||||
if (!enabled || animation[currentAnimation].speed == 0) {
|
||||
if (!enabled_ || animation_[current_animation_].speed == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Calcula el frame actual a partir del contador
|
||||
animation[currentAnimation].currentFrame = animation[currentAnimation].counter / animation[currentAnimation].speed;
|
||||
animation_[current_animation_].current_frame = animation_[current_animation_].counter / animation_[current_animation_].speed;
|
||||
|
||||
// Si alcanza el final de la animación, reinicia el contador de la animación
|
||||
// en función de la variable loop y coloca el nuevo frame
|
||||
if (animation[currentAnimation].currentFrame >= (int)animation[currentAnimation].frames.size()) {
|
||||
if (animation[currentAnimation].loop == -1) { // Si no hay loop, deja el último frame
|
||||
animation[currentAnimation].currentFrame = animation[currentAnimation].frames.size();
|
||||
animation[currentAnimation].completed = true;
|
||||
if (animation_[current_animation_].current_frame >= (int)animation_[current_animation_].frames.size()) {
|
||||
if (animation_[current_animation_].loop == -1) { // Si no hay loop, deja el último frame
|
||||
animation_[current_animation_].current_frame = animation_[current_animation_].frames.size();
|
||||
animation_[current_animation_].completed = true;
|
||||
} else { // Si hay loop, vuelve al frame indicado
|
||||
animation[currentAnimation].counter = 0;
|
||||
animation[currentAnimation].currentFrame = animation[currentAnimation].loop;
|
||||
animation_[current_animation_].counter = 0;
|
||||
animation_[current_animation_].current_frame = animation_[current_animation_].loop;
|
||||
}
|
||||
}
|
||||
// En caso contrario
|
||||
else {
|
||||
// Escoge el frame correspondiente de la animación
|
||||
setSpriteClip(animation[currentAnimation].frames[animation[currentAnimation].currentFrame]);
|
||||
setSpriteClip(animation_[current_animation_].frames[animation_[current_animation_].current_frame]);
|
||||
|
||||
// Incrementa el contador de la animacion
|
||||
animation[currentAnimation].counter++;
|
||||
animation_[current_animation_].counter++;
|
||||
}
|
||||
}
|
||||
|
||||
// Obtiene el numero de frames de la animación actual
|
||||
auto AnimatedSprite::getNumFrames() -> int {
|
||||
return (int)animation[currentAnimation].frames.size();
|
||||
return (int)animation_[current_animation_].frames.size();
|
||||
}
|
||||
|
||||
// Establece el frame actual de la animación
|
||||
void AnimatedSprite::setCurrentFrame(int num) {
|
||||
// Descarta valores fuera de rango
|
||||
if (num >= (int)animation[currentAnimation].frames.size()) {
|
||||
if (num >= (int)animation_[current_animation_].frames.size()) {
|
||||
num = 0;
|
||||
}
|
||||
|
||||
// Cambia el valor de la variable
|
||||
animation[currentAnimation].currentFrame = num;
|
||||
animation[currentAnimation].counter = 0;
|
||||
animation_[current_animation_].current_frame = num;
|
||||
animation_[current_animation_].counter = 0;
|
||||
|
||||
// Escoge el frame correspondiente de la animación
|
||||
setSpriteClip(animation[currentAnimation].frames[animation[currentAnimation].currentFrame]);
|
||||
setSpriteClip(animation_[current_animation_].frames[animation_[current_animation_].current_frame]);
|
||||
}
|
||||
|
||||
// Establece el valor del contador
|
||||
void AnimatedSprite::setAnimationCounter(const std::string &name, int num) {
|
||||
animation[getIndex(name)].counter = num;
|
||||
animation_[getIndex(name)].counter = num;
|
||||
}
|
||||
|
||||
// Establece la velocidad de una animación
|
||||
void AnimatedSprite::setAnimationSpeed(const std::string &name, int speed) {
|
||||
animation[getIndex(name)].counter = speed;
|
||||
animation_[getIndex(name)].counter = speed;
|
||||
}
|
||||
|
||||
// Establece la velocidad de una animación
|
||||
void AnimatedSprite::setAnimationSpeed(int index, int speed) {
|
||||
animation[index].counter = speed;
|
||||
animation_[index].counter = speed;
|
||||
}
|
||||
|
||||
// Establece si la animación se reproduce en bucle
|
||||
void AnimatedSprite::setAnimationLoop(const std::string &name, int loop) {
|
||||
animation[getIndex(name)].loop = loop;
|
||||
animation_[getIndex(name)].loop = loop;
|
||||
}
|
||||
|
||||
// Establece si la animación se reproduce en bucle
|
||||
void AnimatedSprite::setAnimationLoop(int index, int loop) {
|
||||
animation[index].loop = loop;
|
||||
animation_[index].loop = loop;
|
||||
}
|
||||
|
||||
// Establece el valor de la variable
|
||||
void AnimatedSprite::setAnimationCompleted(const std::string &name, bool value) {
|
||||
animation[getIndex(name)].completed = value;
|
||||
animation_[getIndex(name)].completed = value;
|
||||
}
|
||||
|
||||
// OLD - Establece el valor de la variable
|
||||
void AnimatedSprite::setAnimationCompleted(int index, bool value) {
|
||||
animation[index].completed = value;
|
||||
animation_[index].completed = value;
|
||||
}
|
||||
|
||||
// Comprueba si ha terminado la animación
|
||||
auto AnimatedSprite::animationIsCompleted() -> bool {
|
||||
return animation[currentAnimation].completed;
|
||||
return animation_[current_animation_].completed;
|
||||
}
|
||||
|
||||
// Devuelve el rectangulo de una animación y frame concreto
|
||||
auto AnimatedSprite::getAnimationClip(const std::string &name, Uint8 index) -> SDL_Rect {
|
||||
return animation[getIndex(name)].frames[index];
|
||||
return animation_[getIndex(name)].frames[index];
|
||||
}
|
||||
|
||||
// Devuelve el rectangulo de una animación y frame concreto
|
||||
auto AnimatedSprite::getAnimationClip(int indexA, Uint8 indexF) -> SDL_Rect {
|
||||
return animation[indexA].frames[indexF];
|
||||
auto AnimatedSprite::getAnimationClip(int index_a, Uint8 index_f) -> SDL_Rect {
|
||||
return animation_[index_a].frames[index_f];
|
||||
}
|
||||
|
||||
// Carga la animación desde un vector
|
||||
@@ -297,13 +297,13 @@ auto AnimatedSprite::loadFromVector(std::vector<std::string> *source) -> bool {
|
||||
// Lee desde el vector
|
||||
line = source->at(index);
|
||||
|
||||
// Si la linea contiene el texto [animation] se realiza el proceso de carga de una animación
|
||||
// Si la linea contiene el texto [animation_] se realiza el proceso de carga de una animación
|
||||
if (line == "[animation]") {
|
||||
Animation buffer;
|
||||
buffer.speed = 0;
|
||||
buffer.loop = -1;
|
||||
buffer.counter = 0;
|
||||
buffer.currentFrame = 0;
|
||||
buffer.current_frame = 0;
|
||||
buffer.completed = false;
|
||||
|
||||
do {
|
||||
@@ -350,7 +350,7 @@ auto AnimatedSprite::loadFromVector(std::vector<std::string> *source) -> bool {
|
||||
} while (line != "[/animation]");
|
||||
|
||||
// Añade la animación al vector de animaciones
|
||||
animation.push_back(buffer);
|
||||
animation_.push_back(buffer);
|
||||
}
|
||||
|
||||
// En caso contrario se parsea el fichero para buscar las variables y los valores
|
||||
@@ -379,12 +379,12 @@ auto AnimatedSprite::loadFromVector(std::vector<std::string> *source) -> bool {
|
||||
|
||||
// Normaliza valores
|
||||
if (framesPerRow == 0 && frameWidth > 0) {
|
||||
framesPerRow = texture->getWidth() / frameWidth;
|
||||
framesPerRow = texture_->getWidth() / frameWidth;
|
||||
}
|
||||
|
||||
if (maxTiles == 0 && frameWidth > 0 && frameHeight > 0) {
|
||||
const int w = texture->getWidth() / frameWidth;
|
||||
const int h = texture->getHeight() / frameHeight;
|
||||
const int w = texture_->getWidth() / frameWidth;
|
||||
const int h = texture_->getHeight() / frameHeight;
|
||||
maxTiles = w * h;
|
||||
}
|
||||
}
|
||||
@@ -403,22 +403,22 @@ auto AnimatedSprite::loadFromVector(std::vector<std::string> *source) -> bool {
|
||||
// Establece la animacion actual
|
||||
void AnimatedSprite::setCurrentAnimation(const std::string &name) {
|
||||
const int newAnimation = getIndex(name);
|
||||
if (currentAnimation != newAnimation) {
|
||||
currentAnimation = newAnimation;
|
||||
animation[currentAnimation].currentFrame = 0;
|
||||
animation[currentAnimation].counter = 0;
|
||||
animation[currentAnimation].completed = false;
|
||||
if (current_animation_ != newAnimation) {
|
||||
current_animation_ = newAnimation;
|
||||
animation_[current_animation_].current_frame = 0;
|
||||
animation_[current_animation_].counter = 0;
|
||||
animation_[current_animation_].completed = false;
|
||||
}
|
||||
}
|
||||
|
||||
// Establece la animacion actual
|
||||
void AnimatedSprite::setCurrentAnimation(int index) {
|
||||
const int newAnimation = index;
|
||||
if (currentAnimation != newAnimation) {
|
||||
currentAnimation = newAnimation;
|
||||
animation[currentAnimation].currentFrame = 0;
|
||||
animation[currentAnimation].counter = 0;
|
||||
animation[currentAnimation].completed = false;
|
||||
if (current_animation_ != newAnimation) {
|
||||
current_animation_ = newAnimation;
|
||||
animation_[current_animation_].current_frame = 0;
|
||||
animation_[current_animation_].counter = 0;
|
||||
animation_[current_animation_].completed = false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -430,19 +430,19 @@ void AnimatedSprite::update() {
|
||||
|
||||
// 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) {
|
||||
animation[index_animation].frames.push_back({x, y, w, h});
|
||||
animation_[index_animation].frames.push_back({x, y, w, h});
|
||||
}
|
||||
|
||||
// OLD - Establece el contador para todas las animaciones
|
||||
void AnimatedSprite::setAnimationCounter(int value) {
|
||||
for (auto &a : animation) {
|
||||
for (auto &a : animation_) {
|
||||
a.counter = value;
|
||||
}
|
||||
}
|
||||
|
||||
// Reinicia la animación
|
||||
void AnimatedSprite::resetAnimation() {
|
||||
animation[currentAnimation].currentFrame = 0;
|
||||
animation[currentAnimation].counter = 0;
|
||||
animation[currentAnimation].completed = false;
|
||||
animation_[current_animation_].current_frame = 0;
|
||||
animation_[current_animation_].counter = 0;
|
||||
animation_[current_animation_].completed = false;
|
||||
}
|
||||
Reference in New Issue
Block a user