afegida cache a resource manager per evitar accessos a disc
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -26,6 +26,7 @@ Build/
|
|||||||
BUILD/
|
BUILD/
|
||||||
cmake-build-*/
|
cmake-build-*/
|
||||||
.cmake/
|
.cmake/
|
||||||
|
.cache/
|
||||||
|
|
||||||
# Archivos generados por CMake
|
# Archivos generados por CMake
|
||||||
CMakeFiles/
|
CMakeFiles/
|
||||||
|
|||||||
@@ -282,6 +282,13 @@ bool Engine::initialize(int width, int height, int zoom, bool fullscreen, AppMod
|
|||||||
// No es crítico, continuar sin logo
|
// No es crítico, continuar sin logo
|
||||||
app_logo_.reset();
|
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;
|
return success;
|
||||||
|
|||||||
@@ -3,9 +3,11 @@
|
|||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
// Inicializar el puntero estático
|
// Inicializar estáticos
|
||||||
ResourcePack* ResourceManager::resourcePack_ = nullptr;
|
ResourcePack* ResourceManager::resourcePack_ = nullptr;
|
||||||
|
std::map<std::string, std::vector<unsigned char>> ResourceManager::cache_;
|
||||||
|
|
||||||
bool ResourceManager::init(const std::string& packFilePath) {
|
bool ResourceManager::init(const std::string& packFilePath) {
|
||||||
// Si ya estaba inicializado, liberar primero
|
// Si ya estaba inicializado, liberar primero
|
||||||
@@ -29,6 +31,7 @@ bool ResourceManager::init(const std::string& packFilePath) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void ResourceManager::shutdown() {
|
void ResourceManager::shutdown() {
|
||||||
|
cache_.clear();
|
||||||
if (resourcePack_ != nullptr) {
|
if (resourcePack_ != nullptr) {
|
||||||
delete resourcePack_;
|
delete resourcePack_;
|
||||||
resourcePack_ = nullptr;
|
resourcePack_ = nullptr;
|
||||||
@@ -39,36 +42,41 @@ bool ResourceManager::loadResource(const std::string& resourcePath, unsigned cha
|
|||||||
data = nullptr;
|
data = nullptr;
|
||||||
size = 0;
|
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) {
|
if (resourcePack_ != nullptr) {
|
||||||
ResourcePack::ResourceData packData = resourcePack_->loadResource(resourcePath);
|
ResourcePack::ResourceData packData = resourcePack_->loadResource(resourcePath);
|
||||||
if (packData.data != nullptr) {
|
if (packData.data != nullptr) {
|
||||||
|
cache_[resourcePath] = std::vector<unsigned char>(packData.data, packData.data + packData.size);
|
||||||
data = packData.data;
|
data = packData.data;
|
||||||
size = packData.size;
|
size = packData.size;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. Fallback: cargar desde disco
|
// 3. Fallback: cargar desde disco
|
||||||
std::ifstream file(resourcePath, std::ios::binary | std::ios::ate);
|
std::ifstream file(resourcePath, std::ios::binary | std::ios::ate);
|
||||||
if (!file) {
|
if (!file) {
|
||||||
// Intentar con "data/" como prefijo si no se encontró
|
|
||||||
std::string dataPath = "data/" + resourcePath;
|
std::string dataPath = "data/" + resourcePath;
|
||||||
file.open(dataPath, std::ios::binary | std::ios::ate);
|
file.open(dataPath, std::ios::binary | std::ios::ate);
|
||||||
if (!file) {
|
if (!file) { return false; }
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Obtener tamaño del archivo
|
|
||||||
size = static_cast<size_t>(file.tellg());
|
size = static_cast<size_t>(file.tellg());
|
||||||
file.seekg(0, std::ios::beg);
|
file.seekg(0, std::ios::beg);
|
||||||
|
|
||||||
// Alocar buffer y leer
|
|
||||||
data = new unsigned char[size];
|
data = new unsigned char[size];
|
||||||
file.read(reinterpret_cast<char*>(data), size);
|
file.read(reinterpret_cast<char*>(data), size);
|
||||||
file.close();
|
file.close();
|
||||||
|
|
||||||
|
// Guardar en caché
|
||||||
|
cache_[resourcePath] = std::vector<unsigned char>(data, data + size);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <map>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
@@ -79,4 +80,7 @@ private:
|
|||||||
|
|
||||||
// Instancia del pack (nullptr si no está cargado)
|
// Instancia del pack (nullptr si no está cargado)
|
||||||
static ResourcePack* resourcePack_;
|
static ResourcePack* resourcePack_;
|
||||||
|
|
||||||
|
// Caché en RAM para evitar I/O repetido en el bucle principal
|
||||||
|
static std::map<std::string, std::vector<unsigned char>> cache_;
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user