Trabajando en la precarga de animaciones

This commit is contained in:
2022-10-27 14:04:30 +02:00
parent 9d9d9d92f2
commit 6186d1fac1
4 changed files with 159 additions and 9 deletions

View File

@@ -44,7 +44,7 @@ flip=true
[enemy]
tileset=tuno.png
animation=tuno.ani
width=8
width=16
height=16
x=21
y=2
@@ -60,7 +60,7 @@ color=red
[enemy]
tileset=tuno.png
animation=tuno.ani
width=8
width=16
height=16
x=28
y=8

View File

@@ -1,5 +1,148 @@
#include "animatedsprite.h"
// Carga la animación desde un fichero
animatedSprite_t loadFromFile(Texture *texture, std::string filePath)
{
// Inicializa variables
animatedSprite_t as;
as.texture = texture;
int framesPerRow = 0;
int frameWidth = 0;
int frameHeight = 0;
int maxTiles = 0;
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())
{
// Procesa el fichero linea a linea
std::cout << "Animation loaded: " << filename << std::endl;
while (std::getline(file, line))
{
// Si la linea contiene el texto [animation] se realiza el proceso de carga de una animación
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 != (int)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")
{
buffer.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, frameWidth, frameHeight};
while (getline(ss, 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);
}
}
else
{
printf("Warning: file %s, unknown parameter \"%s\"\n", filename.c_str(), line.substr(0, pos).c_str());
}
}
} while (line != "[/animation]");
// Añade la animación al vector de animaciones
as.animations.push_back(buffer);
// 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 != (int)line.npos)
{
if (line.substr(0, pos) == "framesPerRow")
{
framesPerRow = std::stoi(line.substr(pos + 1, line.length()));
}
else if (line.substr(0, pos) == "frameWidth")
{
frameWidth = std::stoi(line.substr(pos + 1, line.length()));
}
else if (line.substr(0, pos) == "frameHeight")
{
frameHeight = 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());
}
// 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;
}
}
}
}
// Cierra el fichero
file.close();
}
// El fichero no se puede abrir
else
{
printf("Warning: Unable to open %s file\n", filename.c_str());
}
// Pone un valor por defecto
//setRect({0, 0, frameWidth, frameHeight});
return as;
}
// Constructor
AnimatedSprite::AnimatedSprite(Texture *texture, SDL_Renderer *renderer, std::string file, std::vector<std::string> *buffer)
{
@@ -12,7 +155,7 @@ AnimatedSprite::AnimatedSprite(Texture *texture, SDL_Renderer *renderer, std::st
if (file != "")
{
as = loadFromFile(file);
// Copia los datos de las animaciones
for (auto animation : as.animations)
{
@@ -30,11 +173,11 @@ AnimatedSprite::AnimatedSprite(Texture *texture, SDL_Renderer *renderer, std::st
}
// Constructor
AnimatedSprite::AnimatedSprite(animatedSprite_t as)
AnimatedSprite::AnimatedSprite(SDL_Renderer *renderer, animatedSprite_t as)
{
// Copia los punteros
setTexture(as.texture);
setRenderer(as.renderer);
setRenderer(renderer);
// Inicializa variables
currentAnimation = 0;
@@ -336,7 +479,6 @@ animatedSprite_t AnimatedSprite::loadFromFile(std::string filePath)
// Añade los punteros
as.texture = texture;
as.renderer = renderer;
return as;
}

View File

@@ -25,10 +25,12 @@ struct t_animation
struct animatedSprite_t
{
std::vector<t_animation> animations; // Vector con las diferentes animaciones
Texture *texture; // Textura con los graficos para el sprite
SDL_Renderer *renderer; // Renderizador para el sprite
Texture *texture; // Textura con los graficos para el sprite
};
// Carga la animación desde un fichero
animatedSprite_t loadFromFile(Texture *texture, std::string filePath);
class AnimatedSprite : public MovingSprite
{
private:
@@ -39,7 +41,7 @@ private:
public:
// Constructor
AnimatedSprite(Texture *texture = nullptr, SDL_Renderer *renderer = nullptr, std::string file = "", std::vector<std::string> *buffer = nullptr);
AnimatedSprite(animatedSprite_t as);
AnimatedSprite(SDL_Renderer *renderer, animatedSprite_t as);
// Destructor
~AnimatedSprite();

View File

@@ -213,11 +213,17 @@ void Director::loadResources(section_t section)
else if (section.name == SECTION_PROG_CREDITS)
{
// Texturas
std::vector<std::string> textureList;
textureList.push_back("shine.png");
textureList.push_back("smb2.png");
resource->loadTextures(textureList);
std::vector<std::string> animationList;
animationList.push_back("shine.ani");
resource->loadAnimations(animationList);
}
else if (section.name == SECTION_PROG_GAME || section.name == SECTION_PROG_DEMO)