Precarga de los ficheros .txt con los offsets del texto

This commit is contained in:
2022-10-29 11:17:33 +02:00
parent c1f0f90b16
commit 0758cf9de1
14 changed files with 203 additions and 36 deletions

View File

@@ -3,17 +3,97 @@
#include <iostream>
#include <fstream>
// Constructor
Text::Text(std::string bitmapFile, std::string textFile, Resource *resource, SDL_Renderer *renderer)
// Llena una estructuta textFile_t desde un fichero
textFile_t LoadTextFile(std::string file)
{
// Copia punteros
this->resource = resource;
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
printf("Text loaded: %s\n", filename.c_str());
rfile.close();
}
// El fichero no se puede abrir
else
{
printf("Warning: Unable to open %s file\n", filename.c_str());
}
// 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;
}
return tf;
}
// Constructor
Text::Text(std::string textFile, Texture *texture, SDL_Renderer *renderer)
{
// Carga los offsets desde el fichero
initOffsetFromFile(textFile);
// Crea los objetos
texture = resource->getTexture(bitmapFile);
sprite = new Sprite({0, 0, boxWidth, boxHeight}, texture, renderer);
}
// Constructor
Text::Text(textFile_t *textFile, Texture *texture, SDL_Renderer *renderer)
{
// Inicializa variables desde la estructura
boxHeight = textFile->boxHeight;
boxWidth = textFile->boxWidth;
for (int i = 0; i < 128; ++i)
{
offset[i].x = textFile->offset[i].x;
offset[i].y = textFile->offset[i].y;
offset[i].w = textFile->offset[i].w;
}
// Crea los objetos
sprite = new Sprite({0, 0, boxWidth, boxHeight}, texture, renderer);
}
@@ -190,5 +270,5 @@ int Text::getCharacterSize()
// Recarga la textura
void Text::reLoadTexture()
{
texture->reLoad();
sprite->getTexture()->reLoad();
}