aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/api
diff options
context:
space:
mode:
Diffstat (limited to 'src/crepe/api')
-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
11 files changed, 39 insertions, 168 deletions
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