From 63bcd54e0e0ea64cfef5950568b4253d5dae5c1e Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Sun, 8 Dec 2024 14:29:25 +0100 Subject: removed singleton from SDLContext, problem now is cannot call functionalities in the constructor sprite and animator and texture because it can only be filled at rendersystem call --- src/crepe/api/LoopManager.h | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'src/crepe/api/LoopManager.h') diff --git a/src/crepe/api/LoopManager.h b/src/crepe/api/LoopManager.h index d8910a0..8a30602 100644 --- a/src/crepe/api/LoopManager.h +++ b/src/crepe/api/LoopManager.h @@ -8,6 +8,7 @@ #include "../system/System.h" #include "LoopTimer.h" +#include "manager/ResourceManager.h" namespace crepe { @@ -96,8 +97,10 @@ private: //! Scene manager instance SceneManager scene_manager{mediator}; - //! SDL context \todo no more singletons! - SDLContext & sdl_context = SDLContext::get_instance(); + SDLContext sdl_context {mediator}; + + ResourceManager res_man {mediator}; + //! Loop timer \todo no more singletons! LoopTimer & loop_timer = LoopTimer::get_instance(); -- cgit v1.2.3 From 519cc5d16e65ff501d330c03eeb35d2d095ead29 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Sun, 8 Dec 2024 19:49:41 +0100 Subject: made sdlcontext not a singleton anymore --- src/crepe/Resource.cpp | 3 ++- src/crepe/Resource.h | 3 ++- src/crepe/api/LoopManager.h | 2 +- src/crepe/api/LoopTimer.cpp | 13 +++++++------ src/crepe/api/LoopTimer.h | 14 +++++--------- src/crepe/api/Sprite.cpp | 9 ++++----- src/crepe/api/Sprite.h | 2 +- src/crepe/api/Texture.cpp | 18 ++++++------------ src/crepe/api/Texture.h | 19 ++++--------------- src/crepe/facade/SDLContext.cpp | 12 +++++------- src/crepe/facade/SDLContext.h | 2 +- src/crepe/facade/Sound.cpp | 2 +- src/crepe/facade/Sound.h | 3 ++- src/crepe/manager/Mediator.h | 4 ++-- src/crepe/manager/ResourceManager.hpp | 2 +- src/crepe/system/InputSystem.cpp | 1 - src/crepe/system/RenderSystem.cpp | 5 +---- src/crepe/system/ScriptSystem.cpp | 2 +- src/example/rendering_particle.cpp | 28 ++++++++++++++++------------ src/test/CMakeLists.txt | 2 +- src/test/ParticleTest.cpp | 3 ++- src/test/Profiling.cpp | 16 ++++++++++------ src/test/RenderSystemTest.cpp | 27 ++++++++++++++++----------- src/test/ResourceManagerTest.cpp | 3 ++- 24 files changed, 93 insertions(+), 102 deletions(-) (limited to 'src/crepe/api/LoopManager.h') diff --git a/src/crepe/Resource.cpp b/src/crepe/Resource.cpp index 27b4c4b..85913ed 100644 --- a/src/crepe/Resource.cpp +++ b/src/crepe/Resource.cpp @@ -1,5 +1,6 @@ #include "Resource.h" +#include "manager/Mediator.h" using namespace crepe; -Resource::Resource(const Asset & asset) {} +Resource::Resource(const Asset & asset, Mediator & mediator) {} diff --git a/src/crepe/Resource.h b/src/crepe/Resource.h index eceb15b..d65206b 100644 --- a/src/crepe/Resource.h +++ b/src/crepe/Resource.h @@ -4,6 +4,7 @@ namespace crepe { class ResourceManager; class Asset; +class Mediator; /** * \brief Resource interface @@ -17,7 +18,7 @@ class Asset; */ class Resource { public: - Resource(const Asset & src); + Resource(const Asset & src, Mediator & mediator); virtual ~Resource() = default; Resource(const Resource &) = delete; diff --git a/src/crepe/api/LoopManager.h b/src/crepe/api/LoopManager.h index 8a30602..b800f5b 100644 --- a/src/crepe/api/LoopManager.h +++ b/src/crepe/api/LoopManager.h @@ -102,7 +102,7 @@ private: ResourceManager res_man {mediator}; //! Loop timer \todo no more singletons! - LoopTimer & loop_timer = LoopTimer::get_instance(); + LoopTimer loop_timer {mediator}; private: /** diff --git a/src/crepe/api/LoopTimer.cpp b/src/crepe/api/LoopTimer.cpp index 40fc94e..d6bf451 100644 --- a/src/crepe/api/LoopTimer.cpp +++ b/src/crepe/api/LoopTimer.cpp @@ -1,16 +1,16 @@ #include #include "../util/Log.h" +#include "facade/SDLContext.h" +#include "manager/Manager.h" #include "LoopTimer.h" using namespace crepe; -LoopTimer::LoopTimer() { dbg_trace(); } - -LoopTimer & LoopTimer::get_instance() { - static LoopTimer instance; - return instance; +LoopTimer::LoopTimer(Mediator & mediator) : Manager(mediator){ + dbg_trace(); + mediator.timer = *this; } void LoopTimer::start() { @@ -66,7 +66,8 @@ void LoopTimer::enforce_frame_rate() { = std::chrono::duration_cast(this->frame_target_time - frame_duration); if (delay_time.count() > 0) { - //SDLContext::get_instance().delay(delay_time.count()); + SDLContext & ctx = this->mediator.sdl_context; + ctx.delay(delay_time.count()); } } diff --git a/src/crepe/api/LoopTimer.h b/src/crepe/api/LoopTimer.h index 9393439..2a0b2a5 100644 --- a/src/crepe/api/LoopTimer.h +++ b/src/crepe/api/LoopTimer.h @@ -1,18 +1,14 @@ #pragma once +#include "manager/Manager.h" #include namespace crepe { -class LoopTimer { -public: - /** - * \brief Get the singleton instance of LoopTimer. - * - * \return A reference to the LoopTimer instance. - */ - static LoopTimer & get_instance(); +class Mediator; +class LoopTimer : public Manager { +public: /** * \brief Get the current delta time for the current frame. * @@ -102,7 +98,7 @@ private: * * Private constructor for singleton pattern to restrict instantiation outside the class. */ - LoopTimer(); + LoopTimer(Mediator & mediator); /** * \brief Update the timer to the current frame. diff --git a/src/crepe/api/Sprite.cpp b/src/crepe/api/Sprite.cpp index bae5ad9..4cf214c 100644 --- a/src/crepe/api/Sprite.cpp +++ b/src/crepe/api/Sprite.cpp @@ -1,5 +1,4 @@ #include -#include #include "../util/Log.h" #include "api/Asset.h" @@ -11,16 +10,16 @@ using namespace std; using namespace crepe; -Sprite::Sprite(game_object_id_t id, const Asset & texture, const Sprite::Data & data) +Sprite::Sprite(game_object_id_t id, const Asset & texture, const ivec2 & size, const Sprite::Data & data) : Component(id), source(texture), data(data) { dbg_trace(); - //this->mask.w = this->texture.get_size().x; - //this->mask.h = this->texture.get_size().y; - //this->aspect_ratio = static_cast(this->mask.w) / this->mask.h; + this->mask.w = size.x; + this->mask.h = size.y; + this->aspect_ratio = static_cast(this->mask.w) / this->mask.h; } Sprite::~Sprite() { dbg_trace(); } diff --git a/src/crepe/api/Sprite.h b/src/crepe/api/Sprite.h index ec120c0..9ef9f03 100644 --- a/src/crepe/api/Sprite.h +++ b/src/crepe/api/Sprite.h @@ -75,7 +75,7 @@ public: * \param texture asset of the image * \param ctx all the sprite data */ - Sprite(game_object_id_t id, const Asset & texture, const Data & data); + Sprite(game_object_id_t id, const Asset & texture, const ivec2 & size, const Data & data); ~Sprite(); //! Texture used for the sprite diff --git a/src/crepe/api/Texture.cpp b/src/crepe/api/Texture.cpp index 9d8e02d..2ac8606 100644 --- a/src/crepe/api/Texture.cpp +++ b/src/crepe/api/Texture.cpp @@ -3,27 +3,21 @@ #include "Asset.h" #include "Resource.h" #include "Texture.h" +#include "facade/SDLContext.h" +#include "manager/Mediator.h" #include "types.h" -#include using namespace crepe; using namespace std; -Texture::Texture(const Asset & src) : Resource(src) { +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); } Texture::~Texture() { dbg_trace(); this->texture.reset(); } - -void Texture::load(std::unique_ptr> texture) { - this->texture = std::move(texture); - this->loaded = true; -} - -ivec2 Texture::get_size() const { - if (this->texture == nullptr) return {}; - return {}; -} diff --git a/src/crepe/api/Texture.h b/src/crepe/api/Texture.h index f9c7919..4eb1058 100644 --- a/src/crepe/api/Texture.h +++ b/src/crepe/api/Texture.h @@ -15,6 +15,7 @@ namespace crepe { class SDLContext; class Animator; +class Mediator; /** * \class Texture @@ -30,31 +31,19 @@ public: * \brief Constructs a Texture from an Asset resource. * \param src Asset with texture data to load. */ - Texture(const Asset & src); + Texture(const Asset & src, Mediator & mediator); /** * \brief Destroys the Texture instance, freeing associated resources. */ ~Texture(); - /** - * \brief Gets the width and height of the texture. - * \return Width and height of the texture in pixels. - */ - ivec2 get_size() const; - -private: - /** - * \brief Loads the texture from an Asset resource. - * \param res Unique pointer to an Asset resource to load the texture from. - */ - void load(std::unique_ptr> texture); - private: //! The texture of the class from the library std::unique_ptr> texture; - bool loaded = false; + // texture size in pixel + ivec2 size; //! Grants SDLContext access to private members. friend class SDLContext; diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 85257d6..82c8c50 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -30,7 +30,7 @@ using namespace crepe; using namespace std; -SDLContext::SDLContext(Mediator & mediator) : Manager(mediator){ +SDLContext::SDLContext(Mediator & mediator) : Manager(mediator) { dbg_trace(); mediator.sdl_context = *this; if (SDL_Init(SDL_INIT_VIDEO) != 0) { @@ -232,7 +232,7 @@ SDL_FRect SDLContext::get_dst_rect(const DestinationRectangleData & ctx) const { const Sprite::Data & data = ctx.sprite.data; - vec2 size; + vec2 size = {data.size.x , data.size.y}; if (data.size.x == 0 && data.size.y != 0) { size.x = data.size.y * ctx.sprite.aspect_ratio; } @@ -258,9 +258,6 @@ SDL_FRect SDLContext::get_dst_rect(const DestinationRectangleData & ctx) const { } void SDLContext::draw(const RenderContext & ctx) { - - if (!ctx.texture.loaded) ctx.texture.load(this->texture_from_path(ctx.sprite.source.get_path())); - const Sprite::Data & data = ctx.sprite.data; SDL_RendererFlip render_flip = (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * data.flip.flip_x) @@ -274,11 +271,12 @@ void SDLContext::draw(const RenderContext & ctx) { .img_scale = ctx.scale, }); + cout << dstrect.w << " " << dstrect.h << " " << dstrect.x << " " << dstrect.y << endl; 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.texture.get(), &srcrect, - &dstrect, angle, NULL, render_flip); + int error = SDL_RenderCopyExF(this->game_renderer.get(), ctx.texture.texture.get(), + &srcrect, &dstrect, angle, NULL, render_flip); } SDLContext::CameraValues SDLContext::set_camera(const Camera & cam) { diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index d95ebec..9676940 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -64,7 +64,7 @@ public: //! rendering data needed to render on screen struct RenderContext { const Sprite & sprite; - Texture & texture; + const Texture & texture; const CameraValues & cam; const vec2 & pos; const double & angle; diff --git a/src/crepe/facade/Sound.cpp b/src/crepe/facade/Sound.cpp index ad50637..97e455e 100644 --- a/src/crepe/facade/Sound.cpp +++ b/src/crepe/facade/Sound.cpp @@ -6,7 +6,7 @@ using namespace crepe; using namespace std; -Sound::Sound(const Asset & src) : Resource(src) { +Sound::Sound(const Asset & src, Mediator & mediator) : Resource(src, mediator) { this->sample.load(src.get_path().c_str()); dbg_trace(); } diff --git a/src/crepe/facade/Sound.h b/src/crepe/facade/Sound.h index 85d141b..4a5d692 100644 --- a/src/crepe/facade/Sound.h +++ b/src/crepe/facade/Sound.h @@ -8,6 +8,7 @@ namespace crepe { class SoundContext; +class Mediator; /** * \brief Sound resource facade @@ -17,7 +18,7 @@ class SoundContext; */ class Sound : public Resource { public: - Sound(const Asset & src); + Sound(const Asset & src, Mediator & mediator); ~Sound(); // dbg_trace private: diff --git a/src/crepe/manager/Mediator.h b/src/crepe/manager/Mediator.h index 6a9e113..bb51d6b 100644 --- a/src/crepe/manager/Mediator.h +++ b/src/crepe/manager/Mediator.h @@ -5,7 +5,6 @@ // TODO: remove these singletons: #include "EventManager.h" #include "SaveManager.h" -#include "api/LoopTimer.h" namespace crepe { @@ -13,6 +12,7 @@ class ComponentManager; class SceneManager; class ResourceManager; class SDLContext; +class LoopTimer; /** * Struct to pass references to classes that would otherwise need to be singletons down to @@ -33,7 +33,7 @@ struct Mediator { OptionalRef save_manager = SaveManager::get_instance(); OptionalRef event_manager = EventManager::get_instance(); OptionalRef resource_manager; - OptionalRef timer = LoopTimer::get_instance(); + OptionalRef timer; }; } // namespace crepe diff --git a/src/crepe/manager/ResourceManager.hpp b/src/crepe/manager/ResourceManager.hpp index 5167d71..cf5c949 100644 --- a/src/crepe/manager/ResourceManager.hpp +++ b/src/crepe/manager/ResourceManager.hpp @@ -13,7 +13,7 @@ T & ResourceManager::get(const Asset & asset) { "cache must recieve a derivative class of Resource"); CacheEntry & entry = this->get_entry(asset); - if (entry.resource == nullptr) entry.resource = make_unique(asset); + if (entry.resource == nullptr) entry.resource = make_unique(asset, this->mediator); T * concrete_resource = dynamic_cast(entry.resource.get()); if (concrete_resource == nullptr) diff --git a/src/crepe/system/InputSystem.cpp b/src/crepe/system/InputSystem.cpp index b36ec09..a710ae2 100644 --- a/src/crepe/system/InputSystem.cpp +++ b/src/crepe/system/InputSystem.cpp @@ -9,7 +9,6 @@ using namespace crepe; void InputSystem::update() { - dbg_trace(); ComponentManager & mgr = this->mediator.component_manager; EventManager & event_mgr = this->mediator.event_manager; SDLContext & context = this->mediator.sdl_context; diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index d81d8dd..daf71c5 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -13,8 +13,6 @@ #include "../manager/ComponentManager.h" #include "api/Texture.h" #include "manager/ResourceManager.h" -#include "util/Log.h" - #include "RenderSystem.h" using namespace crepe; @@ -65,7 +63,6 @@ RefVector RenderSystem::sort(RefVector & objs) const { } void RenderSystem::update() { - dbg_trace(); this->clear_screen(); this->render(); this->present_screen(); @@ -108,7 +105,7 @@ void RenderSystem::render_normal(const Sprite & sprite, const SDLContext::Camera const Transform & tm) { SDLContext & ctx = this->mediator.sdl_context; ResourceManager & resource_manager = this->mediator.resource_manager; - Texture & res = resource_manager.get(sprite.source); + const Texture & res = resource_manager.get(sprite.source); ctx.draw(SDLContext::RenderContext{ .sprite = sprite, diff --git a/src/crepe/system/ScriptSystem.cpp b/src/crepe/system/ScriptSystem.cpp index d6b2ca1..df358e6 100644 --- a/src/crepe/system/ScriptSystem.cpp +++ b/src/crepe/system/ScriptSystem.cpp @@ -8,7 +8,7 @@ using namespace std; using namespace crepe; void ScriptSystem::update() { - dbg_trace(); + //dbg_trace(); ComponentManager & mgr = this->mediator.component_manager; RefVector behavior_scripts = mgr.get_components_by_type(); diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index 13ea591..87a6eb9 100644 --- a/src/example/rendering_particle.cpp +++ b/src/example/rendering_particle.cpp @@ -52,19 +52,22 @@ public: Color color(255, 255, 255, 255); - Asset img{"asset/texture/img.png"}; + Asset img{"asset/texture/test_ap43.png"}; + + Sprite & test_sprite + = game_object.add_component(img, ivec2{259, 195}, + Sprite::Data{ + .color = color, + .flip = Sprite::FlipSettings{false, false}, + .sorting_in_layer = 2, + .order_in_layer = 2, + .size = {0, 100}, + .angle_offset = 0, + .position_offset = {0, 0}, + }); + + /* - Sprite & test_sprite = game_object.add_component( - img, Sprite::Data{ - .color = color, - .flip = Sprite::FlipSettings{false, false}, - .sorting_in_layer = 2, - .order_in_layer = 2, - .size = {0, 100}, - .angle_offset = 0, - .position_offset = {100, 0}, - }); - auto & anim = game_object.add_component(test_sprite, 4, 4, Animator::Data{ .fps = 1, @@ -72,6 +75,7 @@ public: }); anim.set_anim(2); anim.active = false; + */ auto & cam = game_object.add_component(ivec2{1280, 720}, vec2{400, 400}, Camera::Data{ diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index 9f986a0..9d08767 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -15,7 +15,7 @@ target_sources(test_main PUBLIC ValueBrokerTest.cpp DBTest.cpp Vector2Test.cpp - InputTest.cpp + #InputTest.cpp ScriptEventTest.cpp ScriptSceneTest.cpp Profiling.cpp diff --git a/src/test/ParticleTest.cpp b/src/test/ParticleTest.cpp index 1409c4f..38f4bde 100644 --- a/src/test/ParticleTest.cpp +++ b/src/test/ParticleTest.cpp @@ -1,3 +1,4 @@ +#include "api/Asset.h" #include #include #include @@ -30,7 +31,7 @@ public: GameObject game_object = mgr.new_object("", "", vec2{0, 0}, 0, 0); Color color(0, 0, 0, 0); - auto s1 = Texture("asset/texture/img.png"); + auto s1 = Asset("asset/texture/img.png"); Sprite & test_sprite = game_object.add_component( s1, Sprite::Data{ .color = color, diff --git a/src/test/Profiling.cpp b/src/test/Profiling.cpp index c753bca..46da378 100644 --- a/src/test/Profiling.cpp +++ b/src/test/Profiling.cpp @@ -1,4 +1,6 @@ +#include "facade/SDLContext.h" #include "manager/Mediator.h" +#include "manager/ResourceManager.h" #include "system/ParticleSystem.h" #include "system/PhysicsSystem.h" #include "system/RenderSystem.h" @@ -41,7 +43,7 @@ class TestScript : public Script { } }; -class DISABLED_ProfilingTest : public Test { +class ProfilingTest : public Test { public: // Config for test // Minimum amount to let test pass @@ -61,6 +63,8 @@ public: ParticleSystem particle_sys{m}; RenderSystem render_sys{m}; ScriptSystem script_sys{m}; + SDLContext ctx{m}; + ResourceManager resource_manager{m}; // Test data std::map timings; @@ -130,7 +134,7 @@ public: } }; -TEST_F(DISABLED_ProfilingTest, Profiling_1) { +TEST_F(ProfilingTest, Profiling_1) { while (this->total_time / this->average < this->duration) { { @@ -153,7 +157,7 @@ TEST_F(DISABLED_ProfilingTest, Profiling_1) { EXPECT_GE(this->game_object_count, this->min_gameobject_count); } -TEST_F(DISABLED_ProfilingTest, Profiling_2) { +TEST_F(ProfilingTest, Profiling_2) { while (this->total_time / this->average < this->duration) { { @@ -167,7 +171,7 @@ TEST_F(DISABLED_ProfilingTest, Profiling_2) { gameobject.add_component(vec2{0, 0}, vec2{1, 1}); gameobject.add_component().set_script(); - auto img = Texture("asset/texture/square.png"); + auto img = Asset("asset/texture/square.png"); Sprite & test_sprite = gameobject.add_component( img, Sprite::Data{ .color = {0, 0, 0, 0}, @@ -192,7 +196,7 @@ TEST_F(DISABLED_ProfilingTest, Profiling_2) { EXPECT_GE(this->game_object_count, this->min_gameobject_count); } -TEST_F(DISABLED_ProfilingTest, Profiling_3) { +TEST_F(ProfilingTest, Profiling_3) { while (this->total_time / this->average < this->duration) { { @@ -205,7 +209,7 @@ TEST_F(DISABLED_ProfilingTest, Profiling_3) { }); gameobject.add_component(vec2{0, 0}, vec2{1, 1}); gameobject.add_component().set_script(); - auto img = Texture("asset/texture/square.png"); + auto img = Asset("asset/texture/square.png"); Sprite & test_sprite = gameobject.add_component( img, Sprite::Data{ .color = {0, 0, 0, 0}, diff --git a/src/test/RenderSystemTest.cpp b/src/test/RenderSystemTest.cpp index 205f534..1b2de7a 100644 --- a/src/test/RenderSystemTest.cpp +++ b/src/test/RenderSystemTest.cpp @@ -1,3 +1,6 @@ +#include "api/Asset.h" +#include "facade/SDLContext.h" +#include "manager/ResourceManager.h" #include "types.h" #include #include @@ -25,6 +28,8 @@ class RenderSystemTest : public Test { public: ComponentManager mgr{m}; + SDLContext ctx{m}; + ResourceManager resource_manager{m}; RenderSystem sys{m}; GameObject entity1 = this->mgr.new_object("name"); GameObject entity2 = this->mgr.new_object("name"); @@ -32,10 +37,10 @@ public: GameObject entity4 = this->mgr.new_object("name"); void SetUp() override { - auto s1 = Texture("asset/texture/img.png"); - auto s2 = Texture("asset/texture/img.png"); - auto s3 = Texture("asset/texture/img.png"); - auto s4 = Texture("asset/texture/img.png"); + auto s1 = Asset("asset/texture/img.png"); + auto s2 = Asset("asset/texture/img.png"); + auto s3 = Asset("asset/texture/img.png"); + auto s4 = Asset("asset/texture/img.png"); auto & sprite1 = entity1.add_component(s1, Sprite::Data{ .color = Color(0, 0, 0, 0), @@ -45,7 +50,7 @@ public: .size = {10, 10}, }); - ASSERT_NE(sprite1.texture.texture.get(), nullptr); + //ASSERT_NE(sprite1.texture.texture.get(), nullptr); EXPECT_EQ(sprite1.data.order_in_layer, 5); EXPECT_EQ(sprite1.data.sorting_in_layer, 5); auto & sprite2 @@ -55,7 +60,7 @@ public: .sorting_in_layer = 2, .order_in_layer = 1, }); - ASSERT_NE(sprite2.texture.texture.get(), nullptr); + //ASSERT_NE(sprite2.texture.texture.get(), nullptr); EXPECT_EQ(sprite2.data.sorting_in_layer, 2); EXPECT_EQ(sprite2.data.order_in_layer, 1); @@ -66,7 +71,7 @@ public: .sorting_in_layer = 1, .order_in_layer = 2, }); - ASSERT_NE(sprite3.texture.texture.get(), nullptr); + //ASSERT_NE(sprite3.texture.texture.get(), nullptr); EXPECT_EQ(sprite3.data.sorting_in_layer, 1); EXPECT_EQ(sprite3.data.order_in_layer, 2); @@ -77,7 +82,7 @@ public: .sorting_in_layer = 1, .order_in_layer = 1, }); - ASSERT_NE(sprite4.texture.texture.get(), nullptr); + //ASSERT_NE(sprite4.texture.texture.get(), nullptr); EXPECT_EQ(sprite4.data.sorting_in_layer, 1); EXPECT_EQ(sprite4.data.order_in_layer, 1); } @@ -87,8 +92,8 @@ TEST_F(RenderSystemTest, expected_throws) { GameObject entity1 = this->mgr.new_object("NAME"); // no texture img - EXPECT_ANY_THROW({ - auto test = Texture(""); + EXPECT_NO_THROW({ + auto test = Asset(""); auto & sprite1 = entity1.add_component( test, Sprite::Data{ .color = Color(0, 0, 0, 0), @@ -185,7 +190,7 @@ TEST_F(RenderSystemTest, Color) { Camera::Data{.bg_color = Color::WHITE, .zoom = 1.0f}); auto & sprite = this->mgr.get_components_by_id(entity1.id).front().get(); - ASSERT_NE(sprite.texture.texture.get(), nullptr); + //ASSERT_NE(sprite.texture.texture.get(), nullptr); sprite.data.color = Color::GREEN; EXPECT_EQ(sprite.data.color.r, Color::GREEN.r); diff --git a/src/test/ResourceManagerTest.cpp b/src/test/ResourceManagerTest.cpp index 44a5921..e040f54 100644 --- a/src/test/ResourceManagerTest.cpp +++ b/src/test/ResourceManagerTest.cpp @@ -1,3 +1,4 @@ +#include "manager/Mediator.h" #include #define private public @@ -30,7 +31,7 @@ public: public: const unsigned instance; - TestResource(const Asset & src) : Resource(src), instance(this->instances++) {} + TestResource(const Asset & src, Mediator & mediator) : Resource(src, mediator), instance(this->instances++) {} ~TestResource() { this->instances--; } bool operator==(const TestResource & other) const { return this->instance == other.instance; -- cgit v1.2.3 From 68c6e53f195677a3f91deb1526275fd38e00341d Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Wed, 11 Dec 2024 14:38:08 +0100 Subject: make format --- src/crepe/api/Animator.cpp | 3 ++- src/crepe/api/LoopManager.h | 6 ++--- src/crepe/api/LoopTimer.cpp | 4 +-- src/crepe/api/Sprite.h | 1 - src/crepe/facade/SDLContext.cpp | 3 ++- src/crepe/facade/Texture.cpp | 16 ++++-------- src/example/sound.cpp | 54 ---------------------------------------- src/test/ResourceManagerTest.cpp | 4 ++- 8 files changed, 17 insertions(+), 74 deletions(-) delete mode 100644 src/example/sound.cpp (limited to 'src/crepe/api/LoopManager.h') diff --git a/src/crepe/api/Animator.cpp b/src/crepe/api/Animator.cpp index 123f0e7..644363e 100644 --- a/src/crepe/api/Animator.cpp +++ b/src/crepe/api/Animator.cpp @@ -20,7 +20,8 @@ Animator::Animator(game_object_id_t id, Sprite & spritesheet, const ivec2 & sing this->spritesheet.mask.x = 0; this->spritesheet.mask.y = 0; - this->spritesheet.aspect_ratio = static_cast(single_frame_size.x) / single_frame_size.y; + this->spritesheet.aspect_ratio + = static_cast(single_frame_size.x) / single_frame_size.y; } Animator::~Animator() { dbg_trace(); } diff --git a/src/crepe/api/LoopManager.h b/src/crepe/api/LoopManager.h index 7389124..bf1a9f9 100644 --- a/src/crepe/api/LoopManager.h +++ b/src/crepe/api/LoopManager.h @@ -103,12 +103,12 @@ private: //! Save manager instance SaveManager save_manager{mediator}; - SDLContext sdl_context {mediator}; + SDLContext sdl_context{mediator}; - ResourceManager res_man {mediator}; + ResourceManager res_man{mediator}; //! Loop timer \todo no more singletons! - LoopTimer loop_timer {mediator}; + LoopTimer loop_timer{mediator}; private: /** diff --git a/src/crepe/api/LoopTimer.cpp b/src/crepe/api/LoopTimer.cpp index d6bf451..56e48d3 100644 --- a/src/crepe/api/LoopTimer.cpp +++ b/src/crepe/api/LoopTimer.cpp @@ -8,8 +8,8 @@ using namespace crepe; -LoopTimer::LoopTimer(Mediator & mediator) : Manager(mediator){ - dbg_trace(); +LoopTimer::LoopTimer(Mediator & mediator) : Manager(mediator) { + dbg_trace(); mediator.timer = *this; } diff --git a/src/crepe/api/Sprite.h b/src/crepe/api/Sprite.h index 14a873b..a2409c2 100644 --- a/src/crepe/api/Sprite.h +++ b/src/crepe/api/Sprite.h @@ -92,7 +92,6 @@ private: //! Reads the all the variables plus the mask friend class AnimatorSystem; - /** * \aspect_ratio the ratio of the sprite image * diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 0566ecf..1699b53 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -233,7 +233,8 @@ 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; + 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) { diff --git a/src/crepe/facade/Texture.cpp b/src/crepe/facade/Texture.cpp index 7224cb8..b63403d 100644 --- a/src/crepe/facade/Texture.cpp +++ b/src/crepe/facade/Texture.cpp @@ -1,6 +1,6 @@ #include "../util/Log.h" -#include "manager/Mediator.h" #include "facade/SDLContext.h" +#include "manager/Mediator.h" #include "Resource.h" #include "Texture.h" @@ -9,7 +9,7 @@ using namespace crepe; using namespace std; -Texture::Texture(const Asset & src, Mediator & mediator) : Resource(src, mediator){ +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()); @@ -22,13 +22,7 @@ Texture::~Texture() { this->texture.reset(); } -const ivec2 & Texture::get_size() const noexcept{ - return this->size; -} -const float & Texture::get_ratio() const noexcept{ - return this->aspect_ratio; -} +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(); -} +SDL_Texture * Texture::get_img() const noexcept { return this->texture.get(); } diff --git a/src/example/sound.cpp b/src/example/sound.cpp deleted file mode 100644 index a9b0930..0000000 --- a/src/example/sound.cpp +++ /dev/null @@ -1,54 +0,0 @@ - - -#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/ResourceManagerTest.cpp b/src/test/ResourceManagerTest.cpp index e040f54..965eeab 100644 --- a/src/test/ResourceManagerTest.cpp +++ b/src/test/ResourceManagerTest.cpp @@ -31,7 +31,9 @@ public: public: const unsigned instance; - TestResource(const Asset & src, Mediator & mediator) : Resource(src, mediator), instance(this->instances++) {} + TestResource(const Asset & src, Mediator & mediator) + : Resource(src, mediator), + instance(this->instances++) {} ~TestResource() { this->instances--; } bool operator==(const TestResource & other) const { return this->instance == other.instance; -- 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/LoopManager.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