Commit pa que Mon arregle el codi mentre em dutxe

This commit is contained in:
2024-10-09 21:48:01 +02:00
parent 3c1dcad3ab
commit f2fa216b0d
34 changed files with 862 additions and 1218 deletions

View File

@@ -1,19 +1,19 @@
#include "animated_sprite.h"
#include <fstream> // for basic_ostream, operator<<, basic_istream, basic...
#include <iostream> // for cout
#include <sstream> // for basic_stringstream
#include "texture.h" // for Texture
#include <fstream> // for basic_ostream, operator<<, basic_istream, basic...
#include <iostream> // for cout
#include <sstream> // for basic_stringstream
#include "texture.h" // for Texture
// Carga la animación desde un fichero
animatedSprite_t loadAnimationFromFile(Texture *texture, std::string filePath, bool verbose)
animatedSprite_t loadAnimationFromFile(std::shared_ptr<Texture> texture, std::string filePath)
{
// Inicializa variables
animatedSprite_t as;
as.texture = texture;
int framesPerRow = 0;
int frameWidth = 0;
int frameHeight = 0;
int maxTiles = 0;
auto framesPerRow = 0;
auto frameWidth = 0;
auto frameHeight = 0;
auto maxTiles = 0;
const std::string filename = filePath.substr(filePath.find_last_of("\\/") + 1);
std::ifstream file(filePath);
@@ -23,16 +23,15 @@ animatedSprite_t loadAnimationFromFile(Texture *texture, std::string filePath, b
if (file.good())
{
// Procesa el fichero linea a linea
if (verbose)
{
std::cout << "Animation loaded: " << filename << std::endl;
}
#ifdef VERBOSE
std::cout << "Animation loaded: " << filename << std::endl;
#endif
while (std::getline(file, line))
{
// Si la linea contiene el texto [animation] se realiza el proceso de carga de una animación
if (line == "[animation]")
{
animation_t buffer;
Animation buffer;
buffer.counter = 0;
buffer.currentFrame = 0;
buffer.completed = false;
@@ -71,7 +70,7 @@ animatedSprite_t loadAnimationFromFile(Texture *texture, std::string filePath, b
while (getline(ss, tmp, ','))
{
// Comprueba que el tile no sea mayor que el maximo indice permitido
const int numTile = std::stoi(tmp) > maxTiles ? 0 : std::stoi(tmp);
const auto numTile = std::stoi(tmp) > maxTiles ? 0 : std::stoi(tmp);
rect.x = (numTile % framesPerRow) * frameWidth;
rect.y = (numTile / framesPerRow) * frameHeight;
buffer.frames.push_back(rect);
@@ -80,7 +79,9 @@ animatedSprite_t loadAnimationFromFile(Texture *texture, std::string filePath, b
else
{
#ifdef VERBOSE
std::cout << "Warning: file " << filename.c_str() << "\n, unknown parameter \"" << line.substr(0, pos).c_str() << "\"" << std::endl;
#endif
}
}
} while (line != "[/animation]");
@@ -115,7 +116,9 @@ animatedSprite_t loadAnimationFromFile(Texture *texture, std::string filePath, b
else
{
#ifdef VERBOSE
std::cout << "Warning: file " << filename.c_str() << "\n, unknown parameter \"" << line.substr(0, pos).c_str() << "\"" << std::endl;
#endif
}
// Normaliza valores
@@ -126,8 +129,8 @@ animatedSprite_t loadAnimationFromFile(Texture *texture, std::string filePath, b
if (maxTiles == 0 && frameWidth > 0 && frameHeight > 0)
{
const int w = texture->getWidth() / frameWidth;
const int h = texture->getHeight() / frameHeight;
const auto w = texture->getWidth() / frameWidth;
const auto h = texture->getHeight() / frameHeight;
maxTiles = w * h;
}
}
@@ -140,17 +143,16 @@ animatedSprite_t loadAnimationFromFile(Texture *texture, std::string filePath, b
// El fichero no se puede abrir
else
{
if (verbose)
{
std::cout << "Warning: Unable to open " << filename.c_str() << " file" << std::endl;
}
#ifdef VERBOSE
std::cout << "Warning: Unable to open " << filename.c_str() << " file" << std::endl;
#endif
}
return as;
}
// Constructor
AnimatedSprite::AnimatedSprite(Texture *texture, std::string file, std::vector<std::string> *buffer)
AnimatedSprite::AnimatedSprite(std::shared_ptr<Texture> texture, std::string file, std::vector<std::string> *buffer)
{
// Copia los punteros
setTexture(texture);
@@ -163,7 +165,7 @@ AnimatedSprite::AnimatedSprite(Texture *texture, std::string file, std::vector<s
// Copia los datos de las animaciones
for (auto animation : as.animations)
{
this->animation.push_back(animation);
animations_.push_back(animation);
}
}
@@ -173,7 +175,7 @@ AnimatedSprite::AnimatedSprite(Texture *texture, std::string file, std::vector<s
}
// Inicializa variables
currentAnimation = 0;
currentAnimation_ = 0;
}
// Constructor
@@ -183,27 +185,27 @@ AnimatedSprite::AnimatedSprite(animatedSprite_t *animation)
setTexture(animation->texture);
// Inicializa variables
currentAnimation = 0;
currentAnimation_ = 0;
// Copia los datos de las animaciones
for (auto a : animation->animations)
{
this->animation.push_back(a);
animations_.push_back(a);
}
}
// Destructor
AnimatedSprite::~AnimatedSprite()
{
animation.clear();
animations_.clear();
}
// Obtiene el indice de la animación a partir del nombre
int AnimatedSprite::getIndex(std::string name)
{
int index = -1;
auto index = -1;
for (auto a : animation)
for (auto a : animations_)
{
index++;
if (a.name == name)
@@ -211,147 +213,147 @@ int AnimatedSprite::getIndex(std::string name)
return index;
}
}
#ifdef VERBOSE
std::cout << "** Warning: could not find \"" << name.c_str() << "\" animation" << std::endl;
#endif
return -1;
}
// Calcula el frame correspondiente a la animación
void AnimatedSprite::animate()
{
if (!enabled || animation[currentAnimation].speed == 0)
if (!enabled_ || animations_[currentAnimation_].speed == 0)
{
return;
}
// Calcula el frame actual a partir del contador
animation[currentAnimation].currentFrame = animation[currentAnimation].counter / animation[currentAnimation].speed;
animations_[currentAnimation_].currentFrame = animations_[currentAnimation_].counter / animations_[currentAnimation_].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 (animations_[currentAnimation_].currentFrame >= (int)animations_[currentAnimation_].frames.size())
{
if (animation[currentAnimation].loop == -1)
if (animations_[currentAnimation_].loop == -1)
{ // Si no hay loop, deja el último frame
animation[currentAnimation].currentFrame = animation[currentAnimation].frames.size();
animation[currentAnimation].completed = true;
animations_[currentAnimation_].currentFrame = animations_[currentAnimation_].frames.size();
animations_[currentAnimation_].completed = true;
}
else
{ // Si hay loop, vuelve al frame indicado
animation[currentAnimation].counter = 0;
animation[currentAnimation].currentFrame = animation[currentAnimation].loop;
animations_[currentAnimation_].counter = 0;
animations_[currentAnimation_].currentFrame = animations_[currentAnimation_].loop;
}
}
// En caso contrario
else
{
// Escoge el frame correspondiente de la animación
setSpriteClip(animation[currentAnimation].frames[animation[currentAnimation].currentFrame]);
setSpriteClip(animations_[currentAnimation_].frames[animations_[currentAnimation_].currentFrame]);
// Incrementa el contador de la animacion
animation[currentAnimation].counter++;
animations_[currentAnimation_].counter++;
}
}
// Obtiene el número de frames de la animación actual
int AnimatedSprite::getNumFrames()
{
return (int)animation[currentAnimation].frames.size();
return (int)animations_[currentAnimation_].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)animations_[currentAnimation_].frames.size())
{
num = 0;
}
// Cambia el valor de la variable
animation[currentAnimation].currentFrame = num;
animation[currentAnimation].counter = 0;
animations_[currentAnimation_].currentFrame = num;
animations_[currentAnimation_].counter = 0;
// Escoge el frame correspondiente de la animación
setSpriteClip(animation[currentAnimation].frames[animation[currentAnimation].currentFrame]);
setSpriteClip(animations_[currentAnimation_].frames[animations_[currentAnimation_].currentFrame]);
}
// Establece el valor del contador
void AnimatedSprite::setAnimationCounter(std::string name, int num)
{
animation[getIndex(name)].counter = num;
animations_[getIndex(name)].counter = num;
}
// Establece la velocidad de una animación
void AnimatedSprite::setAnimationSpeed(std::string name, int speed)
{
animation[getIndex(name)].counter = speed;
animations_[getIndex(name)].counter = speed;
}
// Establece la velocidad de una animación
void AnimatedSprite::setAnimationSpeed(int index, int speed)
{
animation[index].counter = speed;
animations_[index].counter = speed;
}
// Establece si la animación se reproduce en bucle
void AnimatedSprite::setAnimationLoop(std::string name, int loop)
{
animation[getIndex(name)].loop = loop;
animations_[getIndex(name)].loop = loop;
}
// Establece si la animación se reproduce en bucle
void AnimatedSprite::setAnimationLoop(int index, int loop)
{
animation[index].loop = loop;
animations_[index].loop = loop;
}
// Establece el valor de la variable
void AnimatedSprite::setAnimationCompleted(std::string name, bool value)
{
animation[getIndex(name)].completed = value;
animations_[getIndex(name)].completed = value;
}
// OLD - Establece el valor de la variable
void AnimatedSprite::setAnimationCompleted(int index, bool value)
{
animation[index].completed = value;
animations_[index].completed = value;
}
// Comprueba si ha terminado la animación
bool AnimatedSprite::animationIsCompleted()
{
return animation[currentAnimation].completed;
return animations_[currentAnimation_].completed;
}
// Devuelve el rectangulo de una animación y frame concreto
SDL_Rect AnimatedSprite::getAnimationClip(std::string name, Uint8 index)
{
return animation[getIndex(name)].frames[index];
return animations_[getIndex(name)].frames[index];
}
// Devuelve el rectangulo de una animación y frame concreto
SDL_Rect AnimatedSprite::getAnimationClip(int indexA, Uint8 indexF)
{
return animation[indexA].frames[indexF];
return animations_[indexA].frames[indexF];
}
// Carga la animación desde un vector
bool AnimatedSprite::loadFromVector(std::vector<std::string> *source)
{
// Inicializa variables
int framesPerRow = 0;
int frameWidth = 0;
int frameHeight = 0;
int maxTiles = 0;
auto framesPerRow = 0;
auto frameWidth = 0;
auto frameHeight = 0;
auto maxTiles = 0;
// Indicador de éxito en el proceso
bool success = true;
auto success = true;
std::string line;
// Recorre todo el vector
int index = 0;
auto index = 0;
while (index < (int)source->size())
{
// Lee desde el vector
@@ -360,7 +362,7 @@ bool AnimatedSprite::loadFromVector(std::vector<std::string> *source)
// Si la linea contiene el texto [animation] se realiza el proceso de carga de una animación
if (line == "[animation]")
{
animation_t buffer;
Animation buffer;
buffer.counter = 0;
buffer.currentFrame = 0;
buffer.completed = false;
@@ -410,14 +412,16 @@ bool AnimatedSprite::loadFromVector(std::vector<std::string> *source)
else
{
#ifdef VERBOSE
std::cout << "Warning: unknown parameter " << line.substr(0, pos).c_str() << std::endl;
#endif
success = false;
}
}
} while (line != "[/animation]");
// Añade la animación al vector de animaciones
animation.push_back(buffer);
animations_.push_back(buffer);
}
// En caso contrario se parsea el fichero para buscar las variables y los valores
@@ -446,20 +450,22 @@ bool AnimatedSprite::loadFromVector(std::vector<std::string> *source)
else
{
#ifdef VERBOSE
std::cout << "Warning: unknown parameter " << line.substr(0, pos).c_str() << std::endl;
#endif
success = false;
}
// 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;
}
}
@@ -478,26 +484,26 @@ bool AnimatedSprite::loadFromVector(std::vector<std::string> *source)
// Establece la animacion actual
void AnimatedSprite::setCurrentAnimation(std::string name)
{
const int newAnimation = getIndex(name);
if (currentAnimation != newAnimation)
const auto newAnimation = getIndex(name);
if (currentAnimation_ != newAnimation)
{
currentAnimation = newAnimation;
animation[currentAnimation].currentFrame = 0;
animation[currentAnimation].counter = 0;
animation[currentAnimation].completed = false;
currentAnimation_ = newAnimation;
animations_[currentAnimation_].currentFrame = 0;
animations_[currentAnimation_].counter = 0;
animations_[currentAnimation_].completed = false;
}
}
// Establece la animacion actual
void AnimatedSprite::setCurrentAnimation(int index)
{
const int newAnimation = index;
if (currentAnimation != newAnimation)
const auto newAnimation = index;
if (currentAnimation_ != newAnimation)
{
currentAnimation = newAnimation;
animation[currentAnimation].currentFrame = 0;
animation[currentAnimation].counter = 0;
animation[currentAnimation].completed = false;
currentAnimation_ = newAnimation;
animations_[currentAnimation_].currentFrame = 0;
animations_[currentAnimation_].counter = 0;
animations_[currentAnimation_].completed = false;
}
}
@@ -511,13 +517,13 @@ 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});
animations_[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 : animations_)
{
a.counter = value;
}
@@ -526,7 +532,7 @@ void AnimatedSprite::setAnimationCounter(int value)
// Reinicia la animación
void AnimatedSprite::resetAnimation()
{
animation[currentAnimation].currentFrame = 0;
animation[currentAnimation].counter = 0;
animation[currentAnimation].completed = false;
animations_[currentAnimation_].currentFrame = 0;
animations_[currentAnimation_].counter = 0;
animations_[currentAnimation_].completed = false;
}