aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/system
diff options
context:
space:
mode:
Diffstat (limited to 'src/crepe/system')
-rw-r--r--src/crepe/system/RenderSystem.cpp35
-rw-r--r--src/crepe/system/RenderSystem.h8
2 files changed, 37 insertions, 6 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);
diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h
index 70db21a..a83633a 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 {
@@ -44,7 +47,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.