migrat Assets a la ultima versió (fitxers no hardcoded)

This commit is contained in:
2025-10-31 12:52:22 +01:00
parent 2f20ac422e
commit 70bfced50d
7 changed files with 804 additions and 396 deletions

View File

@@ -1,80 +1,74 @@
#pragma once
#include <string> // para string, basic_string
#include <utility>
#include <vector> // para vector
#include <cstdint> // Para uint8_t
#include <string> // Para string
#include <unordered_map> // Para unordered_map
#include <utility> // Para move
#include <vector> // Para vector
#include "utils/utils.hpp"
enum class AssetType : int {
DATA,
BITMAP,
ANIMATION,
MUSIC,
SOUND,
FONT,
ROOM,
TILEMAP,
PALETTE,
MAX_ASSET_TYPE
};
// Clase Asset
// --- Clase Asset: gestor optimizado de recursos (singleton) ---
class Asset {
public:
// --- Enums ---
enum class Type : int {
DATA, // Datos
BITMAP, // Imágenes
ANIMATION, // Animaciones
MUSIC, // Música
SOUND, // Sonidos
FONT, // Fuentes
ROOM, // Datos de habitación (.room)
TILEMAP, // Tilemaps (.tmx)
PALETTE, // Paletas
SIZE, // Tamaño (para iteración)
};
// --- Métodos de singleton ---
static void init(const std::string& executable_path);
static void destroy();
static auto get() -> Asset*;
Asset(const Asset&) = delete;
auto operator=(const Asset&) -> Asset& = delete;
// --- Métodos para la gestión de recursos ---
void add(const std::string& file_path, Type type, bool required = true, bool absolute = false);
void loadFromFile(const std::string& config_file_path, const std::string& prefix = "", const std::string& system_folder = ""); // Con soporte para variables
[[nodiscard]] auto get(const std::string& filename) const -> std::string; // Obtiene la ruta completa
[[nodiscard]] auto loadData(const std::string& filename) const -> std::vector<uint8_t>; // Carga datos del archivo
[[nodiscard]] auto check() const -> bool;
[[nodiscard]] auto getListByType(Type type) const -> std::vector<std::string>;
[[nodiscard]] auto exists(const std::string& filename) const -> bool; // Verifica si un asset existe
private:
// [SINGLETON] Objeto asset privado para Don Melitón
static Asset* asset;
// --- Estructuras privadas ---
struct Item {
std::string file; // Ruta completa del archivo
Type type; // Tipo de recurso
bool required; // Indica si el archivo es obligatorio
// Estructura para definir un item
struct AssetItem {
std::string file; // Ruta del fichero desde la raíz del directorio
AssetType type; // Indica el tipo de recurso
bool required; // Indica si es un fichero que debe de existir
// Constructor
AssetItem(std::string file_path, AssetType asset_type, bool is_required)
: file(std::move(file_path)),
Item(std::string path, Type asset_type, bool is_required)
: file(std::move(path)),
type(asset_type),
required(is_required) {}
};
// Variables
int longest_name_ = 0; // Contiene la longitud del nombre de fichero mas largo
std::vector<AssetItem> file_list_; // Listado con todas las rutas a los ficheros
std::string executable_path_; // Ruta al ejecutable
// --- Variables internas ---
std::unordered_map<std::string, Item> file_list_; // Mapa para búsqueda O(1)
std::string executable_path_; // Ruta del ejecutable
// Comprueba que existe un fichero
static auto checkFile(const std::string& path) -> bool;
// --- Métodos internos ---
[[nodiscard]] auto checkFile(const std::string& path) const -> bool; // Verifica si un archivo existe
[[nodiscard]] static auto getTypeName(Type type) -> std::string; // Obtiene el nombre del tipo
[[nodiscard]] static auto parseAssetType(const std::string& type_str) -> Type; // Convierte string a tipo
void addToMap(const std::string& file_path, Type type, bool required, bool absolute); // Añade archivo al mapa
[[nodiscard]] static auto replaceVariables(const std::string& path, const std::string& prefix, const std::string& system_folder) -> std::string; // Reemplaza variables en la ruta
static auto parseOptions(const std::string& options, bool& required, bool& absolute) -> void; // Parsea opciones
// Devuelve el nombre del tipo de recurso
static auto getTypeName(AssetType type) -> std::string;
// --- Constructores y destructor privados (singleton) ---
explicit Asset(std::string executable_path) // Constructor privado
: executable_path_(std::move(executable_path)) {}
~Asset() = default; // Destructor privado
// Constructor
explicit Asset(const std::string& executable_path)
: executable_path_(getPath(executable_path)) {}
// Destructor
~Asset() = default;
public:
// [SINGLETON] Crearemos el objeto con esta función estática
static void init(const std::string& executable_path);
// [SINGLETON] Destruiremos el objeto con esta función estática
static void destroy();
// [SINGLETON] Con este método obtenemos el objeto y podemos trabajar con él
static auto get() -> Asset*;
// Añade un elemento a la lista
void add(const std::string& file, AssetType type, bool required = true, bool absolute = false);
// Devuelve la ruta completa a un fichero a partir de una cadena
[[nodiscard]] auto get(const std::string& text) const -> std::string;
// Comprueba que existen todos los elementos
[[nodiscard]] auto check() const -> bool;
// Devuelve la lista de recursos de un tipo
[[nodiscard]] auto getListByType(AssetType type) const -> std::vector<std::string>;
};
// --- Instancia singleton ---
static Asset* instance; // Instancia única de Asset
};