Animatedsprite ya no permite indices fuera de rango

This commit is contained in:
2022-09-25 13:00:39 +02:00
parent 66840ebf11
commit 8a4d2a541d
18 changed files with 79 additions and 71 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;
@@ -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++;
}
}
@@ -139,6 +139,7 @@ bool AnimatedSprite::load(std::string filePath)
int framesPerRow = 0;
int frameWidth = 0;
int frameHeight = 0;
int maxTiles = 0;
// Indicador de éxito en la carga
bool success = true;
@@ -192,7 +193,8 @@ bool AnimatedSprite::load(std::string filePath)
SDL_Rect rect = {0, 0, frameWidth, frameHeight};
while (getline(ss, tmp, ','))
{
int numTile = std::stoi(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;
buffer.frames.push_back(rect);
@@ -227,12 +229,6 @@ bool AnimatedSprite::load(std::string filePath)
else if (line.substr(0, pos) == "frameWidth")
{
frameWidth = std::stoi(line.substr(pos + 1, line.length()));
// Normaliza valores
if (framesPerRow == 0)
{
framesPerRow = texture->getWidth() / frameWidth;
}
}
else if (line.substr(0, pos) == "frameHeight")
@@ -245,6 +241,19 @@ bool AnimatedSprite::load(std::string filePath)
printf("Warning: file %s, unknown parameter \"%s\"\n", filename.c_str(), line.substr(0, pos).c_str());
success = false;
}
// Normaliza valores
if (framesPerRow == 0 && frameWidth > 0)
{
framesPerRow = texture->getWidth() / frameWidth;
}
if (maxTiles == 0 && frameWidth > 0 && frameHeight > 0)
{
const int w = texture->getWidth() / frameWidth;
const int h = texture->getHeight() / frameHeight;
maxTiles = w * h;
}
}
}
}