diff options
author | heavydemon21 <nielsstunnebrink1@gmail.com> | 2024-11-18 15:03:39 +0100 |
---|---|---|
committer | heavydemon21 <nielsstunnebrink1@gmail.com> | 2024-11-18 15:03:39 +0100 |
commit | c9aac3ea385213b7b9a9a4e7e48e45e6a1268902 (patch) | |
tree | f7520e7684ae2cca9cdc4a863f72f5808145cac1 | |
parent | 03e493012edc106b00bc5676c1830ce2085a7ff9 (diff) |
working sorting
-rw-r--r-- | src/crepe/system/RenderSystem.cpp | 37 | ||||
-rw-r--r-- | src/example/rendering.cpp | 25 |
2 files changed, 40 insertions, 22 deletions
diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index aa9910b..ba804d4 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -1,13 +1,13 @@ #include <algorithm> #include <cassert> #include <functional> +#include <iostream> #include <vector> #include "../ComponentManager.h" #include "../api/Sprite.h" #include "../api/Transform.h" #include "../facade/SDLContext.h" -#include "../util/Log.h" #include "RenderSystem.h" @@ -27,38 +27,39 @@ void RenderSystem::update_camera() { } } +bool sorting_comparison(const Sprite & a, const Sprite & b) { + if (a.sorting_in_layer > b.sorting_in_layer) return true; + if (a.sorting_in_layer == b.sorting_in_layer) return a.order_in_layer > b.order_in_layer; + + return false; +} + std::vector<std::reference_wrapper<Sprite>> RenderSystem::sort(std::vector<std::reference_wrapper<Sprite>> & objs) { if (objs.empty()) return {}; - std::vector<std::reference_wrapper<Sprite>> sorted_objs; - sorted_objs.insert(sorted_objs.end(), objs.begin(), objs.end()); - assert(sorted_objs.size() != objs.size()); - - std::sort(sorted_objs.begin(), sorted_objs.end(), - [](const std::reference_wrapper<Sprite> & a, - const std::reference_wrapper<Sprite> & b) { - const Sprite & sprite_a = a.get(); - const Sprite & sprite_b = b.get(); - if (sprite_a.sorting_in_layer == sprite_b.sorting_in_layer) { - return sprite_a.order_in_layer < sprite_b.order_in_layer; - } - return sprite_a.sorting_in_layer < sprite_b.sorting_in_layer; - }); + std::vector<std::reference_wrapper<Sprite>> sorted_objs(objs); + std::sort(sorted_objs.begin(), sorted_objs.end(), sorting_comparison); + return sorted_objs; } void RenderSystem::render_sprites() { ComponentManager & mgr = this->component_manager; - ComponentManager & mgr = ComponentManager::get_instance(); - auto sprites = mgr.get_components_by_type<Sprite>(); + for (const Sprite & s : sprites) { + std::cout << s.game_object_id << " " << unsigned(s.sorting_in_layer) << " " + << unsigned(s.order_in_layer) << std::endl; + } + auto sorted_sprites = this->sort(sprites); - + SDLContext & render = SDLContext::get_instance(); for (const Sprite & sprite : sorted_sprites) { + std::cout << sprite.game_object_id << " " << unsigned(sprite.sorting_in_layer) << " " + << unsigned(sprite.order_in_layer) << std::endl; auto transforms = mgr.get_components_by_id<Transform>(sprite.game_object_id); render.draw(sprite, transforms[0], *curr_cam); } diff --git a/src/example/rendering.cpp b/src/example/rendering.cpp index c9e62f1..ecd3f6a 100644 --- a/src/example/rendering.cpp +++ b/src/example/rendering.cpp @@ -30,14 +30,27 @@ int main() { // Normal adding components { Color color(0, 0, 0, 0); - obj.add_component<Sprite>(make_shared<Texture>("../asset/texture/img.png"), color, - FlipSettings{false, false}); + Sprite & sprite + = obj.add_component<Sprite>(make_shared<Texture>("../asset/texture/img.png"), + color, FlipSettings{false, false}); + sprite.sorting_in_layer = 2; + sprite.order_in_layer = 1; obj.add_component<Camera>(Color::get_red()); } { Color color(0, 0, 0, 0); - obj1.add_component<Sprite>(make_shared<Texture>("../asset/texture/second.png"), color, - FlipSettings{true, true}); + Sprite & sprite = obj1.add_component<Sprite>( + make_shared<Texture>("../asset/texture/img.png"), color, FlipSettings{true, true}); + sprite.sorting_in_layer = 2; + sprite.order_in_layer = 2; + } + + { + Color color(0, 0, 0, 0); + Sprite & sprite = obj2.add_component<Sprite>( + make_shared<Texture>("../asset/texture/img.png"), color, FlipSettings{true, true}); + sprite.sorting_in_layer = 1; + sprite.order_in_layer = 2; } /* @@ -48,8 +61,12 @@ int main() { } */ + sys.update(); + /* + auto start = std::chrono::steady_clock::now(); while (std::chrono::steady_clock::now() - start < std::chrono::seconds(5)) { sys.update(); } + */ } |