From f7ddf3cccbe05dc3795516eeacb20f51ba6c14c5 Mon Sep 17 00:00:00 2001 From: Namonay Date: Sun, 27 Oct 2024 23:12:07 +0100 Subject: [PATCH] add: bring textures to draw layer Adding the Scene::BringToDrawLayer() to fix layering issues after a Scene::ResetScene() call --- runtime/Includes/Core/Graphics.inl | 10 +++++----- runtime/Includes/Graphics/Scene.h | 1 + runtime/Sources/Graphics/Scene.cpp | 11 +++++++++++ 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/runtime/Includes/Core/Graphics.inl b/runtime/Includes/Core/Graphics.inl index c0df6e5..5a8362c 100644 --- a/runtime/Includes/Core/Graphics.inl +++ b/runtime/Includes/Core/Graphics.inl @@ -45,14 +45,14 @@ namespace mlx new_text.SetColor(std::move(vec_color)); // if(m_pixelput_called) { - m_draw_layer++; + // m_draw_layer++; // m_pixelput_called = false; } } else if(!p_scene->IsTextAtGivenDrawLayer(str, m_draw_layer)) { - p_scene->BringToFront(text.Get()); - m_draw_layer++; + p_scene->BringToDrawLayer(text.Get(), m_draw_layer); + //m_draw_layer++; } } @@ -73,8 +73,8 @@ namespace mlx } else if(!p_scene->IsTextureAtGivenDrawLayer(texture, m_draw_layer)) { - p_scene->BringToFront(sprite.Get()); - m_draw_layer++; + p_scene->BringToDrawLayer(sprite.Get(), m_draw_layer); + //m_draw_layer++; } } diff --git a/runtime/Includes/Graphics/Scene.h b/runtime/Includes/Graphics/Scene.h index d7f2e2a..0d46488 100644 --- a/runtime/Includes/Graphics/Scene.h +++ b/runtime/Includes/Graphics/Scene.h @@ -27,6 +27,7 @@ namespace mlx inline void BindFont(std::shared_ptr font) { Verify((bool)font, "invalid fond pointer"); p_bound_font = font; } void BringToFront(NonOwningPtr drawable); + void BringToDrawLayer(NonOwningPtr drawable, std::uint64_t draw_layer); inline void ResetScene() { m_drawables.clear(); } diff --git a/runtime/Sources/Graphics/Scene.cpp b/runtime/Sources/Graphics/Scene.cpp index f7752e6..f64afe7 100644 --- a/runtime/Sources/Graphics/Scene.cpp +++ b/runtime/Sources/Graphics/Scene.cpp @@ -124,4 +124,15 @@ namespace mlx return; std::rotate(it, it + 1, m_drawables.end()); } + void Scene::BringToDrawLayer(NonOwningPtr drawable, std::uint64_t draw_layer) + { + MLX_PROFILE_FUNCTION(); + auto it = std::find_if(m_drawables.begin(), m_drawables.end(), [&drawable](std::shared_ptr drawable_ptr) + { + return drawable_ptr.get() == drawable.Get(); + }); + if (m_drawables.size() > draw_layer) + return; + std::swap(*it, *(m_drawables.begin() + draw_layer)); + } }