aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/api
diff options
context:
space:
mode:
Diffstat (limited to 'src/crepe/api')
-rw-r--r--src/crepe/api/Animator.cpp20
-rw-r--r--src/crepe/api/Animator.h17
-rw-r--r--src/crepe/api/CMakeLists.txt2
-rw-r--r--src/crepe/api/LoopManager.h7
-rw-r--r--src/crepe/api/LoopTimer.cpp79
-rw-r--r--src/crepe/api/Sprite.cpp11
-rw-r--r--src/crepe/api/Sprite.h23
-rw-r--r--src/crepe/api/Texture.cpp38
-rw-r--r--src/crepe/api/Texture.h69
9 files changed, 114 insertions, 152 deletions
diff --git a/src/crepe/api/Animator.cpp b/src/crepe/api/Animator.cpp
index b8a91dc..4ce4bf0 100644
--- a/src/crepe/api/Animator.cpp
+++ b/src/crepe/api/Animator.cpp
@@ -7,23 +7,21 @@
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 & grid_size, const Animator::Data & data)
: Component(id),
spritesheet(spritesheet),
- max_rows(max_row),
- max_columns(max_col),
+ grid_size(grid_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;
+ 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;
- // 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;
+ = static_cast<float>(single_frame_size.x) / single_frame_size.y;
}
Animator::~Animator() { dbg_trace(); }
@@ -54,6 +52,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->grid_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..5918800 100644
--- a/src/crepe/api/Animator.h
+++ b/src/crepe/api/Animator.h
@@ -75,27 +75,28 @@ public:
*
* \param id The unique identifier for the component, typically assigned automatically.
* \param spritesheet the reference to the spritesheet
- * \param max_row maximum of rows inside the given spritesheet
- * \param max_col maximum of columns inside the given spritesheet
+ * \param single_frame_size the width and height in pixels of a single frame inside the
+ * spritesheet
+ * \param grid_size the max rows and columns inside the given spritesheet
* \param data extra animation data for more control
*
* 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 & grid_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;
Animator::Data data;
private:
//! A reference to the Sprite sheet containing.
Sprite & spritesheet;
+
+ //! The maximum number of rows and columns inside the spritesheet
+ const uvec2 grid_size;
+
//! Uses the spritesheet
friend AnimatorSystem;
};
diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt
index 35161e4..fb11c8d 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
@@ -36,7 +35,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/LoopManager.h b/src/crepe/api/LoopManager.h
index 487f07a..50d91d2 100644
--- a/src/crepe/api/LoopManager.h
+++ b/src/crepe/api/LoopManager.h
@@ -12,7 +12,7 @@
#include "../manager/SceneManager.h"
#include "../system/System.h"
-#include "api/Event.h"
+#include "LoopTimer.h"
namespace crepe {
/**
@@ -86,9 +86,8 @@ private:
ResourceManager resource_manager{mediator};
//! Save manager instance
SaveManager save_manager{mediator};
-
- //! SDL context \todo no more singletons!
- SDLContext & sdl_context = SDLContext::get_instance();
+ //! SDLContext instance
+ SDLContext sdl_context{mediator};
private:
/**
diff --git a/src/crepe/api/LoopTimer.cpp b/src/crepe/api/LoopTimer.cpp
new file mode 100644
index 0000000..56e48d3
--- /dev/null
+++ b/src/crepe/api/LoopTimer.cpp
@@ -0,0 +1,79 @@
+#include <chrono>
+
+#include "../util/Log.h"
+#include "facade/SDLContext.h"
+#include "manager/Manager.h"
+
+#include "LoopTimer.h"
+
+using namespace crepe;
+
+LoopTimer::LoopTimer(Mediator & mediator) : Manager(mediator) {
+ dbg_trace();
+ mediator.timer = *this;
+}
+
+void LoopTimer::start() {
+ this->last_frame_time = std::chrono::steady_clock::now();
+ this->elapsed_time = std::chrono::milliseconds(0);
+ this->elapsed_fixed_time = std::chrono::milliseconds(0);
+ this->delta_time = std::chrono::milliseconds(0);
+}
+
+void LoopTimer::update() {
+ auto current_frame_time = std::chrono::steady_clock::now();
+ // Convert to duration in seconds for delta time
+ this->delta_time = std::chrono::duration_cast<std::chrono::duration<double>>(
+ current_frame_time - last_frame_time);
+
+ if (this->delta_time > this->maximum_delta_time) {
+ this->delta_time = this->maximum_delta_time;
+ }
+
+ this->delta_time *= this->game_scale;
+ this->elapsed_time += this->delta_time;
+ this->last_frame_time = current_frame_time;
+}
+
+double LoopTimer::get_delta_time() const { return this->delta_time.count(); }
+
+double LoopTimer::get_current_time() const { return this->elapsed_time.count(); }
+
+void LoopTimer::advance_fixed_update() { this->elapsed_fixed_time += this->fixed_delta_time; }
+
+double LoopTimer::get_fixed_delta_time() const { return this->fixed_delta_time.count(); }
+
+void LoopTimer::set_fps(int fps) {
+ this->fps = fps;
+ // target time per frame in seconds
+ this->frame_target_time = std::chrono::duration<double>(1.0) / fps;
+}
+
+int LoopTimer::get_fps() const { return this->fps; }
+
+void LoopTimer::set_game_scale(double value) { this->game_scale = value; }
+
+double LoopTimer::get_game_scale() const { return this->game_scale; }
+void LoopTimer::enforce_frame_rate() {
+ std::chrono::steady_clock::time_point current_frame_time
+ = std::chrono::steady_clock::now();
+ std::chrono::milliseconds frame_duration
+ = std::chrono::duration_cast<std::chrono::milliseconds>(current_frame_time
+ - this->last_frame_time);
+
+ if (frame_duration < this->frame_target_time) {
+ std::chrono::milliseconds delay_time
+ = std::chrono::duration_cast<std::chrono::milliseconds>(this->frame_target_time
+ - frame_duration);
+ if (delay_time.count() > 0) {
+ SDLContext & ctx = this->mediator.sdl_context;
+ ctx.delay(delay_time.count());
+ }
+ }
+
+ this->last_frame_time = current_frame_time;
+}
+
+double LoopTimer::get_lag() const {
+ return (this->elapsed_time - this->elapsed_fixed_time).count();
+}
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..a2409c2 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;
@@ -101,6 +92,14 @@ private:
//! Reads the all the variables plus the mask
friend class AnimatorSystem;
+ /**
+ * \aspect_ratio the ratio of the sprite image
+ *
+ * - this value will only be set by the \c Animator component for the ratio of the Animation
+ * - if \c Animator component is not added it will not use this ratio (because 0) and will use aspect_ratio of the Asset.
+ */
+ float aspect_ratio = 0;
+
struct Rect {
int w = 0;
int h = 0;
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