resource.pack
This commit is contained in:
110
source/text.cpp
110
source/text.cpp
@@ -3,72 +3,82 @@
|
||||
|
||||
#include <fstream> // for char_traits, basic_ostream, basic_ifstream, ope...
|
||||
#include <iostream> // for cout
|
||||
#include <sstream>
|
||||
|
||||
#include "sprite.h" // for Sprite
|
||||
#include "texture.h" // for Texture
|
||||
#include "utils.h" // for color_t
|
||||
|
||||
// Parser compartido: rellena un textFile_t desde cualquier istream
|
||||
static void parseTextFileStream(std::istream &rfile, textFile_t &tf) {
|
||||
std::string buffer;
|
||||
std::getline(rfile, buffer);
|
||||
std::getline(rfile, buffer);
|
||||
tf.boxWidth = std::stoi(buffer);
|
||||
|
||||
std::getline(rfile, buffer);
|
||||
std::getline(rfile, buffer);
|
||||
tf.boxHeight = std::stoi(buffer);
|
||||
|
||||
int index = 32;
|
||||
int line_read = 0;
|
||||
while (std::getline(rfile, buffer)) {
|
||||
if (line_read % 2 == 1) {
|
||||
tf.offset[index++].w = std::stoi(buffer);
|
||||
}
|
||||
buffer.clear();
|
||||
line_read++;
|
||||
}
|
||||
}
|
||||
|
||||
static void computeTextFileOffsets(textFile_t &tf) {
|
||||
for (int i = 32; i < 128; ++i) {
|
||||
tf.offset[i].x = ((i - 32) % 15) * tf.boxWidth;
|
||||
tf.offset[i].y = ((i - 32) / 15) * tf.boxHeight;
|
||||
}
|
||||
}
|
||||
|
||||
// Llena una estructuta textFile_t desde un fichero
|
||||
textFile_t LoadTextFile(std::string file, bool verbose) {
|
||||
textFile_t tf;
|
||||
|
||||
// Inicializa a cero el vector con las coordenadas
|
||||
for (int i = 0; i < 128; ++i) {
|
||||
tf.offset[i].x = 0;
|
||||
tf.offset[i].y = 0;
|
||||
tf.offset[i].w = 0;
|
||||
}
|
||||
|
||||
// Abre el fichero para leer los valores
|
||||
const std::string filename = file.substr(file.find_last_of("\\/") + 1).c_str();
|
||||
std::ifstream rfile(file);
|
||||
|
||||
if (rfile.is_open() && rfile.good()) {
|
||||
std::string buffer;
|
||||
|
||||
// Lee los dos primeros valores del fichero
|
||||
std::getline(rfile, buffer);
|
||||
std::getline(rfile, buffer);
|
||||
tf.boxWidth = std::stoi(buffer);
|
||||
|
||||
std::getline(rfile, buffer);
|
||||
std::getline(rfile, buffer);
|
||||
tf.boxHeight = std::stoi(buffer);
|
||||
|
||||
// lee el resto de datos del fichero
|
||||
int index = 32;
|
||||
int line_read = 0;
|
||||
while (std::getline(rfile, buffer)) {
|
||||
// Almacena solo las lineas impares
|
||||
if (line_read % 2 == 1) {
|
||||
tf.offset[index++].w = std::stoi(buffer);
|
||||
}
|
||||
|
||||
// Limpia el buffer
|
||||
buffer.clear();
|
||||
line_read++;
|
||||
};
|
||||
|
||||
// Cierra el fichero
|
||||
parseTextFileStream(rfile, tf);
|
||||
if (verbose) {
|
||||
std::cout << "Text loaded: " << filename.c_str() << std::endl;
|
||||
}
|
||||
rfile.close();
|
||||
} else if (verbose) {
|
||||
std::cout << "Warning: Unable to open " << filename.c_str() << " file" << std::endl;
|
||||
}
|
||||
|
||||
// El fichero no se puede abrir
|
||||
else {
|
||||
computeTextFileOffsets(tf);
|
||||
return tf;
|
||||
}
|
||||
|
||||
// Llena una estructura textFile_t desde bytes en memoria
|
||||
textFile_t LoadTextFileFromMemory(const std::vector<uint8_t> &bytes, bool verbose) {
|
||||
textFile_t tf;
|
||||
for (int i = 0; i < 128; ++i) {
|
||||
tf.offset[i].x = 0;
|
||||
tf.offset[i].y = 0;
|
||||
tf.offset[i].w = 0;
|
||||
}
|
||||
if (!bytes.empty()) {
|
||||
std::string content(reinterpret_cast<const char *>(bytes.data()), bytes.size());
|
||||
std::stringstream ss(content);
|
||||
parseTextFileStream(ss, tf);
|
||||
if (verbose) {
|
||||
std::cout << "Warning: Unable to open " << filename.c_str() << " file" << std::endl;
|
||||
std::cout << "Text loaded from memory" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
// Establece las coordenadas para cada caracter ascii de la cadena y su ancho
|
||||
for (int i = 32; i < 128; ++i) {
|
||||
tf.offset[i].x = ((i - 32) % 15) * tf.boxWidth;
|
||||
tf.offset[i].y = ((i - 32) / 15) * tf.boxHeight;
|
||||
}
|
||||
|
||||
computeTextFileOffsets(tf);
|
||||
return tf;
|
||||
}
|
||||
|
||||
@@ -135,6 +145,24 @@ Text::Text(textFile_t *textFile, Texture *texture, SDL_Renderer *renderer) {
|
||||
fixedWidth = false;
|
||||
}
|
||||
|
||||
// Constructor desde bytes
|
||||
Text::Text(const std::vector<uint8_t> &pngBytes, const std::vector<uint8_t> &txtBytes, SDL_Renderer *renderer) {
|
||||
textFile_t tf = LoadTextFileFromMemory(txtBytes);
|
||||
boxHeight = tf.boxHeight;
|
||||
boxWidth = tf.boxWidth;
|
||||
for (int i = 0; i < 128; ++i) {
|
||||
offset[i].x = tf.offset[i].x;
|
||||
offset[i].y = tf.offset[i].y;
|
||||
offset[i].w = tf.offset[i].w;
|
||||
}
|
||||
|
||||
// Crea la textura desde bytes (Text es dueño en este overload)
|
||||
texture = new Texture(renderer, pngBytes);
|
||||
sprite = new Sprite({0, 0, boxWidth, boxHeight}, texture, renderer);
|
||||
|
||||
fixedWidth = false;
|
||||
}
|
||||
|
||||
// Destructor
|
||||
Text::~Text() {
|
||||
delete sprite;
|
||||
|
||||
Reference in New Issue
Block a user