Precàrrega de tots els recursos al inici del joc

8.000.000 de cherrypickings que he anat fent pel codi
This commit is contained in:
2024-10-20 11:06:10 +02:00
parent f23dcae5b6
commit a4b4e188cd
32 changed files with 532 additions and 364 deletions

View File

@@ -2,23 +2,24 @@
#include <SDL2/SDL_rect.h> // for SDL_Rect
#include <fstream> // for basic_ostream, basic_ifstream, basic_istream
#include <iostream> // for cout
#include "resource.h" // for Resource
#include "sprite.h" // for Sprite
#include "texture.h" // for Texture
#include "utils.h" // for Color
// Llena una estructuta TextFile desde un fichero
TextFile LoadTextFile(std::string file_path)
std::shared_ptr<TextFile> loadTextFile(const std::string &file_path)
{
TextFile tf;
auto tf = std::make_shared<TextFile>();
// 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;
tf.box_width = 0;
tf.box_height = 0;
tf->offset[i].x = 0;
tf->offset[i].y = 0;
tf->offset[i].w = 0;
tf->box_width = 0;
tf->box_height = 0;
}
// Abre el fichero para leer los valores
@@ -32,11 +33,11 @@ TextFile LoadTextFile(std::string file_path)
// Lee los dos primeros valores del fichero
std::getline(file, buffer);
std::getline(file, buffer);
tf.box_width = std::stoi(buffer);
tf->box_width = std::stoi(buffer);
std::getline(file, buffer);
std::getline(file, buffer);
tf.box_height = std::stoi(buffer);
tf->box_height = std::stoi(buffer);
// lee el resto de datos del fichero
auto index = 32;
@@ -46,7 +47,7 @@ TextFile LoadTextFile(std::string file_path)
// Almacena solo las lineas impares
if (line_read % 2 == 1)
{
tf.offset[index++].w = std::stoi(buffer);
tf->offset[index++].w = std::stoi(buffer);
}
// Limpia el buffer
@@ -55,21 +56,22 @@ TextFile LoadTextFile(std::string file_path)
};
// Cierra el fichero
std::cout << "Text loaded: " << file_name << std::endl;
printWithDots("Text File : ", file_name, "[ LOADED ]");
file.close();
}
// El fichero no se puede abrir
else
{
std::cout << "Warning: Unable to open " << file_name << " file" << std::endl;
std::cerr << "Error: Fichero no encontrado " << file_path << std::endl;
throw std::runtime_error("Fichero no encontrado: " + file_path);
}
// 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.box_width;
tf.offset[i].y = ((i - 32) / 15) * tf.box_height;
tf->offset[i].x = ((i - 32) % 15) * tf->box_width;
tf->offset[i].y = ((i - 32) / 15) * tf->box_height;
}
return tf;
@@ -79,16 +81,16 @@ TextFile LoadTextFile(std::string file_path)
Text::Text(const std::string &bitmap_file, const std::string &text_file, SDL_Renderer *renderer)
{
// Carga los offsets desde el fichero
auto tf = LoadTextFile(text_file);
auto tf = loadTextFile(text_file);
// Inicializa variables desde la estructura
box_height_ = tf.box_height;
box_width_ = tf.box_width;
box_height_ = tf->box_height;
box_width_ = tf->box_width;
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;
offset_[i].x = tf->offset[i].x;
offset_[i].y = tf->offset[i].y;
offset_[i].w = tf->offset[i].w;
}
// Crea los objetos
@@ -100,19 +102,20 @@ Text::Text(const std::string &bitmap_file, const std::string &text_file, SDL_Ren
}
// Constructor
Text::Text(const std::string &text_file, std::shared_ptr<Texture> texture)
Text::Text(std::shared_ptr<Texture> texture, const std::string &text_file)
: texture_(texture)
{
// Carga los offsets desde el fichero
auto tf = LoadTextFile(text_file);
auto tf = loadTextFile(text_file);
// Inicializa variables desde la estructura
box_height_ = tf.box_height;
box_width_ = tf.box_width;
box_height_ = tf->box_height;
box_width_ = tf->box_width;
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;
offset_[i].x = tf->offset[i].x;
offset_[i].y = tf->offset[i].y;
offset_[i].w = tf->offset[i].w;
}
// Crea los objetos
@@ -123,7 +126,8 @@ Text::Text(const std::string &text_file, std::shared_ptr<Texture> texture)
}
// Constructor
Text::Text(TextFile *text_file, std::shared_ptr<Texture> texture)
Text::Text(std::shared_ptr<Texture> texture, std::shared_ptr<TextFile> text_file)
: texture_(texture)
{
// Inicializa variables desde la estructura
box_height_ = text_file->box_height;