diff options
author | heavydemon21 <nielsstunnebrink1@gmail.com> | 2024-12-08 19:49:41 +0100 |
---|---|---|
committer | heavydemon21 <nielsstunnebrink1@gmail.com> | 2024-12-08 19:49:41 +0100 |
commit | 519cc5d16e65ff501d330c03eeb35d2d095ead29 (patch) | |
tree | 3c7c3a64c922ec248f4136d1282530a6ec6647ac /src/crepe/api | |
parent | 63bcd54e0e0ea64cfef5950568b4253d5dae5c1e (diff) |
made sdlcontext not a singleton anymore
Diffstat (limited to 'src/crepe/api')
-rw-r--r-- | src/crepe/api/LoopManager.h | 2 | ||||
-rw-r--r-- | src/crepe/api/LoopTimer.cpp | 13 | ||||
-rw-r--r-- | src/crepe/api/LoopTimer.h | 14 | ||||
-rw-r--r-- | src/crepe/api/Sprite.cpp | 9 | ||||
-rw-r--r-- | src/crepe/api/Sprite.h | 2 | ||||
-rw-r--r-- | src/crepe/api/Texture.cpp | 18 | ||||
-rw-r--r-- | src/crepe/api/Texture.h | 19 |
7 files changed, 28 insertions, 49 deletions
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 <chrono> #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<std::chrono::milliseconds>(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 <chrono> 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 <cmath> -#include <utility> #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<double>(this->mask.w) / this->mask.h; + this->mask.w = size.x; + this->mask.h = size.y; + this->aspect_ratio = static_cast<double>(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 <utility> 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<SDL_Texture, std::function<void(SDL_Texture *)>> 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<SDL_Texture, std::function<void(SDL_Texture *)>> texture); - private: //! The texture of the class from the library std::unique_ptr<SDL_Texture, std::function<void(SDL_Texture *)>> texture; - bool loaded = false; + // texture size in pixel + ivec2 size; //! Grants SDLContext access to private members. friend class SDLContext; |