diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/crepe/system/RenderSystem.cpp | 34 | ||||
-rw-r--r-- | src/crepe/system/RenderSystem.h | 8 |
2 files changed, 38 insertions, 4 deletions
diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index fa3d0de..aa9910b 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> @@ -24,13 +26,39 @@ 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 = this->component_manager; - std::vector<std::reference_wrapper<Sprite>> sprites = mgr.get_components_by_type<Sprite>(); + ComponentManager & mgr = ComponentManager::get_instance(); + + 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); } diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h index 87ec494..7661cbd 100644 --- a/src/crepe/system/RenderSystem.h +++ b/src/crepe/system/RenderSystem.h @@ -3,6 +3,9 @@ #include "api/Camera.h" #include "System.h" +#include "api/Sprite.h" +#include <functional> +#include <vector> namespace crepe { @@ -34,7 +37,10 @@ private: void update_camera(); //! Renders all active sprites to the screen. - void render_sprites() const; + void render_sprites() ; + + std::vector<std::reference_wrapper<Sprite>> + sort(std::vector<std::reference_wrapper<Sprite>> & objs); /** * \todo Include color handling for sprites. |