aboutsummaryrefslogtreecommitdiff
path: root/src/crepe
diff options
context:
space:
mode:
Diffstat (limited to 'src/crepe')
-rw-r--r--src/crepe/api/CMakeLists.txt6
-rw-r--r--src/crepe/api/Config.h2
-rw-r--r--src/crepe/api/Engine.cpp46
-rw-r--r--src/crepe/api/Engine.h10
-rw-r--r--src/crepe/api/IKeyListener.cpp19
-rw-r--r--src/crepe/api/IKeyListener.h50
-rw-r--r--src/crepe/api/IMouseListener.cpp29
-rw-r--r--src/crepe/api/IMouseListener.h73
-rw-r--r--src/crepe/api/LoopTimer.cpp79
-rw-r--r--src/crepe/api/LoopTimer.h138
-rw-r--r--src/crepe/api/Rigidbody.h4
-rw-r--r--src/crepe/facade/SDLContext.cpp43
-rw-r--r--src/crepe/facade/SDLContext.h23
-rw-r--r--src/crepe/manager/CMakeLists.txt2
-rw-r--r--src/crepe/manager/EventManager.cpp6
-rw-r--r--src/crepe/manager/EventManager.h23
-rw-r--r--src/crepe/manager/LoopTimerManager.cpp91
-rw-r--r--src/crepe/manager/LoopTimerManager.h177
-rw-r--r--src/crepe/manager/Mediator.h10
-rw-r--r--src/crepe/manager/SceneManager.cpp3
-rw-r--r--src/crepe/system/AISystem.cpp10
-rw-r--r--src/crepe/system/AnimatorSystem.cpp6
-rw-r--r--src/crepe/system/EventSystem.cpp1
-rw-r--r--src/crepe/system/PhysicsSystem.cpp130
-rw-r--r--src/crepe/system/RenderSystem.cpp23
-rw-r--r--src/crepe/system/RenderSystem.h11
26 files changed, 452 insertions, 563 deletions
diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt
index 6e062f9..48402ae 100644
--- a/src/crepe/api/CMakeLists.txt
+++ b/src/crepe/api/CMakeLists.txt
@@ -13,10 +13,7 @@ target_sources(crepe PUBLIC
Animator.cpp
BoxCollider.cpp
CircleCollider.cpp
- IKeyListener.cpp
- IMouseListener.cpp
Engine.cpp
- LoopTimer.cpp
Asset.cpp
EventHandler.cpp
Script.cpp
@@ -47,11 +44,8 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES
EventHandler.h
EventHandler.hpp
Event.h
- IKeyListener.h
- IMouseListener.h
Engine.h
Engine.hpp
- LoopTimer.h
Asset.h
Button.h
UIObject.h
diff --git a/src/crepe/api/Config.h b/src/crepe/api/Config.h
index 6472270..ca2d3f1 100644
--- a/src/crepe/api/Config.h
+++ b/src/crepe/api/Config.h
@@ -53,7 +53,7 @@ struct Config final {
*
* Gravity value of game.
*/
- double gravity = 1;
+ float gravity = 10;
} physics;
//! default window settings
diff --git a/src/crepe/api/Engine.cpp b/src/crepe/api/Engine.cpp
index 8ed55fa..7ae89b9 100644
--- a/src/crepe/api/Engine.cpp
+++ b/src/crepe/api/Engine.cpp
@@ -1,36 +1,60 @@
+#include "../util/Log.h"
+
#include "Engine.h"
using namespace crepe;
using namespace std;
void Engine::start() {
- this->setup();
- this->loop();
+ try {
+ this->setup();
+ } catch (const exception & e) {
+ Log::logf(Log::Level::ERROR, "Uncaught exception in setup: {}\n", e.what());
+ return;
+ }
+
+ try {
+ this->loop();
+ } catch (const exception & e) {
+ Log::logf(Log::Level::ERROR, "Uncaught exception in main loop: {}\n", e.what());
+ this->event_manager.trigger_event<ShutDownEvent>();
+ }
}
void Engine::setup() {
- LoopTimer & timer = this->loop_timer;
this->game_running = true;
+ this->loop_timer.start();
this->scene_manager.load_next_scene();
- timer.start();
- timer.set_fps(200);
+
+ this->event_manager.subscribe<ShutDownEvent>([this](const ShutDownEvent & event) {
+ this->game_running = false;
+
+ // propagate to possible user ShutDownEvent listeners
+ return false;
+ });
}
void Engine::loop() {
- LoopTimer & timer = this->loop_timer;
+ LoopTimerManager & timer = this->loop_timer;
SystemManager & systems = this->system_manager;
- timer.start();
-
while (game_running) {
timer.update();
while (timer.get_lag() >= timer.get_fixed_delta_time()) {
- systems.fixed_update();
- timer.advance_fixed_update();
+ try {
+ systems.fixed_update();
+ } catch (const exception & e) {
+ Log::logf(Log::Level::WARNING, "Uncaught exception in fixed update function: {}\n", e.what());
+ }
+ timer.advance_fixed_elapsed_time();
}
- systems.frame_update();
+ try {
+ systems.frame_update();
+ } catch (const exception & e) {
+ Log::logf(Log::Level::WARNING, "Uncaught exception in frame update function: {}\n", e.what());
+ }
timer.enforce_frame_rate();
}
}
diff --git a/src/crepe/api/Engine.h b/src/crepe/api/Engine.h
index 7601015..5421d60 100644
--- a/src/crepe/api/Engine.h
+++ b/src/crepe/api/Engine.h
@@ -8,8 +8,8 @@
#include "../manager/SaveManager.h"
#include "../manager/SceneManager.h"
#include "../manager/SystemManager.h"
-
-#include "LoopTimer.h"
+#include "../manager/LoopTimerManager.h"
+#include "../manager/EventManager.h"
namespace crepe {
@@ -54,14 +54,16 @@ private:
ComponentManager component_manager{mediator};
//! Scene manager instance
SceneManager scene_manager{mediator};
+ //! LoopTimerManager instance
+ LoopTimerManager loop_timer{mediator};
+ //! EventManager instance
+ EventManager event_manager{mediator};
//! Resource manager instance
ResourceManager resource_manager{mediator};
//! Save manager instance
SaveManager save_manager{mediator};
//! SDLContext instance
SDLContext sdl_context{mediator};
- //! LoopTimer instance
- LoopTimer loop_timer{mediator};
//! ReplayManager instance
ReplayManager replay_manager{mediator};
//! SystemManager
diff --git a/src/crepe/api/IKeyListener.cpp b/src/crepe/api/IKeyListener.cpp
deleted file mode 100644
index 8642655..0000000
--- a/src/crepe/api/IKeyListener.cpp
+++ /dev/null
@@ -1,19 +0,0 @@
-#include "IKeyListener.h"
-
-using namespace crepe;
-
-// Constructor with specified channel
-IKeyListener::IKeyListener(event_channel_t channel)
- : event_manager(EventManager::get_instance()) {
- this->press_id = event_manager.subscribe<KeyPressEvent>(
- [this](const KeyPressEvent & event) { return this->on_key_pressed(event); }, channel);
- this->release_id = event_manager.subscribe<KeyReleaseEvent>(
- [this](const KeyReleaseEvent & event) { return this->on_key_released(event); },
- channel);
-}
-
-// Destructor, unsubscribe events
-IKeyListener::~IKeyListener() {
- event_manager.unsubscribe(this->press_id);
- event_manager.unsubscribe(this->release_id);
-}
diff --git a/src/crepe/api/IKeyListener.h b/src/crepe/api/IKeyListener.h
deleted file mode 100644
index 180a0a6..0000000
--- a/src/crepe/api/IKeyListener.h
+++ /dev/null
@@ -1,50 +0,0 @@
-#pragma once
-
-#include "../manager/EventManager.h"
-
-#include "Event.h"
-#include "EventHandler.h"
-
-namespace crepe {
-
-/**
- * \class IKeyListener
- * \brief Interface for keyboard event handling in the application.
- */
-class IKeyListener {
-public:
- /**
- * \brief Constructs an IKeyListener with a specified channel.
- * \param channel The channel ID for event handling.
- */
- IKeyListener(event_channel_t channel = EventManager::CHANNEL_ALL);
- virtual ~IKeyListener();
- IKeyListener(const IKeyListener &) = delete;
- IKeyListener & operator=(const IKeyListener &) = delete;
- IKeyListener & operator=(IKeyListener &&) = delete;
- IKeyListener(IKeyListener &&) = delete;
-
- /**
- * \brief Pure virtual function to handle key press events.
- * \param event The key press event to handle.
- * \return True if the event was handled, false otherwise.
- */
- virtual bool on_key_pressed(const KeyPressEvent & event) = 0;
-
- /**
- * \brief Pure virtual function to handle key release events.
- * \param event The key release event to handle.
- * \return True if the event was handled, false otherwise.
- */
- virtual bool on_key_released(const KeyReleaseEvent & event) = 0;
-
-private:
- //! Key press event id
- subscription_t press_id = -1;
- //! Key release event id
- subscription_t release_id = -1;
- //! EventManager reference
- EventManager & event_manager;
-};
-
-} // namespace crepe
diff --git a/src/crepe/api/IMouseListener.cpp b/src/crepe/api/IMouseListener.cpp
deleted file mode 100644
index 989aeb3..0000000
--- a/src/crepe/api/IMouseListener.cpp
+++ /dev/null
@@ -1,29 +0,0 @@
-#include "IMouseListener.h"
-
-using namespace crepe;
-
-IMouseListener::IMouseListener(event_channel_t channel)
- : event_manager(EventManager::get_instance()) {
- this->click_id = event_manager.subscribe<MouseClickEvent>(
- [this](const MouseClickEvent & event) { return this->on_mouse_clicked(event); },
- channel);
-
- this->press_id = event_manager.subscribe<MousePressEvent>(
- [this](const MousePressEvent & event) { return this->on_mouse_pressed(event); },
- channel);
-
- this->release_id = event_manager.subscribe<MouseReleaseEvent>(
- [this](const MouseReleaseEvent & event) { return this->on_mouse_released(event); },
- channel);
-
- this->move_id = event_manager.subscribe<MouseMoveEvent>(
- [this](const MouseMoveEvent & event) { return this->on_mouse_moved(event); }, channel);
-}
-
-IMouseListener::~IMouseListener() {
- // Unsubscribe event handlers
- event_manager.unsubscribe(this->click_id);
- event_manager.unsubscribe(this->press_id);
- event_manager.unsubscribe(this->release_id);
- event_manager.unsubscribe(this->move_id);
-}
diff --git a/src/crepe/api/IMouseListener.h b/src/crepe/api/IMouseListener.h
deleted file mode 100644
index e19897d..0000000
--- a/src/crepe/api/IMouseListener.h
+++ /dev/null
@@ -1,73 +0,0 @@
-#pragma once
-
-#include "../manager/EventManager.h"
-
-#include "Event.h"
-#include "EventHandler.h"
-
-namespace crepe {
-
-/**
- * \class IMouseListener
- * \brief Interface for mouse event handling in the application.
- */
-class IMouseListener {
-public:
- /**
- * \brief Constructs an IMouseListener with a specified channel.
- * \param channel The channel ID for event handling.
- */
- IMouseListener(event_channel_t channel = EventManager::CHANNEL_ALL);
- virtual ~IMouseListener();
- IMouseListener & operator=(const IMouseListener &) = delete;
- IMouseListener(const IMouseListener &) = delete;
- IMouseListener & operator=(const IMouseListener &&) = delete;
- IMouseListener(IMouseListener &&) = delete;
-
- /**
- * \brief Move assignment operator (deleted).
- */
- IMouseListener & operator=(IMouseListener &&) = delete;
-
- /**
- * \brief Handles a mouse click event.
- * \param event The mouse click event to handle.
- * \return True if the event was handled, false otherwise.
- */
- virtual bool on_mouse_clicked(const MouseClickEvent & event) = 0;
-
- /**
- * \brief Handles a mouse press event.
- * \param event The mouse press event to handle.
- * \return True if the event was handled, false otherwise.
- */
- virtual bool on_mouse_pressed(const MousePressEvent & event) = 0;
-
- /**
- * \brief Handles a mouse release event.
- * \param event The mouse release event to handle.
- * \return True if the event was handled, false otherwise.
- */
- virtual bool on_mouse_released(const MouseReleaseEvent & event) = 0;
-
- /**
- * \brief Handles a mouse move event.
- * \param event The mouse move event to handle.
- * \return True if the event was handled, false otherwise.
- */
- virtual bool on_mouse_moved(const MouseMoveEvent & event) = 0;
-
-private:
- //! Mouse click event id
- subscription_t click_id = -1;
- //! Mouse press event id
- subscription_t press_id = -1;
- //! Mouse release event id
- subscription_t release_id = -1;
- //! Mouse move event id
- subscription_t move_id = -1;
- //! EventManager reference
- EventManager & event_manager;
-};
-
-} //namespace crepe
diff --git a/src/crepe/api/LoopTimer.cpp b/src/crepe/api/LoopTimer.cpp
deleted file mode 100644
index 56e48d3..0000000
--- a/src/crepe/api/LoopTimer.cpp
+++ /dev/null
@@ -1,79 +0,0 @@
-#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/LoopTimer.h b/src/crepe/api/LoopTimer.h
deleted file mode 100644
index fb52a10..0000000
--- a/src/crepe/api/LoopTimer.h
+++ /dev/null
@@ -1,138 +0,0 @@
-#pragma once
-
-#include "manager/Manager.h"
-#include <chrono>
-
-namespace crepe {
-
-class LoopTimer : public Manager {
-public:
- /**
- * \brief Get the current delta time for the current frame.
- *
- * \return Delta time in seconds since the last frame.
- */
- double get_delta_time() const;
-
- /**
- * \brief Get the current game time.
- *
- * \note The current game time may vary from real-world elapsed time. It is the cumulative
- * sum of each frame's delta time.
- *
- * \return Elapsed game time in seconds.
- */
- double get_current_time() const;
-
- /**
- * \brief Set the target frames per second (FPS).
- *
- * \param fps The desired frames rendered per second.
- */
- void set_fps(int fps);
-
- /**
- * \brief Get the current frames per second (FPS).
- *
- * \return Current FPS.
- */
- int get_fps() const;
-
- /**
- * \brief Get the current game scale.
- *
- * \return The current game scale, where 0 = paused, 1 = normal speed, and values > 1 speed
- * up the game.
- */
- double get_game_scale() const;
-
- /**
- * \brief Set the game scale.
- *
- * \param game_scale The desired game scale (0 = pause, 1 = normal speed, > 1 = speed up).
- */
- void set_game_scale(double game_scale);
-
-private:
- friend class Engine;
-
- /**
- * \brief Start the loop timer.
- *
- * Initializes the timer to begin tracking frame times.
- */
- void start();
-
- /**
- * \brief Enforce the frame rate limit.
- *
- * Ensures that the game loop does not exceed the target FPS by delaying frame updates as
- * necessary.
- */
- void enforce_frame_rate();
-
- /**
- * \brief Get the fixed delta time for consistent updates.
- *
- * Fixed delta time is used for operations that require uniform time steps, such as physics
- * calculations.
- *
- * \return Fixed delta time in seconds.
- */
- double get_fixed_delta_time() const;
-
- /**
- * \brief Get the accumulated lag in the game loop.
- *
- * Lag represents the difference between the target frame time and the actual frame time,
- * useful for managing fixed update intervals.
- *
- * \return Accumulated lag in seconds.
- */
- double get_lag() const;
-
- /**
- * \brief Construct a new LoopTimer object.
- *
- * Private constructor for singleton pattern to restrict instantiation outside the class.
- */
- LoopTimer(Mediator & mediator);
-
- /**
- * \brief Update the timer to the current frame.
- *
- * Calculates and updates the delta time for the current frame and adds it to the cumulative
- * game time.
- */
- void update();
-
- /**
- * \brief Advance the game loop by a fixed update interval.
- *
- * This method progresses the game state by a consistent, fixed time step, allowing for
- * stable updates independent of frame rate fluctuations.
- */
- void advance_fixed_update();
-
-private:
- //! Current frames per second
- int fps = 50;
- //! Current game scale
- double game_scale = 1;
- //! Maximum delta time in seconds to avoid large jumps
- std::chrono::duration<double> maximum_delta_time{0.25};
- //! Delta time for the current frame in seconds
- std::chrono::duration<double> delta_time{0.0};
- //! Target time per frame in seconds
- std::chrono::duration<double> frame_target_time = std::chrono::duration<double>(1.0) / fps;
- //! Fixed delta time for fixed updates in seconds
- std::chrono::duration<double> fixed_delta_time = std::chrono::duration<double>(1.0) / 50.0;
- //! Total elapsed game time in seconds
- std::chrono::duration<double> elapsed_time{0.0};
- //! Total elapsed time for fixed updates in seconds
- std::chrono::duration<double> elapsed_fixed_time{0.0};
- //! Time of the last frame
- std::chrono::steady_clock::time_point last_frame_time;
-};
-
-} // namespace crepe
diff --git a/src/crepe/api/Rigidbody.h b/src/crepe/api/Rigidbody.h
index 40c6bf1..b08c8db 100644
--- a/src/crepe/api/Rigidbody.h
+++ b/src/crepe/api/Rigidbody.h
@@ -53,7 +53,7 @@ public:
*/
struct Data {
//! objects mass
- float mass = 0.0;
+ float mass = 1;
/**
* \brief Gravity scale factor.
*
@@ -79,7 +79,7 @@ public:
//! Linear velocity of the object (speed and direction).
vec2 linear_velocity;
//! Maximum linear velocity of the object. This limits the object's speed.
- vec2 max_linear_velocity = {INFINITY, INFINITY};
+ float max_linear_velocity = INFINITY;
//! Linear velocity coefficient. This scales the object's velocity for adjustment or damping.
vec2 linear_velocity_coefficient = {1, 1};
//! \}
diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp
index 1dbd6a5..20bb030 100644
--- a/src/crepe/facade/SDLContext.cpp
+++ b/src/crepe/facade/SDLContext.cpp
@@ -2,6 +2,7 @@
#include <SDL2/SDL_blendmode.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_keycode.h>
+#include <SDL2/SDL_pixels.h>
#include <SDL2/SDL_rect.h>
#include <SDL2/SDL_render.h>
#include <SDL2/SDL_surface.h>
@@ -232,15 +233,12 @@ SDL_FRect SDLContext::get_dst_rect(const DestinationRectangleData & ctx) const {
if (data.size.y == 0 && data.size.x != 0) {
size.y = data.size.x / aspect_ratio;
}
+ size *= cam_aux_data.render_scale * ctx.img_scale * data.scale_offset;
- const CameraValues & cam = ctx.cam;
-
- size *= cam.render_scale * ctx.img_scale * data.scale_offset;
-
- vec2 screen_pos
- = (ctx.pos + data.position_offset - cam.cam_pos + (cam.zoomed_viewport) / 2)
- * cam.render_scale
- - size / 2 + cam.bar_size;
+ vec2 screen_pos = (ctx.pos + data.position_offset - cam_aux_data.cam_pos
+ + (cam_aux_data.zoomed_viewport) / 2)
+ * cam_aux_data.render_scale
+ - size / 2 + cam_aux_data.bar_size;
return SDL_FRect{
.x = screen_pos.x,
@@ -269,7 +267,6 @@ void SDLContext::draw(const RenderContext & ctx) {
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,
});
@@ -281,10 +278,9 @@ void SDLContext::draw(const RenderContext & ctx) {
angle, NULL, render_flip);
}
-SDLContext::CameraValues SDLContext::set_camera(const Camera & cam) {
+void SDLContext::update_camera_view(const Camera & cam, const vec2 & new_pos) {
const Camera::Data & cam_data = cam.data;
- CameraValues ret_cam;
// resize window
int w, h;
SDL_GetWindowSize(this->game_window.get(), &w, &h);
@@ -292,9 +288,10 @@ SDLContext::CameraValues SDLContext::set_camera(const Camera & cam) {
SDL_SetWindowSize(this->game_window.get(), cam.screen.x, cam.screen.y);
}
- vec2 & zoomed_viewport = ret_cam.zoomed_viewport;
- vec2 & bar_size = ret_cam.bar_size;
- vec2 & render_scale = ret_cam.render_scale;
+ vec2 & zoomed_viewport = this->cam_aux_data.zoomed_viewport;
+ vec2 & bar_size = this->cam_aux_data.bar_size;
+ vec2 & render_scale = this->cam_aux_data.render_scale;
+ this->cam_aux_data.cam_pos = new_pos;
zoomed_viewport = cam.viewport_size * cam_data.zoom;
float screen_aspect = static_cast<float>(cam.screen.x) / cam.screen.y;
@@ -336,12 +333,8 @@ SDLContext::CameraValues SDLContext::set_camera(const Camera & cam) {
// fill bg color
SDL_RenderFillRect(this->game_renderer.get(), &bg);
-
- return ret_cam;
}
-uint64_t SDLContext::get_ticks() const { return SDL_GetTicks64(); }
-
std::unique_ptr<SDL_Texture, std::function<void(SDL_Texture *)>>
SDLContext::texture_from_path(const std::string & path) {
@@ -372,12 +365,14 @@ ivec2 SDLContext::get_size(const Texture & ctx) {
return size;
}
-void SDLContext::delay(int ms) const { SDL_Delay(ms); }
-
std::vector<SDLContext::EventData> SDLContext::get_events() {
std::vector<SDLContext::EventData> event_list;
SDL_Event event;
+ const CameraAuxiliaryData & cam = this->cam_aux_data;
while (SDL_PollEvent(&event)) {
+ ivec2 mouse_pos;
+ mouse_pos.x = (event.button.x - cam.bar_size.x) / cam.render_scale.x;
+ mouse_pos.y = (event.button.y - cam.bar_size.y) / cam.render_scale.y;
switch (event.type) {
case SDL_QUIT:
event_list.push_back(EventData{
@@ -401,7 +396,7 @@ std::vector<SDLContext::EventData> SDLContext::get_events() {
event_list.push_back(EventData{
.event_type = SDLContext::EventType::MOUSEDOWN,
.mouse_button = sdl_to_mousebutton(event.button.button),
- .mouse_position = {event.button.x, event.button.y},
+ .mouse_position = mouse_pos,
});
break;
case SDL_MOUSEBUTTONUP: {
@@ -410,21 +405,21 @@ std::vector<SDLContext::EventData> SDLContext::get_events() {
event_list.push_back(EventData{
.event_type = SDLContext::EventType::MOUSEUP,
.mouse_button = sdl_to_mousebutton(event.button.button),
- .mouse_position = {event.button.x, event.button.y},
+ .mouse_position = mouse_pos,
});
} break;
case SDL_MOUSEMOTION: {
event_list.push_back(
EventData{.event_type = SDLContext::EventType::MOUSEMOVE,
- .mouse_position = {event.motion.x, event.motion.y},
+ .mouse_position = mouse_pos,
.rel_mouse_move = {event.motion.xrel, event.motion.yrel}});
} break;
case SDL_MOUSEWHEEL: {
event_list.push_back(EventData{
.event_type = SDLContext::EventType::MOUSEWHEEL,
- .mouse_position = {event.motion.x, event.motion.y},
+ .mouse_position = mouse_pos,
// TODO: why is this needed?
.scroll_direction = event.wheel.y < 0 ? -1 : 1,
.scroll_delta = event.wheel.preciseY,
diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h
index 46b779f..bcadf87 100644
--- a/src/crepe/facade/SDLContext.h
+++ b/src/crepe/facade/SDLContext.h
@@ -24,7 +24,6 @@ class Texture;
class Mediator;
/**
- * \class SDLContext
* \brief Facade for the SDL library
*
* SDLContext is a singleton that handles the SDL window and renderer, provides methods for
@@ -33,7 +32,7 @@ class Mediator;
class SDLContext {
public:
//! data that the camera component cannot hold
- struct CameraValues {
+ struct CameraAuxiliaryData {
//! zoomed in viewport in game_units
vec2 zoomed_viewport;
@@ -64,7 +63,6 @@ public:
struct RenderContext {
const Sprite & sprite;
const Texture & texture;
- const CameraValues & cam;
const vec2 & pos;
const double & angle;
const double & scale;
@@ -193,18 +191,20 @@ public:
void present_screen();
/**
- * \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
+ * \brief calculates camera view settings. such as black_bars, zoomed_viewport, scaling and
+ * adjusting window size.
+ *
+ * \note only supports windowed mode.
+ * \param camera Reference to the current Camera object in the scene.
+ * \param new_pos new camera position from transform and offset
*/
- CameraValues set_camera(const Camera & camera);
+ void update_camera_view(const Camera & camera, const vec2 & new_pos);
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;
};
@@ -233,6 +233,13 @@ private:
//! black bars rectangle to draw
SDL_FRect black_bars[2] = {};
+
+ /**
+ * \cam_aux_data extra data that the component cannot hold.
+ *
+ * - this is defined in this class because get_events() needs this information aswell
+ */
+ CameraAuxiliaryData cam_aux_data;
};
} // namespace crepe
diff --git a/src/crepe/manager/CMakeLists.txt b/src/crepe/manager/CMakeLists.txt
index aa19f3e..48e444f 100644
--- a/src/crepe/manager/CMakeLists.txt
+++ b/src/crepe/manager/CMakeLists.txt
@@ -4,6 +4,7 @@ target_sources(crepe PUBLIC
Manager.cpp
SaveManager.cpp
SceneManager.cpp
+ LoopTimerManager.cpp
ResourceManager.cpp
ReplayManager.cpp
SystemManager.cpp
@@ -19,6 +20,7 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES
SaveManager.h
SceneManager.h
SceneManager.hpp
+ LoopTimerManager.h
ResourceManager.h
ResourceManager.hpp
ReplayManager.h
diff --git a/src/crepe/manager/EventManager.cpp b/src/crepe/manager/EventManager.cpp
index 20f0dd3..6aa49ee 100644
--- a/src/crepe/manager/EventManager.cpp
+++ b/src/crepe/manager/EventManager.cpp
@@ -3,11 +3,9 @@
using namespace crepe;
using namespace std;
-EventManager & EventManager::get_instance() {
- static EventManager instance;
- return instance;
+EventManager::EventManager(Mediator & mediator) : Manager(mediator) {
+ this->mediator.event_manager = *this;
}
-
void EventManager::dispatch_events() {
for (auto & event : this->events_queue) {
this->handle_event(event.type, event.channel, *event.event.get());
diff --git a/src/crepe/manager/EventManager.h b/src/crepe/manager/EventManager.h
index ba5e98b..639e37f 100644
--- a/src/crepe/manager/EventManager.h
+++ b/src/crepe/manager/EventManager.h
@@ -8,6 +8,8 @@
#include "../api/Event.h"
#include "../api/EventHandler.h"
+#include "Manager.h"
+
namespace crepe {
//! Event listener unique ID
@@ -22,27 +24,19 @@ typedef size_t subscription_t;
typedef size_t event_channel_t;
/**
- * \class EventManager
* \brief Manages event subscriptions, triggers, and queues, enabling decoupled event handling.
*
* The `EventManager` acts as a centralized event system. It allows for registering callbacks
* for specific event types, triggering events synchronously, queueing events for later
* processing, and managing subscriptions via unique identifiers.
*/
-class EventManager {
+class EventManager : public Manager {
public:
static constexpr const event_channel_t CHANNEL_ALL = -1;
-
/**
- * \brief Get the singleton instance of the EventManager.
- *
- * This method returns the unique instance of the EventManager, creating it if it
- * doesn't already exist. Ensures only one instance is active in the program.
- *
- * \return Reference to the singleton instance of the EventManager.
+ * \param mediator A reference to a Mediator object used for transfering managers.
*/
- static EventManager & get_instance();
-
+ EventManager(Mediator & mediator);
/**
* \brief Subscribe to a specific event type.
*
@@ -108,13 +102,6 @@ public:
private:
/**
- * \brief Default constructor for the EventManager.
- *
- * Constructor is private to enforce the singleton pattern.
- */
- EventManager() = default;
-
- /**
* \struct QueueEntry
* \brief Represents an entry in the event queue.
*/
diff --git a/src/crepe/manager/LoopTimerManager.cpp b/src/crepe/manager/LoopTimerManager.cpp
new file mode 100644
index 0000000..9819632
--- /dev/null
+++ b/src/crepe/manager/LoopTimerManager.cpp
@@ -0,0 +1,91 @@
+#include <chrono>
+#include <thread>
+
+#include "../util/Log.h"
+
+#include "LoopTimerManager.h"
+
+using namespace crepe;
+using namespace std::chrono;
+using namespace std::chrono_literals;
+
+LoopTimerManager::LoopTimerManager(Mediator & mediator) : Manager(mediator) {
+ this->mediator.loop_timer = *this;
+ dbg_trace();
+}
+
+void LoopTimerManager::start() {
+ this->last_frame_time = std::chrono::steady_clock::now();
+
+ this->elapsed_time = elapsed_time_t{0};
+ this->elapsed_fixed_time = elapsed_time_t{0};
+ this->delta_time = duration_t{0};
+}
+
+void LoopTimerManager::update() {
+ time_point_t current_frame_time = std::chrono::steady_clock::now();
+ // Convert to duration in seconds for delta time
+ this->delta_time = current_frame_time - last_frame_time;
+
+ if (this->delta_time > this->maximum_delta_time) {
+ this->delta_time = this->maximum_delta_time;
+ }
+ if (this->delta_time > 0s) {
+ this->actual_fps = 1.0 / duration_cast<seconds>(this->delta_time).count();
+ } else {
+ this->actual_fps = 0;
+ }
+ this->elapsed_time += duration_cast<elapsed_time_t>(this->delta_time);
+ this->last_frame_time = current_frame_time;
+}
+
+duration_t LoopTimerManager::get_delta_time() const {
+ return this->delta_time * this->time_scale;
+}
+
+elapsed_time_t LoopTimerManager::get_elapsed_time() const { return this->elapsed_time; }
+
+void LoopTimerManager::advance_fixed_elapsed_time() {
+ this->elapsed_fixed_time
+ += std::chrono::duration_cast<elapsed_time_t>(this->fixed_delta_time);
+}
+
+void LoopTimerManager::set_target_framerate(unsigned fps) {
+ this->target_fps = fps;
+ //check if fps is lower or equals 0
+ if (fps <= 0) return;
+ // target time per frame in seconds
+ this->frame_target_time = duration_t(1s) / this->target_fps;
+}
+
+unsigned LoopTimerManager::get_fps() const { return this->actual_fps; }
+
+void LoopTimerManager::set_time_scale(double value) { this->time_scale = value; }
+
+float LoopTimerManager::get_time_scale() const { return this->time_scale; }
+
+void LoopTimerManager::enforce_frame_rate() {
+ time_point_t current_frame_time = std::chrono::steady_clock::now();
+ duration_t frame_duration = current_frame_time - this->last_frame_time;
+ // Check if frame duration is less than the target frame time
+ if (frame_duration < this->frame_target_time) {
+ duration_t delay_time = this->frame_target_time - frame_duration;
+ if (delay_time > 0s) {
+ std::this_thread::sleep_for(delay_time);
+ }
+ }
+}
+
+duration_t LoopTimerManager::get_lag() const {
+ return this->elapsed_time - this->elapsed_fixed_time;
+}
+
+duration_t LoopTimerManager::get_scaled_fixed_delta_time() const {
+ return this->fixed_delta_time * this->time_scale;
+}
+
+void LoopTimerManager::set_fixed_delta_time(float seconds) {
+ this->fixed_delta_time = duration_t(seconds);
+}
+
+duration_t LoopTimerManager::get_fixed_delta_time() const { return this->fixed_delta_time; }
diff --git a/src/crepe/manager/LoopTimerManager.h b/src/crepe/manager/LoopTimerManager.h
new file mode 100644
index 0000000..139294d
--- /dev/null
+++ b/src/crepe/manager/LoopTimerManager.h
@@ -0,0 +1,177 @@
+#pragma once
+
+#include <chrono>
+
+#include "Manager.h"
+
+namespace crepe {
+
+class Engine;
+
+typedef std::chrono::duration<double> duration_t;
+typedef std::chrono::duration<unsigned long long, std::micro> elapsed_time_t;
+
+/**
+ * \brief Manages timing and frame rate for the game loop.
+ *
+ * The LoopTimerManager class is responsible for calculating and managing timing functions
+ * such as delta time, frames per second (FPS), fixed time steps, and time scaling. It ensures
+ * consistent frame updates and supports game loop operations, such as handling fixed updates
+ * for physics and other time-sensitive operations.
+ */
+class LoopTimerManager : public Manager {
+public:
+ /**
+ * \param mediator A reference to a Mediator object used for transfering managers.
+ */
+ LoopTimerManager(Mediator & mediator);
+ /**
+ * \brief Get the current delta time for the current frame.
+ *
+ * This value represents the estimated frame duration of the current frame.
+ * This value can be used in the frame_update to convert pixel based values to time based values.
+ *
+ * \return Delta time in seconds since the last frame.
+ */
+ duration_t get_delta_time() const;
+
+ /**
+ * \brief Get the current elapsed time (total time passed )
+ *
+ * \note The current game time may vary from real-world elapsed time. It is the cumulative
+ * sum of each frame's delta time.
+ *
+ * \return Elapsed game time in seconds.
+ */
+ elapsed_time_t get_elapsed_time() const;
+
+ /**
+ * \brief Set the target frames per second (FPS).
+ *
+ * \param fps The desired frames rendered per second.
+ */
+ void set_target_framerate(unsigned fps);
+
+ /**
+ * \brief Get the current frames per second (FPS).
+ *
+ * \return Current FPS.
+ */
+ unsigned get_fps() const;
+
+ /**
+ * \brief Get the current time scale.
+ *
+ * \return The current time scale, where (0 = pause, < 1 = slow down, 1 = normal speed, > 1 = speed up).
+ * up the game.
+ */
+ float get_time_scale() const;
+
+ /**
+ * \brief Set the time scale.
+ *
+ * time_scale is a value that changes the delta time that can be retrieved using get_delta_time function.
+ *
+ * \param time_scale The desired time scale (0 = pause, < 1 = slow down, 1 = normal speed, > 1 = speed up).
+ */
+ void set_time_scale(double time_scale);
+
+ /**
+ * \brief Get the fixed delta time in seconds without scaling by the time scale.
+ *
+ * This value is used in the LoopManager to determine how many times
+ * the fixed_update should be called within a given interval.
+ *
+ * \return The unscaled fixed delta time in seconds.
+ */
+ duration_t get_fixed_delta_time() const;
+
+ /**
+ * \brief Set the fixed_delta_time in seconds.
+ *
+ * \param seconds fixed_delta_time in seconds.
+ *
+ * The fixed_delta_time value is used to determine how many times per second the fixed_update and process_input functions are called.
+ *
+ */
+ void set_fixed_delta_time(float seconds);
+
+ /**
+ * \brief Retrieves the scaled fixed delta time in seconds.
+ *
+ * The scaled fixed delta time is the timing value used within the `fixed_update` function.
+ * It is adjusted by the time_scale to account for any changes in the simulation's
+ * speed.
+ *
+ * \return The fixed delta time, scaled by the current time scale, in seconds.
+ */
+ duration_t get_scaled_fixed_delta_time() const;
+
+private:
+ //! Friend relation to use start,enforce_frame_rate,get_lag,update,advance_fixed_update.
+ friend class Engine;
+ /**
+ * \brief Start the loop timer.
+ *
+ * Initializes the timer to begin tracking frame times.
+ */
+ void start();
+ /**
+ * \brief Enforce the frame rate limit.
+ *
+ * Ensures that the game loop does not exceed the target FPS by delaying frame updates as
+ * necessary.
+ */
+ void enforce_frame_rate();
+ /**
+ * \brief Get the accumulated lag in the game loop.
+ *
+ * Lag represents the difference between the target frame time and the actual frame time,
+ * useful for managing fixed update intervals.
+ *
+ * \return Accumulated lag in seconds.
+ */
+ duration_t get_lag() const;
+
+ /**
+ * \brief Update the timer to the current frame.
+ *
+ * Calculates and updates the delta time for the current frame and adds it to the cumulative
+ * game time.
+ */
+ void update();
+
+ /**
+ * \brief Progress the elapsed fixed time by the fixed delta time interval.
+ *
+ * This method advances the game's fixed update loop by adding the fixed_delta_time
+ * to elapsed_fixed_time, ensuring the fixed update catches up with the elapsed time.
+ */
+ void advance_fixed_elapsed_time();
+
+private:
+ //! Target frames per second.
+ unsigned target_fps = 60;
+ //! Actual frames per second.
+ unsigned actual_fps = 0;
+ //! Time scale for speeding up or slowing down the game (0 = pause, < 1 = slow down, 1 = normal speed, > 1 = speed up).
+ float time_scale = 1;
+ //! Maximum delta time in seconds to avoid large jumps.
+ duration_t maximum_delta_time{0.25};
+ //! Delta time for the current frame in seconds.
+ duration_t delta_time{0.0};
+ //! Target time per frame in seconds
+ duration_t frame_target_time{1.0 / target_fps};
+ //! Fixed delta time for fixed updates in seconds.
+ duration_t fixed_delta_time{1.0 / 50.0};
+ //! Total elapsed game time in microseconds.
+ elapsed_time_t elapsed_time{0};
+ //! Total elapsed time for fixed updates in microseconds.
+ elapsed_time_t elapsed_fixed_time{0};
+
+ typedef std::chrono::steady_clock::time_point time_point_t;
+ //! Time of the last frame.
+ time_point_t last_frame_time;
+};
+
+} // namespace crepe
diff --git a/src/crepe/manager/Mediator.h b/src/crepe/manager/Mediator.h
index 95ca4b5..842f1de 100644
--- a/src/crepe/manager/Mediator.h
+++ b/src/crepe/manager/Mediator.h
@@ -2,17 +2,15 @@
#include "../util/OptionalRef.h"
-// TODO: remove these singletons:
-#include "EventManager.h"
-
namespace crepe {
class ComponentManager;
class SceneManager;
+class EventManager;
+class LoopTimerManager;
class SaveManager;
class ResourceManager;
class SDLContext;
-class LoopTimer;
class ReplayManager;
class SystemManager;
@@ -32,10 +30,10 @@ struct Mediator {
OptionalRef<SDLContext> sdl_context;
OptionalRef<ComponentManager> component_manager;
OptionalRef<SceneManager> scene_manager;
+ OptionalRef<EventManager> event_manager;
+ OptionalRef<LoopTimerManager> loop_timer;
OptionalRef<SaveManager> save_manager;
- OptionalRef<EventManager> event_manager = EventManager::get_instance();
OptionalRef<ResourceManager> resource_manager;
- OptionalRef<LoopTimer> timer;
OptionalRef<ReplayManager> replay_manager;
OptionalRef<SystemManager> system_manager;
};
diff --git a/src/crepe/manager/SceneManager.cpp b/src/crepe/manager/SceneManager.cpp
index 50a9fbb..d4ca90b 100644
--- a/src/crepe/manager/SceneManager.cpp
+++ b/src/crepe/manager/SceneManager.cpp
@@ -32,4 +32,7 @@ void SceneManager::load_next_scene() {
// Load the new scene
scene->load_scene();
+
+ //clear the next scene
+ next_scene.clear();
}
diff --git a/src/crepe/system/AISystem.cpp b/src/crepe/system/AISystem.cpp
index 3c3fd93..0f35010 100644
--- a/src/crepe/system/AISystem.cpp
+++ b/src/crepe/system/AISystem.cpp
@@ -1,22 +1,22 @@
#include <algorithm>
#include <cmath>
-#include "api/LoopTimer.h"
#include "manager/ComponentManager.h"
+#include "manager/LoopTimerManager.h"
#include "manager/Mediator.h"
#include "AISystem.h"
using namespace crepe;
+using namespace std::chrono;
void AISystem::fixed_update() {
const Mediator & mediator = this->mediator;
ComponentManager & mgr = mediator.component_manager;
- LoopTimer & timer = mediator.timer;
+ LoopTimerManager & loop_timer = mediator.loop_timer;
RefVector<AI> ai_components = mgr.get_components_by_type<AI>();
- //TODO: Use fixed loop dt (this is not available at master at the moment)
- double dt = timer.get_delta_time();
+ float dt = loop_timer.get_scaled_fixed_delta_time().count();
// Loop through all AI components
for (AI & ai : ai_components) {
@@ -144,7 +144,7 @@ vec2 AISystem::arrive(const AI & ai, const Rigidbody & rigidbody,
}
float speed = distance / ai.arrive_deceleration;
- speed = std::min(speed, rigidbody.data.max_linear_velocity.length());
+ speed = std::min(speed, rigidbody.data.max_linear_velocity);
vec2 desired_velocity = to_target * (speed / distance);
return desired_velocity - rigidbody.data.linear_velocity;
diff --git a/src/crepe/system/AnimatorSystem.cpp b/src/crepe/system/AnimatorSystem.cpp
index 02647f1..ad4eaa8 100644
--- a/src/crepe/system/AnimatorSystem.cpp
+++ b/src/crepe/system/AnimatorSystem.cpp
@@ -1,6 +1,6 @@
#include "../api/Animator.h"
#include "../manager/ComponentManager.h"
-#include "api/LoopTimer.h"
+#include "../manager/LoopTimerManager.h"
#include "AnimatorSystem.h"
@@ -8,10 +8,10 @@ using namespace crepe;
void AnimatorSystem::frame_update() {
ComponentManager & mgr = this->mediator.component_manager;
- LoopTimer & timer = this->mediator.timer;
+ LoopTimerManager & timer = this->mediator.loop_timer;
RefVector<Animator> animations = mgr.get_components_by_type<Animator>();
- double elapsed_time = timer.get_current_time();
+ unsigned long long elapsed_time = timer.get_elapsed_time().count();
for (Animator & a : animations) {
if (!a.active) continue;
diff --git a/src/crepe/system/EventSystem.cpp b/src/crepe/system/EventSystem.cpp
index bf2b6bc..5475798 100644
--- a/src/crepe/system/EventSystem.cpp
+++ b/src/crepe/system/EventSystem.cpp
@@ -1,3 +1,4 @@
+#include "../manager/EventManager.h"
#include "EventSystem.h"
using namespace crepe;
diff --git a/src/crepe/system/PhysicsSystem.cpp b/src/crepe/system/PhysicsSystem.cpp
index 714e531..62f8132 100644
--- a/src/crepe/system/PhysicsSystem.cpp
+++ b/src/crepe/system/PhysicsSystem.cpp
@@ -5,88 +5,94 @@
#include "../api/Transform.h"
#include "../api/Vector2.h"
#include "../manager/ComponentManager.h"
+#include "../manager/LoopTimerManager.h"
+#include "../manager/Mediator.h"
#include "PhysicsSystem.h"
using namespace crepe;
void PhysicsSystem::fixed_update() {
- ComponentManager & mgr = this->mediator.component_manager;
+ const Mediator & mediator = this->mediator;
+ ComponentManager & mgr = mediator.component_manager;
+ LoopTimerManager & loop_timer = mediator.loop_timer;
RefVector<Rigidbody> rigidbodies = mgr.get_components_by_type<Rigidbody>();
- RefVector<Transform> transforms = mgr.get_components_by_type<Transform>();
+ float dt = loop_timer.get_scaled_fixed_delta_time().count();
- double gravity = Config::get_instance().physics.gravity;
+ float gravity = Config::get_instance().physics.gravity;
for (Rigidbody & rigidbody : rigidbodies) {
if (!rigidbody.active) continue;
+ Transform & transform
+ = mgr.get_components_by_id<Transform>(rigidbody.game_object_id).front().get();
switch (rigidbody.data.body_type) {
case Rigidbody::BodyType::DYNAMIC:
- for (Transform & transform : transforms) {
- if (transform.game_object_id == rigidbody.game_object_id) {
+ if (transform.game_object_id == rigidbody.game_object_id) {
+ // Add gravity
- // Add gravity
- if (rigidbody.data.gravity_scale > 0) {
- rigidbody.data.linear_velocity.y
- += (rigidbody.data.mass * rigidbody.data.gravity_scale
- * gravity);
- }
- // Add damping
- if (rigidbody.data.angular_velocity_coefficient > 0) {
- rigidbody.data.angular_velocity
- *= rigidbody.data.angular_velocity_coefficient;
- }
- if (rigidbody.data.linear_velocity_coefficient.x > 0
- && rigidbody.data.linear_velocity_coefficient.y > 0) {
- rigidbody.data.linear_velocity
- *= rigidbody.data.linear_velocity_coefficient;
- }
+ if (rigidbody.data.mass <= 0) {
+ throw std::runtime_error("Mass must be greater than 0");
+ }
- // Max velocity check
- if (rigidbody.data.angular_velocity
- > rigidbody.data.max_angular_velocity) {
- rigidbody.data.angular_velocity
- = rigidbody.data.max_angular_velocity;
- } else if (rigidbody.data.angular_velocity
- < -rigidbody.data.max_angular_velocity) {
- rigidbody.data.angular_velocity
- = -rigidbody.data.max_angular_velocity;
- }
+ if (gravity <= 0) {
+ throw std::runtime_error("Config Gravity must be greater than 0");
+ }
- if (rigidbody.data.linear_velocity.x
- > rigidbody.data.max_linear_velocity.x) {
- rigidbody.data.linear_velocity.x
- = rigidbody.data.max_linear_velocity.x;
- } else if (rigidbody.data.linear_velocity.x
- < -rigidbody.data.max_linear_velocity.x) {
- rigidbody.data.linear_velocity.x
- = -rigidbody.data.max_linear_velocity.x;
- }
+ if (rigidbody.data.gravity_scale > 0 && !rigidbody.data.constraints.y) {
+ rigidbody.data.linear_velocity.y
+ += (rigidbody.data.mass * rigidbody.data.gravity_scale * gravity
+ * dt);
+ }
+ // Add coefficient rotation
+ if (rigidbody.data.angular_velocity_coefficient > 0) {
+ rigidbody.data.angular_velocity
+ *= std::pow(rigidbody.data.angular_velocity_coefficient, dt);
+ }
- if (rigidbody.data.linear_velocity.y
- > rigidbody.data.max_linear_velocity.y) {
- rigidbody.data.linear_velocity.y
- = rigidbody.data.max_linear_velocity.y;
- } else if (rigidbody.data.linear_velocity.y
- < -rigidbody.data.max_linear_velocity.y) {
- rigidbody.data.linear_velocity.y
- = -rigidbody.data.max_linear_velocity.y;
- }
+ // Add coefficient movement horizontal
+ if (rigidbody.data.linear_velocity_coefficient.x > 0
+ && !rigidbody.data.constraints.x) {
+ rigidbody.data.linear_velocity.x
+ *= std::pow(rigidbody.data.linear_velocity_coefficient.x, dt);
+ }
- // Move object
- if (!rigidbody.data.constraints.rotation) {
- transform.rotation += rigidbody.data.angular_velocity;
- transform.rotation = std::fmod(transform.rotation, 360.0);
- if (transform.rotation < 0) {
- transform.rotation += 360.0;
- }
- }
- if (!rigidbody.data.constraints.x) {
- transform.position.x += rigidbody.data.linear_velocity.x;
- }
- if (!rigidbody.data.constraints.y) {
- transform.position.y += rigidbody.data.linear_velocity.y;
+ // Add coefficient movement horizontal
+ if (rigidbody.data.linear_velocity_coefficient.y > 0
+ && !rigidbody.data.constraints.y) {
+ rigidbody.data.linear_velocity.y
+ *= std::pow(rigidbody.data.linear_velocity_coefficient.y, dt);
+ }
+
+ // Max velocity check
+ if (rigidbody.data.angular_velocity
+ > rigidbody.data.max_angular_velocity) {
+ rigidbody.data.angular_velocity = rigidbody.data.max_angular_velocity;
+ } else if (rigidbody.data.angular_velocity
+ < -rigidbody.data.max_angular_velocity) {
+ rigidbody.data.angular_velocity = -rigidbody.data.max_angular_velocity;
+ }
+
+ // Set max velocity to maximum length
+ if (rigidbody.data.linear_velocity.length()
+ > rigidbody.data.max_linear_velocity) {
+ rigidbody.data.linear_velocity.normalize();
+ rigidbody.data.linear_velocity *= rigidbody.data.max_linear_velocity;
+ }
+
+ // Move object
+ if (!rigidbody.data.constraints.rotation) {
+ transform.rotation += rigidbody.data.angular_velocity * dt;
+ transform.rotation = std::fmod(transform.rotation, 360.0);
+ if (transform.rotation < 0) {
+ transform.rotation += 360.0;
}
}
+ if (!rigidbody.data.constraints.x) {
+ transform.position.x += rigidbody.data.linear_velocity.x * dt;
+ }
+ if (!rigidbody.data.constraints.y) {
+ transform.position.y += rigidbody.data.linear_velocity.y * dt;
+ }
}
break;
case Rigidbody::BodyType::KINEMATIC:
diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp
index 7e9dfc6..607bbab 100644
--- a/src/crepe/system/RenderSystem.cpp
+++ b/src/crepe/system/RenderSystem.cpp
@@ -15,6 +15,7 @@
#include "../manager/ResourceManager.h"
#include "RenderSystem.h"
+#include "types.h"
using namespace crepe;
using namespace std;
@@ -29,7 +30,7 @@ void RenderSystem::present_screen() {
ctx.present_screen();
}
-SDLContext::CameraValues RenderSystem::update_camera() {
+void RenderSystem::update_camera() {
ComponentManager & mgr = this->mediator.component_manager;
SDLContext & ctx = this->mediator.sdl_context;
RefVector<Camera> cameras = mgr.get_components_by_type<Camera>();
@@ -40,9 +41,9 @@ SDLContext::CameraValues RenderSystem::update_camera() {
if (!cam.active) continue;
const Transform & transform
= mgr.get_components_by_id<Transform>(cam.game_object_id).front().get();
- SDLContext::CameraValues cam_val = ctx.set_camera(cam);
- cam_val.cam_pos = transform.position + cam.data.postion_offset;
- return cam_val;
+ vec2 new_camera_pos = transform.position + cam.data.postion_offset;
+ ctx.update_camera_view(cam, new_camera_pos);
+ return;
}
throw std::runtime_error("No active cameras in current scene");
}
@@ -69,8 +70,7 @@ void RenderSystem::frame_update() {
this->present_screen();
}
-bool RenderSystem::render_particle(const Sprite & sprite, const SDLContext::CameraValues & cam,
- const double & scale) {
+bool RenderSystem::render_particle(const Sprite & sprite, const double & scale) {
ComponentManager & mgr = this->mediator.component_manager;
SDLContext & ctx = this->mediator.sdl_context;
@@ -93,7 +93,6 @@ 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,
.scale = scale,
@@ -102,8 +101,7 @@ bool RenderSystem::render_particle(const Sprite & sprite, const SDLContext::Came
}
return rendering_particles;
}
-void RenderSystem::render_normal(const Sprite & sprite, const SDLContext::CameraValues & cam,
- const Transform & tm) {
+void RenderSystem::render_normal(const Sprite & sprite, 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);
@@ -111,7 +109,6 @@ void RenderSystem::render_normal(const Sprite & sprite, const SDLContext::Camera
ctx.draw(SDLContext::RenderContext{
.sprite = sprite,
.texture = res,
- .cam = cam,
.pos = tm.position,
.angle = tm.rotation,
.scale = tm.scale,
@@ -120,7 +117,7 @@ void RenderSystem::render_normal(const Sprite & sprite, const SDLContext::Camera
void RenderSystem::render() {
ComponentManager & mgr = this->mediator.component_manager;
- const SDLContext::CameraValues & cam = this->update_camera();
+ this->update_camera();
RefVector<Sprite> sprites = mgr.get_components_by_type<Sprite>();
RefVector<Sprite> sorted_sprites = this->sort(sprites);
@@ -130,10 +127,10 @@ void RenderSystem::render() {
const Transform & transform
= mgr.get_components_by_id<Transform>(sprite.game_object_id).front().get();
- bool rendered_particles = this->render_particle(sprite, cam, transform.scale);
+ bool rendered_particles = this->render_particle(sprite, transform.scale);
if (rendered_particles) continue;
- this->render_normal(sprite, cam, transform);
+ this->render_normal(sprite, transform);
}
}
diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h
index a01923d..1a61f99 100644
--- a/src/crepe/system/RenderSystem.h
+++ b/src/crepe/system/RenderSystem.h
@@ -2,8 +2,6 @@
#include <cmath>
-#include "facade/SDLContext.h"
-
#include "System.h"
#include "types.h"
@@ -14,7 +12,6 @@ class Sprite;
class Transform;
/**
- * \class RenderSystem
* \brief Manages rendering operations for all game objects.
*
* RenderSystem is responsible for rendering, clearing and presenting the screen, and
@@ -37,7 +34,7 @@ private:
void present_screen();
//! Updates the active camera used for rendering.
- SDLContext::CameraValues update_camera();
+ void update_camera();
//! Renders the whole screen
void render();
@@ -52,8 +49,7 @@ private:
* constructor is now protected i cannot make tmp inside
* \return true if particles have been rendered
*/
- bool render_particle(const Sprite & sprite, const SDLContext::CameraValues & cam,
- const double & scale);
+ bool render_particle(const Sprite & sprite, const double & scale);
/**
* \brief renders a sprite with a Transform component on the screen
@@ -61,8 +57,7 @@ private:
* \param sprite the sprite component that holds all the data
* \param tm the Transform component that holds the position,rotation and scale
*/
- void render_normal(const Sprite & sprite, const SDLContext::CameraValues & cam,
- const Transform & tm);
+ void render_normal(const Sprite & sprite, const Transform & tm);
/**
* \brief sort a vector sprite objects with