Files
aee-arcade/source/core/resources/resource_cache.hpp
T

62 lines
2.7 KiB
C++

#pragma once
#include <memory>
#include <string>
#include <vector>
#include "core/resources/resource_types.hpp"
namespace Resource {
// Cache mínim per a la fase 2: només surfaces i animacions, càrrega
// explícita per nom + path. Per ara la cerca és lineal sobre vector.
// Meyers singleton: la destrucció estàtica només toca smart pointers, no
// depén d'SDL ni de Screen, així que és segura sense ordenació explícita.
class Cache {
public:
static auto get() -> Cache&;
Cache(const Cache&) = delete;
Cache(Cache&&) = delete;
auto operator=(const Cache&) -> Cache& = delete;
auto operator=(Cache&&) -> Cache& = delete;
// Carrega una surface des de fitxer i la registra amb el nom donat.
void loadSurface(const std::string& name, const std::string& file_path);
// Llig el YAML d'animació en cru i el registra amb el nom donat.
void loadAnimation(const std::string& name, const std::string& file_path);
// Carrega una paleta (.pal JASC o .gif) i la registra amb el nom donat.
// Pensat per a paletes compartides (UI, fonts, tilesets reciclats).
void loadPalette(const std::string& name, const std::string& file_path);
// Carrega una pista OGG de música (streaming) i la registra amb el nom donat.
void loadMusic(const std::string& name, const std::string& file_path);
// Carrega un so WAV (descomprimit a memòria) i el registra amb el nom donat.
void loadSound(const std::string& name, const std::string& file_path);
auto getSurface(const std::string& name) -> std::shared_ptr<Surface>;
auto getAnimationData(const std::string& name) -> const AnimationResource&;
auto getPalette(const std::string& name) -> const Palette&;
// Retornen el punter cru; l'ownership queda al Cache.
auto getMusic(const std::string& name) -> Ja::Music*;
auto getSound(const std::string& name) -> Ja::Sound*;
// Allibera explícitament música i sons. Cal cridar-la des de ~Director
// ABANS de destruir Audio i de SDL_Quit(): com que el Cache és Meyers
// i es destrueix a la sortida del programa, si esperem els seus
// destructors els streams de SDL ja estarien invalidats i la deletion
// de Ja::Music crashejaria.
void clearAudio();
private:
Cache() = default;
~Cache() = default;
std::vector<SurfaceResource> surfaces_;
std::vector<AnimationResource> animations_;
std::vector<PaletteResource> palettes_;
std::vector<MusicResource> musics_;
std::vector<SoundResource> sounds_;
};
} // namespace Resource