aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorheavydemon21 <nielsstunnebrink1@gmail.com>2024-11-27 19:57:16 +0100
committerheavydemon21 <nielsstunnebrink1@gmail.com>2024-11-27 19:57:16 +0100
commitddb5bde6e5dd4d89faf419630086ece66690d6b5 (patch)
tree0b2cf988dbee4915fca002f16fbe88013d075ce2 /src
parent2b35e8f51a3536b62ea21dc82deec1e3b65568f6 (diff)
implemented feedback. biggest changes are teh camera_ref removed
Diffstat (limited to 'src')
-rw-r--r--src/crepe/api/Animator.h5
-rw-r--r--src/crepe/api/Camera.cpp8
-rw-r--r--src/crepe/api/Camera.h12
-rw-r--r--src/crepe/api/Config.h4
-rw-r--r--src/crepe/api/Sprite.cpp8
-rw-r--r--src/crepe/api/Sprite.h18
-rw-r--r--src/crepe/api/Vector2.h1
-rw-r--r--src/crepe/facade/SDLContext.cpp3
-rw-r--r--src/crepe/system/RenderSystem.cpp28
-rw-r--r--src/crepe/system/RenderSystem.h13
-rw-r--r--src/example/rendering_particle.cpp8
11 files changed, 55 insertions, 53 deletions
diff --git a/src/crepe/api/Animator.h b/src/crepe/api/Animator.h
index 53f4b91..7a5ef03 100644
--- a/src/crepe/api/Animator.h
+++ b/src/crepe/api/Animator.h
@@ -40,11 +40,6 @@ public:
Animator(uint32_t id, Sprite & spritesheet, int row, int col, int col_animate);
~Animator(); // dbg_trace
- Animator(const Animator &) = delete;
- Animator(Animator &&) = delete;
- Animator & operator=(const Animator &) = delete;
- Animator & operator=(Animator &&) = delete;
-
private:
//! A reference to the Sprite sheet containing the animation frames.
Sprite & spritesheet;
diff --git a/src/crepe/api/Camera.cpp b/src/crepe/api/Camera.cpp
index 5835bdd..4397ac7 100644
--- a/src/crepe/api/Camera.cpp
+++ b/src/crepe/api/Camera.cpp
@@ -6,9 +6,13 @@
using namespace crepe;
-Camera::Camera(game_object_id_t id, const Color & bg_color)
+Camera::Camera(game_object_id_t id, const Color & bg_color, const ivec2 & screen,
+ const ivec2 & viewport, const double & zoom)
: Component(id),
- bg_color(bg_color) {
+ bg_color(bg_color),
+ screen(screen),
+ viewport(viewport),
+ zoom(zoom) {
dbg_trace();
}
diff --git a/src/crepe/api/Camera.h b/src/crepe/api/Camera.h
index 1505107..2ba37fc 100644
--- a/src/crepe/api/Camera.h
+++ b/src/crepe/api/Camera.h
@@ -21,12 +21,12 @@ public:
* \param id Unique identifier for the camera component.
* \param bg_color Background color for the camera view.
*/
- Camera(game_object_id_t id, const Color & bg_color);
+ Camera(game_object_id_t id, const Color & bg_color, const ivec2 & screen, const ivec2 & viewport, const double & zoom);
~Camera(); // dbg_trace only
public:
//! Background color of the camera view.
- Color bg_color;
+ const Color bg_color;
//! offset postion from the game object transform component
vec2 offset = {0, 0};
@@ -36,19 +36,19 @@ public:
vec2 pos = {0, 0};
//! screen the display size in pixels ( output resolution )
- ivec2 screen = {1080, 720};
+ const ivec2 screen = {1080, 720};
//! viewport is the area of the world visible through the camera (in world units)
- ivec2 viewport = {500, 1000};
+ const ivec2 viewport = {500, 1000};
//! Zoom level of the camera view.
- double zoom = 1.0f;
+ const double zoom = 1.0f;
public:
/**
* \brief Gets the maximum number of camera instances allowed.
* \return Maximum instance count as an integer.
*/
- virtual int get_instances_max() const { return 10; }
+ virtual int get_instances_max() const { return 1; }
};
} // namespace crepe
diff --git a/src/crepe/api/Config.h b/src/crepe/api/Config.h
index 2723461..9b43cdf 100644
--- a/src/crepe/api/Config.h
+++ b/src/crepe/api/Config.h
@@ -66,9 +66,7 @@ public:
//! default window settings
struct {
//TODO make this constexpr because this will never change
- ivec2 def_size = {1080, 720};
- vec2 pos = {0, 0};
- float zoom = 1.0f;
+ const ivec2 def_size = {1080, 720};
} win_set;
//! Asset loading options
diff --git a/src/crepe/api/Sprite.cpp b/src/crepe/api/Sprite.cpp
index 2c2ca65..21c8377 100644
--- a/src/crepe/api/Sprite.cpp
+++ b/src/crepe/api/Sprite.cpp
@@ -10,16 +10,20 @@ using namespace std;
using namespace crepe;
Sprite::Sprite(game_object_id_t id, const Texture & image, const Color & color,
- const FlipSettings & flip)
+ const FlipSettings & flip, uint8_t sort_layer, uint8_t order_layer, int height)
: Component(id),
color(color),
flip(flip),
sprite_image(image),
- aspect_ratio(static_cast<double>(sprite_image.get_width()) / sprite_image.get_height()) {
+ sorting_in_layer(sort_layer),
+ order_in_layer(order_layer),
+ height(height) {
+
dbg_trace();
this->sprite_rect.w = sprite_image.get_width();
this->sprite_rect.h = sprite_image.get_height();
+ this->aspect_ratio = static_cast<double>(this->sprite_rect.w) / this->sprite_rect.h;
}
Sprite::~Sprite() { dbg_trace(); }
diff --git a/src/crepe/api/Sprite.h b/src/crepe/api/Sprite.h
index 82dc4a7..1c40501 100644
--- a/src/crepe/api/Sprite.h
+++ b/src/crepe/api/Sprite.h
@@ -1,12 +1,10 @@
#pragma once
-#include <memory>
-#include <sys/types.h>
-
#include "../Component.h"
#include "Color.h"
#include "Texture.h"
+#include <cstdint>
namespace crepe {
@@ -43,9 +41,12 @@ public:
* \param image Shared pointer to the texture for this sprite.
* \param color Color tint applied to the sprite.
* \param flip Flip settings for horizontal and vertical orientation.
+ * \param order_layer decides the sorting in layer of the sprite.
+ * \param sort_layer decides the order in layer of the sprite.
+ * \param height the height of the image in game units
*/
Sprite(game_object_id_t id, const Texture & image, const Color & color,
- const FlipSettings & flip);
+ const FlipSettings & flip, uint8_t sort_layer, uint8_t order_layer, int height);
/**
* \brief Destroys the Sprite instance.
@@ -54,17 +55,20 @@ public:
//! Texture used for the sprite
const Texture & sprite_image;
+
//! Color tint of the sprite
Color color;
+
//! Flip settings for the sprite
FlipSettings flip;
+
//! Layer sorting level of the sprite
- uint8_t sorting_in_layer = 0;
+ const uint8_t sorting_in_layer;
//! Order within the sorting layer
- uint8_t order_in_layer = 0;
+ const uint8_t order_in_layer;
//! height in world units
- int height = 0;
+ const int height;
/**
* \aspect_ratio ratio of the img so that scaling will not become weird
diff --git a/src/crepe/api/Vector2.h b/src/crepe/api/Vector2.h
index 0688fac..2b31d90 100644
--- a/src/crepe/api/Vector2.h
+++ b/src/crepe/api/Vector2.h
@@ -37,6 +37,7 @@ struct Vector2 {
//! Divides a scalar value to both components of this vector and updates this vector.
Vector2 operator/(const T & other) const;
+ //! Divides a scalar value to both components of this vector and updates this vector.
Vector2 operator/(T other) const;
//! Adds another vector to this vector and updates this vector.
diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp
index 4887d35..6c6af55 100644
--- a/src/crepe/facade/SDLContext.cpp
+++ b/src/crepe/facade/SDLContext.cpp
@@ -8,7 +8,6 @@
#include <cmath>
#include <cstddef>
#include <functional>
-#include <iostream>
#include <memory>
#include <stdexcept>
@@ -16,10 +15,10 @@
#include "../api/Sprite.h"
#include "../api/Texture.h"
#include "../api/Transform.h"
+#include "../api/Config.h"
#include "../util/Log.h"
#include "SDLContext.h"
-#include "api/Config.h"
#include "types.h"
using namespace crepe;
diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp
index 9a8c1ba..a1443cd 100644
--- a/src/crepe/system/RenderSystem.cpp
+++ b/src/crepe/system/RenderSystem.cpp
@@ -10,6 +10,7 @@
#include "../api/Sprite.h"
#include "../api/Transform.h"
#include "../facade/SDLContext.h"
+#include "api/Camera.h"
#include "RenderSystem.h"
@@ -19,7 +20,8 @@ using namespace std;
void RenderSystem::clear_screen() { this->context.clear_screen(); }
void RenderSystem::present_screen() { this->context.present_screen(); }
-void RenderSystem::update_camera() {
+
+const Camera & RenderSystem::update_camera() {
ComponentManager & mgr = this->component_manager;
RefVector<Camera> cameras = mgr.get_components_by_type<Camera>();
@@ -31,9 +33,10 @@ void RenderSystem::update_camera() {
const Transform & transform
= mgr.get_components_by_id<Transform>(cam.game_object_id).front().get();
this->context.set_camera(cam);
- this->curr_cam_ref = &cam;
- this->curr_cam_ref->pos = transform.position + this->curr_cam_ref->offset;
+ cam.pos = transform.position + cam.offset;
+ return cam;
}
+ throw std::runtime_error("No active cameras in current scene");
}
bool sorting_comparison(const Sprite & a, const Sprite & b) {
@@ -52,12 +55,12 @@ RefVector<Sprite> RenderSystem::sort(RefVector<Sprite> & objs) const {
void RenderSystem::update() {
this->clear_screen();
- this->update_camera();
this->render();
this->present_screen();
}
-bool RenderSystem::render_particle(const Sprite & sprite, const double & scale) {
+bool RenderSystem::render_particle(const Sprite & sprite, const Camera & cam,
+ const double & scale) {
ComponentManager & mgr = this->component_manager;
@@ -73,19 +76,20 @@ bool RenderSystem::render_particle(const Sprite & sprite, const double & scale)
for (const Particle & p : em.data.particles) {
if (!p.active) continue;
- this->context.draw_particle(sprite, p.position, p.angle, scale,
- *this->curr_cam_ref);
+ this->context.draw_particle(sprite, p.position, p.angle, scale, cam);
}
}
return rendering_particles;
}
-void RenderSystem::render_normal(const Sprite & sprite, const Transform & tm) {
- this->context.draw(sprite, tm, *this->curr_cam_ref);
+void RenderSystem::render_normal(const Sprite & sprite, const Camera & cam,
+ const Transform & tm) {
+ this->context.draw(sprite, tm, cam);
}
void RenderSystem::render() {
-
ComponentManager & mgr = this->component_manager;
+ const Camera & cam = this->update_camera();
+
RefVector<Sprite> sprites = mgr.get_components_by_type<Sprite>();
RefVector<Sprite> sorted_sprites = this->sort(sprites);
@@ -94,10 +98,10 @@ void RenderSystem::render() {
const Transform & transform
= mgr.get_components_by_id<Transform>(sprite.game_object_id).front().get();
- bool rendered_particles = this->render_particle(sprite, transform.scale);
+ bool rendered_particles = this->render_particle(sprite, cam, transform.scale);
if (rendered_particles) continue;
- this->render_normal(sprite, transform);
+ this->render_normal(sprite, cam, transform);
}
}
diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h
index 08930b0..264dc4c 100644
--- a/src/crepe/system/RenderSystem.h
+++ b/src/crepe/system/RenderSystem.h
@@ -36,7 +36,7 @@ private:
void present_screen();
//! Updates the active camera used for rendering.
- void update_camera();
+ const Camera & update_camera();
//! Renders the whole screen
void render();
@@ -48,7 +48,7 @@ private:
* \param tm the Transform component for scale
* \return true if particles have been rendered
*/
- bool render_particle(const Sprite & sprite, const double & scale);
+ bool render_particle(const Sprite & sprite, const Camera & cam, const double & scale);
/**
* \brief renders a sprite with a Transform component on the screen
@@ -56,7 +56,7 @@ 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);
+ void render_normal(const Sprite & sprite, const Camera & cam, const Transform & tm);
/**
* \brief sort a vector sprite objects with
@@ -70,17 +70,12 @@ private:
* \todo Include color handling for sprites.
* \todo Add text rendering using SDL_ttf for text components.
* \todo Implement a text component and a button component.
- * \todo Ensure each sprite is checked for active status before rendering.
- * \todo Sort all layers by order before rendering.
* \todo Consider adding text input functionality.
*/
private:
- //! Pointer to the current active camera for rendering
- // TODO: needs a better solution
- Camera * curr_cam_ref = nullptr;
-
SDLContext & context = SDLContext::get_instance();
+
};
} // namespace crepe
diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp
index ec71260..6e426cf 100644
--- a/src/example/rendering_particle.cpp
+++ b/src/example/rendering_particle.cpp
@@ -33,9 +33,7 @@ int main(int argc, char * argv[]) {
auto img = Texture("asset/texture/test_ap43.png");
Sprite & test_sprite
- = game_object.add_component<Sprite>(img, color, FlipSettings{true, true});
- test_sprite.order_in_layer = 5;
- test_sprite.height = 195;
+ = game_object.add_component<Sprite>(img, color, FlipSettings{true, true}, 1, 1, 500);
//game_object.add_component<Animator>(test_sprite, 4, 1, 0).active = true;
game_object.add_component<Animator>(test_sprite, 1, 1, 0).active = true;
@@ -62,8 +60,8 @@ int main(int argc, char * argv[]) {
});
*/
- auto & cam = game_object.add_component<Camera>(Color::WHITE);
- cam.pos = {0, 0};
+ auto & cam = game_object.add_component<Camera>(Color::WHITE, ivec2{1080, 720},
+ ivec2{2000, 2000}, 1.0f);
/*
game_object