From 33cd5566909ac089cdf56db38a3d1daf0cb7dd10 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Mon, 9 Dec 2024 15:35:36 +0100 Subject: fixed the aspect ratio and removed the ratio from sprite and give it to texture. however the problem still lies with if animator is given the aspect ratio is then off --- src/crepe/api/Animator.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'src/crepe/api/Animator.h') diff --git a/src/crepe/api/Animator.h b/src/crepe/api/Animator.h index 7c850b8..8ceddad 100644 --- a/src/crepe/api/Animator.h +++ b/src/crepe/api/Animator.h @@ -82,8 +82,9 @@ public: * This constructor sets up the Animator with the given parameters, and initializes the * animation system. */ - Animator(game_object_id_t id, Sprite & spritesheet, unsigned int max_row, - unsigned int max_col, const Animator::Data & data); + Animator(game_object_id_t id, Sprite & spritesheet, unsigned int pixel_frame_x, + unsigned int pixel_frame_y, unsigned int max_row, unsigned int max_col, + const Animator::Data & data); ~Animator(); // dbg_trace public: @@ -96,6 +97,7 @@ public: private: //! A reference to the Sprite sheet containing. Sprite & spritesheet; + //! Uses the spritesheet friend AnimatorSystem; }; -- cgit v1.2.3 From 7b8de90699aea153e73b5f2cee05c69b966b81be Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Tue, 10 Dec 2024 16:21:05 +0100 Subject: implemented feedback wouter, improved animator. however if spritesheet aspect_ratio is not the same as the single frame then the scaling is wrong --- src/crepe/api/Animator.cpp | 18 +++++----- src/crepe/api/Animator.h | 12 +++---- src/crepe/api/CMakeLists.txt | 2 -- src/crepe/api/LoopManager.cpp | 1 - src/crepe/api/Texture.cpp | 35 ------------------ src/crepe/api/Texture.h | 71 ------------------------------------ src/crepe/facade/CMakeLists.txt | 2 ++ src/crepe/facade/SDLContext.cpp | 4 +-- src/crepe/facade/Texture.cpp | 34 ++++++++++++++++++ src/crepe/facade/Texture.h | 72 +++++++++++++++++++++++++++++++++++++ src/crepe/system/AnimatorSystem.cpp | 2 +- src/crepe/system/RenderSystem.cpp | 5 +-- src/example/rendering_particle.cpp | 16 ++------- 13 files changed, 130 insertions(+), 144 deletions(-) delete mode 100644 src/crepe/api/Texture.cpp delete mode 100644 src/crepe/api/Texture.h create mode 100644 src/crepe/facade/Texture.cpp create mode 100644 src/crepe/facade/Texture.h (limited to 'src/crepe/api/Animator.h') diff --git a/src/crepe/api/Animator.cpp b/src/crepe/api/Animator.cpp index ad1778d..4c72cc0 100644 --- a/src/crepe/api/Animator.cpp +++ b/src/crepe/api/Animator.cpp @@ -7,20 +7,18 @@ using namespace crepe; -Animator::Animator(game_object_id_t id, Sprite & spritesheet, unsigned int pixel_frame_x, - unsigned int pixel_frame_y, unsigned int max_row, unsigned int max_col, - const Animator::Data & data) +Animator::Animator(game_object_id_t id, Sprite & spritesheet, const ivec2 & single_frame_size, + const uvec2 & max_cell_size, const Animator::Data & data) : Component(id), spritesheet(spritesheet), - max_rows(max_row), - max_columns(max_col), + max_cell_size(max_cell_size), data(data) { dbg_trace(); - this->spritesheet.mask.h = this->max_columns * pixel_frame_y; - this->spritesheet.mask.w /= this->max_rows * pixel_frame_x; - this->spritesheet.mask.x = this->data.row * this->spritesheet.mask.w; - this->spritesheet.mask.y = this->data.col * this->spritesheet.mask.h; + this->spritesheet.mask.w = single_frame_size.x; + this->spritesheet.mask.h = single_frame_size.y; + this->spritesheet.mask.x = 0; + this->spritesheet.mask.y = 0; } Animator::~Animator() { dbg_trace(); } @@ -51,6 +49,6 @@ void Animator::set_anim(int col) { void Animator::next_anim() { Animator::Data & ctx = this->data; - ctx.row = ctx.row++ % this->max_rows; + ctx.row = ctx.row++ % this->max_cell_size.x; this->spritesheet.mask.x = ctx.row * this->spritesheet.mask.w; } diff --git a/src/crepe/api/Animator.h b/src/crepe/api/Animator.h index 8ceddad..9a26bf0 100644 --- a/src/crepe/api/Animator.h +++ b/src/crepe/api/Animator.h @@ -82,16 +82,14 @@ public: * This constructor sets up the Animator with the given parameters, and initializes the * animation system. */ - Animator(game_object_id_t id, Sprite & spritesheet, unsigned int pixel_frame_x, - unsigned int pixel_frame_y, unsigned int max_row, unsigned int max_col, - const Animator::Data & data); + Animator(game_object_id_t id, Sprite & spritesheet, const ivec2 & single_frame_size, + const uvec2 & max_cell_size, const Animator::Data & data); ~Animator(); // dbg_trace public: - //! The maximum number of columns in the sprite sheet. - const unsigned int max_columns; - //! The maximum number of rows in the sprite sheet. - const unsigned int max_rows; + //! The maximum number of rows and columns size + const uvec2 max_cell_size; + Animator::Data data; private: diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt index a163faf..718e497 100644 --- a/src/crepe/api/CMakeLists.txt +++ b/src/crepe/api/CMakeLists.txt @@ -6,7 +6,6 @@ target_sources(crepe PUBLIC ParticleEmitter.cpp Transform.cpp Color.cpp - Texture.cpp Sprite.cpp Config.cpp Metadata.cpp @@ -38,7 +37,6 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES Vector2.h Vector2.hpp Color.h - Texture.h Scene.h Metadata.h Camera.h diff --git a/src/crepe/api/LoopManager.cpp b/src/crepe/api/LoopManager.cpp index 044f096..88243c4 100644 --- a/src/crepe/api/LoopManager.cpp +++ b/src/crepe/api/LoopManager.cpp @@ -65,7 +65,6 @@ void LoopManager::setup() { this->scene_manager.load_next_scene(); timer.start(); timer.set_fps(200); - this->scene_manager.load_next_scene(); } void LoopManager::render() { diff --git a/src/crepe/api/Texture.cpp b/src/crepe/api/Texture.cpp deleted file mode 100644 index b0863cb..0000000 --- a/src/crepe/api/Texture.cpp +++ /dev/null @@ -1,35 +0,0 @@ -#include "../util/Log.h" -#include "manager/Mediator.h" -#include "facade/SDLContext.h" - -#include "Asset.h" -#include "Resource.h" -#include "Texture.h" -#include "types.h" - -using namespace crepe; -using namespace std; - -Texture::Texture(const Asset & src, Mediator & mediator) : Resource(src, mediator){ - dbg_trace(); - SDLContext & ctx = mediator.sdl_context; - this->texture = ctx.texture_from_path(src.get_path()); - this->size = ctx.get_size(*this); - this->aspect_ratio = static_cast(this->size.x) / this->size.y; -} - -Texture::~Texture() { - dbg_trace(); - this->texture.reset(); -} - -const ivec2 & Texture::get_size() const noexcept{ - return this->size; -} -const float & Texture::get_ratio() const noexcept{ - return this->aspect_ratio; -} - -SDL_Texture * Texture::get_img() const noexcept{ - return this->texture.get(); -} diff --git a/src/crepe/api/Texture.h b/src/crepe/api/Texture.h deleted file mode 100644 index c33d9e5..0000000 --- a/src/crepe/api/Texture.h +++ /dev/null @@ -1,71 +0,0 @@ -#pragma once - -// FIXME: this header can't be included because this is an API header, and SDL2 development -// headers won't be bundled with crepe. Why is this facade in the API namespace? - -#include -#include - -#include "Asset.h" -#include "Resource.h" -#include "types.h" - -namespace crepe { - -class Mediator; - -/** - * \class Texture - * \brief Manages texture loading and properties. - * - * The Texture class is responsible for loading an image from a source and providing access to - * its dimensions. Textures can be used for rendering. - */ -class Texture : public Resource { - -public: - /** - * \brief Constructs a Texture from an Asset resource. - * \param src Asset with texture data to load. - * \param mediator use the SDLContext reference to load the image - */ - Texture(const Asset & src, Mediator & mediator); - - /** - * \brief Destroys the Texture instance - */ - ~Texture(); - - /** - * \brief get width and height of image in pixels - * \return pixel size width and height - * - */ - const ivec2 & get_size() const noexcept; - - /** - * \brief aspect_ratio of image - * \return ratio - * - */ - const float & get_ratio() const noexcept; - - /** - * \brief get the image texture - * \return SDL_Texture - * - */ - SDL_Texture * get_img() const noexcept; - -private: - //! The texture of the class from the library - std::unique_ptr> texture; - - // texture size in pixel - ivec2 size; - - //! ratio of image - float aspect_ratio; -}; - -} // namespace crepe diff --git a/src/crepe/facade/CMakeLists.txt b/src/crepe/facade/CMakeLists.txt index 4cc53bc..0598e16 100644 --- a/src/crepe/facade/CMakeLists.txt +++ b/src/crepe/facade/CMakeLists.txt @@ -1,5 +1,6 @@ target_sources(crepe PUBLIC Sound.cpp + Texture.cpp SoundContext.cpp SDLContext.cpp DB.cpp @@ -7,6 +8,7 @@ target_sources(crepe PUBLIC target_sources(crepe PUBLIC FILE_SET HEADERS FILES Sound.h + Texture.h SoundContext.h SDLContext.h DB.h diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index ac21d15..a92ec8b 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -19,12 +19,12 @@ #include "../api/Color.h" #include "../api/Config.h" #include "../api/Sprite.h" -#include "../api/Texture.h" #include "../util/Log.h" #include "manager/Manager.h" #include "manager/Mediator.h" #include "SDLContext.h" +#include "Texture.h" #include "types.h" using namespace crepe; @@ -273,7 +273,7 @@ void SDLContext::draw(const RenderContext & ctx) { .img_scale = ctx.scale, }); - cout << dstrect.w << " " << dstrect.h << " " << dstrect.x << " " << dstrect.y << endl; + cout << srcrect->w << " " << srcrect->h << " " << srcrect->x << " " << srcrect->y << endl; double angle = ctx.angle + data.angle_offset; this->set_color_texture(ctx.texture, ctx.sprite.data.color); diff --git a/src/crepe/facade/Texture.cpp b/src/crepe/facade/Texture.cpp new file mode 100644 index 0000000..7224cb8 --- /dev/null +++ b/src/crepe/facade/Texture.cpp @@ -0,0 +1,34 @@ +#include "../util/Log.h" +#include "manager/Mediator.h" +#include "facade/SDLContext.h" + +#include "Resource.h" +#include "Texture.h" +#include "types.h" + +using namespace crepe; +using namespace std; + +Texture::Texture(const Asset & src, Mediator & mediator) : Resource(src, mediator){ + dbg_trace(); + SDLContext & ctx = mediator.sdl_context; + this->texture = ctx.texture_from_path(src.get_path()); + this->size = ctx.get_size(*this); + this->aspect_ratio = static_cast(this->size.x) / this->size.y; +} + +Texture::~Texture() { + dbg_trace(); + this->texture.reset(); +} + +const ivec2 & Texture::get_size() const noexcept{ + return this->size; +} +const float & Texture::get_ratio() const noexcept{ + return this->aspect_ratio; +} + +SDL_Texture * Texture::get_img() const noexcept{ + return this->texture.get(); +} diff --git a/src/crepe/facade/Texture.h b/src/crepe/facade/Texture.h new file mode 100644 index 0000000..255e14b --- /dev/null +++ b/src/crepe/facade/Texture.h @@ -0,0 +1,72 @@ +#pragma once + +// FIXME: this header can't be included because this is an API header, and SDL2 development +// headers won't be bundled with crepe. Why is this facade in the API namespace? + +#include +#include + +#include "../Resource.h" + +#include "types.h" + +namespace crepe { + +class Mediator; +class Asset; + +/** + * \class Texture + * \brief Manages texture loading and properties. + * + * The Texture class is responsible for loading an image from a source and providing access to + * its dimensions. Textures can be used for rendering. + */ +class Texture : public Resource { + +public: + /** + * \brief Constructs a Texture from an Asset resource. + * \param src Asset with texture data to load. + * \param mediator use the SDLContext reference to load the image + */ + Texture(const Asset & src, Mediator & mediator); + + /** + * \brief Destroys the Texture instance + */ + ~Texture(); + + /** + * \brief get width and height of image in pixels + * \return pixel size width and height + * + */ + const ivec2 & get_size() const noexcept; + + /** + * \brief aspect_ratio of image + * \return ratio + * + */ + const float & get_ratio() const noexcept; + + /** + * \brief get the image texture + * \return SDL_Texture + * + */ + SDL_Texture * get_img() const noexcept; + +private: + //! The texture of the class from the library + std::unique_ptr> texture; + + // texture size in pixel + ivec2 size; + + //! ratio of image + float aspect_ratio; +}; + +} // namespace crepe diff --git a/src/crepe/system/AnimatorSystem.cpp b/src/crepe/system/AnimatorSystem.cpp index 549c35d..bb22b62 100644 --- a/src/crepe/system/AnimatorSystem.cpp +++ b/src/crepe/system/AnimatorSystem.cpp @@ -23,7 +23,7 @@ void AnimatorSystem::update() { int last_frame = ctx.row; - int cycle_end = (ctx.cycle_end == -1) ? a.max_rows : ctx.cycle_end; + int cycle_end = (ctx.cycle_end == -1) ? a.max_cell_size.x : ctx.cycle_end; int total_frames = cycle_end - ctx.cycle_start; int curr_frame = static_cast(elapsed_time / frame_duration) % total_frames; diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index daf71c5..51340fb 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -10,9 +10,10 @@ #include "../api/Sprite.h" #include "../api/Transform.h" #include "../facade/SDLContext.h" +#include "../facade/Texture.h" #include "../manager/ComponentManager.h" -#include "api/Texture.h" -#include "manager/ResourceManager.h" +#include "../manager/ResourceManager.h" + #include "RenderSystem.h" using namespace crepe; diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index cfc5a84..44bd96a 100644 --- a/src/example/rendering_particle.cpp +++ b/src/example/rendering_particle.cpp @@ -1,5 +1,4 @@ #include "api/Asset.h" -#include "manager/ResourceManager.h" #include #include #include @@ -9,7 +8,6 @@ #include #include #include -#include #include #include #include @@ -52,7 +50,7 @@ public: Color color(255, 255, 255, 255); - Asset img{"asset/texture/test_ap43.png"}; + Asset img{"asset/spritesheet/spritesheet_test.png"}; Sprite & test_sprite = game_object.add_component( img, Sprite::Data{ @@ -65,16 +63,8 @@ public: .position_offset = {0, 0}, }); - /* - - auto & anim = game_object.add_component(test_sprite, 4, 4, - Animator::Data{ - .fps = 1, - .looping = false, - }); - anim.set_anim(2); - anim.active = false; - */ + auto & anim = game_object.add_component(test_sprite,ivec2{32, 64}, uvec2{4,1}, Animator::Data{}); + anim.set_anim(0); auto & cam = game_object.add_component(ivec2{1280, 720}, vec2{400, 400}, Camera::Data{ -- cgit v1.2.3 From 2bcd6ece912ab0a140f9d925718e8787879d1ed7 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Wed, 11 Dec 2024 14:36:44 +0100 Subject: adjusted aspect ratio --- src/crepe/api/Animator.cpp | 2 ++ src/crepe/api/Animator.h | 11 ++++---- src/crepe/api/Sprite.h | 9 +++++++ src/crepe/facade/SDLContext.cpp | 7 ++--- src/crepe/manager/Mediator.h | 2 -- src/crepe/system/AISystem.cpp | 3 ++- src/example/rendering_particle.cpp | 4 +-- src/example/sound.cpp | 54 ++++++++++++++++++++++++++++++++++++++ src/test/ParticleTest.cpp | 1 - src/test/RenderSystemTest.cpp | 1 - 10 files changed, 79 insertions(+), 15 deletions(-) create mode 100644 src/example/sound.cpp (limited to 'src/crepe/api/Animator.h') diff --git a/src/crepe/api/Animator.cpp b/src/crepe/api/Animator.cpp index 4c72cc0..123f0e7 100644 --- a/src/crepe/api/Animator.cpp +++ b/src/crepe/api/Animator.cpp @@ -19,6 +19,8 @@ Animator::Animator(game_object_id_t id, Sprite & spritesheet, const ivec2 & sing this->spritesheet.mask.h = single_frame_size.y; this->spritesheet.mask.x = 0; this->spritesheet.mask.y = 0; + + this->spritesheet.aspect_ratio = static_cast(single_frame_size.x) / single_frame_size.y; } Animator::~Animator() { dbg_trace(); } diff --git a/src/crepe/api/Animator.h b/src/crepe/api/Animator.h index 9a26bf0..09f0134 100644 --- a/src/crepe/api/Animator.h +++ b/src/crepe/api/Animator.h @@ -75,8 +75,9 @@ public: * * \param id The unique identifier for the component, typically assigned automatically. * \param spritesheet the reference to the spritesheet - * \param max_row maximum of rows inside the given spritesheet - * \param max_col maximum of columns inside the given spritesheet + * \param single_frame_size the width and height in pixels of a single frame inside the + * spritesheet + * \param max_cell_size the max rows and columns inside the given spritesheet * \param data extra animation data for more control * * This constructor sets up the Animator with the given parameters, and initializes the @@ -87,15 +88,15 @@ public: ~Animator(); // dbg_trace public: - //! The maximum number of rows and columns size - const uvec2 max_cell_size; - Animator::Data data; private: //! A reference to the Sprite sheet containing. Sprite & spritesheet; + //! The maximum number of rows and columns size + const uvec2 max_cell_size; + //! Uses the spritesheet friend AnimatorSystem; }; diff --git a/src/crepe/api/Sprite.h b/src/crepe/api/Sprite.h index 7e9812d..14a873b 100644 --- a/src/crepe/api/Sprite.h +++ b/src/crepe/api/Sprite.h @@ -92,6 +92,15 @@ private: //! Reads the all the variables plus the mask friend class AnimatorSystem; + + /** + * \aspect_ratio the ratio of the sprite image + * + * - this value will only be set by the \c Animator component for the ratio of the Animation + * - if \c Animator component is not added it will not use this ratio (because 0) and will use aspect_ratio of the Asset. + */ + float aspect_ratio = 0; + struct Rect { int w = 0; int h = 0; diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index a0d7f04..0566ecf 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -233,12 +233,14 @@ SDL_FRect SDLContext::get_dst_rect(const DestinationRectangleData & ctx) const { const Sprite::Data & data = ctx.sprite.data; + float aspect_ratio = (ctx.sprite.aspect_ratio == 0) ? ctx.texture.get_ratio() : ctx.sprite.aspect_ratio; + vec2 size = data.size; if (data.size.x == 0 && data.size.y != 0) { - size.x = data.size.y * ctx.texture.get_ratio(); + size.x = data.size.y * aspect_ratio; } if (data.size.y == 0 && data.size.x != 0) { - size.y = data.size.x / ctx.texture.get_ratio(); + size.y = data.size.x / aspect_ratio; } const CameraValues & cam = ctx.cam; @@ -273,7 +275,6 @@ void SDLContext::draw(const RenderContext & ctx) { .img_scale = ctx.scale, }); - cout << srcrect->w << " " << srcrect->h << " " << srcrect->x << " " << srcrect->y << endl; double angle = ctx.angle + data.angle_offset; this->set_color_texture(ctx.texture, ctx.sprite.data.color); diff --git a/src/crepe/manager/Mediator.h b/src/crepe/manager/Mediator.h index d5f7e00..628154a 100644 --- a/src/crepe/manager/Mediator.h +++ b/src/crepe/manager/Mediator.h @@ -4,8 +4,6 @@ // TODO: remove these singletons: #include "EventManager.h" -#include "SaveManager.h" -#include "api/LoopTimer.h" namespace crepe { diff --git a/src/crepe/system/AISystem.cpp b/src/crepe/system/AISystem.cpp index e2e36a5..7f04432 100644 --- a/src/crepe/system/AISystem.cpp +++ b/src/crepe/system/AISystem.cpp @@ -12,10 +12,11 @@ using namespace crepe; void AISystem::update() { const Mediator & mediator = this->mediator; ComponentManager & mgr = mediator.component_manager; + LoopTimer & timer = mediator.timer; RefVector ai_components = mgr.get_components_by_type(); //TODO: Use fixed loop dt (this is not available at master at the moment) - double dt = LoopTimer::get_instance().get_delta_time(); + double dt = timer.get_delta_time(); // Loop through all AI components for (AI & ai : ai_components) { diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index 44bd96a..bd4ef95 100644 --- a/src/example/rendering_particle.cpp +++ b/src/example/rendering_particle.cpp @@ -63,8 +63,8 @@ public: .position_offset = {0, 0}, }); - auto & anim = game_object.add_component(test_sprite,ivec2{32, 64}, uvec2{4,1}, Animator::Data{}); - anim.set_anim(0); + //auto & anim = game_object.add_component(test_sprite,ivec2{32, 64}, uvec2{4,1}, Animator::Data{}); + //anim.set_anim(0); auto & cam = game_object.add_component(ivec2{1280, 720}, vec2{400, 400}, Camera::Data{ diff --git a/src/example/sound.cpp b/src/example/sound.cpp new file mode 100644 index 0000000..a9b0930 --- /dev/null +++ b/src/example/sound.cpp @@ -0,0 +1,54 @@ + + +#include "api/Asset.h" +#include "api/AudioSource.h" +#include "api/BehaviorScript.h" +#include "api/Camera.h" +#include "api/GameObject.h" +#include "api/LoopManager.h" +#include "api/Scene.h" +#include "api/Script.h" +#include "manager/ComponentManager.h" +#include "types.h" +#include + +using namespace crepe; + + +class ScriptTest : public Script { + void init(){ + auto & audio = this->get_component(); + audio.play(); + } + void update(){ + } +}; + + +class TestSound : public Scene { +public: + void load_scene(){ + Mediator & mediator = this->mediator; + ComponentManager & mgr = mediator.component_manager; + + GameObject obj = mgr.new_object("SOUND"); + GameObject cam = mgr.new_object("cam"); + cam.add_component(ivec2{100,100},vec2{100,100}, Camera::Data{}); + + Asset asset{"asset/audio/sample.ogg"}; + auto & test = obj.add_component(asset); + obj.add_component().set_script(); + + + } + + std::string get_name() const { return "TestScene"; }; + +}; + +int main(){ + LoopManager engine; + engine.add_scene(); + engine.start(); + return 0; +} diff --git a/src/test/ParticleTest.cpp b/src/test/ParticleTest.cpp index 38f4bde..9112a3f 100644 --- a/src/test/ParticleTest.cpp +++ b/src/test/ParticleTest.cpp @@ -5,7 +5,6 @@ #include #include #include -#include #include #include #include diff --git a/src/test/RenderSystemTest.cpp b/src/test/RenderSystemTest.cpp index 1b2de7a..eb5033b 100644 --- a/src/test/RenderSystemTest.cpp +++ b/src/test/RenderSystemTest.cpp @@ -14,7 +14,6 @@ #include #include #include -#include #include #include -- cgit v1.2.3 From 2153593f3a684a6b771c98a8e0c385b50a1e8886 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Wed, 11 Dec 2024 16:09:21 +0100 Subject: implemented feedback --- src/crepe/api/Animator.cpp | 6 +++--- src/crepe/api/Animator.h | 8 ++++---- src/crepe/api/Config.h | 2 +- src/crepe/api/LoopManager.h | 7 ++----- src/crepe/api/LoopTimer.h | 2 -- src/crepe/facade/SDLContext.cpp | 30 ++++++++++++++---------------- src/crepe/facade/SDLContext.h | 12 +----------- src/crepe/facade/Texture.h | 3 --- src/crepe/system/AnimatorSystem.cpp | 2 +- 9 files changed, 26 insertions(+), 46 deletions(-) (limited to 'src/crepe/api/Animator.h') diff --git a/src/crepe/api/Animator.cpp b/src/crepe/api/Animator.cpp index 644363e..4ce4bf0 100644 --- a/src/crepe/api/Animator.cpp +++ b/src/crepe/api/Animator.cpp @@ -8,10 +8,10 @@ using namespace crepe; Animator::Animator(game_object_id_t id, Sprite & spritesheet, const ivec2 & single_frame_size, - const uvec2 & max_cell_size, const Animator::Data & data) + const uvec2 & grid_size, const Animator::Data & data) : Component(id), spritesheet(spritesheet), - max_cell_size(max_cell_size), + grid_size(grid_size), data(data) { dbg_trace(); @@ -52,6 +52,6 @@ void Animator::set_anim(int col) { void Animator::next_anim() { Animator::Data & ctx = this->data; - ctx.row = ctx.row++ % this->max_cell_size.x; + ctx.row = ctx.row++ % this->grid_size.x; this->spritesheet.mask.x = ctx.row * this->spritesheet.mask.w; } diff --git a/src/crepe/api/Animator.h b/src/crepe/api/Animator.h index 09f0134..5918800 100644 --- a/src/crepe/api/Animator.h +++ b/src/crepe/api/Animator.h @@ -77,14 +77,14 @@ public: * \param spritesheet the reference to the spritesheet * \param single_frame_size the width and height in pixels of a single frame inside the * spritesheet - * \param max_cell_size the max rows and columns inside the given spritesheet + * \param grid_size the max rows and columns inside the given spritesheet * \param data extra animation data for more control * * This constructor sets up the Animator with the given parameters, and initializes the * animation system. */ Animator(game_object_id_t id, Sprite & spritesheet, const ivec2 & single_frame_size, - const uvec2 & max_cell_size, const Animator::Data & data); + const uvec2 & grid_size, const Animator::Data & data); ~Animator(); // dbg_trace public: @@ -94,8 +94,8 @@ private: //! A reference to the Sprite sheet containing. Sprite & spritesheet; - //! The maximum number of rows and columns size - const uvec2 max_cell_size; + //! The maximum number of rows and columns inside the spritesheet + const uvec2 grid_size; //! Uses the spritesheet friend AnimatorSystem; diff --git a/src/crepe/api/Config.h b/src/crepe/api/Config.h index 73c9a4e..6472270 100644 --- a/src/crepe/api/Config.h +++ b/src/crepe/api/Config.h @@ -26,7 +26,7 @@ struct Config final { * * Only messages with equal or higher priority than this value will be logged. */ - Log::Level level = Log::Level::TRACE; + Log::Level level = Log::Level::INFO; /** * \brief Colored log output * diff --git a/src/crepe/api/LoopManager.h b/src/crepe/api/LoopManager.h index bf1a9f9..1bafa56 100644 --- a/src/crepe/api/LoopManager.h +++ b/src/crepe/api/LoopManager.h @@ -102,12 +102,9 @@ private: ResourceManager resource_manager{mediator}; //! Save manager instance SaveManager save_manager{mediator}; - + //! SDLContext instance SDLContext sdl_context{mediator}; - - ResourceManager res_man{mediator}; - - //! Loop timer \todo no more singletons! + //! LoopTimer instance LoopTimer loop_timer{mediator}; private: diff --git a/src/crepe/api/LoopTimer.h b/src/crepe/api/LoopTimer.h index 2a0b2a5..0a73a4c 100644 --- a/src/crepe/api/LoopTimer.h +++ b/src/crepe/api/LoopTimer.h @@ -5,8 +5,6 @@ namespace crepe { -class Mediator; - class LoopTimer : public Manager { public: /** diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 1699b53..1dbd6a5 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -11,7 +11,6 @@ #include #include #include -#include #include #include @@ -20,7 +19,6 @@ #include "../api/Config.h" #include "../api/Sprite.h" #include "../util/Log.h" -#include "manager/Manager.h" #include "manager/Mediator.h" #include "SDLContext.h" @@ -30,9 +28,8 @@ using namespace crepe; using namespace std; -SDLContext::SDLContext(Mediator & mediator) : Manager(mediator) { +SDLContext::SDLContext(Mediator & mediator) { dbg_trace(); - mediator.sdl_context = *this; if (SDL_Init(SDL_INIT_VIDEO) != 0) { throw runtime_error(format("SDLContext: SDL_Init error: {}", SDL_GetError())); } @@ -60,6 +57,8 @@ SDLContext::SDLContext(Mediator & mediator) : Manager(mediator) { if (!(IMG_Init(img_flags) & img_flags)) { throw runtime_error("SDLContext: SDL_image could not initialize!"); } + + mediator.sdl_context = *this; } SDLContext::~SDLContext() { @@ -219,16 +218,6 @@ void SDLContext::present_screen() { SDL_RenderPresent(this->game_renderer.get()); } -SDL_Rect * SDLContext::get_src_rect(const Sprite & sprite) { - if (sprite.mask.w == 0 && sprite.mask.h == 0) return NULL; - - this->mask.x = sprite.mask.x; - this->mask.y = sprite.mask.y; - this->mask.w = sprite.mask.w; - this->mask.h = sprite.mask.h; - return &this->mask; -} - SDL_FRect SDLContext::get_dst_rect(const DestinationRectangleData & ctx) const { const Sprite::Data & data = ctx.sprite.data; @@ -267,7 +256,16 @@ void SDLContext::draw(const RenderContext & ctx) { = (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * data.flip.flip_x) | (SDL_FLIP_VERTICAL * data.flip.flip_y)); - SDL_Rect * srcrect = this->get_src_rect(ctx.sprite); + SDL_Rect srcrect; + SDL_Rect * srcrect_ptr = NULL; + if (ctx.sprite.mask.w != 0 || ctx.sprite.mask.h != 0) { + srcrect.w = ctx.sprite.mask.w; + srcrect.h = ctx.sprite.mask.h; + srcrect.x = ctx.sprite.mask.x; + srcrect.y = ctx.sprite.mask.y; + srcrect_ptr = &srcrect; + } + SDL_FRect dstrect = this->get_dst_rect(SDLContext::DestinationRectangleData{ .sprite = ctx.sprite, .texture = ctx.texture, @@ -279,7 +277,7 @@ void SDLContext::draw(const RenderContext & ctx) { double angle = ctx.angle + data.angle_offset; this->set_color_texture(ctx.texture, ctx.sprite.data.color); - SDL_RenderCopyExF(this->game_renderer.get(), ctx.texture.get_img(), srcrect, &dstrect, + SDL_RenderCopyExF(this->game_renderer.get(), ctx.texture.get_img(), srcrect_ptr, &dstrect, angle, NULL, render_flip); } diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index 36e6e97..46b779f 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -15,7 +15,6 @@ #include "api/KeyCodes.h" #include "api/Sprite.h" #include "api/Transform.h" -#include "manager/Manager.h" #include "types.h" @@ -31,7 +30,7 @@ class Mediator; * SDLContext is a singleton that handles the SDL window and renderer, provides methods for * event handling, and rendering to the screen. It is never used directly by the user */ -class SDLContext : public Manager { +class SDLContext { public: //! data that the camera component cannot hold struct CameraValues { @@ -209,13 +208,6 @@ public: const vec2 & pos; const double & img_scale; }; - /** - * \brief calculates the sqaure size of the image - * - * \param sprite Reference to the sprite to calculate the rectangle - * \return sdl rectangle to draw a src image - */ - SDL_Rect * get_src_rect(const Sprite & sprite); /** * \brief calculates the sqaure size of the image for destination @@ -239,8 +231,6 @@ private: //! renderer for the crepe engine std::unique_ptr> game_renderer; - SDL_Rect mask = {}; - //! black bars rectangle to draw SDL_FRect black_bars[2] = {}; }; diff --git a/src/crepe/facade/Texture.h b/src/crepe/facade/Texture.h index 255e14b..cdacac4 100644 --- a/src/crepe/facade/Texture.h +++ b/src/crepe/facade/Texture.h @@ -1,8 +1,5 @@ #pragma once -// FIXME: this header can't be included because this is an API header, and SDL2 development -// headers won't be bundled with crepe. Why is this facade in the API namespace? - #include #include diff --git a/src/crepe/system/AnimatorSystem.cpp b/src/crepe/system/AnimatorSystem.cpp index bb22b62..d61ba35 100644 --- a/src/crepe/system/AnimatorSystem.cpp +++ b/src/crepe/system/AnimatorSystem.cpp @@ -23,7 +23,7 @@ void AnimatorSystem::update() { int last_frame = ctx.row; - int cycle_end = (ctx.cycle_end == -1) ? a.max_cell_size.x : ctx.cycle_end; + int cycle_end = (ctx.cycle_end == -1) ? a.grid_size.x : ctx.cycle_end; int total_frames = cycle_end - ctx.cycle_start; int curr_frame = static_cast(elapsed_time / frame_duration) % total_frames; -- cgit v1.2.3