diff options
author | heavydemon21 <nielsstunnebrink1@gmail.com> | 2024-12-02 18:55:13 +0100 |
---|---|---|
committer | heavydemon21 <nielsstunnebrink1@gmail.com> | 2024-12-02 18:55:13 +0100 |
commit | 47e6d987ced269ec16ea455706513025cb9c50c5 (patch) | |
tree | 5cb48ad8565f7c5bd8b03624bc716375b8d2321b /src/crepe/system | |
parent | f23eaa64df8b0ef27f58b1632c5e659fe3737153 (diff) | |
parent | 647eb8e318f1ed1e3ec18505ea4df57025e6ffd5 (diff) |
Merge branch 'master' into niels/rendering_color
Diffstat (limited to 'src/crepe/system')
-rw-r--r-- | src/crepe/system/AnimatorSystem.cpp | 15 | ||||
-rw-r--r-- | src/crepe/system/AnimatorSystem.h | 3 | ||||
-rw-r--r-- | src/crepe/system/ParticleSystem.cpp | 22 | ||||
-rw-r--r-- | src/crepe/system/PhysicsSystem.cpp | 8 | ||||
-rw-r--r-- | src/crepe/system/RenderSystem.cpp | 107 | ||||
-rw-r--r-- | src/crepe/system/RenderSystem.h | 47 | ||||
-rw-r--r-- | src/crepe/system/ScriptSystem.cpp | 34 | ||||
-rw-r--r-- | src/crepe/system/ScriptSystem.h | 11 |
8 files changed, 133 insertions, 114 deletions
diff --git a/src/crepe/system/AnimatorSystem.cpp b/src/crepe/system/AnimatorSystem.cpp index 9d18873..4c40940 100644 --- a/src/crepe/system/AnimatorSystem.cpp +++ b/src/crepe/system/AnimatorSystem.cpp @@ -1,6 +1,4 @@ #include <cstdint> -#include <functional> -#include <vector> #include "api/Animator.h" #include "facade/SDLContext.h" @@ -13,15 +11,14 @@ using namespace crepe; void AnimatorSystem::update() { ComponentManager & mgr = this->component_manager; - std::vector<std::reference_wrapper<Animator>> animations - = mgr.get_components_by_type<Animator>(); + RefVector<Animator> animations = mgr.get_components_by_type<Animator>(); uint64_t tick = SDLContext::get_instance().get_ticks(); for (Animator & a : animations) { - if (a.active) { - a.curr_row = (tick / 100) % a.row; - a.animator_rect.x = (a.curr_row * a.animator_rect.w) + a.curr_col; - a.spritesheet.sprite_rect = a.animator_rect; - } + if (!a.active) continue; + // (10 frames per second) + a.curr_row = (tick / 100) % a.row; + a.spritesheet.mask.x = (a.curr_row * a.spritesheet.mask.w) + a.curr_col; + a.spritesheet.mask = a.spritesheet.mask; } } diff --git a/src/crepe/system/AnimatorSystem.h b/src/crepe/system/AnimatorSystem.h index 56cc7b3..f8179a9 100644 --- a/src/crepe/system/AnimatorSystem.h +++ b/src/crepe/system/AnimatorSystem.h @@ -21,12 +21,11 @@ public: /** * \brief Updates the Animator components. * - * This method is called periodically (likely every frame) to update the state of all + * This method is called to update the state of all * Animator components, moving the animations forward and managing their behavior (e.g., * looping). */ void update() override; - // FIXME: never say "likely" in the documentation lmao }; } // namespace crepe diff --git a/src/crepe/system/ParticleSystem.cpp b/src/crepe/system/ParticleSystem.cpp index 7316309..0e62a57 100644 --- a/src/crepe/system/ParticleSystem.cpp +++ b/src/crepe/system/ParticleSystem.cpp @@ -4,7 +4,6 @@ #include "api/ParticleEmitter.h" #include "api/Transform.h" -#include "api/Vector2.h" #include "ComponentManager.h" #include "ParticleSystem.h" @@ -14,8 +13,7 @@ using namespace crepe; void ParticleSystem::update() { // Get all emitters ComponentManager & mgr = this->component_manager; - std::vector<std::reference_wrapper<ParticleEmitter>> emitters - = mgr.get_components_by_type<ParticleEmitter>(); + RefVector<ParticleEmitter> emitters = mgr.get_components_by_type<ParticleEmitter>(); for (ParticleEmitter & emitter : emitters) { // Get transform linked to emitter @@ -43,17 +41,15 @@ void ParticleSystem::update() { } void ParticleSystem::emit_particle(ParticleEmitter & emitter, const Transform & transform) { - constexpr double DEG_TO_RAD = M_PI / 180.0; + constexpr float DEG_TO_RAD = M_PI / 180.0; - Vector2 initial_position = emitter.data.position + transform.position; - double random_angle - = generate_random_angle(emitter.data.min_angle, emitter.data.max_angle); + vec2 initial_position = emitter.data.position + transform.position; + float random_angle = generate_random_angle(emitter.data.min_angle, emitter.data.max_angle); - double random_speed - = generate_random_speed(emitter.data.min_speed, emitter.data.max_speed); - double angle_radians = random_angle * DEG_TO_RAD; + float random_speed = generate_random_speed(emitter.data.min_speed, emitter.data.max_speed); + float angle_radians = random_angle * DEG_TO_RAD; - Vector2 velocity + vec2 velocity = {random_speed * std::cos(angle_radians), random_speed * std::sin(angle_radians)}; for (Particle & particle : emitter.data.particles) { @@ -78,7 +74,7 @@ int ParticleSystem::calculate_update(int count, double emission) const { } void ParticleSystem::check_bounds(ParticleEmitter & emitter, const Transform & transform) { - Vector2 offset = emitter.data.boundary.offset + transform.position + emitter.data.position; + vec2 offset = emitter.data.boundary.offset + transform.position + emitter.data.position; double half_width = emitter.data.boundary.width / 2.0; double half_height = emitter.data.boundary.height / 2.0; @@ -88,7 +84,7 @@ void ParticleSystem::check_bounds(ParticleEmitter & emitter, const Transform & t const double BOTTOM = offset.y + half_height; for (Particle & particle : emitter.data.particles) { - const Vector2 & position = particle.position; + const vec2 & position = particle.position; bool within_bounds = (position.x >= LEFT && position.x <= RIGHT && position.y >= TOP && position.y <= BOTTOM); diff --git a/src/crepe/system/PhysicsSystem.cpp b/src/crepe/system/PhysicsSystem.cpp index 4a7dbfb..514a4b3 100644 --- a/src/crepe/system/PhysicsSystem.cpp +++ b/src/crepe/system/PhysicsSystem.cpp @@ -12,10 +12,8 @@ using namespace crepe; void PhysicsSystem::update() { ComponentManager & mgr = this->component_manager; - std::vector<std::reference_wrapper<Rigidbody>> rigidbodies - = mgr.get_components_by_type<Rigidbody>(); - std::vector<std::reference_wrapper<Transform>> transforms - = mgr.get_components_by_type<Transform>(); + RefVector<Rigidbody> rigidbodies = mgr.get_components_by_type<Rigidbody>(); + RefVector<Transform> transforms = mgr.get_components_by_type<Transform>(); double gravity = Config::get_instance().physics.gravity; for (Rigidbody & rigidbody : rigidbodies) { @@ -36,7 +34,7 @@ void PhysicsSystem::update() { if (rigidbody.data.angular_damping != 0) { rigidbody.data.angular_velocity *= rigidbody.data.angular_damping; } - if (rigidbody.data.linear_damping != Vector2{0, 0}) { + if (rigidbody.data.linear_damping != vec2{0, 0}) { rigidbody.data.linear_velocity *= rigidbody.data.linear_damping; } diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index 0a2b85e..1883f8f 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -1,8 +1,12 @@ +#include <algorithm> +#include <cassert> #include <cmath> #include <functional> +#include <stdexcept> #include <vector> #include "../ComponentManager.h" +#include "../api/Camera.h" #include "../api/ParticleEmitter.h" #include "../api/Sprite.h" #include "../api/Transform.h" @@ -12,73 +16,108 @@ #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::update_camera() { +void RenderSystem::present_screen() { this->context.present_screen(); } + +const Camera & RenderSystem::update_camera() { ComponentManager & mgr = this->component_manager; - std::vector<std::reference_wrapper<Camera>> cameras = mgr.get_components_by_type<Camera>(); + RefVector<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().camera(cam); - this->curr_cam = &cam; + if (!cam.active) continue; + const Transform & transform + = mgr.get_components_by_id<Transform>(cam.game_object_id).front().get(); + this->context.set_camera(cam); + this->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) { + 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; } -bool RenderSystem::render_particle(const Sprite & sprite, Transform tm) { +RefVector<Sprite> RenderSystem::sort(RefVector<Sprite> & objs) const { + RefVector<Sprite> sorted_objs(objs); + std::sort(sorted_objs.begin(), sorted_objs.end(), sorting_comparison); + + return sorted_objs; +} + +void RenderSystem::update() { + this->clear_screen(); + this->render(); + this->present_screen(); +} + +bool RenderSystem::render_particle(const Sprite & sprite, const Camera & cam, + const double & scale) { ComponentManager & mgr = this->component_manager; - SDLContext & render = SDLContext::get_instance(); - auto emitters = mgr.get_components_by_id<ParticleEmitter>(sprite.game_object_id); + vector<reference_wrapper<ParticleEmitter>> emitters + = mgr.get_components_by_id<ParticleEmitter>(sprite.game_object_id); bool rendering_particles = false; for (const ParticleEmitter & em : emitters) { - if (!em.active) continue; - if (!(em.data.sprite.game_object_id == sprite.game_object_id)) continue; - + if (!(&em.data.sprite == &sprite)) continue; rendering_particles = true; + if (!em.active) continue; for (const Particle & p : em.data.particles) { if (!p.active) continue; - tm.position = p.position; - tm.rotation = p.angle; - render.draw(em.data.sprite, tm, *curr_cam); + + this->context.draw(SDLContext::RenderContext{ + .sprite = sprite, + .cam = cam, + .cam_pos = this->cam_pos, + .pos = p.position, + .angle = p.angle, + .scale = scale, + }); } } return rendering_particles; } -void RenderSystem::render_normal(const Sprite & sprite, const Transform & tm) { - - ComponentManager & mgr = this->component_manager; - SDLContext & render = SDLContext::get_instance(); - - render.draw(sprite, tm, *curr_cam); +void RenderSystem::render_normal(const Sprite & sprite, const Camera & cam, + const Transform & tm) { + this->context.draw(SDLContext::RenderContext{ + .sprite = sprite, + .cam = cam, + .cam_pos = this->cam_pos, + .pos = tm.position, + .angle = tm.rotation, + .scale = tm.scale, + }); } void RenderSystem::render() { - ComponentManager & mgr = this->component_manager; + const Camera & cam = this->update_camera(); - auto sprites = mgr.get_components_by_type<Sprite>(); - for (const Sprite & sprite : sprites) { + RefVector<Sprite> sprites = mgr.get_components_by_type<Sprite>(); + RefVector<Sprite> sorted_sprites = this->sort(sprites); + + for (const Sprite & sprite : sorted_sprites) { if (!sprite.active) continue; - auto transform = mgr.get_components_by_id<Transform>(sprite.game_object_id); + const Transform & transform + = mgr.get_components_by_id<Transform>(sprite.game_object_id).front().get(); - bool rendered_particles = this->render_particle(sprite, transform[0].get()); + bool rendered_particles = this->render_particle(sprite, cam, transform.scale); if (rendered_particles) continue; - this->render_normal(sprite, transform[0].get()); + this->render_normal(sprite, cam, 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 6126dfe..4d542ec 100644 --- a/src/crepe/system/RenderSystem.h +++ b/src/crepe/system/RenderSystem.h @@ -1,20 +1,24 @@ #pragma once -#include "api/Camera.h" -#include "api/Sprite.h" -#include "api/Transform.h" +#include <cmath> + +#include "facade/SDLContext.h" #include "System.h" +#include "types.h" namespace crepe { +class Camera; +class Sprite; +class Transform; + /** * \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: @@ -27,13 +31,13 @@ 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(); + const Camera & update_camera(); //! Renders the whole screen void render(); @@ -48,22 +52,35 @@ private: * constructor is now protected i cannot make tmp inside * \return true if particles have been rendered */ - bool render_particle(const Sprite &, Transform tm); + bool render_particle(const Sprite & sprite, const Camera & cam, const double & scale); - void render_normal(const Sprite &, const Transform & tm); + /** + * \brief renders a sprite with a Transform component on the screen + * + * \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 Camera & cam, 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 + */ + RefVector<Sprite> sort(RefVector<Sprite> & objs) const; /** * \todo Add text rendering using SDL_ttf for text components. * \todo Implement a text component and a button component. - * \todo Sort all layers by order before rendering. * \todo Consider adding text input functionality. */ private: - //! Pointer to the current active camera for rendering - Camera * curr_cam = nullptr; - // TODO: needs a better solution + SDLContext & context = SDLContext::get_instance(); + + //! camera postion in the current scene + vec2 cam_pos; }; } // namespace crepe diff --git a/src/crepe/system/ScriptSystem.cpp b/src/crepe/system/ScriptSystem.cpp index c4d724c..20a83f7 100644 --- a/src/crepe/system/ScriptSystem.cpp +++ b/src/crepe/system/ScriptSystem.cpp @@ -1,7 +1,3 @@ -#include <forward_list> -#include <functional> -#include <vector> - #include "../ComponentManager.h" #include "../api/BehaviorScript.h" #include "../api/Script.h" @@ -14,31 +10,19 @@ using namespace crepe; void ScriptSystem::update() { dbg_trace(); - forward_list<reference_wrapper<Script>> scripts = this->get_scripts(); - - for (auto & script_ref : scripts) { - Script & script = script_ref.get(); - if (!script.initialized) { - script.init(); - script.initialized = true; - } - script.update(); - } -} - -forward_list<reference_wrapper<Script>> ScriptSystem::get_scripts() const { - forward_list<reference_wrapper<Script>> scripts = {}; ComponentManager & mgr = this->component_manager; - vector<reference_wrapper<BehaviorScript>> behavior_scripts - = mgr.get_components_by_type<BehaviorScript>(); + RefVector<BehaviorScript> behavior_scripts = mgr.get_components_by_type<BehaviorScript>(); - for (auto behavior_script_ref : behavior_scripts) { - BehaviorScript & behavior_script = behavior_script_ref.get(); + for (BehaviorScript & behavior_script : behavior_scripts) { if (!behavior_script.active) continue; + Script * script = behavior_script.script.get(); if (script == nullptr) continue; - scripts.push_front(*script); - } - return scripts; + if (!script->initialized) { + script->init(); + script->initialized = true; + } + script->update(); + } } diff --git a/src/crepe/system/ScriptSystem.h b/src/crepe/system/ScriptSystem.h index deb89cb..936e9ca 100644 --- a/src/crepe/system/ScriptSystem.h +++ b/src/crepe/system/ScriptSystem.h @@ -1,7 +1,5 @@ #pragma once -#include <forward_list> - #include "System.h" namespace crepe { @@ -25,15 +23,6 @@ public: * the \c BehaviorScript instance. */ void update() override; - -private: - /** - * \brief Aggregate all active \c BehaviorScript components and return a list - * of references to their \c Script instances (utility) - * - * \returns List of active \c Script instances - */ - std::forward_list<std::reference_wrapper<Script>> get_scripts() const; }; } // namespace crepe |