singletons

This commit is contained in:
2026-04-17 21:27:30 +02:00
parent 5889df2a47
commit 513eacf356
27 changed files with 536 additions and 505 deletions

View File

@@ -7,6 +7,23 @@
#include "core/resources/resource_helper.h"
// Instancia única
Asset *Asset::instance = nullptr;
// Singleton API
void Asset::init(const std::string &executablePath) {
Asset::instance = new Asset(executablePath);
}
void Asset::destroy() {
delete Asset::instance;
Asset::instance = nullptr;
}
auto Asset::get() -> Asset * {
return Asset::instance;
}
// Constructor
Asset::Asset(std::string executablePath) {
this->executablePath = executablePath.substr(0, executablePath.find_last_of("\\/"));

View File

@@ -39,10 +39,18 @@ class Asset {
// Devuelve el nombre del tipo de recurso
std::string getTypeName(int type);
public:
// Constructor
// Constructor privado (usar Asset::init)
Asset(std::string path);
// Instancia única
static Asset *instance;
public:
// Singleton API
static void init(const std::string &executablePath); // Crea la instancia
static void destroy(); // Libera la instancia
static auto get() -> Asset *; // Obtiene el puntero a la instancia
// Añade un elemento a la lista
void add(std::string file, enum assetType type, bool required = true, bool absolute = false);

View File

@@ -12,6 +12,9 @@
#include "core/resources/resource_helper.h"
#include "game/ui/menu.h"
// Nota: Asset::get() e Input::get() se consultan en preloadAll y al construir
// los menús; no se guardan punteros en el objeto Resource.
Resource *Resource::instance_ = nullptr;
static std::string basename(const std::string &path) {
@@ -25,9 +28,9 @@ static std::string stem(const std::string &path) {
return b.substr(0, dot);
}
void Resource::init(SDL_Renderer *renderer, Asset *asset, Input *input) {
void Resource::init(SDL_Renderer *renderer) {
if (instance_ == nullptr) {
instance_ = new Resource(renderer, asset, input);
instance_ = new Resource(renderer);
instance_->preloadAll();
}
}
@@ -41,10 +44,8 @@ Resource *Resource::get() {
return instance_;
}
Resource::Resource(SDL_Renderer *renderer, Asset *asset, Input *input)
: renderer_(renderer),
asset_(asset),
input_(input) {}
Resource::Resource(SDL_Renderer *renderer)
: renderer_(renderer) {}
Resource::~Resource() {
for (auto &[name, m] : menus_) delete m;
@@ -64,7 +65,7 @@ Resource::~Resource() {
}
void Resource::preloadAll() {
const auto &items = asset_->getAll();
const auto &items = Asset::get()->getAll();
// Pass 1: texturas, sonidos, músicas, animaciones (raw lines), demo, lenguajes
for (const auto &it : items) {
@@ -155,7 +156,7 @@ void Resource::preloadAll() {
if (bname.size() < 4 || bname.substr(bname.size() - 4) != ".men") continue;
auto bytes = ResourceHelper::loadFile(it.file);
if (bytes.empty()) continue;
Menu *m = new Menu(renderer_, asset_, input_, "");
Menu *m = new Menu(renderer_, "");
m->loadFromBytes(bytes, bname);
const std::string s = stem(it.file);
menus_[s] = m;

View File

@@ -7,8 +7,6 @@
#include <unordered_map>
#include <vector>
class Asset;
class Input;
class Menu;
class Text;
class Texture;
@@ -19,7 +17,7 @@ struct JA_Sound_t;
// Singleton inicializado desde Director; las escenas consultan handles via get*().
class Resource {
public:
static void init(SDL_Renderer *renderer, Asset *asset, Input *input);
static void init(SDL_Renderer *renderer);
static void destroy();
static Resource *get();
@@ -32,14 +30,12 @@ class Resource {
const std::vector<uint8_t> &getDemoBytes() const { return demoBytes_; }
private:
Resource(SDL_Renderer *renderer, Asset *asset, Input *input);
Resource(SDL_Renderer *renderer);
~Resource();
void preloadAll();
SDL_Renderer *renderer_;
Asset *asset_;
Input *input_;
std::unordered_map<std::string, Texture *> textures_;
std::unordered_map<std::string, JA_Sound_t *> sounds_;