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/Config.h | 2 +- src/crepe/api/LoopManager.h | 7 ++-- src/crepe/api/LoopTimer.cpp | 3 +- src/crepe/api/Sprite.cpp | 12 +++---- src/crepe/api/Sprite.h | 5 +-- src/crepe/api/Texture.cpp | 23 ++++--------- src/crepe/api/Texture.h | 15 ++++----- src/crepe/facade/SDLContext.cpp | 18 +++++----- src/crepe/facade/SDLContext.h | 62 +++++++++++++++-------------------- src/crepe/manager/EventManager.cpp | 2 ++ src/crepe/manager/Mediator.h | 4 +-- src/crepe/manager/ResourceManager.cpp | 2 +- src/crepe/manager/SceneManager.cpp | 3 ++ src/crepe/system/InputSystem.cpp | 6 +++- src/crepe/system/RenderSystem.cpp | 11 +++++++ src/example/rendering_particle.cpp | 9 +++-- 16 files changed, 95 insertions(+), 89 deletions(-) diff --git a/src/crepe/api/Config.h b/src/crepe/api/Config.h index 6472270..73c9a4e 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::INFO; + Log::Level level = Log::Level::TRACE; /** * \brief Colored log output * 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(); diff --git a/src/crepe/api/LoopTimer.cpp b/src/crepe/api/LoopTimer.cpp index 15a0e3a..40fc94e 100644 --- a/src/crepe/api/LoopTimer.cpp +++ b/src/crepe/api/LoopTimer.cpp @@ -1,6 +1,5 @@ #include -#include "../facade/SDLContext.h" #include "../util/Log.h" #include "LoopTimer.h" @@ -67,7 +66,7 @@ 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::get_instance().delay(delay_time.count()); } } diff --git a/src/crepe/api/Sprite.cpp b/src/crepe/api/Sprite.cpp index cc0e20a..bae5ad9 100644 --- a/src/crepe/api/Sprite.cpp +++ b/src/crepe/api/Sprite.cpp @@ -2,25 +2,25 @@ #include #include "../util/Log.h" +#include "api/Asset.h" #include "Component.h" #include "Sprite.h" -#include "Texture.h" #include "types.h" using namespace std; using namespace crepe; -Sprite::Sprite(game_object_id_t id, Texture & texture, const Sprite::Data & data) +Sprite::Sprite(game_object_id_t id, const Asset & texture, const Sprite::Data & data) : Component(id), - texture(std::move(texture)), + 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 = this->texture.get_size().x; + //this->mask.h = this->texture.get_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 dbf41e4..ec120c0 100644 --- a/src/crepe/api/Sprite.h +++ b/src/crepe/api/Sprite.h @@ -4,6 +4,7 @@ #include "Color.h" #include "Texture.h" +#include "api/Asset.h" #include "types.h" namespace crepe { @@ -74,11 +75,11 @@ public: * \param texture asset of the image * \param ctx all the sprite data */ - Sprite(game_object_id_t id, Texture & texture, const Data & data); + Sprite(game_object_id_t id, const Asset & texture, const Data & data); ~Sprite(); //! Texture used for the sprite - const Texture texture; + const Asset source; Data data; diff --git a/src/crepe/api/Texture.cpp b/src/crepe/api/Texture.cpp index 2b56271..9d8e02d 100644 --- a/src/crepe/api/Texture.cpp +++ b/src/crepe/api/Texture.cpp @@ -1,16 +1,16 @@ -#include "../facade/SDLContext.h" #include "../util/Log.h" #include "Asset.h" +#include "Resource.h" #include "Texture.h" #include "types.h" +#include using namespace crepe; using namespace std; -Texture::Texture(const Asset & src) { +Texture::Texture(const Asset & src) : Resource(src) { dbg_trace(); - this->load(src); } Texture::~Texture() { @@ -18,21 +18,12 @@ Texture::~Texture() { this->texture.reset(); } -Texture::Texture(Texture && other) noexcept : texture(std::move(other.texture)) {} - -Texture & Texture::operator=(Texture && other) noexcept { - if (this != &other) { - texture = std::move(other.texture); - } - return *this; -} - -void Texture::load(const Asset & res) { - SDLContext & ctx = SDLContext::get_instance(); - this->texture = ctx.texture_from_path(res.get_path()); +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 SDLContext::get_instance().get_size(*this); + return {}; } diff --git a/src/crepe/api/Texture.h b/src/crepe/api/Texture.h index 1817910..f9c7919 100644 --- a/src/crepe/api/Texture.h +++ b/src/crepe/api/Texture.h @@ -8,6 +8,7 @@ #include #include "Asset.h" +#include "Resource.h" #include "types.h" namespace crepe { @@ -22,7 +23,7 @@ class Animator; * 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 { +class Texture : public Resource { public: /** @@ -35,12 +36,6 @@ public: * \brief Destroys the Texture instance, freeing associated resources. */ ~Texture(); - // FIXME: this constructor shouldn't be necessary because this class doesn't manage memory - - Texture(Texture && other) noexcept; - Texture & operator=(Texture && other) noexcept; - Texture(const Texture &) = delete; - Texture & operator=(const Texture &) = delete; /** * \brief Gets the width and height of the texture. @@ -53,12 +48,14 @@ private: * \brief Loads the texture from an Asset resource. * \param res Unique pointer to an Asset resource to load the texture from. */ - void load(const Asset & res); - + void load(std::unique_ptr> texture); + private: //! The texture of the class from the library std::unique_ptr> texture; + bool loaded = false; + //! Grants SDLContext access to private members. friend class SDLContext; diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 4cc2206..85257d6 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include #include @@ -22,19 +23,16 @@ #include "../util/Log.h" #include "SDLContext.h" +#include "manager/Manager.h" +#include "manager/Mediator.h" #include "types.h" using namespace crepe; using namespace std; -SDLContext & SDLContext::get_instance() { - static SDLContext instance; - return instance; -} - -SDLContext::SDLContext() { +SDLContext::SDLContext(Mediator & mediator) : Manager(mediator){ dbg_trace(); - + mediator.sdl_context = *this; if (SDL_Init(SDL_INIT_VIDEO) != 0) { throw runtime_error(format("SDLContext: SDL_Init error: {}", SDL_GetError())); } @@ -261,6 +259,8 @@ 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) @@ -276,8 +276,8 @@ void SDLContext::draw(const RenderContext & ctx) { double angle = ctx.angle + data.angle_offset; - this->set_color_texture(ctx.sprite.texture, ctx.sprite.data.color); - SDL_RenderCopyExF(this->game_renderer.get(), ctx.sprite.texture.texture.get(), &srcrect, + 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); } diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index e232511..d95ebec 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -14,14 +14,16 @@ #include "api/Color.h" #include "api/KeyCodes.h" #include "api/Sprite.h" -#include "api/Texture.h" #include "api/Transform.h" + +#include "manager/Manager.h" +#include "manager/Mediator.h" #include "types.h" namespace crepe { -class LoopManager; -class InputSystem; +class Texture; + /** * \class SDLContext * \brief Facade for the SDL library @@ -29,7 +31,7 @@ class InputSystem; * 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 { +class SDLContext : public Manager { public: //! data that the camera component cannot hold struct CameraValues { @@ -62,6 +64,7 @@ public: //! rendering data needed to render on screen struct RenderContext { const Sprite & sprite; + Texture & texture; const CameraValues & cam; const vec2 & pos; const double & angle; @@ -92,20 +95,27 @@ public: float scroll_delta = INFINITY; ivec2 rel_mouse_move = {-1, -1}; }; - /** - * \brief Gets the singleton instance of SDLContext. - * \return Reference to the SDLContext instance. - */ - static SDLContext & get_instance(); +public: SDLContext(const SDLContext &) = delete; SDLContext(SDLContext &&) = delete; SDLContext & operator=(const SDLContext &) = delete; SDLContext & operator=(SDLContext &&) = delete; -private: - //! will only use get_events - friend class InputSystem; +public: + /** + * \brief Constructs an SDLContext instance. + * Initializes SDL, creates a window and renderer. + */ + SDLContext(Mediator & mediator); + + /** + * \brief Destroys the SDLContext instance. + * Cleans up SDL resources, including the window and renderer. + */ + ~SDLContext(); + +public: /** * \brief Retrieves a list of all events from the SDL context. * @@ -139,9 +149,7 @@ private: */ MouseButton sdl_to_mousebutton(Uint8 sdl_button); -private: - //! Will only use delay - friend class LoopTimer; +public: /** * \brief Gets the current SDL ticks since the program started. * \return Current ticks in milliseconds as a constant uint64_t. @@ -157,23 +165,8 @@ private: */ void delay(int ms) const; -private: - /** - * \brief Constructs an SDLContext instance. - * Initializes SDL, creates a window and renderer. - */ - SDLContext(); - - /** - * \brief Destroys the SDLContext instance. - * Cleans up SDL resources, including the window and renderer. - */ - ~SDLContext(); - -private: - //! Will use the funtions: texture_from_path, get_width,get_height. - friend class Texture; +public: /** * \brief Loads a texture from a file path. * \param path Path to the image file. @@ -188,10 +181,7 @@ private: */ ivec2 get_size(const Texture & ctx); -private: - //! Will use draw,clear_screen, present_screen, camera. - friend class RenderSystem; - +public: /** * \brief Draws a sprite to the screen using the specified transform and camera. * \param RenderContext Reference to rendering data to draw @@ -210,7 +200,7 @@ private: */ CameraValues set_camera(const Camera & camera); -private: +public: //! the data needed to construct a sdl dst rectangle struct DestinationRectangleData { const Sprite & sprite; diff --git a/src/crepe/manager/EventManager.cpp b/src/crepe/manager/EventManager.cpp index 20f0dd3..17fe528 100644 --- a/src/crepe/manager/EventManager.cpp +++ b/src/crepe/manager/EventManager.cpp @@ -1,9 +1,11 @@ #include "EventManager.h" +#include "util/Log.h" using namespace crepe; using namespace std; EventManager & EventManager::get_instance() { + dbg_trace(); static EventManager instance; return instance; } diff --git a/src/crepe/manager/Mediator.h b/src/crepe/manager/Mediator.h index ba0b41f..6a9e113 100644 --- a/src/crepe/manager/Mediator.h +++ b/src/crepe/manager/Mediator.h @@ -3,7 +3,6 @@ #include "../util/OptionalRef.h" // TODO: remove these singletons: -#include "../facade/SDLContext.h" #include "EventManager.h" #include "SaveManager.h" #include "api/LoopTimer.h" @@ -13,6 +12,7 @@ namespace crepe { class ComponentManager; class SceneManager; class ResourceManager; +class SDLContext; /** * Struct to pass references to classes that would otherwise need to be singletons down to @@ -27,12 +27,12 @@ class ResourceManager; * \warning This class should never be directly accessible from the API */ struct Mediator { + OptionalRef sdl_context; OptionalRef component_manager; OptionalRef scene_manager; OptionalRef save_manager = SaveManager::get_instance(); OptionalRef event_manager = EventManager::get_instance(); OptionalRef resource_manager; - OptionalRef sdl_context = SDLContext::get_instance(); OptionalRef timer = LoopTimer::get_instance(); }; diff --git a/src/crepe/manager/ResourceManager.cpp b/src/crepe/manager/ResourceManager.cpp index 7c01808..a141a46 100644 --- a/src/crepe/manager/ResourceManager.cpp +++ b/src/crepe/manager/ResourceManager.cpp @@ -6,8 +6,8 @@ using namespace crepe; using namespace std; ResourceManager::ResourceManager(Mediator & mediator) : Manager(mediator) { - mediator.resource_manager = *this; dbg_trace(); + mediator.resource_manager = *this; } ResourceManager::~ResourceManager() { dbg_trace(); } diff --git a/src/crepe/manager/SceneManager.cpp b/src/crepe/manager/SceneManager.cpp index 50a9fbb..a788c51 100644 --- a/src/crepe/manager/SceneManager.cpp +++ b/src/crepe/manager/SceneManager.cpp @@ -4,10 +4,13 @@ #include "ComponentManager.h" #include "SceneManager.h" +#include "util/Log.h" + using namespace crepe; using namespace std; SceneManager::SceneManager(Mediator & mediator) : Manager(mediator) { + dbg_trace(); mediator.scene_manager = *this; } diff --git a/src/crepe/system/InputSystem.cpp b/src/crepe/system/InputSystem.cpp index aaa8bdf..b36ec09 100644 --- a/src/crepe/system/InputSystem.cpp +++ b/src/crepe/system/InputSystem.cpp @@ -1,15 +1,19 @@ #include "../api/Button.h" #include "../manager/ComponentManager.h" #include "../manager/EventManager.h" +#include "facade/SDLContext.h" +#include "util/Log.h" #include "InputSystem.h" using namespace crepe; void InputSystem::update() { + dbg_trace(); ComponentManager & mgr = this->mediator.component_manager; EventManager & event_mgr = this->mediator.event_manager; - std::vector event_list = SDLContext::get_instance().get_events(); + SDLContext & context = this->mediator.sdl_context; + std::vector event_list = context.get_events(); RefVector