neteja tidy a source/core i encamina Texture::loadFromFile pel ResourceHelper
This commit is contained in:
@@ -7,7 +7,7 @@
|
||||
#include "core/rendering/texture.h" // for Texture
|
||||
|
||||
// Parser compartido: lee un istream con el formato .ani
|
||||
static animatedSprite_t parseAnimationStream(std::istream &file, Texture *texture, const std::string &filename, bool verbose) {
|
||||
static auto parseAnimationStream(std::istream &file, Texture *texture, const std::string &filename, bool verbose) -> animatedSprite_t {
|
||||
animatedSprite_t as;
|
||||
as.texture = texture;
|
||||
int framesPerRow = 0;
|
||||
@@ -17,13 +17,15 @@ static animatedSprite_t parseAnimationStream(std::istream &file, Texture *textur
|
||||
std::string line;
|
||||
|
||||
if (verbose) {
|
||||
std::cout << "Animation loaded: " << filename << std::endl;
|
||||
std::cout << "Animation loaded: " << filename << '\n';
|
||||
}
|
||||
// Normalitza CRLF: fitxers .ani amb terminadors de Windows fan que
|
||||
// line == "[animation]" no faci match i el parser entri en bucle
|
||||
// infinit / no carregui cap animació.
|
||||
auto strip_cr = [](std::string &s) {
|
||||
if (!s.empty() && s.back() == '\r') s.pop_back();
|
||||
if (!s.empty() && s.back() == '\r') {
|
||||
s.pop_back();
|
||||
}
|
||||
};
|
||||
|
||||
while (std::getline(file, line)) {
|
||||
@@ -37,10 +39,12 @@ static animatedSprite_t parseAnimationStream(std::istream &file, Texture *textur
|
||||
buffer.completed = false;
|
||||
|
||||
do {
|
||||
if (!std::getline(file, line)) break;
|
||||
if (!std::getline(file, line)) {
|
||||
break;
|
||||
}
|
||||
strip_cr(line);
|
||||
int pos = line.find("=");
|
||||
if (pos != (int)line.npos) {
|
||||
int pos = line.find('=');
|
||||
if (pos != (int)std::string::npos) {
|
||||
if (line.substr(0, pos) == "name") {
|
||||
buffer.name = line.substr(pos + 1, line.length());
|
||||
} else if (line.substr(0, pos) == "speed") {
|
||||
@@ -58,15 +62,15 @@ static animatedSprite_t parseAnimationStream(std::istream &file, Texture *textur
|
||||
buffer.frames.push_back(rect);
|
||||
}
|
||||
} else {
|
||||
std::cout << "Warning: file " << filename.c_str() << "\n, unknown parameter \"" << line.substr(0, pos).c_str() << "\"" << std::endl;
|
||||
std::cout << "Warning: file " << filename.c_str() << "\n, unknown parameter \"" << line.substr(0, pos).c_str() << "\"" << '\n';
|
||||
}
|
||||
}
|
||||
} while (line != "[/animation]");
|
||||
|
||||
as.animations.push_back(buffer);
|
||||
} else {
|
||||
int pos = line.find("=");
|
||||
if (pos != (int)line.npos) {
|
||||
int pos = line.find('=');
|
||||
if (pos != (int)std::string::npos) {
|
||||
if (line.substr(0, pos) == "framesPerRow") {
|
||||
framesPerRow = std::stoi(line.substr(pos + 1, line.length()));
|
||||
} else if (line.substr(0, pos) == "frameWidth") {
|
||||
@@ -74,7 +78,7 @@ static animatedSprite_t parseAnimationStream(std::istream &file, Texture *textur
|
||||
} 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;
|
||||
std::cout << "Warning: file " << filename.c_str() << "\n, unknown parameter \"" << line.substr(0, pos).c_str() << "\"" << '\n';
|
||||
}
|
||||
|
||||
if (framesPerRow == 0 && frameWidth > 0) {
|
||||
@@ -93,12 +97,12 @@ static animatedSprite_t parseAnimationStream(std::istream &file, Texture *textur
|
||||
}
|
||||
|
||||
// Carga la animación desde un fichero
|
||||
animatedSprite_t loadAnimationFromFile(Texture *texture, const std::string &filePath, bool verbose) {
|
||||
auto loadAnimationFromFile(Texture *texture, const std::string &filePath, bool verbose) -> animatedSprite_t {
|
||||
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;
|
||||
std::cout << "Warning: Unable to open " << filename.c_str() << " file" << '\n';
|
||||
}
|
||||
animatedSprite_t as;
|
||||
as.texture = texture;
|
||||
@@ -108,7 +112,7 @@ animatedSprite_t loadAnimationFromFile(Texture *texture, const std::string &file
|
||||
}
|
||||
|
||||
// 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) {
|
||||
auto loadAnimationFromMemory(Texture *texture, const std::vector<uint8_t> &bytes, const std::string &nameForLogs, bool verbose) -> animatedSprite_t {
|
||||
if (bytes.empty()) {
|
||||
animatedSprite_t as;
|
||||
as.texture = texture;
|
||||
@@ -134,7 +138,7 @@ AnimatedSprite::AnimatedSprite(Texture *texture, SDL_Renderer *renderer, const s
|
||||
animation.insert(animation.end(), as.animations.begin(), as.animations.end());
|
||||
}
|
||||
|
||||
else if (buffer) {
|
||||
else if (buffer != nullptr) {
|
||||
loadFromVector(buffer);
|
||||
}
|
||||
}
|
||||
@@ -159,7 +163,7 @@ AnimatedSprite::~AnimatedSprite() {
|
||||
}
|
||||
|
||||
// Obtiene el indice de la animación a partir del nombre
|
||||
int AnimatedSprite::getIndex(const std::string &name) {
|
||||
auto AnimatedSprite::getIndex(const std::string &name) -> int {
|
||||
int index = -1;
|
||||
|
||||
for (const auto &a : animation) {
|
||||
@@ -169,7 +173,7 @@ int AnimatedSprite::getIndex(const std::string &name) {
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << "** Warning: could not find \"" << name.c_str() << "\" animation" << std::endl;
|
||||
std::cout << "** Warning: could not find \"" << name.c_str() << "\" animation" << '\n';
|
||||
|
||||
return -1;
|
||||
}
|
||||
@@ -205,7 +209,7 @@ void AnimatedSprite::animate() {
|
||||
}
|
||||
|
||||
// Obtiene el numero de frames de la animación actual
|
||||
int AnimatedSprite::getNumFrames() {
|
||||
auto AnimatedSprite::getNumFrames() -> int {
|
||||
return (int)animation[currentAnimation].frames.size();
|
||||
}
|
||||
|
||||
@@ -260,22 +264,22 @@ void AnimatedSprite::setAnimationCompleted(int index, bool value) {
|
||||
}
|
||||
|
||||
// Comprueba si ha terminado la animación
|
||||
bool AnimatedSprite::animationIsCompleted() {
|
||||
auto AnimatedSprite::animationIsCompleted() -> bool {
|
||||
return animation[currentAnimation].completed;
|
||||
}
|
||||
|
||||
// Devuelve el rectangulo de una animación y frame concreto
|
||||
SDL_Rect AnimatedSprite::getAnimationClip(const std::string &name, Uint8 index) {
|
||||
auto AnimatedSprite::getAnimationClip(const std::string &name, Uint8 index) -> SDL_Rect {
|
||||
return animation[getIndex(name)].frames[index];
|
||||
}
|
||||
|
||||
// Devuelve el rectangulo de una animación y frame concreto
|
||||
SDL_Rect AnimatedSprite::getAnimationClip(int indexA, Uint8 indexF) {
|
||||
auto AnimatedSprite::getAnimationClip(int indexA, Uint8 indexF) -> SDL_Rect {
|
||||
return animation[indexA].frames[indexF];
|
||||
}
|
||||
|
||||
// Carga la animación desde un vector
|
||||
bool AnimatedSprite::loadFromVector(std::vector<std::string> *source) {
|
||||
auto AnimatedSprite::loadFromVector(std::vector<std::string> *source) -> bool {
|
||||
// Inicializa variables
|
||||
int framesPerRow = 0;
|
||||
int frameWidth = 0;
|
||||
@@ -307,10 +311,10 @@ bool AnimatedSprite::loadFromVector(std::vector<std::string> *source) {
|
||||
line = source->at(index);
|
||||
|
||||
// Encuentra la posición del caracter '='
|
||||
int pos = line.find("=");
|
||||
int pos = line.find('=');
|
||||
|
||||
// Procesa las dos subcadenas
|
||||
if (pos != (int)line.npos) {
|
||||
if (pos != (int)std::string::npos) {
|
||||
if (line.substr(0, pos) == "name") {
|
||||
buffer.name = line.substr(pos + 1, line.length());
|
||||
}
|
||||
@@ -338,7 +342,7 @@ bool AnimatedSprite::loadFromVector(std::vector<std::string> *source) {
|
||||
}
|
||||
|
||||
else {
|
||||
std::cout << "Warning: unknown parameter " << line.substr(0, pos).c_str() << std::endl;
|
||||
std::cout << "Warning: unknown parameter " << line.substr(0, pos).c_str() << '\n';
|
||||
success = false;
|
||||
}
|
||||
}
|
||||
@@ -351,10 +355,10 @@ bool AnimatedSprite::loadFromVector(std::vector<std::string> *source) {
|
||||
// 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("=");
|
||||
int pos = line.find('=');
|
||||
|
||||
// Procesa las dos subcadenas
|
||||
if (pos != (int)line.npos) {
|
||||
if (pos != (int)std::string::npos) {
|
||||
if (line.substr(0, pos) == "framesPerRow") {
|
||||
framesPerRow = std::stoi(line.substr(pos + 1, line.length()));
|
||||
}
|
||||
@@ -368,7 +372,7 @@ bool AnimatedSprite::loadFromVector(std::vector<std::string> *source) {
|
||||
}
|
||||
|
||||
else {
|
||||
std::cout << "Warning: unknown parameter " << line.substr(0, pos).c_str() << std::endl;
|
||||
std::cout << "Warning: unknown parameter " << line.substr(0, pos).c_str() << '\n';
|
||||
success = false;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user