forked from jaildesigner-jailgames/jaildoctors_dilemma
Trabajando en la precarga de animaciones
This commit is contained in:
@@ -44,7 +44,7 @@ flip=true
|
|||||||
[enemy]
|
[enemy]
|
||||||
tileset=tuno.png
|
tileset=tuno.png
|
||||||
animation=tuno.ani
|
animation=tuno.ani
|
||||||
width=8
|
width=16
|
||||||
height=16
|
height=16
|
||||||
x=21
|
x=21
|
||||||
y=2
|
y=2
|
||||||
@@ -60,7 +60,7 @@ color=red
|
|||||||
[enemy]
|
[enemy]
|
||||||
tileset=tuno.png
|
tileset=tuno.png
|
||||||
animation=tuno.ani
|
animation=tuno.ani
|
||||||
width=8
|
width=16
|
||||||
height=16
|
height=16
|
||||||
x=28
|
x=28
|
||||||
y=8
|
y=8
|
||||||
|
|||||||
@@ -1,5 +1,148 @@
|
|||||||
#include "animatedsprite.h"
|
#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
|
// Constructor
|
||||||
AnimatedSprite::AnimatedSprite(Texture *texture, SDL_Renderer *renderer, std::string file, std::vector<std::string> *buffer)
|
AnimatedSprite::AnimatedSprite(Texture *texture, SDL_Renderer *renderer, std::string file, std::vector<std::string> *buffer)
|
||||||
{
|
{
|
||||||
@@ -30,11 +173,11 @@ AnimatedSprite::AnimatedSprite(Texture *texture, SDL_Renderer *renderer, std::st
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Constructor
|
// Constructor
|
||||||
AnimatedSprite::AnimatedSprite(animatedSprite_t as)
|
AnimatedSprite::AnimatedSprite(SDL_Renderer *renderer, animatedSprite_t as)
|
||||||
{
|
{
|
||||||
// Copia los punteros
|
// Copia los punteros
|
||||||
setTexture(as.texture);
|
setTexture(as.texture);
|
||||||
setRenderer(as.renderer);
|
setRenderer(renderer);
|
||||||
|
|
||||||
// Inicializa variables
|
// Inicializa variables
|
||||||
currentAnimation = 0;
|
currentAnimation = 0;
|
||||||
@@ -336,7 +479,6 @@ animatedSprite_t AnimatedSprite::loadFromFile(std::string filePath)
|
|||||||
|
|
||||||
// Añade los punteros
|
// Añade los punteros
|
||||||
as.texture = texture;
|
as.texture = texture;
|
||||||
as.renderer = renderer;
|
|
||||||
|
|
||||||
return as;
|
return as;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,9 +26,11 @@ struct animatedSprite_t
|
|||||||
{
|
{
|
||||||
std::vector<t_animation> animations; // Vector con las diferentes animaciones
|
std::vector<t_animation> animations; // Vector con las diferentes animaciones
|
||||||
Texture *texture; // Textura con los graficos para el sprite
|
Texture *texture; // Textura con los graficos para el sprite
|
||||||
SDL_Renderer *renderer; // Renderizador para el sprite
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Carga la animación desde un fichero
|
||||||
|
animatedSprite_t loadFromFile(Texture *texture, std::string filePath);
|
||||||
|
|
||||||
class AnimatedSprite : public MovingSprite
|
class AnimatedSprite : public MovingSprite
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
@@ -39,7 +41,7 @@ private:
|
|||||||
public:
|
public:
|
||||||
// Constructor
|
// Constructor
|
||||||
AnimatedSprite(Texture *texture = nullptr, SDL_Renderer *renderer = nullptr, std::string file = "", std::vector<std::string> *buffer = nullptr);
|
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
|
// Destructor
|
||||||
~AnimatedSprite();
|
~AnimatedSprite();
|
||||||
|
|||||||
@@ -213,11 +213,17 @@ void Director::loadResources(section_t section)
|
|||||||
|
|
||||||
else if (section.name == SECTION_PROG_CREDITS)
|
else if (section.name == SECTION_PROG_CREDITS)
|
||||||
{
|
{
|
||||||
|
// Texturas
|
||||||
std::vector<std::string> textureList;
|
std::vector<std::string> textureList;
|
||||||
textureList.push_back("shine.png");
|
textureList.push_back("shine.png");
|
||||||
textureList.push_back("smb2.png");
|
textureList.push_back("smb2.png");
|
||||||
|
|
||||||
resource->loadTextures(textureList);
|
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)
|
else if (section.name == SECTION_PROG_GAME || section.name == SECTION_PROG_DEMO)
|
||||||
|
|||||||
Reference in New Issue
Block a user