From 767f40a61952892d60d204ecaaffa07e7e396d28 Mon Sep 17 00:00:00 2001 From: heavydemon21 <nielsstunnebrink1@gmail.com> Date: Thu, 14 Nov 2024 10:05:08 +0100 Subject: color paremeter in sprite works --- src/crepe/facade/SDLContext.cpp | 15 +++++++++++++++ src/crepe/facade/SDLContext.h | 23 +++++++++++++++++++++++ src/crepe/system/RenderSystem.h | 1 + src/example/rendering.cpp | 2 +- src/example/rendering_particle.cpp | 4 ++-- 5 files changed, 42 insertions(+), 3 deletions(-) diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 378ccee..4bc4cf8 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -6,10 +6,12 @@ #include <SDL2/SDL_video.h> #include <cmath> #include <cstddef> +#include <cstdint> #include <functional> #include <iostream> #include <memory> #include <string> +#include <sys/types.h> #include <utility> #include "../api/Sprite.h" @@ -110,6 +112,13 @@ void SDLContext::present_screen() { SDL_RenderPresent(this->game_renderer.get()); } +void SDLContext::set_rbg_texture(const std::shared_ptr<Texture>& texture, const uint8_t& r, const uint8_t& g, const uint8_t& b){ + SDL_SetTextureColorMod(texture->texture.get(), r, g, b); +} +void SDLContext::set_alpha_texture(const std::shared_ptr<Texture>& texture, const uint8_t& alpha){ + SDL_SetTextureAlphaMod(texture->texture.get(), alpha ); +} + void SDLContext::draw(const Sprite & sprite, const Transform & transform, const Camera & cam) { @@ -117,6 +126,11 @@ void SDLContext::draw(const Sprite & sprite, const Transform & transform, = (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * sprite.flip.flip_x) | (SDL_FLIP_VERTICAL * sprite.flip.flip_y)); + sprite.sprite_image->texture.get(); + + this->set_rbg_texture(sprite.sprite_image, sprite.color.r, sprite.color.g, sprite.color.b); + this->set_alpha_texture(sprite.sprite_image, sprite.color.a); + double adjusted_x = (transform.position.x - cam.x) * cam.zoom; double adjusted_y = (transform.position.y - cam.y) * cam.zoom; double adjusted_w = sprite.sprite_rect.w * transform.scale * cam.zoom; @@ -129,6 +143,7 @@ void SDLContext::draw(const Sprite & sprite, const Transform & transform, .h = sprite.sprite_rect.h, }; + SDL_Rect dstrect = { .x = static_cast<int>(adjusted_x), .y = static_cast<int>(adjusted_y), diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index c4392bd..5b5ee3e 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -3,6 +3,7 @@ #include <SDL2/SDL_keycode.h> #include <SDL2/SDL_render.h> #include <SDL2/SDL_video.h> +#include <cstdint> #include <functional> #include <memory> #include <string> @@ -145,6 +146,28 @@ private: */ void camera(const Camera & camera); + /** + * \brief changes the texture rbg values with the given parameters + * it sets the allowed color inside a image. So if all the colors are 255 (MAXIMUM) + * it will show the given texture. however if the one of the colors is reduced it will reduce the + * + * + * \param texture the given texture to adjust + * \param r Red color + * \param g Green color + * \param b Blue color + */ + void set_rbg_texture(const std::shared_ptr<Texture>& texture, const uint8_t& r, const uint8_t& g, const uint8_t& b); + + + /** + * \brief Modifies the transparency of the given texture + * + * \param texture modify the given texture alpha channel + * \param alpha alpha channel + */ + void set_alpha_texture(const std::shared_ptr<Texture>& texture, const uint8_t& alpha); + private: //! sdl Window std::unique_ptr<SDL_Window, std::function<void(SDL_Window *)>> game_window; diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h index 468f79b..feefa09 100644 --- a/src/crepe/system/RenderSystem.h +++ b/src/crepe/system/RenderSystem.h @@ -59,6 +59,7 @@ private: void render_normal(const Sprite &, const Transform & tm); + /** * \todo Include color handling for sprites. * \todo Add text rendering using SDL_ttf for text components. diff --git a/src/example/rendering.cpp b/src/example/rendering.cpp index a7cb5d6..f481f03 100644 --- a/src/example/rendering.cpp +++ b/src/example/rendering.cpp @@ -33,7 +33,7 @@ int main() { obj.add_component<Camera>(Color::get_red()); } { - Color color(0, 0, 0, 0); + Color color(255, 0, 0, 0); obj1.add_component<Sprite>(make_shared<Texture>("../asset/texture/second.png"), color, FlipSettings{true, true}); obj1.add_component<Sprite>(make_shared<Texture>("../asset/texture/second.png"), color, FlipSettings{true, true}); } diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index dd08354..cb95ede 100644 --- a/src/example/rendering_particle.cpp +++ b/src/example/rendering_particle.cpp @@ -22,9 +22,9 @@ using namespace std; int main(int argc, char * argv[]) { GameObject game_object(0, "", "", Vector2{100, 100}, 0, 0.1); - Color color(0, 0, 0, 0); + Color color(255, 255, 255, 0); Sprite test_sprite = game_object.add_component<Sprite>( - make_shared<Texture>("../asset/texture/img.png"), color, + make_shared<Texture>("../asset/texture/img.png"), Color::get_red(), FlipSettings{false, false}); game_object.add_component<ParticleEmitter>(ParticleEmitter::Data{ .position = {0, 0}, -- cgit v1.2.3 From 5e6b80ba9fe18143b994a301ab0ba1ba54072b43 Mon Sep 17 00:00:00 2001 From: heavydemon21 <nielsstunnebrink1@gmail.com> Date: Thu, 14 Nov 2024 10:23:04 +0100 Subject: Working color, fucked up git merge --- src/crepe/Particle.cpp | 4 ++-- src/crepe/Particle.h | 7 +++---- src/crepe/api/AssetManager.h | 3 ++- src/crepe/api/ParticleEmitter.cpp | 14 ++++---------- src/crepe/api/ParticleEmitter.h | 14 ++++---------- src/crepe/facade/SDLContext.cpp | 2 +- src/crepe/system/ParticleSystem.cpp | 2 -- src/crepe/system/ParticleSystem.h | 15 ++++++--------- src/crepe/system/PhysicsSystem.h | 10 ++++------ src/crepe/system/RenderSystem.h | 2 -- src/example/rendering.cpp | 7 ++++--- 11 files changed, 30 insertions(+), 50 deletions(-) diff --git a/src/crepe/Particle.cpp b/src/crepe/Particle.cpp index ab55f37..582edf4 100644 --- a/src/crepe/Particle.cpp +++ b/src/crepe/Particle.cpp @@ -2,8 +2,8 @@ using namespace crepe; -void Particle::reset(uint32_t lifespan, Vector2 position, Vector2 velocity, - double angle) { +void Particle::reset(uint32_t lifespan, const Vector2 & position, + const Vector2 & velocity, double angle) { // Initialize the particle state this->time_in_life = 0; this->lifespan = lifespan; diff --git a/src/crepe/Particle.h b/src/crepe/Particle.h index 06431bb..3eaebc3 100644 --- a/src/crepe/Particle.h +++ b/src/crepe/Particle.h @@ -30,9 +30,8 @@ public: //! The time the particle has been alive, in milliseconds. uint32_t time_in_life = 0; //! The angle at which the particle is oriented or moving. - double angle; + double angle = 0; - Particle() = default; /** * \brief Resets the particle with new properties. * @@ -44,8 +43,8 @@ public: * \param velocity The initial velocity of the particle. * \param angle The angle of the particle's trajectory or orientation. */ - void reset(uint32_t lifespan, Vector2 position, Vector2 velocity, - double angle); + void reset(uint32_t lifespan, const Vector2 & position, + const Vector2 & velocity, double angle); /** * \brief Updates the particle's state. * diff --git a/src/crepe/api/AssetManager.h b/src/crepe/api/AssetManager.h index dbfaef3..86a9902 100644 --- a/src/crepe/api/AssetManager.h +++ b/src/crepe/api/AssetManager.h @@ -56,7 +56,8 @@ public: * cache. */ template <typename T> - std::shared_ptr<T> cache(const std::string & file_path, bool reload = false); + std::shared_ptr<T> cache(const std::string & file_path, + bool reload = false); }; } // namespace crepe diff --git a/src/crepe/api/ParticleEmitter.cpp b/src/crepe/api/ParticleEmitter.cpp index e7f298c..35f960d 100644 --- a/src/crepe/api/ParticleEmitter.cpp +++ b/src/crepe/api/ParticleEmitter.cpp @@ -1,18 +1,12 @@ #include "ParticleEmitter.h" -#include "Particle.h" using namespace crepe; -ParticleEmitter::ParticleEmitter(uint32_t game_object_id, const Data & data) - : Component(game_object_id), data(data) { +ParticleEmitter::ParticleEmitter(game_object_id_t game_object_id, + const Data & data) + : Component(game_object_id), + data(data) { for (size_t i = 0; i < this->data.max_particles; i++) { this->data.particles.emplace_back(); } } - -ParticleEmitter::~ParticleEmitter() { - std::vector<Particle>::iterator it = this->data.particles.begin(); - while (it != this->data.particles.end()) { - it = this->data.particles.erase(it); - } -} diff --git a/src/crepe/api/ParticleEmitter.h b/src/crepe/api/ParticleEmitter.h index 83a1588..a9e872f 100644 --- a/src/crepe/api/ParticleEmitter.h +++ b/src/crepe/api/ParticleEmitter.h @@ -1,24 +1,21 @@ #pragma once -#include <cstdint> #include <vector> #include "Component.h" #include "Particle.h" -#include "Sprite.h" #include "Vector2.h" -// class Sprite; - namespace crepe { +class Sprite; + /** * \brief Data holder for particle emission parameters. * * The ParticleEmitter class stores configuration data for particle properties, * defining the characteristics and boundaries of particle emissions. */ - class ParticleEmitter : public Component { public: /** @@ -48,7 +45,7 @@ public: //! position of the emitter Vector2 position; //! maximum number of particles - const uint32_t max_particles = 0; + const unsigned int max_particles = 0; //! rate of particle emission per update (Lowest value = 0.001 any lower is ignored) double emission_rate = 0; //! min speed of the particles @@ -75,13 +72,10 @@ public: public: /** - * \brief Constructs a ParticleEmitter data holder with specified settings. - * * \param game_object_id Identifier for the game object using this emitter. * \param data Configuration data defining particle properties. */ - ParticleEmitter(uint32_t game_object_id, const Data & data); - ~ParticleEmitter(); + ParticleEmitter(game_object_id_t game_object_id, const Data & data); public: //! Configuration data for particle emission settings. diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 4bc4cf8..c78a3ca 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -174,7 +174,7 @@ SDLContext::texture_from_path(const std::string & path) { SDL_Surface * tmp = IMG_Load(path.c_str()); if (tmp == nullptr) { - tmp = IMG_Load("../asset/texture/ERROR.png"); + tmp = IMG_Load("../asset/texture/ERROR.png"); } std::unique_ptr<SDL_Surface, std::function<void(SDL_Surface *)>> diff --git a/src/crepe/system/ParticleSystem.cpp b/src/crepe/system/ParticleSystem.cpp index 4a25b47..e7a3bec 100644 --- a/src/crepe/system/ParticleSystem.cpp +++ b/src/crepe/system/ParticleSystem.cpp @@ -11,8 +11,6 @@ using namespace crepe; -ParticleSystem::ParticleSystem() {} - void ParticleSystem::update() { // Get all emitters ComponentManager & mgr = ComponentManager::get_instance(); diff --git a/src/crepe/system/ParticleSystem.h b/src/crepe/system/ParticleSystem.h index 3155df1..d7ca148 100644 --- a/src/crepe/system/ParticleSystem.h +++ b/src/crepe/system/ParticleSystem.h @@ -2,23 +2,20 @@ #include <cstdint> +#include "System.h" + namespace crepe { class ParticleEmitter; class Transform; /** * \brief ParticleSystem class responsible for managing particle emission, updates, and bounds checking. */ -class ParticleSystem { +class ParticleSystem : public System { public: - /** - * \brief Default constructor. - */ - ParticleSystem(); - /** * \brief Updates all particle emitters by emitting particles, updating particle states, and checking bounds. */ - void update(); + void update() override; private: /** @@ -66,9 +63,9 @@ private: private: //! Counter to count updates to determine how many times emit_particle is called. - uint32_t update_count = 0; + unsigned int update_count = 0; //! Determines the lowest amount of emission rate (1000 = 0.001 = 1 particle per 1000 updates). - static constexpr uint32_t MAX_UPDATE_COUNT = 100; + static constexpr unsigned int MAX_UPDATE_COUNT = 100; }; } // namespace crepe diff --git a/src/crepe/system/PhysicsSystem.h b/src/crepe/system/PhysicsSystem.h index cc13b70..038c120 100644 --- a/src/crepe/system/PhysicsSystem.h +++ b/src/crepe/system/PhysicsSystem.h @@ -1,5 +1,7 @@ #pragma once +#include "System.h" + namespace crepe { /** * \brief System that controls all physics @@ -7,18 +9,14 @@ namespace crepe { * This class is a physics system that uses a rigidbody and transform * to add physics to a game object. */ -class PhysicsSystem { +class PhysicsSystem : public System { public: - /** - * Constructor is default - */ - PhysicsSystem() = default; /** * \brief updates the physics system. * * It calculates new velocties and changes the postion in the transform. */ - void update(); + void update() override; }; } // namespace crepe diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h index feefa09..6529d41 100644 --- a/src/crepe/system/RenderSystem.h +++ b/src/crepe/system/RenderSystem.h @@ -61,10 +61,8 @@ 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. */ diff --git a/src/example/rendering.cpp b/src/example/rendering.cpp index f481f03..827ad07 100644 --- a/src/example/rendering.cpp +++ b/src/example/rendering.cpp @@ -33,9 +33,10 @@ int main() { obj.add_component<Camera>(Color::get_red()); } { - Color color(255, 0, 0, 0); - obj1.add_component<Sprite>(make_shared<Texture>("../asset/texture/second.png"), color, FlipSettings{true, true}); - obj1.add_component<Sprite>(make_shared<Texture>("../asset/texture/second.png"), color, FlipSettings{true, true}); + Color color(0, 0, 0, 0); + obj1.add_component<Sprite>( + make_shared<Texture>("../asset/texture/second.png"), color, + FlipSettings{true, true}); } /* -- cgit v1.2.3 From 60669b60e63532bc264ecd6d106bd15f0688a5b6 Mon Sep 17 00:00:00 2001 From: heavydemon21 <nielsstunnebrink1@gmail.com> Date: Thu, 14 Nov 2024 20:03:28 +0100 Subject: adapted some files --- asset/texture/img.png | Bin 92742 -> 11476 bytes src/example/rendering_particle.cpp | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/asset/texture/img.png b/asset/texture/img.png index 43b1eca..649a3f1 100644 Binary files a/asset/texture/img.png and b/asset/texture/img.png differ diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index cb95ede..75be7ff 100644 --- a/src/example/rendering_particle.cpp +++ b/src/example/rendering_particle.cpp @@ -22,7 +22,7 @@ using namespace std; int main(int argc, char * argv[]) { GameObject game_object(0, "", "", Vector2{100, 100}, 0, 0.1); - Color color(255, 255, 255, 0); + Color color(255, 255, 255, 255); Sprite test_sprite = game_object.add_component<Sprite>( make_shared<Texture>("../asset/texture/img.png"), Color::get_red(), FlipSettings{false, false}); -- cgit v1.2.3 From d1f379b8f8b157f0ba4c95940e221fc58463d942 Mon Sep 17 00:00:00 2001 From: heavydemon21 <nielsstunnebrink1@gmail.com> Date: Mon, 18 Nov 2024 14:19:06 +0100 Subject: merged master --- src/crepe/system/RenderSystem.cpp | 18 +++++++----------- src/crepe/system/RenderSystem.h | 7 +++++-- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index 52dd5fc..4abd9bd 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -6,11 +6,9 @@ #include "../api/Sprite.h" #include "../api/Transform.h" #include "../facade/SDLContext.h" -#include "../util/log.h" #include "../api/ParticleEmitter.h" #include "../api/Vector2.h" -#include "Particle.h" #include "RenderSystem.h" using namespace crepe; @@ -30,17 +28,15 @@ void RenderSystem::update_camera() { } bool RenderSystem::render_particle(const Sprite & sprite, - const Transform & tm) { + Transform tm) { - ComponentManager & mgr = ComponentManager::get_instance(); + ComponentManager & mgr = this->component_manager; SDLContext & render = SDLContext::get_instance(); auto emitters = mgr.get_components_by_id<ParticleEmitter>(sprite.game_object_id); bool rendering_particles = false; - Transform tmp(0, Vector2{0, 0}, 0, 0); - tmp.scale = tm.scale; for (const ParticleEmitter & em : emitters) { if (!em.active) continue; if (!(em.data.sprite.game_object_id == sprite.game_object_id)) continue; @@ -49,16 +45,16 @@ bool RenderSystem::render_particle(const Sprite & sprite, for (const Particle & p : em.data.particles) { if (!p.active) continue; - tmp.position = p.position; - tmp.rotation = p.angle; - render.draw(em.data.sprite, tmp, *curr_cam); + tm.position = p.position; + tm.rotation = p.angle; + render.draw(em.data.sprite, tm, *curr_cam); } } return rendering_particles; } void RenderSystem::render_normal(const Sprite & sprite, const Transform & tm) { - ComponentManager & mgr = ComponentManager::get_instance(); + ComponentManager & mgr = this->component_manager; SDLContext & render = SDLContext::get_instance(); render.draw(sprite, tm, *curr_cam); @@ -66,7 +62,7 @@ void RenderSystem::render_normal(const Sprite & sprite, const Transform & tm) { void RenderSystem::render() { - ComponentManager & mgr = ComponentManager::get_instance(); + ComponentManager & mgr = this->component_manager; auto sprites = mgr.get_components_by_type<Sprite>(); for (const Sprite & sprite : sprites) { diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h index ebad05d..6126dfe 100644 --- a/src/crepe/system/RenderSystem.h +++ b/src/crepe/system/RenderSystem.h @@ -42,10 +42,13 @@ private: * \brief Renders all the particles on the screen from a given sprite. * * \param sprite renders the particles with given texture - * \param tm the Transform component for scale + * \param tm the Transform component for scale. This is not a const reference because each + * particle has a position and rotation that needs to overwrite the transform position and + * rotation without overwriting the current transform. and because the transform + * constructor is now protected i cannot make tmp inside * \return true if particles have been rendered */ - bool render_particle(const Sprite &, const Transform & tm); + bool render_particle(const Sprite &, Transform tm); void render_normal(const Sprite &, const Transform & tm); -- cgit v1.2.3 From f23eaa64df8b0ef27f58b1632c5e659fe3737153 Mon Sep 17 00:00:00 2001 From: heavydemon21 <nielsstunnebrink1@gmail.com> Date: Mon, 18 Nov 2024 21:19:28 +0100 Subject: implemented PR#35 feedback --- src/crepe/facade/SDLContext.cpp | 3 --- src/crepe/system/RenderSystem.cpp | 9 ++++----- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index b56b5e7..4a0ac1a 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -118,8 +118,6 @@ void SDLContext::draw(const Sprite & sprite, const Transform & transform, const = (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * sprite.flip.flip_x) | (SDL_FLIP_VERTICAL * sprite.flip.flip_y)); - sprite.sprite_image->texture.get(); - this->set_rbg_texture(sprite.sprite_image, sprite.color.r, sprite.color.g, sprite.color.b); this->set_alpha_texture(sprite.sprite_image, sprite.color.a); @@ -135,7 +133,6 @@ void SDLContext::draw(const Sprite & sprite, const Transform & transform, const .h = sprite.sprite_rect.h, }; - SDL_Rect dstrect = { .x = static_cast<int>(adjusted_x), .y = static_cast<int>(adjusted_y), diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index 4abd9bd..0a2b85e 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -3,11 +3,11 @@ #include <vector> #include "../ComponentManager.h" +#include "../api/ParticleEmitter.h" #include "../api/Sprite.h" #include "../api/Transform.h" -#include "../facade/SDLContext.h" -#include "../api/ParticleEmitter.h" #include "../api/Vector2.h" +#include "../facade/SDLContext.h" #include "RenderSystem.h" @@ -27,8 +27,7 @@ void RenderSystem::update_camera() { } } -bool RenderSystem::render_particle(const Sprite & sprite, - Transform tm) { +bool RenderSystem::render_particle(const Sprite & sprite, Transform tm) { ComponentManager & mgr = this->component_manager; SDLContext & render = SDLContext::get_instance(); @@ -56,7 +55,7 @@ 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); } -- cgit v1.2.3 From 9e1d1f0952ed09ee4b0c241fad3d0d66b380b1a3 Mon Sep 17 00:00:00 2001 From: heavydemon21 <nielsstunnebrink1@gmail.com> Date: Mon, 2 Dec 2024 18:59:43 +0100 Subject: fixed the color functionality --- src/crepe/facade/SDLContext.cpp | 20 +++++++++++++++----- src/crepe/facade/SDLContext.h | 10 ++++++---- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index c01d118..cfe79d2 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -19,6 +19,7 @@ #include "../util/Log.h" #include "SDLContext.h" +#include "api/Color.h" #include "types.h" using namespace crepe; @@ -131,10 +132,16 @@ void SDLContext::draw(const RenderContext & ctx) { = (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * ctx.sprite.flip.flip_x) | (SDL_FLIP_VERTICAL * ctx.sprite.flip.flip_y)); + const Color & color = ctx.sprite.color; + SDL_Rect srcrect = this->get_src_rect(ctx.sprite); SDL_Rect dstrect = this->get_dst_rect(ctx.sprite, ctx.pos, ctx.cam, ctx.cam_pos, ctx.scale); + this->set_rbg_texture(ctx.sprite.sprite_image, color.r, color.g, color.b); + this->set_alpha_texture(ctx.sprite.sprite_image, color.a); + + SDL_RenderCopyEx(this->game_renderer.get(), ctx.sprite.sprite_image.texture.get(), &srcrect, &dstrect, ctx.angle, NULL, render_flip); } @@ -221,9 +228,12 @@ int SDLContext::get_height(const Texture & ctx) const { return h; } void SDLContext::delay(int ms) const { SDL_Delay(ms); } -void SDLContext::set_rbg_texture(const std::shared_ptr<Texture>& texture, const uint8_t& r, const uint8_t& g, const uint8_t& b){ - SDL_SetTextureColorMod(texture->texture.get(), r, g, b); + +void SDLContext::set_rbg_texture(const Texture & texture, const uint8_t & r, + const uint8_t & g, const uint8_t & b) { + SDL_SetTextureColorMod(texture.texture.get(), r, g, b); +} +void SDLContext::set_alpha_texture(const Texture & texture, + const uint8_t & alpha) { + SDL_SetTextureAlphaMod(texture.texture.get(), alpha); } -void SDLContext::set_alpha_texture(const std::shared_ptr<Texture>& texture, const uint8_t& alpha){ - SDL_SetTextureAlphaMod(texture->texture.get(), alpha ); -} \ No newline at end of file diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index 6284a9c..8d63c43 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -12,6 +12,7 @@ #include "../api/Camera.h" #include "../api/Sprite.h" +#include "api/Texture.h" #include "types.h" namespace crepe { @@ -160,7 +161,7 @@ private: */ SDL_Rect get_dst_rect(const Sprite & sprite, const vec2 & pos, const Camera & cam, const vec2 & cam_pos, const double & img_scale) const; -/** + /** * \brief changes the texture rbg values with the given parameters * it sets the allowed color inside a image. So if all the colors are 255 (MAXIMUM) * it will show the given texture. however if the one of the colors is reduced it will reduce the @@ -171,8 +172,8 @@ private: * \param g Green color * \param b Blue color */ - void set_rbg_texture(const std::shared_ptr<Texture>& texture, const uint8_t& r, const uint8_t& g, const uint8_t& b); - + void set_rbg_texture(const Texture & texture, const uint8_t & r, + const uint8_t & g, const uint8_t & b); /** * \brief Modifies the transparency of the given texture @@ -180,7 +181,8 @@ private: * \param texture modify the given texture alpha channel * \param alpha alpha channel */ - void set_alpha_texture(const std::shared_ptr<Texture>& texture, const uint8_t& alpha); + void set_alpha_texture(const Texture & texture, const uint8_t & alpha); + private: //! sdl Window std::unique_ptr<SDL_Window, std::function<void(SDL_Window *)>> game_window; -- cgit v1.2.3 From cffa9f014c67133dc48569beb53a5bd6cb166faa Mon Sep 17 00:00:00 2001 From: heavydemon21 <nielsstunnebrink1@gmail.com> Date: Mon, 2 Dec 2024 19:18:41 +0100 Subject: rendering color working --- src/crepe/facade/SDLContext.cpp | 4 ++++ src/crepe/facade/SDLContext.h | 8 +++----- src/example/rendering_particle.cpp | 4 ++-- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index cfe79d2..aaaec6b 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -1,4 +1,5 @@ #include <SDL2/SDL.h> +#include <SDL2/SDL_blendmode.h> #include <SDL2/SDL_image.h> #include <SDL2/SDL_keycode.h> #include <SDL2/SDL_rect.h> @@ -9,6 +10,7 @@ #include <cstddef> #include <cstdint> #include <functional> +#include <iostream> #include <memory> #include <stdexcept> @@ -215,6 +217,7 @@ SDLContext::texture_from_path(const std::string & path) { std::unique_ptr<SDL_Texture, std::function<void(SDL_Texture *)>> img_texture; img_texture = {tmp_texture, [](SDL_Texture * texture) { SDL_DestroyTexture(texture); }}; + SDL_SetTextureBlendMode(img_texture.get(), SDL_BLENDMODE_BLEND); return img_texture; } int SDLContext::get_width(const Texture & ctx) const { @@ -235,5 +238,6 @@ void SDLContext::set_rbg_texture(const Texture & texture, const uint8_t & r, } void SDLContext::set_alpha_texture(const Texture & texture, const uint8_t & alpha) { + SDL_SetTextureAlphaMod(texture.texture.get(), alpha); } diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index 8d63c43..1c67301 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -162,10 +162,7 @@ private: SDL_Rect get_dst_rect(const Sprite & sprite, const vec2 & pos, const Camera & cam, const vec2 & cam_pos, const double & img_scale) const; /** - * \brief changes the texture rbg values with the given parameters - * it sets the allowed color inside a image. So if all the colors are 255 (MAXIMUM) - * it will show the given texture. however if the one of the colors is reduced it will reduce the - * + * \brief Set an additional color value multiplied into render copy operations. * * \param texture the given texture to adjust * \param r Red color @@ -175,8 +172,9 @@ private: void set_rbg_texture(const Texture & texture, const uint8_t & r, const uint8_t & g, const uint8_t & b); + /** - * \brief Modifies the transparency of the given texture + * \brief Set an additional alpha value multiplied into render copy operations. * * \param texture modify the given texture alpha channel * \param alpha alpha channel diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index 3a12144..349d11e 100644 --- a/src/example/rendering_particle.cpp +++ b/src/example/rendering_particle.cpp @@ -28,7 +28,7 @@ int main(int argc, char * argv[]) { ParticleSystem psys{mgr}; AnimatorSystem asys{mgr}; - Color color(255, 255, 255, 255); + Color color(255, 255, 255, 100); auto img = Texture("asset/texture/test_ap43.png"); Sprite & test_sprite = game_object.add_component<Sprite>( @@ -59,7 +59,7 @@ int main(int argc, char * argv[]) { }); */ - auto & cam = game_object.add_component<Camera>(Color::WHITE, ivec2{1080, 720}, + auto & cam = game_object.add_component<Camera>(Color::RED, ivec2{1080, 720}, vec2{2000, 2000}, 1.0f); /* -- cgit v1.2.3 From e1a6c367a7d4233ed5f38a68ee4d69c3cef1c4f5 Mon Sep 17 00:00:00 2001 From: heavydemon21 <nielsstunnebrink1@gmail.com> Date: Mon, 2 Dec 2024 19:19:12 +0100 Subject: make format --- src/crepe/facade/SDLContext.cpp | 8 +++----- src/crepe/facade/SDLContext.h | 5 ++--- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index aaaec6b..fe6aa45 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -143,7 +143,6 @@ void SDLContext::draw(const RenderContext & ctx) { this->set_rbg_texture(ctx.sprite.sprite_image, color.r, color.g, color.b); this->set_alpha_texture(ctx.sprite.sprite_image, color.a); - SDL_RenderCopyEx(this->game_renderer.get(), ctx.sprite.sprite_image.texture.get(), &srcrect, &dstrect, ctx.angle, NULL, render_flip); } @@ -232,12 +231,11 @@ int SDLContext::get_height(const Texture & ctx) const { } void SDLContext::delay(int ms) const { SDL_Delay(ms); } -void SDLContext::set_rbg_texture(const Texture & texture, const uint8_t & r, - const uint8_t & g, const uint8_t & b) { +void SDLContext::set_rbg_texture(const Texture & texture, const uint8_t & r, const uint8_t & g, + const uint8_t & b) { SDL_SetTextureColorMod(texture.texture.get(), r, g, b); } -void SDLContext::set_alpha_texture(const Texture & texture, - const uint8_t & alpha) { +void SDLContext::set_alpha_texture(const Texture & texture, const uint8_t & alpha) { SDL_SetTextureAlphaMod(texture.texture.get(), alpha); } diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index 1c67301..e128bfd 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -169,9 +169,8 @@ private: * \param g Green color * \param b Blue color */ - void set_rbg_texture(const Texture & texture, const uint8_t & r, - const uint8_t & g, const uint8_t & b); - + void set_rbg_texture(const Texture & texture, const uint8_t & r, const uint8_t & g, + const uint8_t & b); /** * \brief Set an additional alpha value multiplied into render copy operations. -- cgit v1.2.3 From b6609b0fbc263f4fd93791f3897c5f2840b962f2 Mon Sep 17 00:00:00 2001 From: heavydemon21 <nielsstunnebrink1@gmail.com> Date: Tue, 3 Dec 2024 09:28:12 +0100 Subject: implemented feedback removed get_width and height --> get_size for both texture and sdlcontext --- src/crepe/api/Sprite.cpp | 4 ++-- src/crepe/api/Texture.cpp | 11 ++++------- src/crepe/api/Texture.h | 13 ++++--------- src/crepe/facade/SDLContext.cpp | 32 +++++++++++--------------------- src/crepe/facade/SDLContext.h | 29 ++++++----------------------- src/crepe/system/RenderSystem.cpp | 3 +-- src/crepe/system/RenderSystem.h | 1 + 7 files changed, 29 insertions(+), 64 deletions(-) diff --git a/src/crepe/api/Sprite.cpp b/src/crepe/api/Sprite.cpp index 8647794..0a2ad4c 100644 --- a/src/crepe/api/Sprite.cpp +++ b/src/crepe/api/Sprite.cpp @@ -22,8 +22,8 @@ Sprite::Sprite(game_object_id_t id, Texture & image, const Color & color, dbg_trace(); - this->mask.w = sprite_image.get_width(); - this->mask.h = sprite_image.get_height(); + this->mask.w = sprite_image.get_size().x; + this->mask.h = sprite_image.get_size().y; this->aspect_ratio = static_cast<double>(this->mask.w) / this->mask.h; } diff --git a/src/crepe/api/Texture.cpp b/src/crepe/api/Texture.cpp index e43bdaa..c23b91e 100644 --- a/src/crepe/api/Texture.cpp +++ b/src/crepe/api/Texture.cpp @@ -2,6 +2,7 @@ #include "../util/Log.h" #include "Asset.h" +#include "types.h" #include "Texture.h" using namespace crepe; @@ -31,11 +32,7 @@ void Texture::load(const Asset & res) { this->texture = ctx.texture_from_path(res.get_path()); } -int Texture::get_width() const { - if (this->texture == nullptr) return 0; - return SDLContext::get_instance().get_width(*this); -} -int Texture::get_height() const { - if (this->texture == nullptr) return 0; - return SDLContext::get_instance().get_height(*this); +ivec2 Texture::get_size() const { + if (this->texture == nullptr) return {}; + return SDLContext::get_instance().get_size(*this); } diff --git a/src/crepe/api/Texture.h b/src/crepe/api/Texture.h index 7206a66..1817910 100644 --- a/src/crepe/api/Texture.h +++ b/src/crepe/api/Texture.h @@ -8,6 +8,7 @@ #include <memory> #include "Asset.h" +#include "types.h" namespace crepe { @@ -42,16 +43,10 @@ public: Texture & operator=(const Texture &) = delete; /** - * \brief Gets the width of the texture. - * \return Width of the texture in pixels. + * \brief Gets the width and height of the texture. + * \return Width and height of the texture in pixels. */ - int get_width() const; - - /** - * \brief Gets the height of the texture. - * \return Height of the texture in pixels. - */ - int get_height() const; + ivec2 get_size() const; private: /** diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index fe6aa45..8d8a87d 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -134,15 +134,11 @@ void SDLContext::draw(const RenderContext & ctx) { = (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * ctx.sprite.flip.flip_x) | (SDL_FLIP_VERTICAL * ctx.sprite.flip.flip_y)); - const Color & color = ctx.sprite.color; - SDL_Rect srcrect = this->get_src_rect(ctx.sprite); SDL_Rect dstrect = this->get_dst_rect(ctx.sprite, ctx.pos, ctx.cam, ctx.cam_pos, ctx.scale); - this->set_rbg_texture(ctx.sprite.sprite_image, color.r, color.g, color.b); - this->set_alpha_texture(ctx.sprite.sprite_image, color.a); - + this->set_color_texture(ctx.sprite.sprite_image, ctx.sprite.color); SDL_RenderCopyEx(this->game_renderer.get(), ctx.sprite.sprite_image.texture.get(), &srcrect, &dstrect, ctx.angle, NULL, render_flip); } @@ -219,23 +215,17 @@ SDLContext::texture_from_path(const std::string & path) { SDL_SetTextureBlendMode(img_texture.get(), SDL_BLENDMODE_BLEND); return img_texture; } -int SDLContext::get_width(const Texture & ctx) const { - int w; - SDL_QueryTexture(ctx.texture.get(), NULL, NULL, &w, NULL); - return w; -} -int SDLContext::get_height(const Texture & ctx) const { - int h; - SDL_QueryTexture(ctx.texture.get(), NULL, NULL, NULL, &h); - return h; -} -void SDLContext::delay(int ms) const { SDL_Delay(ms); } -void SDLContext::set_rbg_texture(const Texture & texture, const uint8_t & r, const uint8_t & g, - const uint8_t & b) { - SDL_SetTextureColorMod(texture.texture.get(), r, g, b); + +ivec2 SDLContext::get_size(const Texture & ctx){ + ivec2 size; + SDL_QueryTexture(ctx.texture.get(), NULL, NULL, &size.x, &size.y); + return size; } -void SDLContext::set_alpha_texture(const Texture & texture, const uint8_t & alpha) { - SDL_SetTextureAlphaMod(texture.texture.get(), alpha); +void SDLContext::delay(int ms) const { SDL_Delay(ms); } + +void SDLContext::set_color_texture(const Texture & texture, const Color & color) { + SDL_SetTextureColorMod(texture.texture.get(), color.r, color.g, color.b); + SDL_SetTextureAlphaMod(texture.texture.get(), color.a); } diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index e128bfd..e49ca78 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -12,6 +12,7 @@ #include "../api/Camera.h" #include "../api/Sprite.h" +#include "api/Color.h" #include "api/Texture.h" #include "types.h" @@ -105,18 +106,11 @@ private: std::unique_ptr<SDL_Texture, std::function<void(SDL_Texture *)>> texture_from_path(const std::string & path); /** - * \brief Gets the width of a texture. + * \brief Gets the size of a texture. * \param texture Reference to the Texture object. - * \return Width of the texture as an integer. + * \return Width and height of the texture as an integer. */ - int get_width(const Texture & texture) const; - - /** - * \brief Gets the height of a texture. - * \param texture Reference to the Texture object. - * \return Height of the texture as an integer. - */ - int get_height(const Texture & texture) const; + ivec2 get_size(const Texture & ctx); private: //! Will use draw,clear_screen, present_screen, camera. @@ -165,20 +159,9 @@ private: * \brief Set an additional color value multiplied into render copy operations. * * \param texture the given texture to adjust - * \param r Red color - * \param g Green color - * \param b Blue color - */ - void set_rbg_texture(const Texture & texture, const uint8_t & r, const uint8_t & g, - const uint8_t & b); - - /** - * \brief Set an additional alpha value multiplied into render copy operations. - * - * \param texture modify the given texture alpha channel - * \param alpha alpha channel + * \param color the color data for the texture */ - void set_alpha_texture(const Texture & texture, const uint8_t & alpha); + void set_color_texture(const Texture & texture, const Color & color); private: //! sdl Window diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index 1883f8f..11c9669 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -10,7 +10,6 @@ #include "../api/ParticleEmitter.h" #include "../api/Sprite.h" #include "../api/Transform.h" -#include "../api/Vector2.h" #include "../facade/SDLContext.h" #include "RenderSystem.h" @@ -71,7 +70,7 @@ bool RenderSystem::render_particle(const Sprite & sprite, const Camera & cam, bool rendering_particles = false; for (const ParticleEmitter & em : emitters) { - if (!(&em.data.sprite == &sprite)) continue; + if (&em.data.sprite != &sprite) continue; rendering_particles = true; if (!em.active) continue; diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h index 4d542ec..096d058 100644 --- a/src/crepe/system/RenderSystem.h +++ b/src/crepe/system/RenderSystem.h @@ -77,6 +77,7 @@ private: */ private: + // FIXME: retrieve sdlcontext via mediator after #PR57 SDLContext & context = SDLContext::get_instance(); //! camera postion in the current scene -- cgit v1.2.3 From cc821016c8ddce45a1e3f192415f58be237b8a1e Mon Sep 17 00:00:00 2001 From: Loek Le Blansch <loek@pipeframe.xyz> Date: Tue, 3 Dec 2024 11:24:12 +0100 Subject: `make format` --- src/crepe/api/Texture.cpp | 2 +- src/crepe/facade/SDLContext.cpp | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/crepe/api/Texture.cpp b/src/crepe/api/Texture.cpp index c23b91e..2b56271 100644 --- a/src/crepe/api/Texture.cpp +++ b/src/crepe/api/Texture.cpp @@ -2,8 +2,8 @@ #include "../util/Log.h" #include "Asset.h" -#include "types.h" #include "Texture.h" +#include "types.h" using namespace crepe; using namespace std; diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 8d8a87d..e8be7ca 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -216,8 +216,7 @@ SDLContext::texture_from_path(const std::string & path) { return img_texture; } - -ivec2 SDLContext::get_size(const Texture & ctx){ +ivec2 SDLContext::get_size(const Texture & ctx) { ivec2 size; SDL_QueryTexture(ctx.texture.get(), NULL, NULL, &size.x, &size.y); return size; -- cgit v1.2.3 From 41f9352ec236bbd0035644d983d9a2402bb8326f Mon Sep 17 00:00:00 2001 From: Loek Le Blansch <loek@pipeframe.xyz> Date: Tue, 3 Dec 2024 20:47:44 +0100 Subject: wrap lines --- src/doc/feature/scene.dox | 41 ++++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/src/doc/feature/scene.dox b/src/doc/feature/scene.dox index d81df4c..4124e37 100644 --- a/src/doc/feature/scene.dox +++ b/src/doc/feature/scene.dox @@ -6,10 +6,11 @@ namespace crepe { \ingroup feature \brief User-defined scenes -Scenes can be used to implement game environments, and allow arbitrary game objects to be organized -as part of the game structure. Scenes are implemented as derivative classes of Scene, which are -added to the game using the SceneManager. Scenes describe the start of a Scene and cannot modify -GameObjects during runtime of a Scene (use \ref feature_script "Scripting" for this purpose). +Scenes can be used to implement game environments, and allow arbitrary game +objects to be organized as part of the game structure. Scenes are implemented as +derivative classes of Scene, which are added to the game using the SceneManager. +Scenes describe the start of a Scene and cannot modify GameObjects during +runtime of a Scene (use \ref feature_script for this purpose). \see SceneManager \see GameObject @@ -18,19 +19,25 @@ GameObjects during runtime of a Scene (use \ref feature_script "Scripting" for t \par Example -This example demonstrates how to define and add scenes to the loop/scene manager in the `crepe` framework. -Each concrete scene should be derived from Scene. In the example below, the concrete scene is named MyScene. -A concrete scene should, at least, implement (override) two methods, namely load_scene() and get_name(). The -scene is build (using GameObjects) in the load_scene() method. GameObjects should be made using the -component_manager::new_object(). In the example below, two GameObjects (named object1 and object2) are added -to MyScene. object1 and object2 do not have any non-default Components attached to them, however, if needed, -this should also be done in load_scene(). Each concrete scene must have a unique name. This unique name is -used to load a new concrete scene (via a Script). The unique name is set using the get_name() method. In the -example below, MyScene's unique name is my_scene. -After setting up one or more concrete scene(s), the concrete scene(s) should be added to the loop/scene manager. -This is done in your main(). Firstly, the LoopManager should be instantiated. Than, all the concrete scene(s) -should be added to the loop/scene manger via loop_mgr::add_scene<>(). The templated argument should define the -concrete scene to be added. +This example demonstrates how to define and add scenes to the loop/scene manager +in the `crepe` framework. Each concrete scene should be derived from Scene. In +the example below, the concrete scene is named MyScene. A concrete scene should, +at least, implement (override) two methods, namely load_scene() and get_name(). +The scene is build (using GameObjects) in the load_scene() method. GameObjects +should be made using the component_manager::new_object(). + +In the example below, two GameObjects (named object1 and object2) are added to +MyScene. object1 and object2 do not have any non-default Components attached to +them, however, if needed, this should also be done in load_scene(). Each +concrete scene must have a unique name. This unique name is used to load a new +concrete scene (via a Script). The unique name is set using the get_name() +method. In the example below, MyScene's unique name is my_scene. + +After setting up one or more concrete scene(s), the concrete scene(s) should be +added to the loop/scene manager. This is done in your main(). Firstly, the +LoopManager should be instantiated. Than, all the concrete scene(s) should be +added to the loop/scene manger via loop_mgr::add_scene<>(). The templated +argument should define the concrete scene to be added. ```cpp #include <crepe/api/LoopManager.h> -- cgit v1.2.3 From f137451d0edb13543919cf2d1a3af379cb3a1485 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch <loek@pipeframe.xyz> Date: Tue, 3 Dec 2024 21:14:53 +0100 Subject: update feature listing --- src/doc/features.dox | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/doc/features.dox b/src/doc/features.dox index 4786bed..96b7c6c 100644 --- a/src/doc/features.dox +++ b/src/doc/features.dox @@ -1,10 +1,28 @@ // vim:ft=doxygen /** +\htmlonly +<style> +table.memberdecls { display: none; } +ul { margin: 1ex 0pt; } +</style> +\endhtmlonly + \defgroup feature Features \brief Engine components This page lists engine features and contains usage instructions for each feature. +\par Features + +- Scripting + - \ref feature_script <br>\copybrief feature_script + +- Game flow management + - \ref feature_scene <br>\copybrief feature_scene + +- Entity + - \ref feature_gameobject <br>\copybrief feature_gameobject + */ -- cgit v1.2.3 From c7c4cc0e3b1a3152256bc8ebf6494c19519538db Mon Sep 17 00:00:00 2001 From: WBoerenkamps <wrj.boerenkamps@student.avans.nl> Date: Wed, 4 Dec 2024 10:26:32 +0100 Subject: looptimer no singleton --- src/crepe/api/LoopManager.cpp | 31 +++++++++++++++++++------------ src/crepe/api/LoopManager.h | 4 +++- src/crepe/api/LoopTimer.cpp | 37 ++++++++++++++++++------------------- src/crepe/api/LoopTimer.h | 15 ++------------- 4 files changed, 42 insertions(+), 45 deletions(-) diff --git a/src/crepe/api/LoopManager.cpp b/src/crepe/api/LoopManager.cpp index 7edf4d1..4a6d2cd 100644 --- a/src/crepe/api/LoopManager.cpp +++ b/src/crepe/api/LoopManager.cpp @@ -6,20 +6,25 @@ #include "../system/PhysicsSystem.h" #include "../system/RenderSystem.h" #include "../system/ScriptSystem.h" - +#include "../api/EventManager.h" #include "LoopManager.h" #include "LoopTimer.h" - +#include <iostream> using namespace crepe; using namespace std; LoopManager::LoopManager() { + this->loop_timer = make_unique<LoopTimer>(); this->load_system<AnimatorSystem>(); this->load_system<CollisionSystem>(); this->load_system<ParticleSystem>(); this->load_system<PhysicsSystem>(); this->load_system<RenderSystem>(); this->load_system<ScriptSystem>(); + EventManager::get_instance().subscribe<ShutDownEvent>([this](const ShutDownEvent& event) { + return this->on_shutdown(event); + }); + } void LoopManager::process_input() { @@ -35,29 +40,27 @@ void LoopManager::set_running(bool running) { this->game_running = running; } void LoopManager::fixed_update() {} void LoopManager::loop() { - LoopTimer & timer = LoopTimer::get_instance(); - timer.start(); + this->loop_timer->start(); while (game_running) { - timer.update(); + this->loop_timer->update(); - while (timer.get_lag() >= timer.get_fixed_delta_time()) { + while (this->loop_timer->get_lag() >= this->loop_timer->get_fixed_delta_time()) { this->process_input(); this->fixed_update(); - timer.advance_fixed_update(); + this->loop_timer->advance_fixed_update(); } this->update(); this->render(); - - timer.enforce_frame_rate(); + this->loop_timer->enforce_frame_rate(); } } void LoopManager::setup() { this->game_running = true; - LoopTimer::get_instance().start(); - LoopTimer::get_instance().set_fps(200); + this->loop_timer->start(); + this->loop_timer->set_fps(60); } void LoopManager::render() { @@ -65,5 +68,9 @@ void LoopManager::render() { this->get_system<RenderSystem>().update(); } } +bool LoopManager::on_shutdown(const ShutDownEvent & e){ + this->game_running = false; + return false; +} -void LoopManager::update() { LoopTimer & timer = LoopTimer::get_instance(); } +void LoopManager::update() {} diff --git a/src/crepe/api/LoopManager.h b/src/crepe/api/LoopManager.h index 13e6dac..ff1ff55 100644 --- a/src/crepe/api/LoopManager.h +++ b/src/crepe/api/LoopManager.h @@ -7,7 +7,7 @@ #include "api/SceneManager.h" namespace crepe { - +class LoopTimer; /** * \brief Main game loop manager * @@ -91,6 +91,8 @@ private: SceneManager scene_manager{component_manager}; private: + std::unique_ptr<LoopTimer> loop_timer; + bool on_shutdown(const ShutDownEvent & e); /** * \brief Collection of System instances * diff --git a/src/crepe/api/LoopTimer.cpp b/src/crepe/api/LoopTimer.cpp index 15a0e3a..d0a19d7 100644 --- a/src/crepe/api/LoopTimer.cpp +++ b/src/crepe/api/LoopTimer.cpp @@ -9,10 +9,6 @@ using namespace crepe; LoopTimer::LoopTimer() { dbg_trace(); } -LoopTimer & LoopTimer::get_instance() { - static LoopTimer instance; - return instance; -} void LoopTimer::start() { this->last_frame_time = std::chrono::steady_clock::now(); @@ -56,22 +52,25 @@ void LoopTimer::set_game_scale(double value) { this->game_scale = value; } double LoopTimer::get_game_scale() const { return this->game_scale; } void LoopTimer::enforce_frame_rate() { - std::chrono::steady_clock::time_point current_frame_time - = std::chrono::steady_clock::now(); - std::chrono::milliseconds frame_duration - = std::chrono::duration_cast<std::chrono::milliseconds>(current_frame_time - - this->last_frame_time); - - if (frame_duration < this->frame_target_time) { - std::chrono::milliseconds delay_time - = std::chrono::duration_cast<std::chrono::milliseconds>(this->frame_target_time - - frame_duration); - if (delay_time.count() > 0) { - SDLContext::get_instance().delay(delay_time.count()); - } - } + auto current_frame_time = std::chrono::steady_clock::now(); + auto frame_duration = current_frame_time - this->last_frame_time; - this->last_frame_time = current_frame_time; + if (frame_duration < this->frame_target_time) { + auto remaining_time = this->frame_target_time - frame_duration; + + // Sleep for most of the remaining time using SDLContext + if (remaining_time > std::chrono::microseconds(2000)) { // 2ms threshold + SDLContext::get_instance().delay( + std::chrono::duration_cast<std::chrono::milliseconds>(remaining_time).count()); + } + + // Busy-wait for the last tiny remaining duration + while (std::chrono::steady_clock::now() - current_frame_time < remaining_time) { + // Busy wait + } + } + + this->last_frame_time = std::chrono::steady_clock::now(); // Update frame time } double LoopTimer::get_lag() const { diff --git a/src/crepe/api/LoopTimer.h b/src/crepe/api/LoopTimer.h index 9393439..b20203d 100644 --- a/src/crepe/api/LoopTimer.h +++ b/src/crepe/api/LoopTimer.h @@ -6,13 +6,7 @@ namespace crepe { class LoopTimer { public: - /** - * \brief Get the singleton instance of LoopTimer. - * - * \return A reference to the LoopTimer instance. - */ - static LoopTimer & get_instance(); - + LoopTimer(); /** * \brief Get the current delta time for the current frame. * @@ -97,12 +91,7 @@ private: */ double get_lag() const; - /** - * \brief Construct a new LoopTimer object. - * - * Private constructor for singleton pattern to restrict instantiation outside the class. - */ - LoopTimer(); + /** * \brief Update the timer to the current frame. -- cgit v1.2.3