integrat Text amb ResourceHelper
This commit is contained in:
@@ -12,6 +12,7 @@
|
|||||||
#include "sprite.h" // Para Sprite
|
#include "sprite.h" // Para Sprite
|
||||||
#include "texture.h" // Para Texture
|
#include "texture.h" // Para Texture
|
||||||
#include "utils.h" // Para getFileName, printWithDots
|
#include "utils.h" // Para getFileName, printWithDots
|
||||||
|
#include "resource_helper.h" // Para ResourceHelper
|
||||||
|
|
||||||
// Constructor
|
// Constructor
|
||||||
Text::Text(const std::shared_ptr<Texture> &texture, const std::string &text_file) {
|
Text::Text(const std::shared_ptr<Texture> &texture, const std::string &text_file) {
|
||||||
@@ -372,25 +373,41 @@ auto Text::loadFile(const std::string &file_path) -> std::shared_ptr<Text::File>
|
|||||||
tf->box_height = 0;
|
tf->box_height = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Abre el fichero para leer los valores
|
// Intenta cargar desde ResourceHelper primero
|
||||||
std::ifstream file(file_path);
|
auto resource_data = ResourceHelper::loadFile(file_path);
|
||||||
|
std::istringstream stream;
|
||||||
|
bool using_resource_data = false;
|
||||||
|
|
||||||
if (file.is_open() && file.good()) {
|
if (!resource_data.empty()) {
|
||||||
|
std::string content(resource_data.begin(), resource_data.end());
|
||||||
|
stream.str(content);
|
||||||
|
using_resource_data = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fallback a archivo directo
|
||||||
|
std::ifstream file;
|
||||||
|
if (!using_resource_data) {
|
||||||
|
file.open(file_path);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::istream& input_stream = using_resource_data ? stream : static_cast<std::istream&>(file);
|
||||||
|
|
||||||
|
if ((using_resource_data && stream.good()) || (!using_resource_data && file.is_open() && file.good())) {
|
||||||
std::string buffer;
|
std::string buffer;
|
||||||
|
|
||||||
// Lee los dos primeros valores del fichero
|
// Lee los dos primeros valores del fichero
|
||||||
std::getline(file, buffer);
|
std::getline(input_stream, buffer);
|
||||||
std::getline(file, buffer);
|
std::getline(input_stream, buffer);
|
||||||
tf->box_width = std::stoi(buffer);
|
tf->box_width = std::stoi(buffer);
|
||||||
|
|
||||||
std::getline(file, buffer);
|
std::getline(input_stream, buffer);
|
||||||
std::getline(file, buffer);
|
std::getline(input_stream, buffer);
|
||||||
tf->box_height = std::stoi(buffer);
|
tf->box_height = std::stoi(buffer);
|
||||||
|
|
||||||
// lee el resto de datos del fichero
|
// lee el resto de datos del fichero
|
||||||
auto index = 32;
|
auto index = 32;
|
||||||
auto line_read = 0;
|
auto line_read = 0;
|
||||||
while (std::getline(file, buffer)) {
|
while (std::getline(input_stream, buffer)) {
|
||||||
// Almacena solo las lineas impares
|
// Almacena solo las lineas impares
|
||||||
if (line_read % 2 == 1) {
|
if (line_read % 2 == 1) {
|
||||||
tf->offset[index++].w = std::stoi(buffer);
|
tf->offset[index++].w = std::stoi(buffer);
|
||||||
@@ -401,10 +418,12 @@ auto Text::loadFile(const std::string &file_path) -> std::shared_ptr<Text::File>
|
|||||||
line_read++;
|
line_read++;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Cierra el fichero
|
// Cierra el fichero si se usó
|
||||||
printWithDots("Text File : ", getFileName(file_path), "[ LOADED ]");
|
printWithDots("Text File : ", getFileName(file_path), "[ LOADED ]");
|
||||||
|
if (!using_resource_data && file.is_open()) {
|
||||||
file.close();
|
file.close();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// El fichero no se puede abrir
|
// El fichero no se puede abrir
|
||||||
else {
|
else {
|
||||||
|
|||||||
Reference in New Issue
Block a user