removing all singletons

This commit is contained in:
2024-09-20 17:29:42 +02:00
parent 8348f9ce7a
commit 1e2705ef06
10 changed files with 59 additions and 49 deletions

View File

@@ -4,6 +4,8 @@
#include <Core/Graphics.h> #include <Core/Graphics.h>
#include <Platform/Inputs.h> #include <Platform/Inputs.h>
#include <Core/ImagesRegistry.h> #include <Core/ImagesRegistry.h>
#include <Core/SDLManager.h>
#include <Core/Memory.h>
#include <Core/Fps.h> #include <Core/Fps.h>
namespace mlx namespace mlx
@@ -47,12 +49,17 @@ namespace mlx
~Application(); ~Application();
private: private:
std::unique_ptr<MemManager> p_mem_manager; // Putting ptr here to initialise them before inputs, even if it f*cks the padding
std::unique_ptr<SDLManager> p_sdl_manager;
FpsManager m_fps; FpsManager m_fps;
Inputs m_in; Inputs m_in;
ImageRegistry m_image_registry; ImageRegistry m_image_registry;
std::vector<std::unique_ptr<GraphicsSupport>> m_graphics; std::vector<std::unique_ptr<GraphicsSupport>> m_graphics;
std::function<int(Handle)> f_loop_hook; std::function<int(Handle)> f_loop_hook;
std::unique_ptr<RenderCore> p_render_core; std::unique_ptr<RenderCore> p_render_core;
#ifdef PROFILER
std::unique_ptr<Profiler> p_profiler;
#endif
Handle p_param = nullptr; Handle p_param = nullptr;
}; };
} }

View File

@@ -3,21 +3,23 @@
namespace mlx namespace mlx
{ {
class MemManager : public Singleton<MemManager> class MemManager
{ {
friend class Singleton<MemManager>;
public: public:
MemManager();
static void* Malloc(std::size_t size); static void* Malloc(std::size_t size);
static void* Calloc(std::size_t n, std::size_t size); static void* Calloc(std::size_t n, std::size_t size);
static void* Realloc(void* ptr, std::size_t size); static void* Realloc(void* ptr, std::size_t size);
static void Free(void* ptr); static void Free(void* ptr);
private: inline static bool IsInit() noexcept { return s_instance != nullptr; }
MemManager() = default; inline static MemManager& Get() noexcept { return *s_instance; }
~MemManager(); ~MemManager();
private: private:
static MemManager* s_instance;
inline static std::list<void*> s_blocks; inline static std::list<void*> s_blocks;
}; };
} }

View File

@@ -1,8 +1,6 @@
#ifndef __MLX_PROFILER__ #ifndef __MLX_PROFILER__
#define __MLX_PROFILER__ #define __MLX_PROFILER__
#include <Utils/Singleton.h>
namespace mlx namespace mlx
{ {
using FloatingPointMilliseconds = std::chrono::duration<double, std::milli>; using FloatingPointMilliseconds = std::chrono::duration<double, std::milli>;
@@ -14,20 +12,21 @@ namespace mlx
std::thread::id thread_id; std::thread::id thread_id;
}; };
class Profiler : public Singleton<Profiler> class Profiler
{ {
friend class Singleton<Profiler>;
public: public:
Profiler(const Profiler&) = delete; Profiler(const Profiler&) = delete;
Profiler(Profiler&&) = delete; Profiler(Profiler&&) = delete;
Profiler() { BeginRuntimeSession(); s_instance = this; }
void AppendProfileData(ProfileResult&& result); void AppendProfileData(ProfileResult&& result);
private: inline static bool IsInit() noexcept { return s_instance != nullptr; }
Profiler() { BeginRuntimeSession(); } inline static Profiler& Get() noexcept { return *s_instance; }
~Profiler(); ~Profiler();
private:
void BeginRuntimeSession(); void BeginRuntimeSession();
void WriteProfile(const ProfileResult& result); void WriteProfile(const ProfileResult& result);
void EndRuntimeSession(); void EndRuntimeSession();
@@ -44,6 +43,8 @@ namespace mlx
} }
private: private:
static Profiler* s_instance;
std::unordered_map<std::string, std::pair<std::size_t, ProfileResult>> m_profile_data; std::unordered_map<std::string, std::pair<std::size_t, ProfileResult>> m_profile_data;
std::ofstream m_output_stream; std::ofstream m_output_stream;
std::mutex m_mutex; std::mutex m_mutex;

View File

@@ -5,13 +5,10 @@
namespace mlx namespace mlx
{ {
class SDLManager : public Singleton<SDLManager> class SDLManager
{ {
friend class Singleton<SDLManager>;
public: public:
void Init() noexcept; SDLManager();
void Shutdown() noexcept;
Handle CreateWindow(const std::string& title, std::size_t w, std::size_t h, bool hidden); Handle CreateWindow(const std::string& title, std::size_t w, std::size_t h, bool hidden);
void DestroyWindow(Handle window) noexcept; void DestroyWindow(Handle window) noexcept;
@@ -30,18 +27,17 @@ namespace mlx
std::int32_t GetXRel() const noexcept; std::int32_t GetXRel() const noexcept;
std::int32_t GetYRel() const noexcept; std::int32_t GetYRel() const noexcept;
private: inline static bool IsInit() noexcept { return s_instance != nullptr; }
SDLManager() = default; inline static SDLManager& Get() noexcept { return *s_instance; }
~SDLManager() = default;
~SDLManager();
private: private:
static SDLManager* s_instance;
std::unordered_set<Handle> m_windows_registry; std::unordered_set<Handle> m_windows_registry;
func::function<void(mlx_event_type, int, int, void*)> f_callback; func::function<void(mlx_event_type, int, int, void*)> f_callback;
void* p_callback_data = nullptr; void* p_callback_data = nullptr;
std::int32_t m_x;
std::int32_t m_y;
std::int32_t m_rel_x;
std::int32_t m_rel_y;
bool m_drop_sdl_responsability = false; bool m_drop_sdl_responsability = false;
}; };
} }

View File

@@ -93,6 +93,7 @@
#include <Core/EventBus.h> #include <Core/EventBus.h>
#include <Core/Profiler.h> #include <Core/Profiler.h>
#include <Utils/NonOwningPtr.h> #include <Utils/NonOwningPtr.h>
#include <Utils/NonCopyable.h>
using Handle = void*; using Handle = void*;

View File

@@ -1,20 +0,0 @@
#ifndef __MLX_SINGLETON__
#define __MLX_SINGLETON__
#include <Utils/NonCopyable.h>
namespace mlx
{
template <typename T>
class Singleton : public NonCopyable
{
public:
inline static T& Get()
{
static T instance;
return instance;
}
};
}
#endif // __MLX_SINGLETON__

View File

@@ -8,7 +8,7 @@
namespace mlx namespace mlx
{ {
Application::Application() : m_fps(), m_in() Application::Application() : p_mem_manager(std::make_unique<MemManager>()), p_sdl_manager(std::make_unique<SDLManager>()), m_fps(), m_in()
{ {
EventBus::RegisterListener({[](const EventBase& event) EventBus::RegisterListener({[](const EventBase& event)
{ {
@@ -16,8 +16,11 @@ namespace mlx
std::abort(); std::abort();
}, "__MlxApplication" }); }, "__MlxApplication" });
#ifdef PROFILER
p_profiler = std::make_unique<Profiler>();
#endif
m_fps.Init(); m_fps.Init();
SDLManager::Get().Init();
p_render_core = std::make_unique<RenderCore>(); p_render_core = std::make_unique<RenderCore>();
} }
@@ -89,6 +92,10 @@ namespace mlx
Application::~Application() Application::~Application()
{ {
p_render_core.reset(); p_render_core.reset();
SDLManager::Get().Shutdown(); p_sdl_manager.reset();
#ifdef PROFILER
p_profiler.reset();
#endif
p_mem_manager.reset();
} }
} }

View File

@@ -4,6 +4,13 @@
namespace mlx namespace mlx
{ {
MemManager* MemManager::s_instance = nullptr;
MemManager::MemManager()
{
s_instance = this;
}
void* MemManager::Malloc(std::size_t size) void* MemManager::Malloc(std::size_t size)
{ {
void* ptr = std::malloc(size); void* ptr = std::malloc(size);
@@ -49,5 +56,6 @@ namespace mlx
{ {
std::free(ptr); std::free(ptr);
}); });
s_instance = nullptr;
} }
} }

View File

@@ -4,6 +4,8 @@
namespace mlx namespace mlx
{ {
Profiler* Profiler::s_instance = nullptr;
void Profiler::BeginRuntimeSession() void Profiler::BeginRuntimeSession()
{ {
std::lock_guard lock(m_mutex); std::lock_guard lock(m_mutex);
@@ -63,5 +65,6 @@ namespace mlx
if(!m_runtime_session_began) if(!m_runtime_session_began)
return; return;
EndRuntimeSession(); EndRuntimeSession();
s_instance = nullptr;
} }
} }

View File

@@ -26,9 +26,13 @@ namespace mlx
}; };
} }
void SDLManager::Init() noexcept SDLManager* SDLManager::s_instance = nullptr;
SDLManager::SDLManager()
{ {
MLX_PROFILE_FUNCTION(); MLX_PROFILE_FUNCTION();
s_instance = this;
m_drop_sdl_responsability = SDL_WasInit(SDL_INIT_VIDEO); m_drop_sdl_responsability = SDL_WasInit(SDL_INIT_VIDEO);
if(m_drop_sdl_responsability) // is case the mlx is running in a sandbox like MacroUnitTester where SDL is already init if(m_drop_sdl_responsability) // is case the mlx is running in a sandbox like MacroUnitTester where SDL is already init
return; return;
@@ -224,12 +228,13 @@ namespace mlx
return y; return y;
} }
void SDLManager::Shutdown() noexcept SDLManager::~SDLManager()
{ {
if(m_drop_sdl_responsability) if(m_drop_sdl_responsability)
return; return;
SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_EVENTS); SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_EVENTS);
SDL_Quit(); SDL_Quit();
s_instance = nullptr;
DebugLog("SDL Manager uninitialized"); DebugLog("SDL Manager uninitialized");
} }
} }