Ya empieza a funcionar la INTRO

This commit is contained in:
2022-09-28 19:41:12 +02:00
parent c213124193
commit e2298dc2b2
3 changed files with 238 additions and 239 deletions

View File

@@ -163,9 +163,10 @@ SDL_Rect AnimatedSprite::getAnimationClip(int indexA, Uint8 indexF)
// 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;
int maxTiles = 0;
// Indicador de éxito en la carga
bool success = true;
@@ -216,12 +217,13 @@ 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;
// 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);
}
}
@@ -246,25 +248,19 @@ bool AnimatedSprite::load(std::string filePath)
// Procesa las dos subcadenas
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()));
// Normaliza valores
if (frames_per_row == 0)
{
frames_per_row = texture->getWidth() / frame_width;
}
frameWidth = std::stoi(line.substr(pos + 1, line.length()));
}
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
@@ -272,6 +268,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;
}
}
}
}
@@ -288,7 +297,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;
}