diff options
author | WBoerenkamps <wrj.boerenkamps@student.avans.nl> | 2024-12-05 00:03:10 +0100 |
---|---|---|
committer | WBoerenkamps <wrj.boerenkamps@student.avans.nl> | 2024-12-05 00:03:10 +0100 |
commit | 8aa43f634c1a89f05681ffc5f3cd0a3477e50e71 (patch) | |
tree | a1321d75328da0952ba7bf294a8036f50debb60f /src/test/LoopManagerTest.cpp | |
parent | d9e46281c1e24a5f23d779d314e5df87fa3317a3 (diff) | |
parent | cfb67ffddb9f4bb0357c2b9df4239bfee7364c5a (diff) |
added loopTimer to mediator and fixed update loop
Diffstat (limited to 'src/test/LoopManagerTest.cpp')
-rw-r--r-- | src/test/LoopManagerTest.cpp | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/src/test/LoopManagerTest.cpp b/src/test/LoopManagerTest.cpp new file mode 100644 index 0000000..af6cb1c --- /dev/null +++ b/src/test/LoopManagerTest.cpp @@ -0,0 +1,44 @@ +#include <gtest/gtest.h> +#include <chrono> +#include <thread> +#define private public +#define protected public +#include "api/LoopTimer.h" +#include "api/LoopManager.h" + +using namespace std::chrono; +using namespace crepe; + +class LoopManagerTest : public ::testing::Test { +protected: + LoopManager loop_manager; + + void SetUp() override { + // Setting up loop manager and start the loop + loop_manager.loop_timer->set_target_fps(60); + } +}; + +//Test to check if exactly 5 fixed updates are done every second (50Hz) +TEST_F(LoopManagerTest, FixedUpdate) { + loop_manager.loop_timer->fixed_delta_time = std::chrono::milliseconds(20); + loop_manager.loop_timer->set_target_fps(50); + int fixed_update_count = 0; + loop_manager.loop_timer->start(); + // We want to simulate the game loop for about 1 second + auto start_time = steady_clock::now(); + + // Simulate the game loop for 1 second + while (duration_cast<milliseconds>(steady_clock::now() - start_time) < std::chrono::milliseconds(1000)) { + loop_manager.loop_timer->update(); + // Simulate processing fixed updates while there's lag to advance + while (loop_manager.loop_timer->get_lag() >= loop_manager.loop_timer->get_fixed_delta_time()) { + fixed_update_count++; + loop_manager.loop_timer->advance_fixed_update(); + } + + loop_manager.loop_timer->enforce_frame_rate(); + } + // gameloop is 99 because it first takes 20 millisecond to build the lag to execute the fixed loop + ASSERT_EQ(fixed_update_count, 50); +} |