Merge branch 'feat/preload-resources': precàrrega completa al boot
This commit is contained in:
@@ -23,6 +23,11 @@ auto loadFile(const std::string& filepath) -> std::vector<uint8_t> {
|
|||||||
return Loader::get().loadResource(normalized);
|
return Loader::get().loadResource(normalized);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Llistar recursos amb un prefix donat
|
||||||
|
auto listResources(const std::string& prefix) -> std::vector<std::string> {
|
||||||
|
return Loader::get().listResources(prefix);
|
||||||
|
}
|
||||||
|
|
||||||
// Comprovar si existeix un file
|
// Comprovar si existeix un file
|
||||||
auto fileExists(const std::string& filepath) -> bool {
|
auto fileExists(const std::string& filepath) -> bool {
|
||||||
std::string normalized = normalizePath(filepath);
|
std::string normalized = normalizePath(filepath);
|
||||||
|
|||||||
@@ -17,6 +17,9 @@ auto initializeResourceSystem(const std::string& pack_file, bool fallback) -> bo
|
|||||||
auto loadFile(const std::string& filepath) -> std::vector<uint8_t>;
|
auto loadFile(const std::string& filepath) -> std::vector<uint8_t>;
|
||||||
auto fileExists(const std::string& filepath) -> bool;
|
auto fileExists(const std::string& filepath) -> bool;
|
||||||
|
|
||||||
|
// Llistat de recursos disponibles amb un prefix (ex. "shapes/", "sounds/").
|
||||||
|
auto listResources(const std::string& prefix) -> std::vector<std::string>;
|
||||||
|
|
||||||
// Normalització de rutes
|
// Normalització de rutes
|
||||||
auto getPackPath(const std::string& asset_path) -> std::string;
|
auto getPackPath(const std::string& asset_path) -> std::string;
|
||||||
auto normalizePath(const std::string& path) -> std::string;
|
auto normalizePath(const std::string& path) -> std::string;
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
#include "resource_loader.hpp"
|
#include "resource_loader.hpp"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
@@ -67,6 +68,42 @@ auto Loader::loadResource(const std::string& filename) -> std::vector<uint8_t> {
|
|||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
auto Loader::listResources(const std::string& prefix) -> std::vector<std::string> {
|
||||||
|
std::vector<std::string> result;
|
||||||
|
|
||||||
|
if (pack_) {
|
||||||
|
for (const auto& path : pack_->getResourceList()) {
|
||||||
|
if (path.starts_with(prefix)) {
|
||||||
|
result.push_back(path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!fallback_enabled_) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string root = base_path_.empty() ? "data/" + prefix : base_path_ + "/data/" + prefix;
|
||||||
|
if (!std::filesystem::exists(root)) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const auto& entry : std::filesystem::recursive_directory_iterator(root)) {
|
||||||
|
if (!entry.is_regular_file()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
std::string full = entry.path().generic_string();
|
||||||
|
if (auto pos = full.find("/data/"); pos != std::string::npos) {
|
||||||
|
result.push_back(full.substr(pos + 6));
|
||||||
|
} else if (full.starts_with("data/")) {
|
||||||
|
result.push_back(full.substr(5));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
std::ranges::sort(result);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
// Comprovar si existeix un recurs
|
// Comprovar si existeix un recurs
|
||||||
auto Loader::resourceExists(const std::string& filename) -> bool {
|
auto Loader::resourceExists(const std::string& filename) -> bool {
|
||||||
// Comprovar al paquet
|
// Comprovar al paquet
|
||||||
|
|||||||
@@ -25,6 +25,11 @@ class Loader {
|
|||||||
auto loadResource(const std::string& filename) -> std::vector<uint8_t>;
|
auto loadResource(const std::string& filename) -> std::vector<uint8_t>;
|
||||||
auto resourceExists(const std::string& filename) -> bool;
|
auto resourceExists(const std::string& filename) -> bool;
|
||||||
|
|
||||||
|
// Llistat de recursos amb prefix (ex. "shapes/", "sounds/"). Si hi ha
|
||||||
|
// pack, retorna els fitxers del pack filtrats; si no, escaneja el
|
||||||
|
// sistema de fitxers recursivament a `data/<prefix>`.
|
||||||
|
auto listResources(const std::string& prefix) -> std::vector<std::string>;
|
||||||
|
|
||||||
// Validació
|
// Validació
|
||||||
auto validatePack() -> bool;
|
auto validatePack() -> bool;
|
||||||
[[nodiscard]] auto isPackLoaded() const -> bool;
|
[[nodiscard]] auto isPackLoaded() const -> bool;
|
||||||
|
|||||||
@@ -12,6 +12,7 @@
|
|||||||
#include "core/audio/audio.hpp"
|
#include "core/audio/audio.hpp"
|
||||||
#include "core/audio/audio_adapter.hpp"
|
#include "core/audio/audio_adapter.hpp"
|
||||||
#include "core/defaults/window.hpp"
|
#include "core/defaults/window.hpp"
|
||||||
|
#include "core/graphics/shape_loader.hpp"
|
||||||
#include "core/input/input.hpp"
|
#include "core/input/input.hpp"
|
||||||
#include "core/input/mouse.hpp"
|
#include "core/input/mouse.hpp"
|
||||||
#include "core/locale/locale.hpp"
|
#include "core/locale/locale.hpp"
|
||||||
@@ -147,10 +148,21 @@ Director::Director(int argc, char* argv[])
|
|||||||
Audio::init(AUDIO_CONFIG);
|
Audio::init(AUDIO_CONFIG);
|
||||||
Audio::get()->applySettings(AUDIO_CONFIG);
|
Audio::get()->applySettings(AUDIO_CONFIG);
|
||||||
|
|
||||||
AudioResource::getMusic("title.ogg");
|
// Precàrrega blocant de tots els recursos al boot per evitar hits d'I/O i
|
||||||
AudioResource::getMusic("game.ogg");
|
// de decodificació en transicions (TITLE → GAME, primera explosió, etc.).
|
||||||
|
// Mateix patró que aee_arcade: iterem `listResources` i forcem la càrrega
|
||||||
|
// al cache de cada subsistema.
|
||||||
|
for (const auto& path : Resource::Helper::listResources("music/")) {
|
||||||
|
AudioResource::getMusic(path.substr(std::string_view{"music/"}.size()));
|
||||||
|
}
|
||||||
|
for (const auto& path : Resource::Helper::listResources("sounds/")) {
|
||||||
|
AudioResource::getSound(path.substr(std::string_view{"sounds/"}.size()));
|
||||||
|
}
|
||||||
|
for (const auto& path : Resource::Helper::listResources("shapes/")) {
|
||||||
|
Graphics::ShapeLoader::load(path.substr(std::string_view{"shapes/"}.size()));
|
||||||
|
}
|
||||||
if (cfg_->console) {
|
if (cfg_->console) {
|
||||||
std::cout << "Música precacheada\n";
|
std::cout << "Recursos precachejats (música, sons, shapes)\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
context_ = std::make_unique<SceneContext>();
|
context_ = std::make_unique<SceneContext>();
|
||||||
|
|||||||
Reference in New Issue
Block a user