refactor: fase 5 — singletons a std::unique_ptr (elimina new/delete manual)

5 singletons afectats: Audio, Screen, Director, Resource::Cache, Resource::List.

- static T* instance → static std::unique_ptr<T> instance
- init(): new T() adoptat immediatament per unique_ptr (ownership RAII)
- destroy(): instance.reset() (sense delete manual)
- get(): retorna instance.get()
- Destructors moguts a public perquè std::default_delete hi pugui accedir
  (ctors privats + copy/move deleted → encapsulació efectiva mantinguda)

Ordre de destrucció preservat: SDL_AppQuit segueix cridant destroy() en
l'ordre invers a init() — la RAII automàtica no s'activa fins al final
del programa (LIFO de variables static).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-18 14:02:01 +02:00
parent 5e57034a38
commit c6e37af7d1
10 changed files with 37 additions and 42 deletions

View File

@@ -55,19 +55,18 @@ namespace {
} // namespace
#endif // __EMSCRIPTEN__
Screen* Screen::instance_ = nullptr;
std::unique_ptr<Screen> Screen::instance_;
void Screen::init() {
instance_ = new Screen();
instance_ = std::unique_ptr<Screen>(new Screen());
}
void Screen::destroy() {
delete instance_;
instance_ = nullptr;
instance_.reset();
}
auto Screen::get() -> Screen* {
return instance_;
return instance_.get();
}
Screen::Screen() {

View File

@@ -13,6 +13,8 @@ class Screen {
static void destroy();
static auto get() -> Screen*;
~Screen(); // públic per a std::unique_ptr
// Presentació — rep el buffer ARGB de 320x200 de JD8
void present(Uint32* pixel_data);
@@ -62,7 +64,6 @@ class Screen {
private:
Screen();
~Screen();
void adjustWindowSize();
void calculateMaxZoom();
@@ -70,7 +71,7 @@ class Screen {
void applyFallbackPresentation(); // Logical presentation + scale mode per al path SDL_Renderer
void ensureFallbackInternalTexture(); // Recrea internal_texture_sdl_ si cal (fallback path)
static Screen* instance_;
static std::unique_ptr<Screen> instance_;
SDL_Window* window_{nullptr};
SDL_Renderer* renderer_{nullptr};