aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/system/RenderSystem.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/crepe/system/RenderSystem.cpp')
-rw-r--r--src/crepe/system/RenderSystem.cpp34
1 files changed, 31 insertions, 3 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);
}