Finalizado el nuevo motor de animaciones

This commit is contained in:
2022-08-13 11:07:04 +02:00
parent d2a2a1625d
commit e85f138be5
7 changed files with 142 additions and 59 deletions

View File

@@ -12,13 +12,18 @@ AnimatedSprite::AnimatedSprite(LTexture *texture, SDL_Renderer *renderer)
// Destructor
AnimatedSprite::~AnimatedSprite()
{
for (auto a : animation)
{
a.frames.clear();
}
animation.clear();
}
// Obtiene el indice de la animación a partir del nombre
int AnimatedSprite::getIndex(std::string name)
{
int result = -1;
for (int i = 0; i < animation.size(), i++)
for (int i = 0; i < animation.size(); i++)
{
if (animation[i].name == name)
{
@@ -104,55 +109,124 @@ SDL_Rect AnimatedSprite::getAnimationClip(std::string name, Uint8 index)
// Carga la animación desde un fichero
bool AnimatedSprite::load(std::string filePath)
{
}
// Asigna variables a partir de dos cadenas
bool AnimatedSprite::setVars(int index, std::string var, std::string value)
{
// Indicador de éxito en la asignación
int png_width_in_tiles = 0;
int tile_width = 0;
int tile_height = 0;
// Indicador de éxito en la carga
bool success = true;
if (var == "name")
const std::string filename = filePath.substr(filePath.find_last_of("\\/") + 1);
std::ifstream file(filePath);
std::string line;
// El fichero se puede abrir
if (file.good())
{
animation[index].name = value;
}
else if (var == "speed")
{
animation[index].speed = std::stoi(value);
}
else if (var == "loop")
{
if (value == "yes" || value == "true")
// Procesa el fichero linea a linea
printf("Reading file %s\n", filename.c_str());
while (std::getline(file, line))
{
animation[index].loop = true;
}
else
{
animation[index].loop = false;
// Si la linea contiene el texto [enemy] se realiza el proceso de carga de un enemigo
if (line == "[animation]")
{
t_animation buffer;
buffer.counter = 0;
buffer.currentFrame = 0;
buffer.completed = false;
do
{
std::getline(file, line);
// Encuentra la posición del caracter '='
int pos = line.find("=");
// Procesa las dos subcadenas
if (pos != line.npos)
{
if (line.substr(0, pos) == "name")
{
buffer.name = line.substr(pos + 1, line.length());
}
else if (line.substr(0, pos) == "speed")
{
buffer.speed = std::stoi(line.substr(pos + 1, line.length()));
}
else if (line.substr(0, pos) == "loop")
{
if (line.substr(pos + 1, line.length()) == "yes" || line.substr(pos + 1, line.length()) == "true")
{
buffer.loop = true;
}
else
{
buffer.loop = false;
}
}
else if (line.substr(0, pos) == "frames")
{
// 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, tile_width, tile_height};
while (getline(ss, tmp, ','))
{
int num_tile = std::stoi(tmp);
rect.x = (num_tile % png_width_in_tiles) * tile_width;
rect.y = (num_tile / png_width_in_tiles) * tile_height;
buffer.frames.push_back(rect);
}
}
else
{
printf("Warning: file %s, unknown parameter \"%s\"\n", filename.c_str(), line.substr(0, pos).c_str());
success = false;
}
}
} while (line != "[/animation]");
// Añade el enemigo al vector de enemigos
animation.push_back(buffer);
}
// En caso contrario se parsea el fichero para buscar las variables y los valores
else
{
// Encuentra la posición del caracter '='
int pos = line.find("=");
// Procesa las dos subcadenas
if (pos != line.npos)
{
if (line.substr(0, pos) == "png_width_in_tiles")
{
png_width_in_tiles = std::stoi(line.substr(pos + 1, line.length()));
}
else if (line.substr(0, pos) == "tile_width")
{
tile_width = std::stoi(line.substr(pos + 1, line.length()));
}
else if (line.substr(0, pos) == "tile_height")
{
tile_height = std::stoi(line.substr(pos + 1, line.length()));
}
else
{
printf("Warning: file %s, unknown parameter \"%s\"\n", filename.c_str(), line.substr(0, pos).c_str());
success = false;
}
}
}
}
// Cierra el fichero
printf("Closing file %s\n", filename.c_str());
file.close();
}
else if (var == "frames")
{
const int w = 16;
const int h = 24;
const int png_width_tiles = 8;
// Se introducen los valores separados por comas en un vector
std::stringstream ss(value);
std::string tmp;
SDL_Rect rect = {0, 0, w, h};
while (getline(ss, tmp, ','))
{
int num_tile = std::stoi(tmp);
rect.x = (num_tile % png_width_tiles) * w;
rect.y = (num_tile / png_width_tiles) * h;
animation[index].frames.push_back(rect);
}
}
else if (var == "")
{
}
// El fichero no se puede abrir
else
{
printf("Warning: Unable to open %s file\n", filename.c_str());
success = false;
}