aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/api
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-12-17 15:20:29 +0100
committerLoek Le Blansch <loek@pipeframe.xyz>2024-12-17 15:20:29 +0100
commitba99bcbac33d05bc2067c9211c0fe453b2930a8d (patch)
treea032c8268fece08866d7f398bf781fd356560579 /src/crepe/api
parent45a1ab16f29e85de7c2df8832f51967c10c43e92 (diff)
parent9232a98b72eee7af4f7f2153c1b2ccedbfa4cc65 (diff)
merge master
Diffstat (limited to 'src/crepe/api')
-rw-r--r--src/crepe/api/BoxCollider.cpp4
-rw-r--r--src/crepe/api/BoxCollider.h3
-rw-r--r--src/crepe/api/Button.cpp3
-rw-r--r--src/crepe/api/Button.h12
-rw-r--r--src/crepe/api/CircleCollider.cpp4
-rw-r--r--src/crepe/api/CircleCollider.h3
-rw-r--r--src/crepe/api/Config.h5
-rw-r--r--src/crepe/api/Event.h98
-rw-r--r--src/crepe/api/KeyCodes.h6
-rw-r--r--src/crepe/api/ParticleEmitter.cpp9
-rw-r--r--src/crepe/api/ParticleEmitter.h53
-rw-r--r--src/crepe/api/Rigidbody.h2
-rw-r--r--src/crepe/api/Script.cpp15
-rw-r--r--src/crepe/api/Script.h19
14 files changed, 158 insertions, 78 deletions
diff --git a/src/crepe/api/BoxCollider.cpp b/src/crepe/api/BoxCollider.cpp
index c097a24..a893d41 100644
--- a/src/crepe/api/BoxCollider.cpp
+++ b/src/crepe/api/BoxCollider.cpp
@@ -4,7 +4,7 @@
using namespace crepe;
-BoxCollider::BoxCollider(game_object_id_t game_object_id, const vec2 & offset,
- const vec2 & dimensions)
+BoxCollider::BoxCollider(game_object_id_t game_object_id, const vec2 & dimensions,
+ const vec2 & offset)
: Collider(game_object_id, offset),
dimensions(dimensions) {}
diff --git a/src/crepe/api/BoxCollider.h b/src/crepe/api/BoxCollider.h
index 1ac4d46..d643e7f 100644
--- a/src/crepe/api/BoxCollider.h
+++ b/src/crepe/api/BoxCollider.h
@@ -13,7 +13,8 @@ namespace crepe {
*/
class BoxCollider : public Collider {
public:
- BoxCollider(game_object_id_t game_object_id, const vec2 & offset, const vec2 & dimensions);
+ BoxCollider(game_object_id_t game_object_id, const vec2 & dimensions,
+ const vec2 & offset = {0, 0});
//! Width and height of the box collider
vec2 dimensions;
diff --git a/src/crepe/api/Button.cpp b/src/crepe/api/Button.cpp
index 76f74f0..305922c 100644
--- a/src/crepe/api/Button.cpp
+++ b/src/crepe/api/Button.cpp
@@ -3,9 +3,8 @@
namespace crepe {
Button::Button(game_object_id_t id, const vec2 & dimensions, const vec2 & offset,
- const std::function<void()> & on_click, bool is_toggle)
+ const std::function<void()> & on_click)
: UIObject(id, dimensions, offset),
- is_toggle(is_toggle),
on_click(on_click) {}
} // namespace crepe
diff --git a/src/crepe/api/Button.h b/src/crepe/api/Button.h
index 61b18d7..08f5dec 100644
--- a/src/crepe/api/Button.h
+++ b/src/crepe/api/Button.h
@@ -15,19 +15,11 @@ public:
* \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 is_toggle Optional flag to indicate if the button is a toggle button. Defaults to false.
* \param on_click callback function that will be invoked when the button is clicked.
*/
Button(game_object_id_t id, const vec2 & dimensions, const vec2 & offset,
- const std::function<void()> & on_click, bool is_toggle = false);
+ const std::function<void()> & on_click);
- /**
- * \brief Indicates if the button is a toggle button (can be pressed and released).
- *
- * A toggle button allows for a pressed/released state, whereas a regular button
- * typically only has an on-click state.
- */
- bool is_toggle = false;
// TODO: create separate toggle button class
/**
* \brief The callback function to be executed when the button is clicked.
@@ -56,8 +48,6 @@ public:
private:
//! friend relation for is_pressed and hover variables
friend class InputSystem;
- //! Indicates whether the toggle button is pressed
- bool is_pressed = false;
//! Indicates whether the mouse is currently hovering over the button
bool hover = false;
diff --git a/src/crepe/api/CircleCollider.cpp b/src/crepe/api/CircleCollider.cpp
index a4271e9..90ab5e7 100644
--- a/src/crepe/api/CircleCollider.cpp
+++ b/src/crepe/api/CircleCollider.cpp
@@ -2,7 +2,7 @@
using namespace crepe;
-CircleCollider::CircleCollider(game_object_id_t game_object_id, const vec2 & offset,
- float radius)
+CircleCollider::CircleCollider(game_object_id_t game_object_id, float radius,
+ const vec2 & offset)
: Collider(game_object_id, offset),
radius(radius) {}
diff --git a/src/crepe/api/CircleCollider.h b/src/crepe/api/CircleCollider.h
index c7bf66e..22da836 100644
--- a/src/crepe/api/CircleCollider.h
+++ b/src/crepe/api/CircleCollider.h
@@ -13,7 +13,8 @@ namespace crepe {
*/
class CircleCollider : public Collider {
public:
- CircleCollider(game_object_id_t game_object_id, const vec2 & offset, float radius);
+ CircleCollider(game_object_id_t game_object_id, float radius,
+ const vec2 & offset = {0, 0});
//! Radius of the circle collider.
float radius;
diff --git a/src/crepe/api/Config.h b/src/crepe/api/Config.h
index ca2d3f1..ed1cf38 100644
--- a/src/crepe/api/Config.h
+++ b/src/crepe/api/Config.h
@@ -76,6 +76,11 @@ struct Config final {
*/
std::string root_pattern = ".crepe-root";
} asset;
+ //! Configuration for click tolerance.
+ struct {
+ //! The maximum number of pixels the mouse can move between MouseDown and MouseUp events to be considered a click.
+ int click_tolerance = 5;
+ } input;
//! Audio system settings
struct {
diff --git a/src/crepe/api/Event.h b/src/crepe/api/Event.h
index f2f3daf..73bf461 100644
--- a/src/crepe/api/Event.h
+++ b/src/crepe/api/Event.h
@@ -3,7 +3,8 @@
#include <string>
-#include "KeyCodes.h"
+#include "api/KeyCodes.h"
+#include "types.h"
namespace crepe {
@@ -38,11 +39,8 @@ public:
*/
class MousePressEvent : public Event {
public:
- //! X-coordinate of the mouse position at the time of the event.
- int mouse_x = 0;
-
- //! Y-coordinate of the mouse position at the time of the event.
- int mouse_y = 0;
+ //! mouse position in world coordinates (game units).
+ vec2 mouse_pos = {0, 0};
//! The mouse button that was pressed.
MouseButton button = MouseButton::NONE;
@@ -53,11 +51,8 @@ public:
*/
class MouseClickEvent : public Event {
public:
- //! X-coordinate of the mouse position at the time of the event.
- int mouse_x = 0;
-
- //! Y-coordinate of the mouse position at the time of the event.
- int mouse_y = 0;
+ //! mouse position in world coordinates (game units).
+ vec2 mouse_pos = {0, 0};
//! The mouse button that was clicked.
MouseButton button = MouseButton::NONE;
@@ -68,11 +63,8 @@ public:
*/
class MouseReleaseEvent : public Event {
public:
- //! X-coordinate of the mouse position at the time of the event.
- int mouse_x = 0;
-
- //! Y-coordinate of the mouse position at the time of the event.
- int mouse_y = 0;
+ //! mouse position in world coordinates (game units).
+ vec2 mouse_pos = {0, 0};
//! The mouse button that was released.
MouseButton button = MouseButton::NONE;
@@ -83,17 +75,10 @@ public:
*/
class MouseMoveEvent : public Event {
public:
- //! X-coordinate of the mouse position at the time of the event.
- int mouse_x = 0;
-
- //! Y-coordinate of the mouse position at the time of the event.
- int mouse_y = 0;
-
- // Movement since last event in x
- int delta_x = 0;
-
- // Movement since last event in y
- int delta_y = 0;
+ //! mouse position in world coordinates (game units).
+ vec2 mouse_pos = {0, 0};
+ //! The change in mouse position relative to the last position (in pixels).
+ ivec2 mouse_delta = {0, 0};
};
/**
@@ -101,12 +86,8 @@ public:
*/
class MouseScrollEvent : public Event {
public:
- //! X-coordinate of the mouse position at the time of the event.
- int mouse_x = 0;
-
- //! Y-coordinate of the mouse position at the time of the event.
- int mouse_y = 0;
-
+ //! mouse position in world coordinates (game units) when the scroll happened.
+ vec2 mouse_pos = {0, 0};
//! scroll direction (-1 = down, 1 = up)
int scroll_direction = 0;
//! scroll amount in y axis (from and away from the person).
@@ -127,4 +108,55 @@ public:
*/
class ShutDownEvent : public Event {};
+/**
+ * \brief Event triggered to indicate the window is overlapped by another window.
+ *
+ * When two windows overlap the bottom window gets distorted and that window has to be redrawn.
+ */
+class WindowExposeEvent : public Event {};
+
+/**
+ * \brief Event triggered to indicate the window is resized.
+ */
+class WindowResizeEvent : public Event {
+public:
+ //! new window dimensions
+ ivec2 dimensions = {0, 0};
+};
+
+/**
+ * \brief Event triggered to indicate the window is moved.
+ */
+class WindowMoveEvent : public Event {
+public:
+ //! The change in position relative to the last position (in pixels).
+ ivec2 delta_move = {0, 0};
+};
+
+/**
+ * \brief Event triggered to indicate the window is minimized.
+ */
+class WindowMinimizeEvent : public Event {};
+
+/**
+ * \brief Event triggered to indicate the window is maximized
+ */
+class WindowMaximizeEvent : public Event {};
+
+/**
+ * \brief Event triggered to indicate the window gained focus
+ *
+ * This event is triggered when the window receives focus, meaning it becomes the active window
+ * for user interaction.
+ */
+class WindowFocusGainEvent : public Event {};
+
+/**
+ * \brief Event triggered to indicate the window lost focus
+ *
+ * This event is triggered when the window loses focus, meaning it is no longer the active window
+ * for user interaction.
+ */
+class WindowFocusLostEvent : public Event {};
+
} // namespace crepe
diff --git a/src/crepe/api/KeyCodes.h b/src/crepe/api/KeyCodes.h
index fcfc080..1b9573a 100644
--- a/src/crepe/api/KeyCodes.h
+++ b/src/crepe/api/KeyCodes.h
@@ -1,5 +1,9 @@
#pragma once
+
+#include <unordered_map>
+
namespace crepe {
+
//! Enumeration for mouse button inputs, including standard and extended buttons.
enum class MouseButton {
NONE = 0, //!< No mouse button input.
@@ -151,4 +155,6 @@ enum class Keycode {
/// \}
MENU = 348, //!< Menu key.
};
+//! Typedef for keyboard state.
+typedef std::unordered_map<Keycode, bool> keyboard_state_t;
} // namespace crepe
diff --git a/src/crepe/api/ParticleEmitter.cpp b/src/crepe/api/ParticleEmitter.cpp
index a9c5cf6..9a70334 100644
--- a/src/crepe/api/ParticleEmitter.cpp
+++ b/src/crepe/api/ParticleEmitter.cpp
@@ -1,13 +1,16 @@
#include "ParticleEmitter.h"
+#include "api/Sprite.h"
using namespace crepe;
using namespace std;
-ParticleEmitter::ParticleEmitter(game_object_id_t game_object_id, const Data & data)
+ParticleEmitter::ParticleEmitter(game_object_id_t game_object_id, const Sprite & sprite,
+ const Data & data)
: Component(game_object_id),
+ sprite(sprite),
data(data) {
for (size_t i = 0; i < this->data.max_particles; i++) {
- this->data.particles.emplace_back();
+ this->particles.emplace_back();
}
}
@@ -20,6 +23,6 @@ void ParticleEmitter::restore(const Component & snapshot) {
}
ParticleEmitter & ParticleEmitter::operator=(const ParticleEmitter & other) {
- data.particles = other.data.particles;
+ this->particles = other.particles;
return *this;
}
diff --git a/src/crepe/api/ParticleEmitter.h b/src/crepe/api/ParticleEmitter.h
index 5f563de..1edd2b5 100644
--- a/src/crepe/api/ParticleEmitter.h
+++ b/src/crepe/api/ParticleEmitter.h
@@ -1,7 +1,11 @@
#pragma once
+#include <cmath>
#include <vector>
+#include "system/ParticleSystem.h"
+#include "system/RenderSystem.h"
+
#include "Component.h"
#include "Particle.h"
#include "types.h"
@@ -26,15 +30,18 @@ public:
*/
struct Boundary {
//! boundary width (midpoint is emitter location)
- double width = 0.0;
+ float width = INFINITY;
//! boundary height (midpoint is emitter location)
- double height = 0.0;
+ float height = INFINITY;
//! boundary offset from particle emitter location
vec2 offset;
//! reset on exit or stop velocity and set max postion
bool reset_on_exit = false;
};
+ //! sprite reference of displayed sprite
+ const Sprite & sprite;
+
/**
* \brief Holds parameters that control particle emission.
*
@@ -42,32 +49,28 @@ public:
* and the sprite used for rendering particles.
*/
struct Data {
- //! position of the emitter
- vec2 position;
+ //! offset of the emitter relative to transform
+ vec2 offset;
//! maximum number of particles
- const unsigned int max_particles = 0;
- //! rate of particle emission per update (Lowest value = 0.001 any lower is ignored)
- double emission_rate = 0;
+ const unsigned int max_particles = 256;
+ //! rate of particle emission per second
+ float emission_rate = 50;
//! min speed of the particles
- double min_speed = 0;
+ float min_speed = 100;
//! min speed of the particles
- double max_speed = 0;
+ float max_speed = 100;
//! min angle of particle emission
- double min_angle = 0;
+ float min_angle = 0;
//! max angle of particle emission
- double max_angle = 0;
- //! begin Lifespan of particle (only visual)
- double begin_lifespan = 0.0;
- //! end Lifespan of particle
- double end_lifespan = 0.0;
+ float max_angle = 0;
+ //! begin Lifespan of particle in seconds (only visual)
+ float begin_lifespan = 0.0;
+ //! end Lifespan of particle in seconds
+ float end_lifespan = 10.0;
//! force over time (physics)
vec2 force_over_time;
//! particle boundary
Boundary boundary;
- //! collection of particles
- std::vector<Particle> particles;
- //! sprite reference
- const Sprite & sprite;
};
public:
@@ -75,7 +78,7 @@ public:
* \param game_object_id Identifier for the game object using this emitter.
* \param data Configuration data defining particle properties.
*/
- ParticleEmitter(game_object_id_t game_object_id, const Data & data);
+ ParticleEmitter(game_object_id_t game_object_id, const Sprite & sprite, const Data & data);
public:
//! Configuration data for particle emission settings.
@@ -86,6 +89,16 @@ protected:
ParticleEmitter(const ParticleEmitter &) = default;
virtual void restore(const Component & snapshot);
virtual ParticleEmitter & operator=(const ParticleEmitter &);
+
+private:
+ //! Only ParticleSystem can move and read particles
+ friend ParticleSystem;
+ //! Only RenderSystem can read particles
+ friend RenderSystem;
+ //! Saves time left over from last update event.
+ float spawn_accumulator = 0;
+ //! collection of particles
+ std::vector<Particle> particles;
};
} // namespace crepe
diff --git a/src/crepe/api/Rigidbody.h b/src/crepe/api/Rigidbody.h
index b08c8db..6900295 100644
--- a/src/crepe/api/Rigidbody.h
+++ b/src/crepe/api/Rigidbody.h
@@ -139,7 +139,7 @@ public:
* Each element represents a layer ID, and the GameObject will only detect
* collisions with other GameObjects that belong to these layers.
*/
- std::set<int> collision_layers;
+ std::set<int> collision_layers = {0};
};
public:
diff --git a/src/crepe/api/Script.cpp b/src/crepe/api/Script.cpp
index 34e7908..ee76011 100644
--- a/src/crepe/api/Script.cpp
+++ b/src/crepe/api/Script.cpp
@@ -1,7 +1,7 @@
#include <string>
+#include "../facade/SDLContext.h"
#include "../manager/SceneManager.h"
-
#include "Script.h"
using namespace crepe;
@@ -45,3 +45,16 @@ void Script::replay::release(recording_t recording) {
ReplayManager & mgr = this->mediator->replay_manager;
return mgr.release(recording);
}
+
+const keyboard_state_t & Script::get_keyboard_state() const {
+ SDLContext & sdl_context = this->mediator->sdl_context;
+ return sdl_context.get_keyboard_state();
+}
+
+bool Script::get_key_state(Keycode key) const noexcept {
+ try {
+ return this->get_keyboard_state().at(key);
+ } catch (...) {
+ return false;
+ }
+}
diff --git a/src/crepe/api/Script.h b/src/crepe/api/Script.h
index 6536fa4..2750df0 100644
--- a/src/crepe/api/Script.h
+++ b/src/crepe/api/Script.h
@@ -2,6 +2,7 @@
#include <vector>
+#include "../api/KeyCodes.h"
#include "../manager/EventManager.h"
#include "../manager/Mediator.h"
#include "../manager/ReplayManager.h"
@@ -157,7 +158,23 @@ protected:
replay(OptionalRef<Mediator> & mediator) : mediator(mediator) {}
friend class Script;
} replay{mediator};
- //! \}
+
+ /**
+ * \brief Utility function to retrieve the keyboard state
+ * \see SDLContext::get_keyboard_state
+ *
+ * \return current keyboard state map with Keycode as key and bool as value(true = pressed, false = not pressed)
+ *
+ */
+ const keyboard_state_t & get_keyboard_state() const;
+ /**
+ * \brief Utility function to retrieve a single key state.
+ * \see SDLContext::get_keyboard_state
+ *
+ * \return Keycode state (true if pressed, false if not pressed).
+ *
+ */
+ bool get_key_state(Keycode key) const noexcept;
private:
/**