diff options
Diffstat (limited to 'src/crepe/system/RenderSystem.cpp')
-rw-r--r-- | src/crepe/system/RenderSystem.cpp | 35 |
1 files changed, 30 insertions, 5 deletions
diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index 10211a3..718036c 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -1,3 +1,5 @@ +#include <algorithm> +#include <cassert> #include <functional> #include <vector> @@ -6,6 +8,7 @@ #include "../api/Transform.h" #include "../facade/SDLContext.h" #include "../util/log.h" +#include "Asset.h" #include "RenderSystem.h" @@ -29,7 +32,6 @@ void RenderSystem::present_screen() const { } void RenderSystem::update_camera() { ComponentManager & mgr = ComponentManager::get_instance(); - std::vector<std::reference_wrapper<Camera>> cameras = mgr.get_components_by_type<Camera>(); @@ -38,15 +40,38 @@ void RenderSystem::update_camera() { this->curr_cam = &cam; } } -void RenderSystem::render_sprites() const { + +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; + }); + return sorted_objs; +} + +void RenderSystem::render_sprites() { ComponentManager & mgr = ComponentManager::get_instance(); - std::vector<std::reference_wrapper<Sprite>> sprites - = mgr.get_components_by_type<Sprite>(); + auto sprites = mgr.get_components_by_type<Sprite>(); + auto sorted_sprites = this->sort(sprites); + SDLContext & render = SDLContext::get_instance(); - for (const Sprite & sprite : sprites) { + for (const Sprite & sprite : sorted_sprites) { auto transforms = mgr.get_components_by_id<Transform>(sprite.game_object_id); render.draw(sprite, transforms[0], *curr_cam); |