aboutsummaryrefslogtreecommitdiff
path: root/src/crepe
diff options
context:
space:
mode:
authorheavydemon21 <nielsstunnebrink1@gmail.com>2024-11-20 14:52:52 +0100
committerheavydemon21 <nielsstunnebrink1@gmail.com>2024-11-20 14:52:52 +0100
commita01c2dc46daeffa0a5c92230d1513047a072c04d (patch)
treeb0df9754cf9c8bd900594816f9bdac20679308c3 /src/crepe
parent81664b46f5cb7c28d629ddafe4cbea7e717d62d2 (diff)
parentf78e8011ee77ba5303a5e608cc81c3b303d35943 (diff)
Merge branch 'master' into niels/RenderingParticle
Diffstat (limited to 'src/crepe')
-rw-r--r--src/crepe/api/Color.cpp37
-rw-r--r--src/crepe/api/Color.h49
-rw-r--r--src/crepe/api/Sprite.h3
-rw-r--r--src/crepe/facade/SDLContext.h6
-rw-r--r--src/crepe/system/RenderSystem.cpp59
-rw-r--r--src/crepe/system/RenderSystem.h34
6 files changed, 89 insertions, 99 deletions
diff --git a/src/crepe/api/Color.cpp b/src/crepe/api/Color.cpp
index 9e5f187..29bd77a 100644
--- a/src/crepe/api/Color.cpp
+++ b/src/crepe/api/Color.cpp
@@ -2,32 +2,11 @@
using namespace crepe;
-Color Color::white = Color(255, 255, 255, 0);
-Color Color::red = Color(255, 0, 0, 0);
-Color Color::green = Color(0, 255, 0, 0);
-Color Color::blue = Color(0, 0, 255, 0);
-Color Color::black = Color(0, 0, 0, 0);
-Color Color::cyan = Color(0, 255, 255, 0);
-Color Color::yellow = Color(255, 255, 0, 0);
-Color Color::magenta = Color(255, 0, 255, 0);
-
-Color::Color(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha) {
- this->a = alpha;
- this->r = red;
- this->g = green;
- this->b = blue;
-};
-
-const Color & Color::get_white() { return Color::white; };
-
-const Color & Color::get_red() { return Color::red; };
-const Color & Color::get_green() { return Color::green; };
-const Color & Color::get_blue() { return Color::blue; };
-
-const Color & Color::get_black() { return Color::black; };
-
-const Color & Color::get_cyan() { return Color::cyan; };
-
-const Color & Color::get_yellow() { return Color::yellow; };
-
-const Color & Color::get_magenta() { return Color::magenta; };
+const Color Color::WHITE{0xff, 0xff, 0xff};
+const Color Color::RED{0xff, 0x00, 0x00};
+const Color Color::GREEN{0x00, 0xff, 0x00};
+const Color Color::BLUE{0x00, 0x00, 0xff};
+const Color Color::BLACK{0x00, 0x00, 0x00};
+const Color Color::CYAN{0x00, 0xff, 0xff};
+const Color Color::YELLOW{0xff, 0xff, 0x00};
+const Color Color::MAGENTA{0xff, 0x00, 0xff};
diff --git a/src/crepe/api/Color.h b/src/crepe/api/Color.h
index aa47bf4..84edb5c 100644
--- a/src/crepe/api/Color.h
+++ b/src/crepe/api/Color.h
@@ -4,41 +4,20 @@
namespace crepe {
-// TODO: make Color a struct w/o constructors/destructors
-class Color {
-
- // FIXME: can't these colors be defined as a `static constexpr const Color`
- // instead?
-
-public:
- Color(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha);
- static const Color & get_white();
- static const Color & get_red();
- static const Color & get_green();
- static const Color & get_blue();
- static const Color & get_cyan();
- static const Color & get_magenta();
- static const Color & get_yellow();
- static const Color & get_black();
-
-private:
- // TODO: why are these private!?
- uint8_t r;
- uint8_t g;
- uint8_t b;
- uint8_t a;
-
- static Color white;
- static Color red;
- static Color green;
- static Color blue;
- static Color cyan;
- static Color magenta;
- static Color yellow;
- static Color black;
-
-private:
- friend class SDLContext;
+struct Color {
+ uint8_t r = 0x00;
+ uint8_t g = 0x00;
+ uint8_t b = 0x00;
+ uint8_t a = 0xff;
+
+ static const Color WHITE;
+ static const Color RED;
+ static const Color GREEN;
+ static const Color BLUE;
+ static const Color CYAN;
+ static const Color MAGENTA;
+ static const Color YELLOW;
+ static const Color BLACK;
};
} // namespace crepe
diff --git a/src/crepe/api/Sprite.h b/src/crepe/api/Sprite.h
index 0192793..74a55d4 100644
--- a/src/crepe/api/Sprite.h
+++ b/src/crepe/api/Sprite.h
@@ -2,8 +2,9 @@
#include <memory>
+#include "../Component.h"
+
#include "Color.h"
-#include "Component.h"
#include "Texture.h"
namespace crepe {
diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h
index 718c40f..9a514cb 100644
--- a/src/crepe/facade/SDLContext.h
+++ b/src/crepe/facade/SDLContext.h
@@ -20,9 +20,6 @@ namespace crepe {
// typedef is unusable when crepe is packaged. Wouter will fix this later.
typedef SDL_Keycode CREPE_KEYCODES;
-class Texture;
-class LoopManager;
-
/**
* \class SDLContext
* \brief Facade for the SDL library
@@ -90,9 +87,6 @@ private:
//! Will use the funtions: texture_from_path, get_width,get_height.
friend class Texture;
- //! Will use the funtions: texture_from_path, get_width,get_height.
- friend class Animator;
-
/**
* \brief Loads a texture from a file path.
* \param path Path to the image file.
diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp
index c137de1..bc5422d 100644
--- a/src/crepe/system/RenderSystem.cpp
+++ b/src/crepe/system/RenderSystem.cpp
@@ -1,4 +1,7 @@
+#include <algorithm>
+#include <cassert>
#include <cmath>
+#include <stdexcept>
#include <vector>
#include "../ComponentManager.h"
@@ -11,25 +14,52 @@
#include "RenderSystem.h"
using namespace crepe;
+using namespace std;
-void RenderSystem::clear_screen() const { SDLContext::get_instance().clear_screen(); }
+void RenderSystem::clear_screen() { this->context.clear_screen(); }
-void RenderSystem::present_screen() const { SDLContext::get_instance().present_screen(); }
+void RenderSystem::present_screen() { this->context.present_screen(); }
void RenderSystem::update_camera() {
ComponentManager & mgr = this->component_manager;
- auto cameras = mgr.get_components_by_type<Camera>();
+ std::vector<std::reference_wrapper<Camera>> cameras = mgr.get_components_by_type<Camera>();
+
+ if (cameras.size() == 0) throw std::runtime_error("No cameras in current scene");
for (Camera & cam : cameras) {
- SDLContext::get_instance().set_camera(cam);
- this->curr_cam = &cam;
+ this->context.set_camera(cam);
+ this->curr_cam_ref = &cam;
}
}
+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) {
+
+ std::vector<std::reference_wrapper<Sprite>> sorted_objs(objs);
+ std::sort(sorted_objs.begin(), sorted_objs.end(), sorting_comparison);
+
+ return sorted_objs;
+}
+
+
+
+void RenderSystem::update() {
+ this->clear_screen();
+ this->update_camera();
+ this->render();
+ this->present_screen();
+}
+
bool RenderSystem::render_particle(const Sprite & sprite, const double & scale) const {
ComponentManager & mgr = this->component_manager;
- SDLContext & render = SDLContext::get_instance();
auto emitters = mgr.get_components_by_id<ParticleEmitter>(sprite.game_object_id);
@@ -43,7 +73,7 @@ bool RenderSystem::render_particle(const Sprite & sprite, const double & scale)
for (const Particle & p : em.data.particles) {
if (!p.active) continue;
- render.draw_particle(sprite, p.position, p.angle, scale, *this->curr_cam);
+ this->context.draw_particle(sprite, p.position, p.angle, scale, *this->curr_cam);
}
}
return rendering_particles;
@@ -51,17 +81,17 @@ bool RenderSystem::render_particle(const Sprite & sprite, const double & scale)
void RenderSystem::render_normal(const Sprite & sprite, const Transform & tm) const {
ComponentManager & mgr = this->component_manager;
- SDLContext & render = SDLContext::get_instance();
- render.draw(sprite, tm, *curr_cam);
+ this->context.draw(sprite, tm, *this->curr_cam);
}
void RenderSystem::render() const {
ComponentManager & mgr = this->component_manager;
+ vector<reference_wrapper<Sprite>> sprites = mgr.get_components_by_type<Sprite>();
+ vector<reference_wrapper<Sprite>> sorted_sprites = this->sort(sprites);
- auto sprites = mgr.get_components_by_type<Sprite>();
- for (const Sprite & sprite : sprites) {
+ for (const Sprite & sprite : sorted_sprites) {
if (!sprite.active) continue;
const Transform & transform
= mgr.get_components_by_id<Transform>(sprite.game_object_id).front().get();
@@ -73,10 +103,3 @@ void RenderSystem::render() const {
this->render_normal(sprite, transform);
}
}
-
-void RenderSystem::update() {
- this->clear_screen();
- this->update_camera();
- this->render();
- this->present_screen();
-}
diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h
index 8841f72..b9033fb 100644
--- a/src/crepe/system/RenderSystem.h
+++ b/src/crepe/system/RenderSystem.h
@@ -1,21 +1,24 @@
#pragma once
-#include "api/Camera.h"
-#include "api/Sprite.h"
-#include "api/Transform.h"
+#include <functional>
+#include <vector>
+
+#include "facade/SDLContext.h"
#include "System.h"
#include <cmath>
namespace crepe {
+class Camera;
+class Sprite;
+
/**
* \class RenderSystem
* \brief Manages rendering operations for all game objects.
*
- * RenderSystem is responsible for rendering sprites, clearing and presenting the screen, and
- * managing the active camera. It functions as a singleton, providing centralized rendering
- * services for the application.
+ * RenderSystem is responsible for rendering, clearing and presenting the screen, and
+ * managing the active camera.
*/
class RenderSystem : public System {
public:
@@ -28,10 +31,10 @@ public:
private:
//! Clears the screen in preparation for rendering.
- void clear_screen() const;
+ void clear_screen();
//! Presents the rendered frame to the display.
- void present_screen() const;
+ void present_screen();
//! Updates the active camera used for rendering.
void update_camera();
@@ -54,7 +57,16 @@ private:
* \param sprite the sprite component that holds all the data
* \param tm the Transform component that holds the position,rotation and scale
*/
- void render_normal(const Sprite & sprite, const Transform & tm) const;
+ void render_normal(const Sprite & sprite, const Transform & tm);
+
+ /**
+ * \brief sort a vector sprite objects with
+ *
+ * \param objs the vector that will do a sorting algorithm on
+ * \return returns a sorted reference vector
+ */
+ std::vector<std::reference_wrapper<Sprite>>
+ sort(std::vector<std::reference_wrapper<Sprite>> & objs);
/**
* \todo Include color handling for sprites.
@@ -67,8 +79,10 @@ private:
private:
//! Pointer to the current active camera for rendering
- Camera * curr_cam = nullptr;
+ Camera * curr_cam_ref = nullptr;
// TODO: needs a better solution
+
+ SDLContext & context = SDLContext::get_instance();
};
} // namespace crepe