arreglos d'estil en source/core/resources
This commit is contained in:
@@ -7,10 +7,10 @@
|
||||
#include <stdexcept> // Para runtime_error
|
||||
#include <utility>
|
||||
|
||||
#include "external/fkyaml_node.hpp" // Para fkyaml::node
|
||||
#include "core/rendering/surface.hpp" // Para Surface
|
||||
#include "core/resources/resource_cache.hpp" // Para Resource
|
||||
#include "core/resources/resource_helper.hpp" // Para ResourceHelper
|
||||
#include "external/fkyaml_node.hpp" // Para fkyaml::node
|
||||
#include "utils/utils.hpp" // Para printWithDots
|
||||
|
||||
// Helper: Convierte un nodo YAML de frames (array) a vector de SDL_FRect
|
||||
@@ -105,8 +105,7 @@ auto SurfaceAnimatedSprite::loadAnimationsFromYAML(const std::string& file_path,
|
||||
frame_width,
|
||||
frame_height,
|
||||
frames_per_row,
|
||||
max_tiles
|
||||
);
|
||||
max_tiles);
|
||||
}
|
||||
|
||||
animations.push_back(animation);
|
||||
@@ -125,7 +124,7 @@ auto SurfaceAnimatedSprite::loadAnimationsFromYAML(const std::string& file_path,
|
||||
}
|
||||
|
||||
// Constructor con bytes YAML del cache (parsing lazy)
|
||||
SurfaceAnimatedSprite::SurfaceAnimatedSprite(const ResourceAnimation& cached_data) {
|
||||
SurfaceAnimatedSprite::SurfaceAnimatedSprite(const AnimationResource& cached_data) {
|
||||
// Parsear YAML desde los bytes cargados en cache
|
||||
std::string yaml_content(cached_data.yaml_data.begin(), cached_data.yaml_data.end());
|
||||
|
||||
@@ -190,8 +189,7 @@ SurfaceAnimatedSprite::SurfaceAnimatedSprite(const ResourceAnimation& cached_dat
|
||||
frame_width,
|
||||
frame_height,
|
||||
frames_per_row,
|
||||
max_tiles
|
||||
);
|
||||
max_tiles);
|
||||
}
|
||||
|
||||
animations_.push_back(animation);
|
||||
@@ -216,7 +214,6 @@ SurfaceAnimatedSprite::SurfaceAnimatedSprite(const ResourceAnimation& cached_dat
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Obtiene el indice de la animación a partir del nombre
|
||||
auto SurfaceAnimatedSprite::getIndex(const std::string& name) -> int {
|
||||
auto index = -1;
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
#include "core/rendering/surface_moving_sprite.hpp" // Para SMovingSprite
|
||||
|
||||
class Surface;
|
||||
struct ResourceAnimation; // Forward declaration
|
||||
struct AnimationResource; // Forward declaration
|
||||
|
||||
class SurfaceAnimatedSprite : public SurfaceMovingSprite {
|
||||
public:
|
||||
@@ -31,7 +31,7 @@ class SurfaceAnimatedSprite : public SurfaceMovingSprite {
|
||||
static auto loadAnimationsFromYAML(const std::string& file_path, std::shared_ptr<Surface>& surface, float& frame_width, float& frame_height) -> std::vector<AnimationData>; // Carga las animaciones desde fichero YAML
|
||||
|
||||
// Constructores
|
||||
explicit SurfaceAnimatedSprite(const ResourceAnimation& cached_data); // Constructor con datos pre-cargados del cache
|
||||
explicit SurfaceAnimatedSprite(const AnimationResource& cached_data); // Constructor con datos pre-cargados del cache
|
||||
|
||||
~SurfaceAnimatedSprite() override = default; // Destructor
|
||||
|
||||
|
||||
@@ -148,7 +148,7 @@ auto Cache::getText(const std::string& name) -> std::shared_ptr<Text> {
|
||||
}
|
||||
|
||||
// Obtiene los datos de animación parseados a partir de un nombre
|
||||
auto Cache::getAnimationData(const std::string& name) -> const ResourceAnimation& {
|
||||
auto Cache::getAnimationData(const std::string& name) -> const AnimationResource& {
|
||||
auto it = std::ranges::find_if(animations_, [&name](const auto& a) { return a.name == name; });
|
||||
|
||||
if (it != animations_.end()) {
|
||||
@@ -172,7 +172,7 @@ auto Cache::getRoom(const std::string& name) -> std::shared_ptr<Room::Data> {
|
||||
}
|
||||
|
||||
// Obtiene todas las habitaciones
|
||||
auto Cache::getRooms() -> std::vector<ResourceRoom>& {
|
||||
auto Cache::getRooms() -> std::vector<RoomResource>& {
|
||||
return rooms_;
|
||||
}
|
||||
|
||||
@@ -197,7 +197,7 @@ void Cache::loadSounds() {
|
||||
sound = JA_LoadSound(l.c_str());
|
||||
}
|
||||
|
||||
sounds_.emplace_back(name, sound);
|
||||
sounds_.emplace_back(SoundResource{name, sound});
|
||||
printWithDots("Sound : ", name, "[ LOADED ]");
|
||||
updateLoadingProgress();
|
||||
}
|
||||
@@ -224,7 +224,7 @@ void Cache::loadMusics() {
|
||||
music = JA_LoadMusic(l.c_str());
|
||||
}
|
||||
|
||||
musics_.emplace_back(name, music);
|
||||
musics_.emplace_back(MusicResource{name, music});
|
||||
printWithDots("Music : ", name, "[ LOADED ]");
|
||||
updateLoadingProgress(1);
|
||||
}
|
||||
@@ -238,7 +238,7 @@ void Cache::loadSurfaces() {
|
||||
|
||||
for (const auto& l : list) {
|
||||
auto name = getFileName(l);
|
||||
surfaces_.emplace_back(name, std::make_shared<Surface>(l));
|
||||
surfaces_.emplace_back(SurfaceResource{name, std::make_shared<Surface>(l)});
|
||||
surfaces_.back().surface->setTransparentColor(0);
|
||||
updateLoadingProgress();
|
||||
}
|
||||
@@ -261,7 +261,7 @@ void Cache::loadPalettes() {
|
||||
|
||||
for (const auto& l : list) {
|
||||
auto name = getFileName(l);
|
||||
palettes_.emplace_back(name, readPalFile(l));
|
||||
palettes_.emplace_back(ResourcePalette{name, readPalFile(l)});
|
||||
updateLoadingProgress();
|
||||
}
|
||||
}
|
||||
@@ -274,7 +274,7 @@ void Cache::loadTextFiles() {
|
||||
|
||||
for (const auto& l : list) {
|
||||
auto name = getFileName(l);
|
||||
text_files_.emplace_back(name, Text::loadTextFile(l));
|
||||
text_files_.emplace_back(TextFileResource{name, Text::loadTextFile(l)});
|
||||
updateLoadingProgress();
|
||||
}
|
||||
}
|
||||
@@ -291,7 +291,7 @@ void Cache::loadAnimations() {
|
||||
// Cargar bytes del archivo YAML sin parsear (carga lazy)
|
||||
auto yaml_bytes = Helper::loadFile(l);
|
||||
|
||||
animations_.emplace_back(name, yaml_bytes);
|
||||
animations_.emplace_back(AnimationResource{name, yaml_bytes});
|
||||
printWithDots("Animation : ", name, "[ LOADED ]");
|
||||
updateLoadingProgress();
|
||||
}
|
||||
@@ -305,7 +305,7 @@ void Cache::loadRooms() {
|
||||
|
||||
for (const auto& l : list) {
|
||||
auto name = getFileName(l);
|
||||
rooms_.emplace_back(name, std::make_shared<Room::Data>(Room::loadYAML(l)));
|
||||
rooms_.emplace_back(RoomResource{name, std::make_shared<Room::Data>(Room::loadYAML(l))});
|
||||
printWithDots("Room : ", name, "[ LOADED ]");
|
||||
updateLoadingProgress();
|
||||
}
|
||||
@@ -313,15 +313,9 @@ void Cache::loadRooms() {
|
||||
|
||||
void Cache::createText() {
|
||||
struct ResourceInfo {
|
||||
std::string key; // Identificador del recurso
|
||||
std::string texture_file; // Nombre del archivo de textura
|
||||
std::string text_file; // Nombre del archivo de texto
|
||||
|
||||
// Constructor para facilitar la creación de objetos ResourceInfo
|
||||
ResourceInfo(std::string k, std::string t_file, std::string txt_file)
|
||||
: key(std::move(k)),
|
||||
texture_file(std::move(t_file)),
|
||||
text_file(std::move(txt_file)) {}
|
||||
std::string key{}; // Identificador del recurso
|
||||
std::string texture_file{}; // Nombre del archivo de textura
|
||||
std::string text_file{}; // Nombre del archivo de texto
|
||||
};
|
||||
|
||||
std::cout << "\n>> CREATING TEXT_OBJECTS" << '\n';
|
||||
@@ -334,7 +328,7 @@ void Cache::createText() {
|
||||
{"8bithud", "8bithud.gif", "8bithud.txt"}};
|
||||
|
||||
for (const auto& res_info : resources) {
|
||||
texts_.emplace_back(res_info.key, std::make_shared<Text>(getSurface(res_info.texture_file), getTextFile(res_info.text_file)));
|
||||
texts_.emplace_back(TextResource{res_info.key, std::make_shared<Text>(getSurface(res_info.texture_file), getTextFile(res_info.text_file))});
|
||||
printWithDots("Text : ", res_info.key, "[ DONE ]");
|
||||
}
|
||||
}
|
||||
@@ -374,13 +368,13 @@ void Cache::calculateTotal() {
|
||||
List::Type::ANIMATION,
|
||||
List::Type::ROOM};
|
||||
|
||||
size_t total = 0;
|
||||
int total = 0;
|
||||
for (const auto& asset_type : asset_types) {
|
||||
auto list = List::get()->getListByType(asset_type);
|
||||
total += list.size();
|
||||
}
|
||||
|
||||
count_ = ResourceCount(total, 0);
|
||||
count_ = ResourceCount{total, 0};
|
||||
}
|
||||
|
||||
// Muestra el progreso de carga
|
||||
|
||||
@@ -13,107 +13,78 @@ struct JA_Music_t; // lines 11-11
|
||||
struct JA_Sound_t; // lines 12-12
|
||||
|
||||
// Estructura para almacenar ficheros de sonido y su nombre
|
||||
struct ResourceSound {
|
||||
std::string name; // Nombre del sonido
|
||||
JA_Sound_t* sound; // Objeto con el sonido
|
||||
|
||||
// Constructor
|
||||
ResourceSound(std::string name, JA_Sound_t* sound)
|
||||
: name(std::move(name)),
|
||||
sound(sound) {}
|
||||
struct SoundResource {
|
||||
std::string name{}; // Nombre del sonido
|
||||
JA_Sound_t* sound{nullptr}; // Objeto con el sonido
|
||||
};
|
||||
|
||||
// Estructura para almacenar ficheros musicales y su nombre
|
||||
struct ResourceMusic {
|
||||
std::string name; // Nombre de la musica
|
||||
JA_Music_t* music; // Objeto con la música
|
||||
|
||||
// Constructor
|
||||
ResourceMusic(std::string name, JA_Music_t* music)
|
||||
: name(std::move(name)),
|
||||
music(music) {}
|
||||
struct MusicResource {
|
||||
std::string name{}; // Nombre de la musica
|
||||
JA_Music_t* music{nullptr}; // Objeto con la música
|
||||
};
|
||||
|
||||
// Estructura para almacenar objetos Surface y su nombre
|
||||
struct ResourceSurface {
|
||||
std::string name; // Nombre de la surface
|
||||
std::shared_ptr<Surface> surface; // Objeto con la surface
|
||||
|
||||
// Constructor
|
||||
ResourceSurface(std::string name, std::shared_ptr<Surface> surface)
|
||||
: name(std::move(name)),
|
||||
surface(std::move(std::move(surface))) {}
|
||||
struct SurfaceResource {
|
||||
std::string name{}; // Nombre de la surface
|
||||
std::shared_ptr<Surface> surface{}; // Objeto con la surface
|
||||
};
|
||||
|
||||
// Estructura para almacenar objetos Palette y su nombre
|
||||
struct ResourcePalette {
|
||||
std::string name; // Nombre de la surface
|
||||
Palette palette; // Paleta
|
||||
|
||||
// Constructor
|
||||
ResourcePalette(std::string name, const Palette& palette)
|
||||
: name(std::move(name)),
|
||||
palette(palette) {}
|
||||
std::string name{}; // Nombre de la surface
|
||||
Palette palette{}; // Paleta
|
||||
};
|
||||
|
||||
// Estructura para almacenar ficheros TextFile y su nombre
|
||||
struct ResourceTextFile {
|
||||
std::string name; // Nombre del fichero
|
||||
std::shared_ptr<Text::File> text_file; // Objeto con los descriptores de la fuente de texto
|
||||
|
||||
// Constructor
|
||||
ResourceTextFile(std::string name, std::shared_ptr<Text::File> text_file)
|
||||
: name(std::move(name)),
|
||||
text_file(std::move(std::move(text_file))) {}
|
||||
struct TextFileResource {
|
||||
std::string name{}; // Nombre del fichero
|
||||
std::shared_ptr<Text::File> text_file{}; // Objeto con los descriptores de la fuente de texto
|
||||
};
|
||||
|
||||
// Estructura para almacenar objetos Text y su nombre
|
||||
struct ResourceText {
|
||||
std::string name; // Nombre del objeto
|
||||
std::shared_ptr<Text> text; // Objeto
|
||||
|
||||
// Constructor
|
||||
ResourceText(std::string name, std::shared_ptr<Text> text)
|
||||
: name(std::move(name)),
|
||||
text(std::move(std::move(text))) {}
|
||||
struct TextResource {
|
||||
std::string name{}; // Nombre del objeto
|
||||
std::shared_ptr<Text> text{}; // Objeto
|
||||
};
|
||||
|
||||
// Estructura para almacenar ficheros animaciones y su nombre
|
||||
struct ResourceAnimation {
|
||||
std::string name; // Nombre del fichero
|
||||
std::vector<uint8_t> yaml_data; // Bytes del archivo YAML sin parsear
|
||||
|
||||
// Constructor
|
||||
ResourceAnimation(std::string name, std::vector<uint8_t> yaml_data)
|
||||
: name(std::move(name)),
|
||||
yaml_data(std::move(yaml_data)) {}
|
||||
struct AnimationResource {
|
||||
std::string name{}; // Nombre del fichero
|
||||
std::vector<uint8_t> yaml_data{}; // Bytes del archivo YAML sin parsear
|
||||
};
|
||||
|
||||
// Estructura para almacenar habitaciones y su nombre
|
||||
struct ResourceRoom {
|
||||
std::string name; // Nombre de la habitación
|
||||
std::shared_ptr<Room::Data> room; // Habitación
|
||||
|
||||
// Constructor
|
||||
ResourceRoom(std::string name, std::shared_ptr<Room::Data> room)
|
||||
: name(std::move(name)),
|
||||
room(std::move(std::move(room))) {}
|
||||
struct RoomResource {
|
||||
std::string name{}; // Nombre de la habitación
|
||||
std::shared_ptr<Room::Data> room{}; // Habitación
|
||||
};
|
||||
|
||||
// Estructura para llevar la cuenta de los recursos cargados
|
||||
struct ResourceCount {
|
||||
int total; // Número total de recursos
|
||||
int loaded; // Número de recursos cargados
|
||||
namespace Resource {
|
||||
|
||||
// Constructor
|
||||
ResourceCount()
|
||||
: total(0),
|
||||
loaded(0) {}
|
||||
class Cache {
|
||||
public:
|
||||
static void init(); // Inicialización singleton
|
||||
static void destroy(); // Destrucción singleton
|
||||
static auto get() -> Cache*; // Acceso al singleton
|
||||
|
||||
// Constructor
|
||||
ResourceCount(int total, int loaded)
|
||||
: total(total),
|
||||
loaded(loaded) {}
|
||||
auto getSound(const std::string& name) -> JA_Sound_t*; // Getters de recursos
|
||||
auto getMusic(const std::string& name) -> JA_Music_t*;
|
||||
auto getSurface(const std::string& name) -> std::shared_ptr<Surface>;
|
||||
auto getPalette(const std::string& name) -> Palette;
|
||||
auto getTextFile(const std::string& name) -> std::shared_ptr<Text::File>;
|
||||
auto getText(const std::string& name) -> std::shared_ptr<Text>;
|
||||
auto getAnimationData(const std::string& name) -> const AnimationResource&;
|
||||
auto getRoom(const std::string& name) -> std::shared_ptr<Room::Data>;
|
||||
auto getRooms() -> std::vector<RoomResource>&;
|
||||
|
||||
void reload(); // Recarga todos los recursos
|
||||
|
||||
private:
|
||||
// Estructura para llevar la cuenta de los recursos cargados
|
||||
struct ResourceCount {
|
||||
int total{0}; // Número total de recursos
|
||||
int loaded{0}; // Número de recursos cargados
|
||||
|
||||
// Añade una cantidad a los recursos cargados
|
||||
void add(int amount) {
|
||||
@@ -124,122 +95,49 @@ struct ResourceCount {
|
||||
[[nodiscard]] auto getPercentage() const -> float {
|
||||
return static_cast<float>(loaded) / static_cast<float>(total);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
namespace Resource {
|
||||
|
||||
class Cache {
|
||||
private:
|
||||
// [SINGLETON] Objeto cache privado para Don Melitón
|
||||
static Cache* cache;
|
||||
|
||||
std::vector<ResourceSound> sounds_; // Vector con los sonidos
|
||||
std::vector<ResourceMusic> musics_; // Vector con las musicas
|
||||
std::vector<ResourceSurface> surfaces_; // Vector con las surfaces
|
||||
std::vector<ResourcePalette> palettes_; // Vector con las paletas
|
||||
std::vector<ResourceTextFile> text_files_; // Vector con los ficheros de texto
|
||||
std::vector<ResourceText> texts_; // Vector con los objetos de texto
|
||||
std::vector<ResourceAnimation> animations_; // Vector con las animaciones
|
||||
std::vector<ResourceRoom> rooms_; // Vector con las habitaciones
|
||||
|
||||
ResourceCount count_; // Contador de recursos
|
||||
std::shared_ptr<Text> loading_text_; // Texto para la pantalla de carga
|
||||
|
||||
// Carga los sonidos
|
||||
// Métodos de carga de recursos
|
||||
void loadSounds();
|
||||
|
||||
// Carga las musicas
|
||||
void loadMusics();
|
||||
|
||||
// Carga las surfaces
|
||||
void loadSurfaces();
|
||||
|
||||
// Carga las paletas
|
||||
void loadPalettes();
|
||||
|
||||
// Carga los ficheros de texto
|
||||
void loadTextFiles();
|
||||
|
||||
// Carga las animaciones
|
||||
void loadAnimations();
|
||||
|
||||
// Carga las habitaciones
|
||||
void loadRooms();
|
||||
|
||||
// Crea los objetos de texto
|
||||
void createText();
|
||||
|
||||
// Vacia todos los vectores de recursos
|
||||
// Métodos de limpieza
|
||||
void clear();
|
||||
|
||||
// Carga todos los recursos
|
||||
void load();
|
||||
|
||||
// Vacía el vector de sonidos
|
||||
void clearSounds();
|
||||
|
||||
// Vacía el vector de musicas
|
||||
void clearMusics();
|
||||
|
||||
// Calcula el numero de recursos para cargar
|
||||
// Métodos de gestión de carga
|
||||
void load();
|
||||
void calculateTotal();
|
||||
|
||||
// Muestra el progreso de carga
|
||||
void renderProgress();
|
||||
|
||||
// Comprueba los eventosstatic
|
||||
static void checkEvents();
|
||||
|
||||
// Actualiza el progreso de carga
|
||||
void updateLoadingProgress(int steps = 5);
|
||||
|
||||
// [SINGLETON] Ahora el constructor y el destructor son privados, para no poder crear objetos cache desde fuera
|
||||
|
||||
// Constructor
|
||||
// Constructor y destructor
|
||||
Cache();
|
||||
|
||||
// Destructor
|
||||
~Cache() = default;
|
||||
|
||||
public:
|
||||
// [SINGLETON] Crearemos el objeto cache con esta función estática
|
||||
static void init();
|
||||
// Singleton instance
|
||||
static Cache* cache;
|
||||
|
||||
// [SINGLETON] Destruiremos el objeto cache con esta función estática
|
||||
static void destroy();
|
||||
// Variables miembro
|
||||
std::vector<SoundResource> sounds_{}; // Vector con los sonidos
|
||||
std::vector<MusicResource> musics_{}; // Vector con las musicas
|
||||
std::vector<SurfaceResource> surfaces_{}; // Vector con las surfaces
|
||||
std::vector<ResourcePalette> palettes_{}; // Vector con las paletas
|
||||
std::vector<TextFileResource> text_files_{}; // Vector con los ficheros de texto
|
||||
std::vector<TextResource> texts_{}; // Vector con los objetos de texto
|
||||
std::vector<AnimationResource> animations_{}; // Vector con las animaciones
|
||||
std::vector<RoomResource> rooms_{}; // Vector con las habitaciones
|
||||
|
||||
// [SINGLETON] Con este método obtenemos el objeto cache y podemos trabajar con él
|
||||
static auto get() -> Cache*;
|
||||
|
||||
// Obtiene el sonido a partir de un nombre
|
||||
auto getSound(const std::string& name) -> JA_Sound_t*;
|
||||
|
||||
// Obtiene la música a partir de un nombre
|
||||
auto getMusic(const std::string& name) -> JA_Music_t*;
|
||||
|
||||
// Obtiene la surface a partir de un nombre
|
||||
auto getSurface(const std::string& name) -> std::shared_ptr<Surface>;
|
||||
|
||||
// Obtiene la paleta a partir de un nombre
|
||||
auto getPalette(const std::string& name) -> Palette;
|
||||
|
||||
// Obtiene el fichero de texto a partir de un nombre
|
||||
auto getTextFile(const std::string& name) -> std::shared_ptr<Text::File>;
|
||||
|
||||
// Obtiene el objeto de texto a partir de un nombre
|
||||
auto getText(const std::string& name) -> std::shared_ptr<Text>;
|
||||
|
||||
// Obtiene los bytes YAML de animación a partir de un nombre (parsing lazy)
|
||||
auto getAnimationData(const std::string& name) -> const ResourceAnimation&;
|
||||
|
||||
// Obtiene la habitación a partir de un nombre
|
||||
auto getRoom(const std::string& name) -> std::shared_ptr<Room::Data>;
|
||||
|
||||
// Obtiene todas las habitaciones
|
||||
auto getRooms() -> std::vector<ResourceRoom>&;
|
||||
|
||||
// Recarga todos los recursos
|
||||
void reload();
|
||||
ResourceCount count_{}; // Contador de recursos
|
||||
std::shared_ptr<Text> loading_text_{}; // Texto para la pantalla de carga
|
||||
};
|
||||
|
||||
} // namespace Resource
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
// resource_helper.hpp
|
||||
// Helper functions for resource loading (bridge to pack system)
|
||||
|
||||
#ifndef RESOURCE_HELPER_HPP
|
||||
#define RESOURCE_HELPER_HPP
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
@@ -37,5 +36,3 @@ auto shouldUseResourcePack(const std::string& filepath) -> bool;
|
||||
auto isPackLoaded() -> bool;
|
||||
|
||||
} // namespace Resource::Helper
|
||||
|
||||
#endif // RESOURCE_HELPER_HPP
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
// resource_loader.hpp
|
||||
// Singleton resource loader for managing pack and filesystem access
|
||||
|
||||
#ifndef RESOURCE_LOADER_HPP
|
||||
#define RESOURCE_LOADER_HPP
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
@@ -15,35 +14,21 @@ namespace Resource {
|
||||
// Singleton class for loading resources from pack or filesystem
|
||||
class Loader {
|
||||
public:
|
||||
// Get singleton instance
|
||||
static auto get() -> Loader&;
|
||||
static auto get() -> Loader&; // Singleton instance access
|
||||
|
||||
// Initialize with a pack file (optional)
|
||||
auto initialize(const std::string& pack_file, bool enable_fallback = true) -> bool;
|
||||
auto initialize(const std::string& pack_file, bool enable_fallback = true) -> bool; // Initialize loader with pack file
|
||||
|
||||
// Load a resource (tries pack first, then filesystem if fallback enabled)
|
||||
auto loadResource(const std::string& filename) -> std::vector<uint8_t>;
|
||||
auto loadResource(const std::string& filename) -> std::vector<uint8_t>; // Load resource data
|
||||
auto resourceExists(const std::string& filename) -> bool; // Check resource availability
|
||||
|
||||
// Check if a resource exists
|
||||
auto resourceExists(const std::string& filename) -> bool;
|
||||
|
||||
// Check if pack is loaded
|
||||
[[nodiscard]] auto isPackLoaded() const -> bool;
|
||||
|
||||
// Get pack statistics
|
||||
[[nodiscard]] auto isPackLoaded() const -> bool; // Pack status queries
|
||||
[[nodiscard]] auto getPackResourceCount() const -> size_t;
|
||||
[[nodiscard]] auto validatePack() const -> bool; // Validate pack integrity
|
||||
[[nodiscard]] auto loadAssetsConfig() const -> std::string; // Load assets.yaml from pack
|
||||
|
||||
// Validate pack integrity (checksum)
|
||||
[[nodiscard]] auto validatePack() const -> bool;
|
||||
void shutdown(); // Cleanup
|
||||
|
||||
// Load assets.yaml from pack (for release builds)
|
||||
[[nodiscard]] auto loadAssetsConfig() const -> std::string;
|
||||
|
||||
// Cleanup
|
||||
void shutdown();
|
||||
|
||||
// Disable copy/move
|
||||
Loader(const Loader&) = delete;
|
||||
Loader(const Loader&) = delete; // Deleted copy/move constructors
|
||||
auto operator=(const Loader&) -> Loader& = delete;
|
||||
Loader(Loader&&) = delete;
|
||||
auto operator=(Loader&&) -> Loader& = delete;
|
||||
@@ -52,18 +37,12 @@ class Loader {
|
||||
Loader() = default;
|
||||
~Loader() = default;
|
||||
|
||||
// Load from filesystem
|
||||
static auto loadFromFilesystem(const std::string& filepath) -> std::vector<uint8_t>;
|
||||
|
||||
// Check if file exists on filesystem
|
||||
static auto loadFromFilesystem(const std::string& filepath) -> std::vector<uint8_t>; // Filesystem helpers
|
||||
static auto fileExistsOnFilesystem(const std::string& filepath) -> bool;
|
||||
|
||||
// Member data
|
||||
std::unique_ptr<Pack> resource_pack_;
|
||||
std::unique_ptr<Pack> resource_pack_; // Member variables
|
||||
bool fallback_to_files_{true};
|
||||
bool initialized_{false};
|
||||
};
|
||||
|
||||
} // namespace Resource
|
||||
|
||||
#endif // RESOURCE_LOADER_HPP
|
||||
|
||||
@@ -64,11 +64,12 @@ auto Pack::addFile(const std::string& filepath, const std::string& pack_name)
|
||||
return false;
|
||||
}
|
||||
|
||||
ResourceEntry entry;
|
||||
entry.filename = pack_name;
|
||||
entry.offset = data_.size();
|
||||
entry.size = file_data.size();
|
||||
entry.checksum = calculateChecksum(file_data);
|
||||
ResourceEntry entry{
|
||||
.filename = pack_name,
|
||||
.offset = data_.size(),
|
||||
.size = file_data.size(),
|
||||
.checksum = calculateChecksum(file_data)
|
||||
};
|
||||
|
||||
// Append file data to the data block
|
||||
data_.insert(data_.end(), file_data.begin(), file_data.end());
|
||||
@@ -200,7 +201,7 @@ auto Pack::loadPack(const std::string& pack_file) -> bool {
|
||||
file.read(filename.data(), name_len);
|
||||
|
||||
// Read entry data
|
||||
ResourceEntry entry;
|
||||
ResourceEntry entry{};
|
||||
entry.filename = filename;
|
||||
file.read(reinterpret_cast<char*>(&entry.offset), sizeof(entry.offset));
|
||||
file.read(reinterpret_cast<char*>(&entry.size), sizeof(entry.size));
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
// resource_pack.hpp
|
||||
// Resource pack file format and management for JailDoctor's Dilemma
|
||||
// Based on Coffee Crisis Arcade Edition resource pack system
|
||||
|
||||
#ifndef RESOURCE_PACK_HPP
|
||||
#define RESOURCE_PACK_HPP
|
||||
#pragma once
|
||||
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
@@ -15,10 +13,10 @@ namespace Resource {
|
||||
|
||||
// Entry metadata for each resource in the pack
|
||||
struct ResourceEntry {
|
||||
std::string filename; // Relative path within pack
|
||||
uint64_t offset; // Byte offset in data block
|
||||
uint64_t size; // Size in bytes
|
||||
uint32_t checksum; // CRC32 checksum for verification
|
||||
std::string filename{}; // Relative path within pack
|
||||
uint64_t offset{0}; // Byte offset in data block
|
||||
uint64_t size{0}; // Size in bytes
|
||||
uint32_t checksum{0}; // CRC32 checksum for verification
|
||||
};
|
||||
|
||||
// Resource pack file format
|
||||
@@ -30,65 +28,41 @@ class Pack {
|
||||
Pack() = default;
|
||||
~Pack() = default;
|
||||
|
||||
// Disable copy/move
|
||||
Pack(const Pack&) = delete;
|
||||
Pack(const Pack&) = delete; // Deleted copy/move constructors
|
||||
auto operator=(const Pack&) -> Pack& = delete;
|
||||
Pack(Pack&&) = delete;
|
||||
auto operator=(Pack&&) -> Pack& = delete;
|
||||
|
||||
// Add a single file to the pack
|
||||
auto addFile(const std::string& filepath, const std::string& pack_name) -> bool;
|
||||
auto addFile(const std::string& filepath, const std::string& pack_name) -> bool; // Building packs
|
||||
auto addDirectory(const std::string& dir_path, const std::string& base_path = "") -> bool;
|
||||
|
||||
// Add all files from a directory recursively
|
||||
auto addDirectory(const std::string& dir_path, const std::string& base_path = "")
|
||||
-> bool;
|
||||
|
||||
// Save the pack to a file
|
||||
auto savePack(const std::string& pack_file) -> bool;
|
||||
|
||||
// Load a pack from a file
|
||||
auto savePack(const std::string& pack_file) -> bool; // Pack I/O
|
||||
auto loadPack(const std::string& pack_file) -> bool;
|
||||
|
||||
// Get a resource by name
|
||||
auto getResource(const std::string& filename) -> std::vector<uint8_t>;
|
||||
|
||||
// Check if a resource exists
|
||||
auto getResource(const std::string& filename) -> std::vector<uint8_t>; // Resource access
|
||||
auto hasResource(const std::string& filename) const -> bool;
|
||||
|
||||
// Get list of all resources
|
||||
auto getResourceList() const -> std::vector<std::string>;
|
||||
|
||||
// Check if pack is loaded
|
||||
auto isLoaded() const -> bool { return loaded_; }
|
||||
|
||||
// Get pack statistics
|
||||
auto isLoaded() const -> bool { return loaded_; } // Status queries
|
||||
auto getResourceCount() const -> size_t { return resources_.size(); }
|
||||
auto getDataSize() const -> size_t { return data_.size(); }
|
||||
|
||||
// Calculate overall pack checksum (for validation)
|
||||
auto calculatePackChecksum() const -> uint32_t;
|
||||
auto calculatePackChecksum() const -> uint32_t; // Validation
|
||||
|
||||
private:
|
||||
static constexpr std::array<char, 4> MAGIC_HEADER = {'J', 'D', 'D', 'I'};
|
||||
static constexpr std::array<char, 4> MAGIC_HEADER = {'J', 'D', 'D', 'I'}; // Pack format constants
|
||||
static constexpr uint32_t VERSION = 1;
|
||||
static constexpr const char* DEFAULT_ENCRYPT_KEY = "JDDI_RESOURCES_2024";
|
||||
|
||||
// Calculate CRC32 checksum
|
||||
static auto calculateChecksum(const std::vector<uint8_t>& data) -> uint32_t;
|
||||
static auto calculateChecksum(const std::vector<uint8_t>& data) -> uint32_t; // Utility methods
|
||||
|
||||
// XOR encryption/decryption
|
||||
static void encryptData(std::vector<uint8_t>& data, const std::string& key);
|
||||
static void encryptData(std::vector<uint8_t>& data, const std::string& key); // Encryption/decryption
|
||||
static void decryptData(std::vector<uint8_t>& data, const std::string& key);
|
||||
|
||||
// Read file from disk
|
||||
static auto readFile(const std::string& filepath) -> std::vector<uint8_t>;
|
||||
static auto readFile(const std::string& filepath) -> std::vector<uint8_t>; // File I/O
|
||||
|
||||
// Member data
|
||||
std::unordered_map<std::string, ResourceEntry> resources_;
|
||||
std::vector<uint8_t> data_; // Encrypted data block
|
||||
std::unordered_map<std::string, ResourceEntry> resources_{}; // Member variables
|
||||
std::vector<uint8_t> data_{}; // Encrypted data block
|
||||
bool loaded_{false};
|
||||
};
|
||||
|
||||
} // namespace Resource
|
||||
|
||||
#endif // RESOURCE_PACK_HPP
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
/home/sergio/gitea/jaildoctors_dilemma/source/utils/utils.hpp:8:1: error: syntax error [syntaxError]
|
||||
enum class PaletteColor : Uint8 {
|
||||
^
|
||||
source/core/resources/resource_pack.cpp:19:18: style: Consider using std::accumulate algorithm instead of a raw loop. [useStlAlgorithm]
|
||||
checksum = ((checksum << 5) + checksum) + byte;
|
||||
^
|
||||
|
||||
Reference in New Issue
Block a user