resource.pack
This commit is contained in:
@@ -6,128 +6,108 @@
|
||||
|
||||
#include "texture.h" // for Texture
|
||||
|
||||
// Carga la animación desde un fichero
|
||||
animatedSprite_t loadAnimationFromFile(Texture *texture, std::string filePath, bool verbose) {
|
||||
// Inicializa variables
|
||||
// Parser compartido: lee un istream con el formato .ani
|
||||
static animatedSprite_t parseAnimationStream(std::istream &file, Texture *texture, const std::string &filename, bool verbose) {
|
||||
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
|
||||
if (verbose) {
|
||||
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]") {
|
||||
animation_t buffer;
|
||||
buffer.counter = 0;
|
||||
buffer.currentFrame = 0;
|
||||
buffer.completed = false;
|
||||
if (verbose) {
|
||||
std::cout << "Animation loaded: " << filename << std::endl;
|
||||
}
|
||||
while (std::getline(file, line)) {
|
||||
if (line == "[animation]") {
|
||||
animation_t 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 {
|
||||
std::cout << "Warning: file " << filename.c_str() << "\n, unknown parameter \"" << line.substr(0, pos).c_str() << "\"" << std::endl;
|
||||
}
|
||||
}
|
||||
} while (line != "[/animation]");
|
||||
|
||||
// Añade la animación al vector de animaciones
|
||||
as.animations.push_back(buffer);
|
||||
}
|
||||
|
||||
// En caso contrario se parsea el fichero para buscar las variables y los valores
|
||||
else {
|
||||
// Encuentra la posición del caracter '='
|
||||
do {
|
||||
std::getline(file, line);
|
||||
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 {
|
||||
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") {
|
||||
std::stringstream ss(line.substr(pos + 1, line.length()));
|
||||
std::string tmp;
|
||||
SDL_Rect rect = {0, 0, frameWidth, frameHeight};
|
||||
while (getline(ss, tmp, ',')) {
|
||||
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 {
|
||||
std::cout << "Warning: file " << filename.c_str() << "\n, unknown parameter \"" << line.substr(0, pos).c_str() << "\"" << std::endl;
|
||||
}
|
||||
}
|
||||
} while (line != "[/animation]");
|
||||
|
||||
// Normaliza valores
|
||||
if (framesPerRow == 0 && frameWidth > 0) {
|
||||
framesPerRow = texture->getWidth() / frameWidth;
|
||||
}
|
||||
as.animations.push_back(buffer);
|
||||
} else {
|
||||
int pos = line.find("=");
|
||||
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 {
|
||||
std::cout << "Warning: file " << filename.c_str() << "\n, unknown parameter \"" << line.substr(0, pos).c_str() << "\"" << std::endl;
|
||||
}
|
||||
|
||||
if (maxTiles == 0 && frameWidth > 0 && frameHeight > 0) {
|
||||
const int w = texture->getWidth() / frameWidth;
|
||||
const int h = texture->getHeight() / frameHeight;
|
||||
maxTiles = w * h;
|
||||
}
|
||||
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 {
|
||||
if (verbose) {
|
||||
std::cout << "Warning: Unable to open " << filename.c_str() << " file" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
return as;
|
||||
}
|
||||
|
||||
// Carga la animación desde un fichero
|
||||
animatedSprite_t loadAnimationFromFile(Texture *texture, std::string filePath, bool verbose) {
|
||||
const std::string filename = filePath.substr(filePath.find_last_of("\\/") + 1);
|
||||
std::ifstream file(filePath);
|
||||
if (!file.good()) {
|
||||
if (verbose) {
|
||||
std::cout << "Warning: Unable to open " << filename.c_str() << " file" << std::endl;
|
||||
}
|
||||
animatedSprite_t as;
|
||||
as.texture = texture;
|
||||
return as;
|
||||
}
|
||||
return parseAnimationStream(file, texture, filename, verbose);
|
||||
}
|
||||
|
||||
// Carga la animación desde bytes en memoria
|
||||
animatedSprite_t loadAnimationFromMemory(Texture *texture, const std::vector<uint8_t> &bytes, const std::string &nameForLogs, bool verbose) {
|
||||
if (bytes.empty()) {
|
||||
animatedSprite_t as;
|
||||
as.texture = texture;
|
||||
return as;
|
||||
}
|
||||
std::string content(reinterpret_cast<const char *>(bytes.data()), bytes.size());
|
||||
std::stringstream ss(content);
|
||||
return parseAnimationStream(ss, texture, nameForLogs, verbose);
|
||||
}
|
||||
|
||||
// Constructor
|
||||
AnimatedSprite::AnimatedSprite(Texture *texture, SDL_Renderer *renderer, std::string file, std::vector<std::string> *buffer) {
|
||||
// Copia los punteros
|
||||
|
||||
Reference in New Issue
Block a user