From f7ddf3cccbe05dc3795516eeacb20f51ba6c14c5 Mon Sep 17 00:00:00 2001 From: Namonay Date: Sun, 27 Oct 2024 23:12:07 +0100 Subject: [PATCH 1/3] 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)); + } } From df05148d223791a640498642ae646a198da7da92 Mon Sep 17 00:00:00 2001 From: Namonay Date: Mon, 28 Oct 2024 00:29:11 +0100 Subject: [PATCH 2/3] Cleaned code --- runtime/Includes/Core/Graphics.inl | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/runtime/Includes/Core/Graphics.inl b/runtime/Includes/Core/Graphics.inl index 5a8362c..24b6dd5 100644 --- a/runtime/Includes/Core/Graphics.inl +++ b/runtime/Includes/Core/Graphics.inl @@ -40,19 +40,18 @@ namespace mlx NonOwningPtr text = p_scene->GetTextFromPositionAndColor(str, Vec2f{ static_cast(x), static_cast(y) }, vec_color); if(!text) { + if(m_pixelput_called) + { + m_draw_layer++; + m_pixelput_called = false; + } Text& new_text = p_scene->CreateText(str); new_text.SetPosition(Vec2f{ static_cast(x), static_cast(y) }); new_text.SetColor(std::move(vec_color)); - // if(m_pixelput_called) - { - // m_draw_layer++; - // m_pixelput_called = false; - } } else if(!p_scene->IsTextAtGivenDrawLayer(str, m_draw_layer)) { p_scene->BringToDrawLayer(text.Get(), m_draw_layer); - //m_draw_layer++; } } @@ -62,19 +61,18 @@ namespace mlx NonOwningPtr sprite = p_scene->GetSpriteFromTextureAndPosition(texture, Vec2f{ static_cast(x), static_cast(y) }); if(!sprite) { - - Sprite& new_sprite = p_scene->CreateSprite(texture); - new_sprite.SetPosition(Vec2f{ static_cast(x), static_cast(y) }); if(m_pixelput_called) { m_draw_layer++; m_pixelput_called = false; - } + } + Sprite& new_sprite = p_scene->CreateSprite(texture); + new_sprite.SetPosition(Vec2f{ static_cast(x), static_cast(y) }); + } else if(!p_scene->IsTextureAtGivenDrawLayer(texture, m_draw_layer)) - { - p_scene->BringToDrawLayer(sprite.Get(), m_draw_layer); - //m_draw_layer++; + { + p_scene->BringToDrawLayer(sprite.Get(), m_draw_layer); } } From 245d5561052bd55c4e4dbc7b1b4d7f01628dca94 Mon Sep 17 00:00:00 2001 From: Namonay <105780726+Namonay@users.noreply.github.com> Date: Mon, 28 Oct 2024 20:02:42 +0100 Subject: [PATCH 3/3] Formatting --- runtime/Includes/Core/Graphics.inl | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/runtime/Includes/Core/Graphics.inl b/runtime/Includes/Core/Graphics.inl index 24b6dd5..5bbd14d 100644 --- a/runtime/Includes/Core/Graphics.inl +++ b/runtime/Includes/Core/Graphics.inl @@ -50,9 +50,7 @@ namespace mlx new_text.SetColor(std::move(vec_color)); } else if(!p_scene->IsTextAtGivenDrawLayer(str, m_draw_layer)) - { p_scene->BringToDrawLayer(text.Get(), m_draw_layer); - } } void GraphicsSupport::TexturePut(NonOwningPtr texture, int x, int y) @@ -70,10 +68,8 @@ namespace mlx new_sprite.SetPosition(Vec2f{ static_cast(x), static_cast(y) }); } - else if(!p_scene->IsTextureAtGivenDrawLayer(texture, m_draw_layer)) - { - p_scene->BringToDrawLayer(sprite.Get(), m_draw_layer); - } + else if(!p_scene->IsTextureAtGivenDrawLayer(texture, m_draw_layer)) + p_scene->BringToDrawLayer(sprite.Get(), m_draw_layer); } void GraphicsSupport::TryEraseSpritesInScene(NonOwningPtr texture) noexcept