Merge branch 'main' of https://gitea.sustancia.synology.me/JailDesigner/coffee_crisis_arcade_edition
This commit is contained in:
@@ -16,18 +16,16 @@ AnimatedSprite::AnimatedSprite(std::shared_ptr<Texture> texture, const std::stri
|
||||
{
|
||||
animations_ = loadFromFile(file_path);
|
||||
}
|
||||
// setSpriteClip(animations_[current_animation_].frames[animations_[current_animation_].current_frame]);
|
||||
}
|
||||
|
||||
AnimatedSprite::AnimatedSprite(std::shared_ptr<Texture> texture, std::vector<std::string> *animations)
|
||||
AnimatedSprite::AnimatedSprite(std::shared_ptr<Texture> texture, const std::vector<std::string> &animations)
|
||||
: MovingSprite(texture),
|
||||
current_animation_(0)
|
||||
{
|
||||
if (animations)
|
||||
if (!animations.empty())
|
||||
{
|
||||
loadFromVector(animations);
|
||||
}
|
||||
// setSpriteClip(animations_[current_animation_].frames[animations_[current_animation_].current_frame]);
|
||||
}
|
||||
|
||||
// Constructor
|
||||
@@ -178,6 +176,149 @@ SDL_Rect AnimatedSprite::getAnimationClip(int indexA, Uint8 indexF)
|
||||
return animations_[indexA].frames[indexF];
|
||||
}
|
||||
|
||||
// Carga la animación desde un vector
|
||||
bool AnimatedSprite::loadFromVector(const std::vector<std::string> &source)
|
||||
{
|
||||
// Inicializa variables
|
||||
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;
|
||||
std::string line;
|
||||
|
||||
// Recorre todo el vector
|
||||
auto index = 0;
|
||||
while (index < (int)source.size())
|
||||
{
|
||||
// Lee desde el vector
|
||||
line = source.at(index);
|
||||
|
||||
// Si la linea contiene el texto [animation] se realiza el proceso de carga de una animación
|
||||
if (line == "[animation]")
|
||||
{
|
||||
Animation animation;
|
||||
animation.counter = 0;
|
||||
animation.current_frame = 0;
|
||||
animation.completed = false;
|
||||
animation.name.clear();
|
||||
animation.speed = 5;
|
||||
animation.loop = 0;
|
||||
animation.frames.clear();
|
||||
|
||||
do
|
||||
{
|
||||
// Aumenta el indice para leer la siguiente linea
|
||||
index++;
|
||||
line = source.at(index);
|
||||
|
||||
// Encuentra la posición del caracter '='
|
||||
int pos = line.find("=");
|
||||
|
||||
// Procesa las dos subcadenas
|
||||
if (pos != static_cast<int>(line.npos))
|
||||
{
|
||||
if (line.substr(0, pos) == "name")
|
||||
{
|
||||
animation.name = line.substr(pos + 1, line.length());
|
||||
}
|
||||
|
||||
else if (line.substr(0, pos) == "speed")
|
||||
{
|
||||
animation.speed = std::stoi(line.substr(pos + 1, line.length()));
|
||||
}
|
||||
|
||||
else if (line.substr(0, pos) == "loop")
|
||||
{
|
||||
animation.loop = std::stoi(line.substr(pos + 1, line.length()));
|
||||
}
|
||||
|
||||
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, frame_width, frame_height};
|
||||
while (getline(ss, tmp, ','))
|
||||
{
|
||||
// Comprueba que el tile no sea mayor que el maximo indice permitido
|
||||
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;
|
||||
animation.frames.push_back(rect);
|
||||
}
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
std::cout << "Warning: unknown parameter " << line.substr(0, pos).c_str() << std::endl;
|
||||
success = false;
|
||||
}
|
||||
}
|
||||
} while (line != "[/animation]");
|
||||
|
||||
// Añade la animación al vector de animaciones
|
||||
animations_.push_back(animation);
|
||||
}
|
||||
|
||||
// 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 != (int)line.npos)
|
||||
{
|
||||
if (line.substr(0, pos) == "frames_per_row")
|
||||
{
|
||||
frames_per_row = std::stoi(line.substr(pos + 1, line.length()));
|
||||
}
|
||||
|
||||
else if (line.substr(0, pos) == "frame_width")
|
||||
{
|
||||
frame_width = std::stoi(line.substr(pos + 1, line.length()));
|
||||
}
|
||||
|
||||
else if (line.substr(0, pos) == "frame_height")
|
||||
{
|
||||
frame_height = std::stoi(line.substr(pos + 1, line.length()));
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
std::cout << "Warning: unknown parameter " << line.substr(0, pos).c_str() << std::endl;
|
||||
success = false;
|
||||
}
|
||||
|
||||
// Normaliza valores
|
||||
if (frames_per_row == 0 && frame_width > 0)
|
||||
{
|
||||
frames_per_row = texture_->getWidth() / frame_width;
|
||||
}
|
||||
|
||||
if (max_tiles == 0 && frame_width > 0 && frame_height > 0)
|
||||
{
|
||||
const int w = texture_->getWidth() / frame_width;
|
||||
const int h = texture_->getHeight() / frame_height;
|
||||
max_tiles = w * h;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Una vez procesada la linea, aumenta el indice para pasar a la siguiente
|
||||
index++;
|
||||
}
|
||||
|
||||
// Pone un valor por defecto
|
||||
setWidth(frame_width);
|
||||
setHeight(frame_height);
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
// Establece la animacion actual
|
||||
void AnimatedSprite::setCurrentAnimation(const std::string &name)
|
||||
{
|
||||
@@ -235,7 +376,7 @@ void AnimatedSprite::resetAnimation()
|
||||
}
|
||||
|
||||
// Carga la animación desde un fichero
|
||||
std::vector<Animation> AnimatedSprite::loadFromFile(std::string file_path)
|
||||
std::vector<Animation> AnimatedSprite::loadFromFile(const std::string &file_path)
|
||||
{
|
||||
// Inicializa variables
|
||||
std::vector<Animation> animations;
|
||||
|
||||
Reference in New Issue
Block a user