From b79f1c34248a5df050e2345ae1c9b4bb7bb605a6 Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Wed, 11 Mar 2026 18:59:56 +0100 Subject: [PATCH] afegida cache a resource manager per evitar accessos a disc --- .gitignore | 1 + source/engine.cpp | 7 +++++++ source/resource_manager.cpp | 30 +++++++++++++++++++----------- source/resource_manager.hpp | 4 ++++ 4 files changed, 31 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index a981103..1cb6f68 100644 --- a/.gitignore +++ b/.gitignore @@ -26,6 +26,7 @@ Build/ BUILD/ cmake-build-*/ .cmake/ +.cache/ # Archivos generados por CMake CMakeFiles/ diff --git a/source/engine.cpp b/source/engine.cpp index 69f8e1d..b001930 100644 --- a/source/engine.cpp +++ b/source/engine.cpp @@ -282,6 +282,13 @@ bool Engine::initialize(int width, int height, int zoom, bool fullscreen, AppMod // No es crítico, continuar sin logo app_logo_.reset(); } + + // Precalentar caché: shapes PNG (evitar I/O en primera activación de PNG_SHAPE) + { + unsigned char* tmp = nullptr; size_t tmp_size = 0; + ResourceManager::loadResource("shapes/jailgames.png", tmp, tmp_size); + delete[] tmp; + } } return success; diff --git a/source/resource_manager.cpp b/source/resource_manager.cpp index f906546..3eef8be 100644 --- a/source/resource_manager.cpp +++ b/source/resource_manager.cpp @@ -3,9 +3,11 @@ #include #include +#include -// Inicializar el puntero estático +// Inicializar estáticos ResourcePack* ResourceManager::resourcePack_ = nullptr; +std::map> ResourceManager::cache_; bool ResourceManager::init(const std::string& packFilePath) { // Si ya estaba inicializado, liberar primero @@ -29,6 +31,7 @@ bool ResourceManager::init(const std::string& packFilePath) { } void ResourceManager::shutdown() { + cache_.clear(); if (resourcePack_ != nullptr) { delete resourcePack_; resourcePack_ = nullptr; @@ -39,36 +42,41 @@ bool ResourceManager::loadResource(const std::string& resourcePath, unsigned cha data = nullptr; size = 0; - // 1. Intentar cargar desde pack (si está disponible) + // 1. Consultar caché en RAM (sin I/O) + auto it = cache_.find(resourcePath); + if (it != cache_.end()) { + size = it->second.size(); + data = new unsigned char[size]; + std::memcpy(data, it->second.data(), size); + return true; + } + + // 2. Intentar cargar desde pack (si está disponible) if (resourcePack_ != nullptr) { ResourcePack::ResourceData packData = resourcePack_->loadResource(resourcePath); if (packData.data != nullptr) { + cache_[resourcePath] = std::vector(packData.data, packData.data + packData.size); data = packData.data; size = packData.size; return true; } } - // 2. Fallback: cargar desde disco + // 3. Fallback: cargar desde disco std::ifstream file(resourcePath, std::ios::binary | std::ios::ate); if (!file) { - // Intentar con "data/" como prefijo si no se encontró std::string dataPath = "data/" + resourcePath; file.open(dataPath, std::ios::binary | std::ios::ate); - if (!file) { - return false; - } + if (!file) { return false; } } - - // Obtener tamaño del archivo size = static_cast(file.tellg()); file.seekg(0, std::ios::beg); - - // Alocar buffer y leer data = new unsigned char[size]; file.read(reinterpret_cast(data), size); file.close(); + // Guardar en caché + cache_[resourcePath] = std::vector(data, data + size); return true; } diff --git a/source/resource_manager.hpp b/source/resource_manager.hpp index a47faa1..6403be9 100644 --- a/source/resource_manager.hpp +++ b/source/resource_manager.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include #include @@ -79,4 +80,7 @@ private: // Instancia del pack (nullptr si no está cargado) static ResourcePack* resourcePack_; + + // Caché en RAM para evitar I/O repetido en el bucle principal + static std::map> cache_; };