aboutsummaryrefslogtreecommitdiff
path: root/src/crepe
diff options
context:
space:
mode:
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/Animator.cpp21
-rw-r--r--src/crepe/api/Animator.h12
-rw-r--r--src/crepe/api/CMakeLists.txt2
-rw-r--r--src/crepe/api/Config.h2
-rw-r--r--src/crepe/api/LoopManager.h9
-rw-r--r--src/crepe/api/LoopTimer.cpp14
-rw-r--r--src/crepe/api/LoopTimer.h14
-rw-r--r--src/crepe/api/Sprite.cpp11
-rw-r--r--src/crepe/api/Sprite.h15
-rw-r--r--src/crepe/api/Texture.cpp38
-rw-r--r--src/crepe/api/Texture.h69
-rw-r--r--src/crepe/facade/CMakeLists.txt2
-rw-r--r--src/crepe/facade/SDLContext.cpp50
-rw-r--r--src/crepe/facade/SDLContext.h77
-rw-r--r--src/crepe/facade/Sound.cpp2
-rw-r--r--src/crepe/facade/Sound.h3
-rw-r--r--src/crepe/facade/Texture.cpp34
-rw-r--r--src/crepe/facade/Texture.h72
-rw-r--r--src/crepe/manager/EventManager.cpp2
-rw-r--r--src/crepe/manager/Mediator.h8
-rw-r--r--src/crepe/manager/ResourceManager.cpp2
-rw-r--r--src/crepe/manager/ResourceManager.hpp2
-rw-r--r--src/crepe/manager/SceneManager.cpp3
-rw-r--r--src/crepe/system/AnimatorSystem.cpp2
-rw-r--r--src/crepe/system/InputSystem.cpp5
-rw-r--r--src/crepe/system/RenderSystem.cpp9
-rw-r--r--src/crepe/system/ScriptSystem.cpp2
29 files changed, 239 insertions, 249 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/Animator.cpp b/src/crepe/api/Animator.cpp
index b8a91dc..4c72cc0 100644
--- a/src/crepe/api/Animator.cpp
+++ b/src/crepe/api/Animator.cpp
@@ -7,23 +7,18 @@
using namespace crepe;
-Animator::Animator(game_object_id_t id, Sprite & spritesheet, 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;
- this->spritesheet.mask.w /= this->max_rows;
- this->spritesheet.mask.x = this->data.row * this->spritesheet.mask.w;
- this->spritesheet.mask.y = this->data.col * this->spritesheet.mask.h;
-
- // need to do this for to get the aspect ratio for a single clipping in the spritesheet
- this->spritesheet.aspect_ratio
- = static_cast<double>(this->spritesheet.mask.w) / 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(); }
@@ -54,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 7c850b8..9a26bf0 100644
--- a/src/crepe/api/Animator.h
+++ b/src/crepe/api/Animator.h
@@ -82,20 +82,20 @@ 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, 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:
//! A reference to the Sprite sheet containing.
Sprite & spritesheet;
+
//! Uses the spritesheet
friend AnimatorSystem;
};
diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt
index 118c7ce..46deb67 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
@@ -39,7 +38,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/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 700afe4..7389124 100644
--- a/src/crepe/api/LoopManager.h
+++ b/src/crepe/api/LoopManager.h
@@ -10,6 +10,7 @@
#include "../system/System.h"
#include "LoopTimer.h"
+#include "manager/ResourceManager.h"
namespace crepe {
@@ -102,10 +103,12 @@ private:
//! Save manager instance
SaveManager save_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();
+ LoopTimer loop_timer {mediator};
private:
/**
diff --git a/src/crepe/api/LoopTimer.cpp b/src/crepe/api/LoopTimer.cpp
index 15a0e3a..d6bf451 100644
--- a/src/crepe/api/LoopTimer.cpp
+++ b/src/crepe/api/LoopTimer.cpp
@@ -1,17 +1,16 @@
#include <chrono>
-#include "../facade/SDLContext.h"
#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() {
@@ -67,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 cc0e20a..ba684ba 100644
--- a/src/crepe/api/Sprite.cpp
+++ b/src/crepe/api/Sprite.cpp
@@ -1,26 +1,21 @@
#include <cmath>
-#include <utility>
#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<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 dbf41e4..7e9812d 100644
--- a/src/crepe/api/Sprite.h
+++ b/src/crepe/api/Sprite.h
@@ -1,9 +1,9 @@
#pragma once
#include "../Component.h"
+#include "api/Asset.h"
#include "Color.h"
-#include "Texture.h"
#include "types.h"
namespace crepe {
@@ -74,24 +74,15 @@ 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;
private:
- /**
- * \brief ratio of the img
- *
- * - This will multiply one of \c size variable if it is 0.
- * - Will be adjusted if \c Animator component is added to an GameObject that is why this
- * value cannot be const.
- */
- float aspect_ratio;
-
//! Reads the mask of sprite
friend class SDLContext;
diff --git a/src/crepe/api/Texture.cpp b/src/crepe/api/Texture.cpp
deleted file mode 100644
index 2b56271..0000000
--- a/src/crepe/api/Texture.cpp
+++ /dev/null
@@ -1,38 +0,0 @@
-#include "../facade/SDLContext.h"
-#include "../util/Log.h"
-
-#include "Asset.h"
-#include "Texture.h"
-#include "types.h"
-
-using namespace crepe;
-using namespace std;
-
-Texture::Texture(const Asset & src) {
- dbg_trace();
- this->load(src);
-}
-
-Texture::~Texture() {
- dbg_trace();
- 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());
-}
-
-ivec2 Texture::get_size() const {
- if (this->texture == nullptr) return {};
- return SDLContext::get_instance().get_size(*this);
-}
diff --git a/src/crepe/api/Texture.h b/src/crepe/api/Texture.h
deleted file mode 100644
index 1817910..0000000
--- a/src/crepe/api/Texture.h
+++ /dev/null
@@ -1,69 +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 <SDL2/SDL_render.h>
-#include <functional>
-#include <memory>
-
-#include "Asset.h"
-#include "types.h"
-
-namespace crepe {
-
-class SDLContext;
-class Animator;
-
-/**
- * \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:
- /**
- * \brief Constructs a Texture from an Asset resource.
- * \param src Asset with texture data to load.
- */
- Texture(const Asset & src);
-
- /**
- * \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.
- * \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(const Asset & res);
-
-private:
- //! The texture of the class from the library
- std::unique_ptr<SDL_Texture, std::function<void(SDL_Texture *)>> texture;
-
- //! Grants SDLContext access to private members.
- friend class SDLContext;
-
- //! Grants Animator access to private members.
- friend class Animator;
-};
-
-} // 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 6becf60..a0d7f04 100644
--- a/src/crepe/facade/SDLContext.cpp
+++ b/src/crepe/facade/SDLContext.cpp
@@ -11,6 +11,7 @@
#include <cstddef>
#include <cstdint>
#include <functional>
+#include <iostream>
#include <memory>
#include <stdexcept>
@@ -18,23 +19,20 @@
#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;
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()));
}
@@ -221,13 +219,14 @@ void SDLContext::present_screen() {
SDL_RenderPresent(this->game_renderer.get());
}
-SDL_Rect SDLContext::get_src_rect(const Sprite & sprite) const {
- return SDL_Rect{
- .x = sprite.mask.x,
- .y = sprite.mask.y,
- .w = sprite.mask.w,
- .h = sprite.mask.h,
- };
+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 {
@@ -236,10 +235,10 @@ SDL_FRect SDLContext::get_dst_rect(const DestinationRectangleData & ctx) const {
vec2 size = data.size;
if (data.size.x == 0 && data.size.y != 0) {
- size.x = data.size.y * ctx.sprite.aspect_ratio;
+ size.x = data.size.y * ctx.texture.get_ratio();
}
if (data.size.y == 0 && data.size.x != 0) {
- size.y = data.size.x / ctx.sprite.aspect_ratio;
+ size.y = data.size.x / ctx.texture.get_ratio();
}
const CameraValues & cam = ctx.cam;
@@ -260,25 +259,26 @@ SDL_FRect SDLContext::get_dst_rect(const DestinationRectangleData & ctx) const {
}
void SDLContext::draw(const RenderContext & ctx) {
-
const Sprite::Data & data = ctx.sprite.data;
SDL_RendererFlip render_flip
= (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 = this->get_src_rect(ctx.sprite);
SDL_FRect dstrect = this->get_dst_rect(SDLContext::DestinationRectangleData{
.sprite = ctx.sprite,
+ .texture = ctx.texture,
.cam = ctx.cam,
.pos = ctx.pos,
.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.sprite.texture, ctx.sprite.data.color);
- SDL_RenderCopyExF(this->game_renderer.get(), ctx.sprite.texture.texture.get(), &srcrect,
- &dstrect, angle, NULL, render_flip);
+ this->set_color_texture(ctx.texture, ctx.sprite.data.color);
+ SDL_RenderCopyExF(this->game_renderer.get(), ctx.texture.get_img(), srcrect, &dstrect,
+ angle, NULL, render_flip);
}
SDLContext::CameraValues SDLContext::set_camera(const Camera & cam) {
@@ -368,7 +368,7 @@ SDLContext::texture_from_path(const std::string & path) {
ivec2 SDLContext::get_size(const Texture & ctx) {
ivec2 size;
- SDL_QueryTexture(ctx.texture.get(), NULL, NULL, &size.x, &size.y);
+ SDL_QueryTexture(ctx.get_img(), NULL, NULL, &size.x, &size.y);
return size;
}
@@ -435,6 +435,6 @@ std::vector<SDLContext::EventData> SDLContext::get_events() {
return event_list;
}
void SDLContext::set_color_texture(const Texture & texture, const Color & color) {
- SDL_SetTextureColorMod(texture.texture.get(), color.r, color.g, color.b);
- SDL_SetTextureAlphaMod(texture.texture.get(), color.a);
+ SDL_SetTextureColorMod(texture.get_img(), color.r, color.g, color.b);
+ SDL_SetTextureAlphaMod(texture.get_img(), color.a);
}
diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h
index e232511..36e6e97 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 "types.h"
namespace crepe {
-class LoopManager;
-class InputSystem;
+class Texture;
+class Mediator;
+
/**
* \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;
+ const 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,7 @@ 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.
@@ -184,14 +176,11 @@ private:
/**
* \brief Gets the size of a texture.
* \param texture Reference to the Texture object.
- * \return Width and height of the texture as an integer.
+ * \return Width and height of the texture as an integer in pixels.
*/
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
@@ -207,13 +196,15 @@ private:
/**
* \brief sets the background of the camera (will be adjusted in future PR)
* \param camera Reference to the Camera object.
+ * \return camera data the component cannot store
*/
CameraValues set_camera(const Camera & camera);
-private:
+public:
//! the data needed to construct a sdl dst rectangle
struct DestinationRectangleData {
const Sprite & sprite;
+ const Texture & texture;
const CameraValues & cam;
const vec2 & pos;
const double & img_scale;
@@ -224,16 +215,12 @@ private:
* \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) const;
+ SDL_Rect * get_src_rect(const Sprite & sprite);
/**
* \brief calculates the sqaure size of the image for destination
*
- * \param sprite Reference to the sprite to calculate rectangle
- * \param pos the pos in world units
- * \param cam the camera of the current scene
- * \param cam_pos the current postion of the camera
- * \param img_scale the image multiplier for increasing img size
+ * \param data needed to calculate a destination rectangle
* \return sdl rectangle to draw a dst image to draw on the screen
*/
SDL_FRect get_dst_rect(const DestinationRectangleData & data) const;
@@ -252,6 +239,8 @@ private:
//! renderer for the crepe engine
std::unique_ptr<SDL_Renderer, std::function<void(SDL_Renderer *)>> game_renderer;
+ SDL_Rect mask = {};
+
//! black bars rectangle to draw
SDL_FRect black_bars[2] = {};
};
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/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<float>(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 <SDL2/SDL_render.h>
+#include <memory>
+
+#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<SDL_Texture, std::function<void(SDL_Texture *)>> texture;
+
+ // texture size in pixel
+ ivec2 size;
+
+ //! ratio of image
+ float aspect_ratio;
+};
+
+} // namespace crepe
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 35ac181..d5f7e00 100644
--- a/src/crepe/manager/Mediator.h
+++ b/src/crepe/manager/Mediator.h
@@ -3,8 +3,8 @@
#include "../util/OptionalRef.h"
// TODO: remove these singletons:
-#include "../facade/SDLContext.h"
#include "EventManager.h"
+#include "SaveManager.h"
#include "api/LoopTimer.h"
namespace crepe {
@@ -13,6 +13,8 @@ class ComponentManager;
class SceneManager;
class SaveManager;
class ResourceManager;
+class SDLContext;
+class LoopTimer;
/**
* Struct to pass references to classes that would otherwise need to be singletons down to
@@ -27,13 +29,13 @@ class ResourceManager;
* \warning This class should never be directly accessible from the API
*/
struct Mediator {
+ OptionalRef<SDLContext> sdl_context;
OptionalRef<ComponentManager> component_manager;
OptionalRef<SceneManager> scene_manager;
OptionalRef<SaveManager> save_manager;
OptionalRef<EventManager> event_manager = EventManager::get_instance();
OptionalRef<ResourceManager> resource_manager;
- OptionalRef<SDLContext> sdl_context = SDLContext::get_instance();
- OptionalRef<LoopTimer> timer = LoopTimer::get_instance();
+ OptionalRef<LoopTimer> timer;
};
} // namespace crepe
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/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/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/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<int>(elapsed_time / frame_duration) % total_frames;
diff --git a/src/crepe/system/InputSystem.cpp b/src/crepe/system/InputSystem.cpp
index aaa8bdf..a710ae2 100644
--- a/src/crepe/system/InputSystem.cpp
+++ b/src/crepe/system/InputSystem.cpp
@@ -1,6 +1,8 @@
#include "../api/Button.h"
#include "../manager/ComponentManager.h"
#include "../manager/EventManager.h"
+#include "facade/SDLContext.h"
+#include "util/Log.h"
#include "InputSystem.h"
@@ -9,7 +11,8 @@ using namespace crepe;
void InputSystem::update() {
ComponentManager & mgr = this->mediator.component_manager;
EventManager & event_mgr = this->mediator.event_manager;
- std::vector<SDLContext::EventData> event_list = SDLContext::get_instance().get_events();
+ SDLContext & context = this->mediator.sdl_context;
+ std::vector<SDLContext::EventData> event_list = context.get_events();
RefVector<Button> buttons = mgr.get_components_by_type<Button>();
RefVector<Camera> cameras = mgr.get_components_by_type<Camera>();
OptionalRef<Camera> curr_cam_ref;
diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp
index 26f2c85..51340fb 100644
--- a/src/crepe/system/RenderSystem.cpp
+++ b/src/crepe/system/RenderSystem.cpp
@@ -10,7 +10,9 @@
#include "../api/Sprite.h"
#include "../api/Transform.h"
#include "../facade/SDLContext.h"
+#include "../facade/Texture.h"
#include "../manager/ComponentManager.h"
+#include "../manager/ResourceManager.h"
#include "RenderSystem.h"
@@ -72,6 +74,8 @@ bool RenderSystem::render_particle(const Sprite & sprite, const SDLContext::Came
ComponentManager & mgr = this->mediator.component_manager;
SDLContext & ctx = this->mediator.sdl_context;
+ ResourceManager & resource_manager = this->mediator.resource_manager;
+ Texture & res = resource_manager.get<Texture>(sprite.source);
vector<reference_wrapper<ParticleEmitter>> emitters
= mgr.get_components_by_id<ParticleEmitter>(sprite.game_object_id);
@@ -88,6 +92,7 @@ bool RenderSystem::render_particle(const Sprite & sprite, const SDLContext::Came
ctx.draw(SDLContext::RenderContext{
.sprite = sprite,
+ .texture = res,
.cam = cam,
.pos = p.position,
.angle = p.angle,
@@ -100,8 +105,12 @@ bool RenderSystem::render_particle(const Sprite & sprite, const SDLContext::Came
void RenderSystem::render_normal(const Sprite & sprite, const SDLContext::CameraValues & cam,
const Transform & tm) {
SDLContext & ctx = this->mediator.sdl_context;
+ ResourceManager & resource_manager = this->mediator.resource_manager;
+ const Texture & res = resource_manager.get<Texture>(sprite.source);
+
ctx.draw(SDLContext::RenderContext{
.sprite = sprite,
+ .texture = res,
.cam = cam,
.pos = tm.position,
.angle = tm.rotation,
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>();