adding multiple hooks supports for events and loop

This commit is contained in:
2024-12-17 02:33:04 +01:00
parent 30f4602b1c
commit 62bd63c364
15 changed files with 77 additions and 59 deletions

View File

@@ -21,7 +21,7 @@ namespace mlx
inline void GetScreenSize(mlx_window win, int* w, int* h) noexcept;
inline void SetFPSCap(std::uint32_t fps) noexcept;
inline void OnEvent(mlx_window win, int event, int (*funct_ptr)(int, void*), void* param) noexcept;
inline void OnEvent(mlx_window win, int event, void(*f)(int, void*), void* param) noexcept;
inline mlx_window NewGraphicsSuport(const mlx_window_create_info* info);
inline NonOwningPtr<GraphicsSupport> GetGraphicsSupport(mlx_window win);
@@ -32,7 +32,7 @@ namespace mlx
inline NonOwningPtr<Texture> GetTexture(mlx_image image);
void DestroyTexture(mlx_image img);
inline void LoopHook(int (*f)(void*), void* param);
inline void AddLoopHook(void(*f)(void*), void* param);
inline void LoopEnd() noexcept;
inline void LoadFont(const std::filesystem::path& filepath, float scale);
@@ -41,6 +41,15 @@ namespace mlx
~Application();
private:
struct Hook
{
func::function<void(void*)> fn;
void* param;
Hook(func::function<void(void*)> fn, void* param) : fn(fn), param(param) {}
};
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;
@@ -50,13 +59,12 @@ namespace mlx
ImageRegistry m_image_registry;
MeshRegistry m_mesh_registry;
std::vector<std::unique_ptr<GraphicsSupport>> m_graphics;
std::vector<Hook> m_hooks;
std::shared_ptr<Font> p_last_font_bound;
std::function<int(Handle)> f_loop_hook;
std::unique_ptr<RenderCore> p_render_core;
#ifdef PROFILER
std::unique_ptr<Profiler> p_profiler;
#endif
Handle p_param = nullptr;
};
}

View File

@@ -40,12 +40,12 @@ namespace mlx
*y = m_in.GetY();
}
void Application::OnEvent(mlx_window win, int event, int (*funct_ptr)(int, void*), void* param) noexcept
void Application::OnEvent(mlx_window win, int event, void(*f)(int, void*), void* param) noexcept
{
CHECK_WINDOW_PTR(win, );
if(!m_graphics[win->id]->HasWindow())
return;
m_in.OnEvent(m_graphics[win->id]->GetWindow()->GetID(), event, funct_ptr, param);
m_in.OnEvent(m_graphics[win->id]->GetWindow()->GetID(), event, f, param);
}
void Application::SetFPSCap(std::uint32_t fps) noexcept
@@ -122,10 +122,9 @@ namespace mlx
return texture;
}
void Application::LoopHook(int (*f)(void*), void* param)
void Application::AddLoopHook(void(*f)(void*), void* param)
{
f_loop_hook = f;
p_param = param;
m_hooks.emplace_back(f, param);
}
void Application::LoopEnd() noexcept

View File

@@ -93,7 +93,7 @@ namespace mlx
try
{
std::stringstream ss;
ss << Format("Verification failed : %", message, args...);
ss << Format("Verification failed: %", message, args...);
Logs::Report(LogType::FatalError, line, file, function, ss.str());
}
catch(const std::exception& e)
@@ -112,7 +112,7 @@ namespace mlx
try
{
std::stringstream ss;
ss << Format("Assertion failed : %", message, args...);
ss << Format("Assertion failed: %", message, args...);
Logs::Report(LogType::FatalError, line, file, function, ss.str());
}
catch(const std::exception& e)