diff --git a/source/text.cpp b/source/text.cpp index f138f60..b78067f 100644 --- a/source/text.cpp +++ b/source/text.cpp @@ -12,6 +12,7 @@ #include "sprite.h" // Para Sprite #include "texture.h" // Para Texture #include "utils.h" // Para getFileName, printWithDots +#include "resource_helper.h" // Para ResourceHelper // Constructor Text::Text(const std::shared_ptr &texture, const std::string &text_file) { @@ -372,25 +373,41 @@ auto Text::loadFile(const std::string &file_path) -> std::shared_ptr tf->box_height = 0; } - // Abre el fichero para leer los valores - std::ifstream file(file_path); + // Intenta cargar desde ResourceHelper primero + auto resource_data = ResourceHelper::loadFile(file_path); + std::istringstream stream; + bool using_resource_data = false; + + 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(file); - if (file.is_open() && file.good()) { + if ((using_resource_data && stream.good()) || (!using_resource_data && file.is_open() && file.good())) { std::string buffer; // Lee los dos primeros valores del fichero - std::getline(file, buffer); - std::getline(file, buffer); + std::getline(input_stream, buffer); + std::getline(input_stream, buffer); tf->box_width = std::stoi(buffer); - std::getline(file, buffer); - std::getline(file, buffer); + std::getline(input_stream, buffer); + std::getline(input_stream, buffer); tf->box_height = std::stoi(buffer); // lee el resto de datos del fichero auto index = 32; auto line_read = 0; - while (std::getline(file, buffer)) { + while (std::getline(input_stream, buffer)) { // Almacena solo las lineas impares if (line_read % 2 == 1) { tf->offset[index++].w = std::stoi(buffer); @@ -401,9 +418,11 @@ auto Text::loadFile(const std::string &file_path) -> std::shared_ptr line_read++; }; - // Cierra el fichero + // Cierra el fichero si se usó printWithDots("Text File : ", getFileName(file_path), "[ LOADED ]"); - file.close(); + if (!using_resource_data && file.is_open()) { + file.close(); + } } // El fichero no se puede abrir