Estandaritzant noms segons convencions

This commit is contained in:
2024-10-10 20:27:31 +02:00
parent 9e5f41644e
commit d6c3c89872
67 changed files with 1457 additions and 1504 deletions

View File

@@ -5,18 +5,18 @@
#include "texture.h" // for Texture
// Carga la animación desde un fichero
animatedSprite_t loadAnimationFromFile(std::shared_ptr<Texture> texture, std::string filePath)
AnimatedFile loadAnimationFromFile(std::shared_ptr<Texture> texture, std::string file_path)
{
// Inicializa variables
animatedSprite_t as;
as.texture = texture;
auto framesPerRow = 0;
auto frameWidth = 0;
auto frameHeight = 0;
auto maxTiles = 0;
AnimatedFile af;
af.texture = texture;
auto frames_per_row = 0;
auto frame_width = 0;
auto frame_height = 0;
auto max_tiles = 0;
const std::string filename = filePath.substr(filePath.find_last_of("\\/") + 1);
std::ifstream file(filePath);
const std::string file_name = file_path.substr(file_path.find_last_of("\\/") + 1);
std::ifstream file(file_path);
std::string line;
// El fichero se puede abrir
@@ -24,7 +24,7 @@ animatedSprite_t loadAnimationFromFile(std::shared_ptr<Texture> texture, std::st
{
// Procesa el fichero linea a linea
#ifdef VERBOSE
std::cout << "Animation loaded: " << filename << std::endl;
std::cout << "Animation loaded: " << file_name << std::endl;
#endif
while (std::getline(file, line))
{
@@ -33,7 +33,7 @@ animatedSprite_t loadAnimationFromFile(std::shared_ptr<Texture> texture, std::st
{
Animation buffer;
buffer.counter = 0;
buffer.currentFrame = 0;
buffer.current_frame = 0;
buffer.completed = false;
do
@@ -66,13 +66,13 @@ animatedSprite_t loadAnimationFromFile(std::shared_ptr<Texture> texture, std::st
// Se introducen los valores separados por comas en un vector
std::stringstream ss(line.substr(pos + 1, line.length()));
std::string tmp;
SDL_Rect rect = {0, 0, frameWidth, frameHeight};
SDL_Rect rect = {0, 0, frame_width, frame_height};
while (getline(ss, tmp, ','))
{
// Comprueba que el tile no sea mayor que el maximo indice permitido
const auto numTile = std::stoi(tmp) > maxTiles ? 0 : std::stoi(tmp);
rect.x = (numTile % framesPerRow) * frameWidth;
rect.y = (numTile / framesPerRow) * frameHeight;
const auto num_tile = std::stoi(tmp) > max_tiles ? 0 : std::stoi(tmp);
rect.x = (num_tile % frames_per_row) * frame_width;
rect.y = (num_tile / frames_per_row) * frame_height;
buffer.frames.push_back(rect);
}
}
@@ -80,14 +80,14 @@ animatedSprite_t loadAnimationFromFile(std::shared_ptr<Texture> texture, std::st
else
{
#ifdef VERBOSE
std::cout << "Warning: file " << filename.c_str() << "\n, unknown parameter \"" << line.substr(0, pos).c_str() << "\"" << std::endl;
std::cout << "Warning: file " << file_name.c_str() << "\n, unknown parameter \"" << line.substr(0, pos).c_str() << "\"" << std::endl;
#endif
}
}
} while (line != "[/animation]");
// Añade la animación al vector de animaciones
as.animations.push_back(buffer);
af.animations.push_back(buffer);
}
// En caso contrario se parsea el fichero para buscar las variables y los valores
@@ -99,39 +99,39 @@ animatedSprite_t loadAnimationFromFile(std::shared_ptr<Texture> texture, std::st
// Procesa las dos subcadenas
if (pos != (int)line.npos)
{
if (line.substr(0, pos) == "framesPerRow")
if (line.substr(0, pos) == "frames_per_row")
{
framesPerRow = std::stoi(line.substr(pos + 1, line.length()));
frames_per_row = std::stoi(line.substr(pos + 1, line.length()));
}
else if (line.substr(0, pos) == "frameWidth")
else if (line.substr(0, pos) == "frame_width")
{
frameWidth = std::stoi(line.substr(pos + 1, line.length()));
frame_width = std::stoi(line.substr(pos + 1, line.length()));
}
else if (line.substr(0, pos) == "frameHeight")
else if (line.substr(0, pos) == "frame_height")
{
frameHeight = std::stoi(line.substr(pos + 1, line.length()));
frame_height = std::stoi(line.substr(pos + 1, line.length()));
}
else
{
#ifdef VERBOSE
std::cout << "Warning: file " << filename.c_str() << "\n, unknown parameter \"" << line.substr(0, pos).c_str() << "\"" << std::endl;
std::cout << "Warning: file " << file_name.c_str() << "\n, unknown parameter \"" << line.substr(0, pos).c_str() << "\"" << std::endl;
#endif
}
// Normaliza valores
if (framesPerRow == 0 && frameWidth > 0)
if (frames_per_row == 0 && frame_width > 0)
{
framesPerRow = texture->getWidth() / frameWidth;
frames_per_row = texture->getWidth() / frame_width;
}
if (maxTiles == 0 && frameWidth > 0 && frameHeight > 0)
if (max_tiles == 0 && frame_width > 0 && frame_height > 0)
{
const auto w = texture->getWidth() / frameWidth;
const auto h = texture->getHeight() / frameHeight;
maxTiles = w * h;
const auto w = texture->getWidth() / frame_width;
const auto h = texture->getHeight() / frame_height;
max_tiles = w * h;
}
}
}
@@ -144,11 +144,11 @@ animatedSprite_t loadAnimationFromFile(std::shared_ptr<Texture> texture, std::st
else
{
#ifdef VERBOSE
std::cout << "Warning: Unable to open " << filename.c_str() << " file" << std::endl;
std::cout << "Warning: Unable to open " << file_name.c_str() << " file" << std::endl;
#endif
}
return as;
return af;
}
// Constructor
@@ -160,7 +160,7 @@ AnimatedSprite::AnimatedSprite(std::shared_ptr<Texture> texture, std::string fil
// Carga las animaciones
if (file != "")
{
animatedSprite_t as = loadAnimationFromFile(texture, file);
AnimatedFile as = loadAnimationFromFile(texture, file);
// Copia los datos de las animaciones
for (auto animation : as.animations)
@@ -175,17 +175,17 @@ AnimatedSprite::AnimatedSprite(std::shared_ptr<Texture> texture, std::string fil
}
// Inicializa variables
currentAnimation_ = 0;
current_animation_ = 0;
}
// Constructor
AnimatedSprite::AnimatedSprite(animatedSprite_t *animation)
AnimatedSprite::AnimatedSprite(AnimatedFile *animation)
{
// Copia los punteros
setTexture(animation->texture);
// Inicializa variables
currentAnimation_ = 0;
current_animation_ = 0;
// Copia los datos de las animaciones
for (auto a : animation->animations)
@@ -222,61 +222,61 @@ int AnimatedSprite::getIndex(std::string name)
// Calcula el frame correspondiente a la animación
void AnimatedSprite::animate()
{
if (!enabled_ || animations_[currentAnimation_].speed == 0)
if (!enabled_ || animations_[current_animation_].speed == 0)
{
return;
}
// Calcula el frame actual a partir del contador
animations_[currentAnimation_].currentFrame = animations_[currentAnimation_].counter / animations_[currentAnimation_].speed;
animations_[current_animation_].current_frame = animations_[current_animation_].counter / animations_[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 (animations_[currentAnimation_].currentFrame >= (int)animations_[currentAnimation_].frames.size())
if (animations_[current_animation_].current_frame >= (int)animations_[current_animation_].frames.size())
{
if (animations_[currentAnimation_].loop == -1)
if (animations_[current_animation_].loop == -1)
{ // Si no hay loop, deja el último frame
animations_[currentAnimation_].currentFrame = animations_[currentAnimation_].frames.size();
animations_[currentAnimation_].completed = true;
animations_[current_animation_].current_frame = animations_[current_animation_].frames.size();
animations_[current_animation_].completed = true;
}
else
{ // Si hay loop, vuelve al frame indicado
animations_[currentAnimation_].counter = 0;
animations_[currentAnimation_].currentFrame = animations_[currentAnimation_].loop;
animations_[current_animation_].counter = 0;
animations_[current_animation_].current_frame = animations_[current_animation_].loop;
}
}
// En caso contrario
else
{
// Escoge el frame correspondiente de la animación
setSpriteClip(animations_[currentAnimation_].frames[animations_[currentAnimation_].currentFrame]);
setSpriteClip(animations_[current_animation_].frames[animations_[current_animation_].current_frame]);
// Incrementa el contador de la animacion
animations_[currentAnimation_].counter++;
animations_[current_animation_].counter++;
}
}
// Obtiene el número de frames de la animación actual
int AnimatedSprite::getNumFrames()
{
return (int)animations_[currentAnimation_].frames.size();
return (int)animations_[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)animations_[currentAnimation_].frames.size())
if (num >= (int)animations_[current_animation_].frames.size())
{
num = 0;
}
// Cambia el valor de la variable
animations_[currentAnimation_].currentFrame = num;
animations_[currentAnimation_].counter = 0;
animations_[current_animation_].current_frame = num;
animations_[current_animation_].counter = 0;
// Escoge el frame correspondiente de la animación
setSpriteClip(animations_[currentAnimation_].frames[animations_[currentAnimation_].currentFrame]);
setSpriteClip(animations_[current_animation_].frames[animations_[current_animation_].current_frame]);
}
// Establece el valor del contador
@@ -324,7 +324,7 @@ void AnimatedSprite::setAnimationCompleted(int index, bool value)
// Comprueba si ha terminado la animación
bool AnimatedSprite::animationIsCompleted()
{
return animations_[currentAnimation_].completed;
return animations_[current_animation_].completed;
}
// Devuelve el rectangulo de una animación y frame concreto
@@ -343,10 +343,10 @@ SDL_Rect AnimatedSprite::getAnimationClip(int indexA, Uint8 indexF)
bool AnimatedSprite::loadFromVector(std::vector<std::string> *source)
{
// Inicializa variables
auto framesPerRow = 0;
auto frameWidth = 0;
auto frameHeight = 0;
auto maxTiles = 0;
auto frames_per_row = 0;
auto frame_width = 0;
auto frame_height = 0;
auto max_tiles = 0;
// Indicador de éxito en el proceso
auto success = true;
@@ -364,7 +364,7 @@ bool AnimatedSprite::loadFromVector(std::vector<std::string> *source)
{
Animation buffer;
buffer.counter = 0;
buffer.currentFrame = 0;
buffer.current_frame = 0;
buffer.completed = false;
do
@@ -399,13 +399,13 @@ bool AnimatedSprite::loadFromVector(std::vector<std::string> *source)
// Se introducen los valores separados por comas en un vector
std::stringstream ss(line.substr(pos + 1, line.length()));
std::string tmp;
SDL_Rect rect = {0, 0, frameWidth, frameHeight};
SDL_Rect rect = {0, 0, frame_width, frame_height};
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);
rect.x = (numTile % framesPerRow) * frameWidth;
rect.y = (numTile / framesPerRow) * frameHeight;
const int num_tile = std::stoi(tmp) > max_tiles ? 0 : std::stoi(tmp);
rect.x = (num_tile % frames_per_row) * frame_width;
rect.y = (num_tile / frames_per_row) * frame_height;
buffer.frames.push_back(rect);
}
}
@@ -433,19 +433,19 @@ bool AnimatedSprite::loadFromVector(std::vector<std::string> *source)
// Procesa las dos subcadenas
if (pos != (int)line.npos)
{
if (line.substr(0, pos) == "framesPerRow")
if (line.substr(0, pos) == "frames_per_row")
{
framesPerRow = std::stoi(line.substr(pos + 1, line.length()));
frames_per_row = std::stoi(line.substr(pos + 1, line.length()));
}
else if (line.substr(0, pos) == "frameWidth")
else if (line.substr(0, pos) == "frame_width")
{
frameWidth = std::stoi(line.substr(pos + 1, line.length()));
frame_width = std::stoi(line.substr(pos + 1, line.length()));
}
else if (line.substr(0, pos) == "frameHeight")
else if (line.substr(0, pos) == "frame_height")
{
frameHeight = std::stoi(line.substr(pos + 1, line.length()));
frame_height = std::stoi(line.substr(pos + 1, line.length()));
}
else
@@ -457,16 +457,16 @@ bool AnimatedSprite::loadFromVector(std::vector<std::string> *source)
}
// Normaliza valores
if (framesPerRow == 0 && frameWidth > 0)
if (frames_per_row == 0 && frame_width > 0)
{
framesPerRow = texture_->getWidth() / frameWidth;
frames_per_row = texture_->getWidth() / frame_width;
}
if (maxTiles == 0 && frameWidth > 0 && frameHeight > 0)
if (max_tiles == 0 && frame_width > 0 && frame_height > 0)
{
const int w = texture_->getWidth() / frameWidth;
const int h = texture_->getHeight() / frameHeight;
maxTiles = w * h;
const int w = texture_->getWidth() / frame_width;
const int h = texture_->getHeight() / frame_height;
max_tiles = w * h;
}
}
}
@@ -476,7 +476,7 @@ bool AnimatedSprite::loadFromVector(std::vector<std::string> *source)
}
// Pone un valor por defecto
setRect({0, 0, frameWidth, frameHeight});
setRect({0, 0, frame_width, frame_height});
return success;
}
@@ -484,26 +484,26 @@ bool AnimatedSprite::loadFromVector(std::vector<std::string> *source)
// Establece la animacion actual
void AnimatedSprite::setCurrentAnimation(std::string name)
{
const auto newAnimation = getIndex(name);
if (currentAnimation_ != newAnimation)
const auto new_animation = getIndex(name);
if (current_animation_ != new_animation)
{
currentAnimation_ = newAnimation;
animations_[currentAnimation_].currentFrame = 0;
animations_[currentAnimation_].counter = 0;
animations_[currentAnimation_].completed = false;
current_animation_ = new_animation;
animations_[current_animation_].current_frame = 0;
animations_[current_animation_].counter = 0;
animations_[current_animation_].completed = false;
}
}
// Establece la animacion actual
void AnimatedSprite::setCurrentAnimation(int index)
{
const auto newAnimation = index;
if (currentAnimation_ != newAnimation)
const auto new_animation = index;
if (current_animation_ != new_animation)
{
currentAnimation_ = newAnimation;
animations_[currentAnimation_].currentFrame = 0;
animations_[currentAnimation_].counter = 0;
animations_[currentAnimation_].completed = false;
current_animation_ = new_animation;
animations_[current_animation_].current_frame = 0;
animations_[current_animation_].counter = 0;
animations_[current_animation_].completed = false;
}
}
@@ -535,7 +535,7 @@ void AnimatedSprite::setAnimationCounter(int value)
// Reinicia la animación
void AnimatedSprite::resetAnimation()
{
animations_[currentAnimation_].currentFrame = 0;
animations_[currentAnimation_].counter = 0;
animations_[currentAnimation_].completed = false;
animations_[current_animation_].current_frame = 0;
animations_[current_animation_].counter = 0;
animations_[current_animation_].completed = false;
}