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

@@ -38,8 +38,11 @@ namespace mlx
m_in.FetchInputs();
if(f_loop_hook)
f_loop_hook(p_param);
for(const auto& hook : m_hooks)
{
if(hook.fn)
hook.fn(hook.param);
}
for(auto& gs : m_graphics)
{

View File

@@ -31,12 +31,13 @@ extern "C"
mlx::MemManager::Get(); // just to initialize the C garbage collector
try { __internal_application_ptr = new mlx::Application; }
catch(...) { mlx::FatalError("internal application memory allocation failed"); }
__internal_application_ptr = new mlx::Application;
if(__internal_application_ptr == nullptr)
mlx::FatalError("internal application memory allocation failed");
mlx_context_handler* context;
try { context = new mlx_context_handler; }
catch(...) { mlx::FatalError("mlx_context memory allocation failed"); }
mlx_context_handler* context = new mlx_context_handler;
if(context == nullptr)
mlx::FatalError("mlx_context memory allocation failed");
context->app = __internal_application_ptr;
return context;
}
@@ -150,10 +151,10 @@ extern "C"
gs->GetWindow()->GetScreenSizeWindowIsOn(w, h);
}
void mlx_loop_hook(mlx_context mlx, int (*f)(void*), void* param)
void mlx_add_loop_hook(mlx_context mlx, void(*f)(void*), void* param)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
mlx->app->LoopHook(f, param);
mlx->app->AddLoopHook(f, param);
}
void mlx_loop(mlx_context mlx)
@@ -195,10 +196,10 @@ extern "C"
mlx->app->GetMousePos(x, y);
}
void mlx_on_event(mlx_context mlx, mlx_window win, mlx_event_type event, int (*funct_ptr)(int, void*), void* param)
void mlx_on_event(mlx_context mlx, mlx_window win, mlx_event_type event, void(*f)(int, void*), void* param)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
mlx->app->OnEvent(win, static_cast<int>(event), funct_ptr, param);
mlx->app->OnEvent(win, static_cast<int>(event), f, param);
}
void mlx_pixel_put(mlx_context mlx, mlx_window win, int x, int y, mlx_color color)

View File

@@ -54,13 +54,14 @@ namespace mlx
if(m_current_texture_index >= m_textures.size())
{
VkExtent2D extent;
VkExtent2D extent{ .width = 0, .height = 0 };
if(p_renderer->GetWindow())
extent = kvfGetSwapchainImagesSize(p_renderer->GetSwapchain().Get());
else if(p_renderer->GetRenderTarget())
extent = VkExtent2D{ .width = p_renderer->GetRenderTarget()->GetWidth(), .height = p_renderer->GetRenderTarget()->GetHeight() };
else
FatalError("a renderer was created without window nor render target attached (wtf)");
#ifdef DEBUG
m_textures.push_back(std::make_unique<Texture>(CPUBuffer{}, extent.width, extent.height, VK_FORMAT_R8G8B8A8_SRGB, false, "mlx_put_pixel_layer_" + std::to_string(m_current_texture_index)));
#else

View File

@@ -12,9 +12,13 @@ namespace mlx
{
if(!m_windows.contains(window_id))
return;
if(!m_events_hooks.contains(window_id) || !m_events_hooks[window_id][event].hook)
if(!m_events_hooks.contains(window_id) || m_events_hooks[window_id][event].empty())
return;
m_events_hooks[window_id][event].hook(code, m_events_hooks[window_id][event].param);
for(const auto& hook : m_events_hooks[window_id][event])
{
if(hook.fn)
hook.fn(code, hook.param);
}
});
}

View File

@@ -80,7 +80,7 @@ namespace mlx
case ShaderType::Vertex: vulkan_shader_stage = VK_SHADER_STAGE_VERTEX_BIT; break;
case ShaderType::Fragment: vulkan_shader_stage = VK_SHADER_STAGE_FRAGMENT_BIT; break;
default : FatalError("wtf"); break;
default: FatalError("wtf"); vulkan_shader_stage = VK_SHADER_STAGE_VERTEX_BIT; /* Just to shut up warnings */ break;
}
std::vector<VkDescriptorSetLayoutBinding> bindings(layout.binds.size());

View File

@@ -43,6 +43,8 @@ namespace mlx
void Image::Init(ImageType type, std::uint32_t width, std::uint32_t height, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage, bool is_multisampled, [[maybe_unused]] std::string_view debug_name)
{
MLX_PROFILE_FUNCTION();
Verify(width > 0 && height > 0, "width or height cannot be null");
m_type = type;
m_width = width;
m_height = height;
@@ -387,6 +389,11 @@ namespace mlx
int channels;
std::uint8_t* data = stbi_load(filename.c_str(), &size.x, &size.y, &channels, 4);
if(data == nullptr)
{
Error("Image loader: could not load % due to %", file, stbi_failure_reason());
return nullptr;
}
CallOnExit defer([&]() { stbi_image_free(data); });
CPUBuffer buffer(size.x * size.y * 4);
@@ -396,10 +403,6 @@ namespace mlx
*w = size.x;
if(h != nullptr)
*h = size.y;
Texture* texture;
try { texture = new Texture(std::move(buffer), size.x, size.y, VK_FORMAT_R8G8B8A8_SRGB, false, std::move(filename)); }
catch(...) { return nullptr; }
return texture;
return new Texture(std::move(buffer), size.x, size.y, VK_FORMAT_R8G8B8A8_SRGB, false, std::move(filename));
}
}