aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/api
diff options
context:
space:
mode:
Diffstat (limited to 'src/crepe/api')
-rw-r--r--src/crepe/api/Animator.cpp18
-rw-r--r--src/crepe/api/Animator.h28
-rw-r--r--src/crepe/api/BehaviorScript.h2
-rw-r--r--src/crepe/api/BehaviorScript.hpp1
-rw-r--r--src/crepe/api/Button.cpp7
-rw-r--r--src/crepe/api/Button.h18
-rw-r--r--src/crepe/api/Color.cpp2
-rw-r--r--src/crepe/api/Color.h2
-rw-r--r--src/crepe/api/Config.h2
-rw-r--r--src/crepe/api/Engine.cpp28
-rw-r--r--src/crepe/api/Engine.h5
-rw-r--r--src/crepe/api/Sprite.cpp13
-rw-r--r--src/crepe/api/Sprite.h23
-rw-r--r--src/crepe/api/Text.cpp19
-rw-r--r--src/crepe/api/Text.h30
-rw-r--r--src/crepe/api/UIObject.h4
-rw-r--r--src/crepe/api/Vector2.h46
-rw-r--r--src/crepe/api/Vector2.hpp15
18 files changed, 168 insertions, 95 deletions
diff --git a/src/crepe/api/Animator.cpp b/src/crepe/api/Animator.cpp
index 5ac0617..c558d86 100644
--- a/src/crepe/api/Animator.cpp
+++ b/src/crepe/api/Animator.cpp
@@ -36,12 +36,24 @@ void Animator::pause() { this->active = false; }
void Animator::stop() {
this->active = false;
- this->data.frame = this->data.cycle_start;
+ this->data.col = 0;
+ this->data.row = 0;
}
void Animator::set_fps(int fps) { this->data.fps = fps; }
void Animator::set_cycle_range(int start, int end) {
- this->data.cycle_start = start;
- this->data.cycle_end = end;
+ this->data.cycle_start = start, this->data.cycle_end = end;
}
+void Animator::set_anim(int col) {
+ Animator::Data & ctx = this->data;
+ this->spritesheet.mask.x = ctx.row = 0;
+ ctx.col = col;
+ this->spritesheet.mask.y = ctx.col * this->spritesheet.mask.h;
+}
+
+void Animator::next_anim() {
+ Animator::Data & ctx = this->data;
+ 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 8be693e..95539d3 100644
--- a/src/crepe/api/Animator.h
+++ b/src/crepe/api/Animator.h
@@ -1,7 +1,7 @@
#pragma once
-#include "../types.h"
#include "../manager/LoopTimerManager.h"
+#include "../types.h"
#include "Component.h"
#include "Sprite.h"
@@ -23,15 +23,16 @@ public:
struct Data {
//! frames per second for animation
unsigned int fps = 1;
- //! The current frame being shown
- unsigned int frame = 0;
-
+ //! The current col being animated.
+ unsigned int col = 0;
+ //! The current row being animated.
+ unsigned int row = 0;
//! should the animation loop
bool looping = false;
//! starting frame for cycling
unsigned int cycle_start = 0;
//! end frame for cycling (-1 = use last frame)
- unsigned int cycle_end = -1;
+ int cycle_end = -1;
};
public:
@@ -60,6 +61,14 @@ public:
* \param end of row animation
*/
void set_cycle_range(int start, int end);
+ /**
+ * \brief select which column to animate from
+ *
+ * \param col animation column
+ */
+ void set_anim(int col);
+ //! will go to the next animaiton of current row
+ void next_anim();
public:
/**
@@ -91,11 +100,14 @@ private:
//! The maximum number of rows and columns inside the spritesheet
const uvec2 grid_size;
+ // the time elapsed from a frame duration
+ duration_t elapsed_time = {};
+
+ // frame counter
+ unsigned int frame = 0;
+
//! Uses the spritesheet
friend AnimatorSystem;
-
- //! Elasped time since last frame change
- duration_t elapsed;
};
} // namespace crepe
diff --git a/src/crepe/api/BehaviorScript.h b/src/crepe/api/BehaviorScript.h
index 52cf259..3909b96 100644
--- a/src/crepe/api/BehaviorScript.h
+++ b/src/crepe/api/BehaviorScript.h
@@ -48,8 +48,6 @@ public:
BehaviorScript & set_script(Args &&... args);
protected:
- //! Script type name
- std::string name = "unknown script";
//! Script instance
std::unique_ptr<Script> script = nullptr;
//! ScriptSystem needs direct access to the script instance
diff --git a/src/crepe/api/BehaviorScript.hpp b/src/crepe/api/BehaviorScript.hpp
index 218f27c..353d5e2 100644
--- a/src/crepe/api/BehaviorScript.hpp
+++ b/src/crepe/api/BehaviorScript.hpp
@@ -11,7 +11,6 @@ template <class T, typename... Args>
BehaviorScript & BehaviorScript::set_script(Args &&... args) {
static_assert(std::is_base_of<Script, T>::value);
this->script = std::unique_ptr<Script>(new T(std::forward<Args>(args)...));
- this->name = typeid(T).name();
this->script->game_object_id = this->game_object_id;
this->script->active = this->active;
diff --git a/src/crepe/api/Button.cpp b/src/crepe/api/Button.cpp
index 40153c9..8eadd89 100644
--- a/src/crepe/api/Button.cpp
+++ b/src/crepe/api/Button.cpp
@@ -2,7 +2,10 @@
namespace crepe {
-Button::Button(game_object_id_t id, const vec2 & dimensions, const vec2 & offset)
- : UIObject(id, dimensions, offset) {}
+Button::Button(
+ game_object_id_t id, const vec2 & dimensions, const Data & data, const vec2 & offset
+)
+ : UIObject(id, dimensions, offset),
+ data(data) {}
} // namespace crepe
diff --git a/src/crepe/api/Button.h b/src/crepe/api/Button.h
index d42527e..e986c04 100644
--- a/src/crepe/api/Button.h
+++ b/src/crepe/api/Button.h
@@ -1,8 +1,7 @@
#pragma once
-#include <functional>
+#include "../types.h"
-#include "Event.h"
#include "UIObject.h"
namespace crepe {
@@ -21,14 +20,24 @@ namespace crepe {
*/
class Button : public UIObject {
public:
+ struct Data {
+ //! variable indicating if transform is relative to camera(false) or world(true)
+ bool world_space = false;
+ };
+
+public:
/**
* \brief Constructs a Button with the specified game object ID and dimensions.
*
* \param id The unique ID of the game object associated with this button.
* \param dimensions The width and height of the UIObject
* \param offset The offset relative this GameObjects Transform
+ * \param data additional data the button has
*/
- Button(game_object_id_t id, const vec2 & dimensions, const vec2 & offset);
+ Button(
+ game_object_id_t id, const vec2 & dimensions, const Data & data,
+ const vec2 & offset = {0, 0}
+ );
/**
* \brief Get the maximum number of instances for this component
*
@@ -38,6 +47,9 @@ public:
*/
virtual int get_instances_max() const { return 1; }
+public:
+ Data data;
+
private:
//! friend relation hover variable
friend class InputSystem;
diff --git a/src/crepe/api/Color.cpp b/src/crepe/api/Color.cpp
index 6858aa8..d0e3b35 100644
--- a/src/crepe/api/Color.cpp
+++ b/src/crepe/api/Color.cpp
@@ -10,3 +10,5 @@ const Color Color::BLACK {0x00, 0x00, 0x00};
const Color Color::CYAN {0x00, 0xff, 0xff};
const Color Color::YELLOW {0xff, 0xff, 0x00};
const Color Color::MAGENTA {0xff, 0x00, 0xff};
+const Color Color::GREY {0x80, 0x80, 0x80};
+const Color Color::GOLD {249, 205, 91};
diff --git a/src/crepe/api/Color.h b/src/crepe/api/Color.h
index 84edb5c..dbfd0ed 100644
--- a/src/crepe/api/Color.h
+++ b/src/crepe/api/Color.h
@@ -18,6 +18,8 @@ struct Color {
static const Color MAGENTA;
static const Color YELLOW;
static const Color BLACK;
+ static const Color GREY;
+ static const Color GOLD;
};
} // namespace crepe
diff --git a/src/crepe/api/Config.h b/src/crepe/api/Config.h
index 32f1a2e..7475528 100644
--- a/src/crepe/api/Config.h
+++ b/src/crepe/api/Config.h
@@ -86,7 +86,7 @@ struct Config final {
* This config option is the font size at which all fonts will be loaded initially.
*
*/
- unsigned int size = 16;
+ unsigned int size = 500;
} font;
//! Configuration for click tolerance.
struct {
diff --git a/src/crepe/api/Engine.cpp b/src/crepe/api/Engine.cpp
index bf3f50c..0bbe51f 100644
--- a/src/crepe/api/Engine.cpp
+++ b/src/crepe/api/Engine.cpp
@@ -1,7 +1,4 @@
-#include <segvcatch.h>
-
#include "../util/Log.h"
-#include "../facade/SignalCatch.h"
#include "Engine.h"
@@ -9,8 +6,6 @@ using namespace crepe;
using namespace std;
int Engine::main() noexcept {
- SignalCatch signal_catch;
-
try {
this->setup();
} catch (const exception & e) {
@@ -42,39 +37,32 @@ void Engine::setup() {
void Engine::loop() {
LoopTimerManager & timer = this->loop_timer;
+ SystemManager & systems = this->system_manager;
while (this->game_running) {
timer.update();
while (timer.get_lag() >= timer.get_fixed_delta_time()) {
try {
- this->fixed_update();
+ systems.fixed_update();
} catch (const exception & e) {
Log::logf(
- Log::Level::WARNING, "Uncaught exception in fixed update function: {}",
+ Log::Level::WARNING, "Uncaught exception in fixed update function: {}\n",
e.what()
);
}
+ timer.advance_fixed_elapsed_time();
}
try {
- this->frame_update();
+ systems.frame_update();
+ this->scene_manager.load_next_scene();
} catch (const exception & e) {
Log::logf(
- Log::Level::WARNING, "Uncaught exception in frame update function: {}",
+ Log::Level::WARNING, "Uncaught exception in frame update function: {}\n",
e.what()
);
}
+ timer.enforce_frame_rate();
}
}
-
-void Engine::fixed_update() {
- this->system_manager.fixed_update();
- this->loop_timer.advance_fixed_elapsed_time();
-}
-
-void Engine::frame_update() {
- this->system_manager.frame_update();
- this->loop_timer.enforce_frame_rate();
-}
-
diff --git a/src/crepe/api/Engine.h b/src/crepe/api/Engine.h
index 23acfb4..452a856 100644
--- a/src/crepe/api/Engine.h
+++ b/src/crepe/api/Engine.h
@@ -46,11 +46,6 @@ private:
*/
void loop();
- //! Fixed update function
- void fixed_update();
- //! Frame update function
- void frame_update();
-
//! Game loop condition
bool game_running = true;
diff --git a/src/crepe/api/Sprite.cpp b/src/crepe/api/Sprite.cpp
index 0107c7b..3c77e2e 100644
--- a/src/crepe/api/Sprite.cpp
+++ b/src/crepe/api/Sprite.cpp
@@ -19,3 +19,16 @@ Sprite::Sprite(game_object_id_t id, const Asset & texture, const Sprite::Data &
}
Sprite::~Sprite() { dbg_trace(); }
+
+unique_ptr<Component> Sprite::save() const { return unique_ptr<Component>(new Sprite(*this)); }
+
+void Sprite::restore(const Component & snapshot) {
+ *this = static_cast<const Sprite &>(snapshot);
+}
+
+Sprite & Sprite::operator=(const Sprite & snapshot) {
+ this->active = snapshot.active;
+ this->data = snapshot.data;
+ this->mask = snapshot.mask;
+ return *this;
+}
diff --git a/src/crepe/api/Sprite.h b/src/crepe/api/Sprite.h
index ef65f64..3565bed 100644
--- a/src/crepe/api/Sprite.h
+++ b/src/crepe/api/Sprite.h
@@ -42,10 +42,10 @@ public:
FlipSettings flip;
//! Layer sorting level of the sprite
- const int sorting_in_layer = 0;
+ int sorting_in_layer = 0;
//! Order within the sorting layer
- const int order_in_layer = 0;
+ int order_in_layer = 0;
/**
* \brief width and height of the sprite in game units
@@ -110,16 +110,21 @@ private:
*/
float aspect_ratio = 0;
-public:
- struct Mask {
- unsigned w = 0;
- unsigned h = 0;
- unsigned x = 0;
- unsigned y = 0;
+ struct Rect {
+ int w = 0;
+ int h = 0;
+ int x = 0;
+ int y = 0;
};
//! Render area of the sprite this will also be adjusted by the AnimatorSystem if an Animator
// object is present in GameObject. this is in sprite pixels
- Mask mask;
+ Rect mask;
+
+protected:
+ virtual std::unique_ptr<Component> save() const;
+ Sprite(const Sprite &) = default;
+ virtual void restore(const Component & snapshot);
+ virtual Sprite & operator=(const Sprite &);
};
} // namespace crepe
diff --git a/src/crepe/api/Text.cpp b/src/crepe/api/Text.cpp
index b24f0ac..e5cc39d 100644
--- a/src/crepe/api/Text.cpp
+++ b/src/crepe/api/Text.cpp
@@ -1,12 +1,27 @@
+#include "../types.h"
+
#include "Text.h"
using namespace crepe;
+using namespace std;
Text::Text(
- game_object_id_t id, const vec2 & dimensions, const vec2 & offset,
- const std::string & font_family, const Data & data, const std::string & text
+ game_object_id_t id, const vec2 & dimensions, const std::string & font_family,
+ const Data & data, const vec2 & offset, const std::string & text
)
: UIObject(id, dimensions, offset),
text(text),
data(data),
font_family(font_family) {}
+
+unique_ptr<Component> Text::save() const { return unique_ptr<Component>(new Text(*this)); }
+
+void Text::restore(const Component & snapshot) { *this = static_cast<const Text &>(snapshot); }
+
+Text & Text::operator=(const Text & snapshot) {
+ this->active = snapshot.active;
+ this->data = snapshot.data;
+ this->text = snapshot.text;
+ this->font_family = snapshot.font_family;
+ return *this;
+}
diff --git a/src/crepe/api/Text.h b/src/crepe/api/Text.h
index 0289b85..859490e 100644
--- a/src/crepe/api/Text.h
+++ b/src/crepe/api/Text.h
@@ -3,6 +3,8 @@
#include <optional>
#include <string>
+#include "../types.h"
+
#include "Asset.h"
#include "Color.h"
#include "UIObject.h"
@@ -17,22 +19,8 @@ class Text : public UIObject {
public:
//! Text data that does not have to be set in the constructor
struct Data {
- /**
- * \brief fontsize for text rendering
- *
- * \note this is not the actual font size that is loaded in.
- *
- * Since SDL_TTF requires the font size when loading in the font it is not possible to switch the font size.
- * The default font size that is loaded is set in the Config.
- * Instead this value is used to upscale the font texture which can cause blurring or distorted text when upscaling or downscaling too much.
- */
- unsigned int font_size = 16;
-
- //! Layer sorting level of the text
- const int sorting_in_layer = 0;
-
- //! Order within the sorting text
- const int order_in_layer = 0;
+ //! variable indicating if transform is relative to camera(false) or world(true)
+ bool world_space = false;
//! Label text color.
Color text_color = Color::BLACK;
@@ -49,8 +37,8 @@ public:
* \param font Optional font asset that can be passed or left empty.
*/
Text(
- game_object_id_t id, const vec2 & dimensions, const vec2 & offset,
- const std::string & font_family, const Data & data, const std::string & text = ""
+ game_object_id_t id, const vec2 & dimensions, const std::string & font_family,
+ const Data & data, const vec2 & offset = {0, 0}, const std::string & text = ""
);
//! Label text.
@@ -61,6 +49,12 @@ public:
std::optional<Asset> font;
//! Data instance
Data data;
+
+protected:
+ virtual std::unique_ptr<Component> save() const;
+ Text(const Text &) = default;
+ virtual void restore(const Component & snapshot);
+ virtual Text & operator=(const Text &);
};
} // namespace crepe
diff --git a/src/crepe/api/UIObject.h b/src/crepe/api/UIObject.h
index f1318ab..0d9b1f7 100644
--- a/src/crepe/api/UIObject.h
+++ b/src/crepe/api/UIObject.h
@@ -15,13 +15,11 @@ public:
* \param dimensions width and height of the UIObject
* \param offset Offset relative to the GameObject Transform
*/
- UIObject(game_object_id_t id, const vec2 & dimensions, const vec2 & offset);
+ UIObject(game_object_id_t id, const vec2 & dimensions, const vec2 & offset = {0, 0});
//! Width and height of the UIObject
vec2 dimensions;
//! Position offset relative to this GameObjects Transform
vec2 offset;
- //! variable indicating if transform is relative to camera(false) or world(true)
- bool world_space = false;
};
} // namespace crepe
diff --git a/src/crepe/api/Vector2.h b/src/crepe/api/Vector2.h
index 52e1bb6..6613641 100644
--- a/src/crepe/api/Vector2.h
+++ b/src/crepe/api/Vector2.h
@@ -1,5 +1,7 @@
#pragma once
+#include <format>
+
namespace crepe {
//! 2D vector
@@ -11,55 +13,55 @@ struct Vector2 {
T y = 0;
//! Subtracts another vector from this vector and returns the result.
- Vector2 operator-(const Vector2<T> & other) const;
+ Vector2<T> operator-(const Vector2<T> & other) const;
//! Subtracts a scalar value from both components of this vector and returns the result.
- Vector2 operator-(T scalar) const;
+ Vector2<T> operator-(T scalar) const;
//! Adds another vector to this vector and returns the result.
- Vector2 operator+(const Vector2<T> & other) const;
+ Vector2<T> operator+(const Vector2<T> & other) const;
//! Adds a scalar value to both components of this vector and returns the result.
- Vector2 operator+(T scalar) const;
+ Vector2<T> operator+(T scalar) const;
//! Multiplies this vector by another vector element-wise and returns the result.
- Vector2 operator*(const Vector2<T> & other) const;
+ Vector2<T> operator*(const Vector2<T> & other) const;
//! Multiplies this vector by a scalar and returns the result.
- Vector2 operator*(T scalar) const;
+ Vector2<T> operator*(T scalar) const;
//! Divides this vector by another vector element-wise and returns the result.
- Vector2 operator/(const Vector2<T> & other) const;
+ Vector2<T> operator/(const Vector2<T> & other) const;
//! Divides this vector by a scalar and returns the result.
- Vector2 operator/(T scalar) const;
+ Vector2<T> operator/(T scalar) const;
//! Adds another vector to this vector and updates this vector.
- Vector2 & operator+=(const Vector2<T> & other);
+ Vector2<T> & operator+=(const Vector2<T> & other);
//! Adds a scalar value to both components of this vector and updates this vector.
- Vector2 & operator+=(T other);
+ Vector2<T> & operator+=(T other);
//! Subtracts another vector from this vector and updates this vector.
- Vector2 & operator-=(const Vector2<T> & other);
+ Vector2<T> & operator-=(const Vector2<T> & other);
//! Subtracts a scalar value from both components of this vector and updates this vector.
- Vector2 & operator-=(T other);
+ Vector2<T> & operator-=(T other);
//! Multiplies this vector by another vector element-wise and updates this vector.
- Vector2 & operator*=(const Vector2<T> & other);
+ Vector2<T> & operator*=(const Vector2<T> & other);
//! Multiplies this vector by a scalar and updates this vector.
- Vector2 & operator*=(T other);
+ Vector2<T> & operator*=(T other);
//! Divides this vector by another vector element-wise and updates this vector.
- Vector2 & operator/=(const Vector2<T> & other);
+ Vector2<T> & operator/=(const Vector2<T> & other);
//! Divides this vector by a scalar and updates this vector.
- Vector2 & operator/=(T other);
+ Vector2<T> & operator/=(T other);
//! Returns the negation of this vector.
- Vector2 operator-() const;
+ Vector2<T> operator-() const;
//! Checks if this vector is equal to another vector.
bool operator==(const Vector2<T> & other) const;
@@ -89,12 +91,20 @@ struct Vector2 {
T distance_squared(const Vector2<T> & other) const;
//! Returns the perpendicular vector to this vector.
- Vector2 perpendicular() const;
+ Vector2<T> perpendicular() const;
//! Checks if both components of the vector are NaN.
bool is_nan() const;
+
+ //! Rotate this vector clockwise by \c deg degrees
+ Vector2<T> rotate(float deg) const;
};
} // namespace crepe
+template <typename T>
+struct std::formatter<crepe::Vector2<T>> : std::formatter<std::string> {
+ format_context::iterator format(crepe::Vector2<T> vec, format_context & ctx) const;
+};
+
#include "Vector2.hpp"
diff --git a/src/crepe/api/Vector2.hpp b/src/crepe/api/Vector2.hpp
index e195760..30441d2 100644
--- a/src/crepe/api/Vector2.hpp
+++ b/src/crepe/api/Vector2.hpp
@@ -168,4 +168,19 @@ bool Vector2<T>::is_nan() const {
return std::isnan(x) && std::isnan(y);
}
+template <class T>
+Vector2<T> Vector2<T>::rotate(float deg) const {
+ float rad = -deg / 180 * M_PI;
+ return {
+ x * std::cos(rad) - y * std::sin(rad),
+ x * std::sin(rad) + y * std::cos(rad),
+ };
+}
+
} // namespace crepe
+
+template <typename T>
+std::format_context::iterator
+std::formatter<crepe::Vector2<T>>::format(crepe::Vector2<T> vec, format_context & ctx) const {
+ return formatter<string>::format(std::format("{{{}, {}}}", vec.x, vec.y), ctx);
+}