From 4b9bbc89915d3cf38846827def5f71274712aee8 Mon Sep 17 00:00:00 2001 From: Joey Pollack Date: Thu, 6 Oct 2022 15:12:02 -0400 Subject: [PATCH] Sorting render groups by render layer --- docs/tasks/core.todo | 1 + docs/tasks/editor.todo | 6 +++- src/run_modes/editor/component_guis.cpp | 4 +++ src/world/components.h | 1 + src/world/entity.cpp | 6 +++- src/world/world.cpp | 37 +++++++++++++++++++++---- 6 files changed, 47 insertions(+), 8 deletions(-) diff --git a/docs/tasks/core.todo b/docs/tasks/core.todo index c2d4d98..800cab7 100644 --- a/docs/tasks/core.todo +++ b/docs/tasks/core.todo @@ -126,6 +126,7 @@ ECS: ☐ Box Collider (Box2D) World (Lunariums version of a "Scene"): + ☐ Add Render layer property to all renderable components ☐ Implement memento pattern to save the initial state of the world ☐ Implement running the world and resetting to initial state ☐ Implement the world without Regions first diff --git a/docs/tasks/editor.todo b/docs/tasks/editor.todo index c5e7710..dbc54f9 100644 --- a/docs/tasks/editor.todo +++ b/docs/tasks/editor.todo @@ -1,6 +1,10 @@ Editor: - ☐ Give Entities a name property separate from the tag component + ☐ Remove Entity + ☐ Remove Component + ✔ Add Entity children @done(22-09-14 15:07) + ✔ Entity Parent/Child hierarchy system @done(22-09-14 15:07) + ✔ Give Entities a name property separate from the tag component @done(22-09-14 15:06) ☐ Add button to flip image assets vertically ☐ Design a custom editor style @low ☐ Save and unload world data when a new world is selected diff --git a/src/run_modes/editor/component_guis.cpp b/src/run_modes/editor/component_guis.cpp index a4df6ae..147cf75 100644 --- a/src/run_modes/editor/component_guis.cpp +++ b/src/run_modes/editor/component_guis.cpp @@ -107,5 +107,9 @@ namespace lunarium { namespace editor comp.Size.x = _size.x; comp.Size.y = _size.y; } + + ImGui::Text("Render Layer: "); + ImGui::SameLine(); + ImGui::InputInt("##Render Layer", &comp.RenderLayer, 1, 10); } }} \ No newline at end of file diff --git a/src/world/components.h b/src/world/components.h index af8314b..2433099 100644 --- a/src/world/components.h +++ b/src/world/components.h @@ -78,6 +78,7 @@ namespace lunarium { glm::vec4 Color; glm::vec2 Size; + int RenderLayer; BlockOutComponent() = default; BlockOutComponent(const BlockOutComponent&) = default; diff --git a/src/world/entity.cpp b/src/world/entity.cpp index f1290ea..73407fd 100644 --- a/src/world/entity.cpp +++ b/src/world/entity.cpp @@ -247,6 +247,8 @@ namespace lunarium size["width"] = comp.Size.x; size["height"] = comp.Size.y; + blockout["render_layer"] = comp.RenderLayer; + components.emplace_back(blockout); } @@ -384,7 +386,9 @@ namespace lunarium auto& size = comp["size"]; glm::vec2 Size(size["width"].get(), size["height"].get()); - AddComponent(Color, Size); + int layer = comp["render_layer"].get(); + + AddComponent(Color, Size, layer); } if ("ParentEntityComponent" == comp_type_name) diff --git a/src/world/world.cpp b/src/world/world.cpp index f165cee..825a66e 100644 --- a/src/world/world.cpp +++ b/src/world/world.cpp @@ -79,17 +79,42 @@ namespace lunarium // pGraphics->DrawQuad(rect, color, nullptr, 0.0f, Rectangle(-1, -1, -1, -1), parent_transform); // }); - mECSRegistry.view().each([&](auto entity, auto &transform, auto &blockout) + // OLD RENDER HEIRARCHY RENDER SYSTEM + // mECSRegistry.view().each([&](auto entity, auto &transform, auto &blockout) + // { + // Rectangle rect(transform.Position.x, transform.Position.y, blockout.Size.x, blockout.Size.y); + // Color color(blockout.Color.x, blockout.Color.y, blockout.Color.z, blockout.Color.w); + + // if (!mECSRegistry.all_of(entity)) + // { + // DrawHeirarchy(pGraphics, entity, transform, blockout, glm::mat4(1.0f)); + // } + // }); + + // NEW RENDER SYSTEM + // First get the group we want + auto group = mECSRegistry.group<>(entt::get); + + // Next sort the group + group.sort([](const BlockOutComponent &lhs, const BlockOutComponent &rhs) { + return lhs.RenderLayer < rhs.RenderLayer; + }); + + // Render the group + for(auto entity: group) + { + auto &transform = group.get(entity); + auto &blockout = group.get(entity); + + Rectangle rect(transform.Position.x, transform.Position.y, blockout.Size.x, blockout.Size.y); Color color(blockout.Color.x, blockout.Color.y, blockout.Color.z, blockout.Color.w); - if (!mECSRegistry.all_of(entity)) - { - DrawHeirarchy(pGraphics, entity, transform, blockout, glm::mat4(1.0f)); - } - }); + // TODO: Get and apply parent transforms + pGraphics->DrawQuad(rect, color, nullptr, -transform.Rotation.z, Rectangle(-1, -1, -1, -1), glm::mat4(1.0f)); + } }