Optimizaciones de código

This commit is contained in:
2022-09-24 19:16:39 +02:00
parent f3aeed9428
commit b44869341c
99 changed files with 441 additions and 509 deletions

View File

@@ -32,7 +32,7 @@ int AnimatedSprite::getIndex(std::string name)
for (auto a : animation)
{
index++;
++index;
if (a.name == name)
{
return index;
@@ -57,7 +57,7 @@ void AnimatedSprite::animate()
// 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 >= animation[currentAnimation].frames.size())
if (animation[currentAnimation].currentFrame >= (int)animation[currentAnimation].frames.size())
{
if (animation[currentAnimation].loop == -1)
{ // Si no hay loop, deja el último frame
@@ -77,7 +77,7 @@ void AnimatedSprite::animate()
setSpriteClip(animation[currentAnimation].frames[animation[currentAnimation].currentFrame]);
// Incrementa el contador de la animacion
animation[currentAnimation].counter++;
++animation[currentAnimation].counter;
}
}
@@ -85,7 +85,7 @@ void AnimatedSprite::animate()
void AnimatedSprite::setCurrentFrame(int num)
{
// Descarta valores fuera de rango
if (num >= animation[currentAnimation].frames.size())
if (num >= (int)animation[currentAnimation].frames.size())
{
num = 0;
}
@@ -136,9 +136,9 @@ SDL_Rect AnimatedSprite::getAnimationClip(std::string name, Uint8 index)
// Carga la animación desde un fichero
bool AnimatedSprite::load(std::string filePath)
{
int frames_per_row = 0;
int frame_width = 0;
int frame_height = 0;
int framesPerRow = 0;
int frameWidth = 0;
int frameHeight = 0;
// Indicador de éxito en la carga
bool success = true;
@@ -170,7 +170,7 @@ bool AnimatedSprite::load(std::string filePath)
int pos = line.find("=");
// Procesa las dos subcadenas
if (pos != line.npos)
if (pos != (int)line.npos)
{
if (line.substr(0, pos) == "name")
{
@@ -189,12 +189,12 @@ bool AnimatedSprite::load(std::string filePath)
// 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, frame_width, frame_height};
SDL_Rect rect = {0, 0, frameWidth, frameHeight};
while (getline(ss, tmp, ','))
{
int num_tile = std::stoi(tmp);
rect.x = (num_tile % frames_per_row) * frame_width;
rect.y = (num_tile / frames_per_row) * frame_height;
int numTile = std::stoi(tmp);
rect.x = (numTile % framesPerRow) * frameWidth;
rect.y = (numTile / framesPerRow) * frameHeight;
buffer.frames.push_back(rect);
}
}
@@ -217,27 +217,27 @@ bool AnimatedSprite::load(std::string filePath)
int pos = line.find("=");
// Procesa las dos subcadenas
if (pos != line.npos)
if (pos != (int)line.npos)
{
if (line.substr(0, pos) == "frames_per_row")
if (line.substr(0, pos) == "framesPerRow")
{
frames_per_row = std::stoi(line.substr(pos + 1, line.length()));
framesPerRow = std::stoi(line.substr(pos + 1, line.length()));
}
else if (line.substr(0, pos) == "frame_width")
else if (line.substr(0, pos) == "frameWidth")
{
frame_width = std::stoi(line.substr(pos + 1, line.length()));
frameWidth = std::stoi(line.substr(pos + 1, line.length()));
// Normaliza valores
if (frames_per_row == 0)
if (framesPerRow == 0)
{
frames_per_row = texture->getWidth() / frame_width;
framesPerRow = texture->getWidth() / frameWidth;
}
}
else if (line.substr(0, pos) == "frame_height")
else if (line.substr(0, pos) == "frameHeight")
{
frame_height = std::stoi(line.substr(pos + 1, line.length()));
frameHeight = std::stoi(line.substr(pos + 1, line.length()));
}
else
@@ -261,7 +261,7 @@ bool AnimatedSprite::load(std::string filePath)
}
// Pone un valor por defecto
setPos({0, 0, frame_width, frame_height});
setPos({0, 0, frameWidth, frameHeight});
return success;
}