aboutsummaryrefslogtreecommitdiff
path: root/src/crepe
diff options
context:
space:
mode:
authorheavydemon21 <nielsstunnebrink1@gmail.com>2024-12-08 19:49:41 +0100
committerheavydemon21 <nielsstunnebrink1@gmail.com>2024-12-08 19:49:41 +0100
commit519cc5d16e65ff501d330c03eeb35d2d095ead29 (patch)
tree3c7c3a64c922ec248f4136d1282530a6ec6647ac /src/crepe
parent63bcd54e0e0ea64cfef5950568b4253d5dae5c1e (diff)
made sdlcontext not a singleton anymore
Diffstat (limited to 'src/crepe')
-rw-r--r--src/crepe/Resource.cpp3
-rw-r--r--src/crepe/Resource.h3
-rw-r--r--src/crepe/api/LoopManager.h2
-rw-r--r--src/crepe/api/LoopTimer.cpp13
-rw-r--r--src/crepe/api/LoopTimer.h14
-rw-r--r--src/crepe/api/Sprite.cpp9
-rw-r--r--src/crepe/api/Sprite.h2
-rw-r--r--src/crepe/api/Texture.cpp18
-rw-r--r--src/crepe/api/Texture.h19
-rw-r--r--src/crepe/facade/SDLContext.cpp12
-rw-r--r--src/crepe/facade/SDLContext.h2
-rw-r--r--src/crepe/facade/Sound.cpp2
-rw-r--r--src/crepe/facade/Sound.h3
-rw-r--r--src/crepe/manager/Mediator.h4
-rw-r--r--src/crepe/manager/ResourceManager.hpp2
-rw-r--r--src/crepe/system/InputSystem.cpp1
-rw-r--r--src/crepe/system/RenderSystem.cpp5
-rw-r--r--src/crepe/system/ScriptSystem.cpp2
18 files changed, 46 insertions, 70 deletions
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 <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;
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<SaveManager> save_manager = SaveManager::get_instance();
OptionalRef<EventManager> event_manager = EventManager::get_instance();
OptionalRef<ResourceManager> resource_manager;
- OptionalRef<LoopTimer> timer = LoopTimer::get_instance();
+ OptionalRef<LoopTimer> 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<T>(asset);
+ if (entry.resource == nullptr) entry.resource = make_unique<T>(asset, this->mediator);
T * concrete_resource = dynamic_cast<T *>(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<Sprite> RenderSystem::sort(RefVector<Sprite> & 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<Texture>(sprite.source);
+ const Texture & res = resource_manager.get<Texture>(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<BehaviorScript> behavior_scripts = mgr.get_components_by_type<BehaviorScript>();