diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/crepe/api/LoopManager.cpp | 10 | ||||
-rw-r--r-- | src/crepe/api/LoopManager.h | 5 | ||||
-rw-r--r-- | src/crepe/api/LoopTimer.cpp | 17 | ||||
-rw-r--r-- | src/example/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/test/loopTimerTest.cpp | 45 |
5 files changed, 44 insertions, 35 deletions
diff --git a/src/crepe/api/LoopManager.cpp b/src/crepe/api/LoopManager.cpp index 4a6d2cd..e584ba7 100644 --- a/src/crepe/api/LoopManager.cpp +++ b/src/crepe/api/LoopManager.cpp @@ -1,3 +1,5 @@ +#include <iostream> + #include "../facade/SDLContext.h" #include "../system/AnimatorSystem.h" @@ -60,12 +62,12 @@ void LoopManager::loop() { void LoopManager::setup() { this->game_running = true; this->loop_timer->start(); - this->loop_timer->set_fps(60); + this->loop_timer->set_target_fps(60); } void LoopManager::render() { if (this->game_running) { - this->get_system<RenderSystem>().update(); + //this->get_system<RenderSystem>().update(); } } bool LoopManager::on_shutdown(const ShutDownEvent & e){ @@ -73,4 +75,6 @@ bool LoopManager::on_shutdown(const ShutDownEvent & e){ return false; } -void LoopManager::update() {} +void LoopManager::update() { + std::cout << this->loop_timer->get_fps() << std::endl; +} diff --git a/src/crepe/api/LoopManager.h b/src/crepe/api/LoopManager.h index ff1ff55..3bf54b9 100644 --- a/src/crepe/api/LoopManager.h +++ b/src/crepe/api/LoopManager.h @@ -5,9 +5,10 @@ #include "../ComponentManager.h" #include "../system/System.h" #include "api/SceneManager.h" +#include "api/Event.h" +#include "api/LoopTimer.h" namespace crepe { -class LoopTimer; /** * \brief Main game loop manager * @@ -91,7 +92,9 @@ private: SceneManager scene_manager{component_manager}; private: + //! loop timer instance std::unique_ptr<LoopTimer> loop_timer; + //! callback function for shutdown event bool on_shutdown(const ShutDownEvent & e); /** * \brief Collection of System instances diff --git a/src/crepe/api/LoopTimer.cpp b/src/crepe/api/LoopTimer.cpp index fe5544d..15011ca 100644 --- a/src/crepe/api/LoopTimer.cpp +++ b/src/crepe/api/LoopTimer.cpp @@ -1,4 +1,5 @@ #include <chrono> +#include <thread> #include "../facade/SDLContext.h" #include "../util/Log.h" @@ -56,14 +57,14 @@ void LoopTimer::enforce_frame_rate() { auto current_frame_time = std::chrono::steady_clock::now(); auto frame_duration = 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::get_instance().delay(delay_time.count()); - } - } + // Check if frame duration is less than the target frame time + if (frame_duration < this->frame_target_time) { + auto delay_time = std::chrono::duration_cast<std::chrono::microseconds>(this->frame_target_time - frame_duration); + + if (delay_time.count() > 0) { + std::this_thread::sleep_for(delay_time); + } + } } double LoopTimer::get_lag() const { diff --git a/src/example/CMakeLists.txt b/src/example/CMakeLists.txt index 85ec466..54100cf 100644 --- a/src/example/CMakeLists.txt +++ b/src/example/CMakeLists.txt @@ -19,4 +19,4 @@ endfunction() add_example(asset_manager) add_example(savemgr) add_example(rendering_particle) - +add_example(gameloop) diff --git a/src/test/loopTimerTest.cpp b/src/test/loopTimerTest.cpp index a3b1646..d2f7d9b 100644 --- a/src/test/loopTimerTest.cpp +++ b/src/test/loopTimerTest.cpp @@ -1,32 +1,33 @@ -#define private public -#define protected public -#include "api/LoopManager.h" -#include "api/LoopTimer.h" -#include <gmock/gmock.h> #include <gtest/gtest.h> +#include <chrono> +#include <thread> +#include "api/LoopTimer.h" -using namespace std; -using namespace std::chrono_literals; +using namespace std::chrono; using namespace crepe; class LoopTimerTest : public ::testing::Test { -public: -LoopTimer loop_timer = LoopTimer::get_instance(); protected: - void SetUp() override { - loop_timer.start(); - } + LoopTimer loop_timer; - void TearDown() override { - - } + void SetUp() override { + loop_timer.start(); // Reset loop timer before each test. + } }; -TEST_F(LoopTimerTest, TestDeltaTime) { - auto start_time = std::chrono::steady_clock::now(); - - loop_timer.update(); - double delta_time = loop_timer.get_delta_time(); +TEST_F(LoopTimerTest, EnforcesTargetFrameRate) { + // Set the target FPS to 60 (which gives a target time per frame of ~16.67 ms) + loop_timer.set_target_fps(60); + + // Simulate a short update (frame duration less than the target frame time) + auto start_time = steady_clock::now(); + loop_timer.enforce_frame_rate(); // Enforce the frame rate + + // Check that the loop timer's current time is greater than or equal to the target frame time + auto elapsed_time = steady_clock::now() - start_time; + auto elapsed_ms = duration_cast<milliseconds>(elapsed_time).count(); - auto elapsed_time = std::chrono::steady_clock::now() - start_time; - EXPECT_LE(delta_time, std::chrono::duration<double>(elapsed_time).count()); + // Assert that the elapsed time is close to the target frame time + // For 60 FPS, the target frame time is around 16.67ms + ASSERT_GE(elapsed_ms, 16); // Make sure it's at least 16 ms (could be slightly more) + ASSERT_LE(elapsed_ms, 18); // Ensure it's not too much longer } |