aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/crepe/api/LoopManager.cpp13
-rw-r--r--src/crepe/api/LoopManager.h16
-rw-r--r--src/example/CMakeLists.txt1
-rw-r--r--src/test/CMakeLists.txt1
-rw-r--r--src/test/loopManagerTest.cpp50
-rw-r--r--src/test/loopTimerTest.cpp36
6 files changed, 94 insertions, 23 deletions
diff --git a/src/crepe/api/LoopManager.cpp b/src/crepe/api/LoopManager.cpp
index e584ba7..9bedbcc 100644
--- a/src/crepe/api/LoopManager.cpp
+++ b/src/crepe/api/LoopManager.cpp
@@ -1,17 +1,17 @@
#include <iostream>
#include "../facade/SDLContext.h"
-
#include "../system/AnimatorSystem.h"
#include "../system/CollisionSystem.h"
#include "../system/ParticleSystem.h"
#include "../system/PhysicsSystem.h"
#include "../system/RenderSystem.h"
#include "../system/ScriptSystem.h"
+
#include "../api/EventManager.h"
#include "LoopManager.h"
#include "LoopTimer.h"
-#include <iostream>
+
using namespace crepe;
using namespace std;
@@ -37,7 +37,6 @@ void LoopManager::start() {
this->setup();
this->loop();
}
-void LoopManager::set_running(bool running) { this->game_running = running; }
void LoopManager::fixed_update() {}
@@ -46,7 +45,7 @@ void LoopManager::loop() {
while (game_running) {
this->loop_timer->update();
-
+
while (this->loop_timer->get_lag() >= this->loop_timer->get_fixed_delta_time()) {
this->process_input();
this->fixed_update();
@@ -67,7 +66,7 @@ void LoopManager::setup() {
void LoopManager::render() {
if (this->game_running) {
- //this->get_system<RenderSystem>().update();
+ this->get_system<RenderSystem>().update();
}
}
bool LoopManager::on_shutdown(const ShutDownEvent & e){
@@ -75,6 +74,4 @@ bool LoopManager::on_shutdown(const ShutDownEvent & e){
return false;
}
-void LoopManager::update() {
- std::cout << this->loop_timer->get_fps() << std::endl;
-}
+void LoopManager::update() {}
diff --git a/src/crepe/api/LoopManager.h b/src/crepe/api/LoopManager.h
index 3bf54b9..eb2b525 100644
--- a/src/crepe/api/LoopManager.h
+++ b/src/crepe/api/LoopManager.h
@@ -16,6 +16,12 @@ namespace crepe {
*/
class LoopManager {
public:
+ /**
+ * \brief Start the gameloop
+ *
+ * This is the start of the engine where the setup is called and then the loop keeps running until the game stops running.
+ * Developers need to call this function to run the game.
+ */
void start();
LoopManager();
@@ -68,14 +74,6 @@ private:
* This function updates physics and game logic based on LoopTimer's fixed_delta_time.
*/
void fixed_update();
-
- /**
- * \brief Set game running variable
- *
- * \param running running (false = game shutdown, true = game running)
- */
- void set_running(bool running);
-
/**
* \brief Function for executing render-related systems.
*
@@ -100,7 +98,7 @@ private:
* \brief Collection of System instances
*
* This map holds System instances indexed by the system's class typeid. It is filled in the
- * constructor of \c LoopManager using LoopManager::load_system.
+ * constructor of LoopManager using LoopManager::load_system.
*/
std::unordered_map<std::type_index, std::unique_ptr<System>> systems;
/**
diff --git a/src/example/CMakeLists.txt b/src/example/CMakeLists.txt
index 54100cf..6f92d45 100644
--- a/src/example/CMakeLists.txt
+++ b/src/example/CMakeLists.txt
@@ -19,4 +19,3 @@ endfunction()
add_example(asset_manager)
add_example(savemgr)
add_example(rendering_particle)
-add_example(gameloop)
diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt
index d310f6a..b126add 100644
--- a/src/test/CMakeLists.txt
+++ b/src/test/CMakeLists.txt
@@ -12,4 +12,5 @@ target_sources(test_main PUBLIC
ValueBrokerTest.cpp
DBTest.cpp
Vector2Test.cpp
+ loopTimerTest.cpp
)
diff --git a/src/test/loopManagerTest.cpp b/src/test/loopManagerTest.cpp
new file mode 100644
index 0000000..6e66ce7
--- /dev/null
+++ b/src/test/loopManagerTest.cpp
@@ -0,0 +1,50 @@
+#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 LoopTimerTest : public ::testing::Test {
+protected:
+ LoopManager loop_manager;
+
+ void SetUp() override {
+ // Setting up loop manager and start the loop
+ loop_manager.setup();
+ loop_manager.loop_timer->set_target_fps(60);
+ }
+};
+
+// Test to check if exactly 5 fixed updates are done every second (50Hz)
+TEST_F(LoopTimerTest, FixedUpdateCalledAt50Hz) {
+ // Set target fixed delta time to 20ms (50Hz fixed updates)
+ loop_manager.loop_timer->set_fixed_delta_time(milliseconds(20));
+
+ int fixed_update_count = 0;
+
+ // 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<seconds>(steady_clock::now() - start_time).count() < 1) {
+ 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()) {
+ loop_manager.fixed_update(); // Process fixed update
+ fixed_update_count++; // Count the number of fixed updates
+ loop_manager.loop_timer->advance_fixed_update();
+ }
+
+ // We do not need to call render or update for this test
+ loop_manager.loop_timer->enforce_frame_rate(); // Enforce the frame rate (this would normally go to the display)
+ }
+
+ // We expect 5 fixed updates to occur in 1 second at 50Hz
+ ASSERT_EQ(fixed_update_count, 5);
+}
diff --git a/src/test/loopTimerTest.cpp b/src/test/loopTimerTest.cpp
index d2f7d9b..9bbbff3 100644
--- a/src/test/loopTimerTest.cpp
+++ b/src/test/loopTimerTest.cpp
@@ -1,6 +1,8 @@
#include <gtest/gtest.h>
#include <chrono>
#include <thread>
+#define private public
+#define protected public
#include "api/LoopTimer.h"
using namespace std::chrono;
@@ -11,23 +13,47 @@ protected:
LoopTimer loop_timer;
void SetUp() override {
- loop_timer.start(); // Reset loop timer before each test.
+ loop_timer.start();
}
};
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
+ loop_timer.enforce_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();
- // 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
}
+TEST_F(LoopTimerTest, SetTargetFps) {
+ // Set the target FPS to 120
+ loop_timer.set_target_fps(120);
+
+ // Calculate the expected frame time (~8.33ms per frame)
+ auto expected_frame_time = std::chrono::duration<double>(1.0 / 120.0);
+
+ ASSERT_NEAR(loop_timer.frame_target_time.count(), expected_frame_time.count(), 0.001);
+}
+TEST_F(LoopTimerTest, DeltaTimeCalculation) {
+ // Set the target FPS to 60 (16.67 ms per frame)
+ loop_timer.set_target_fps(60);
+
+ auto start_time = steady_clock::now();
+ loop_timer.update();
+ auto end_time = steady_clock::now();
+
+ // Check the delta time
+ double delta_time = loop_timer.get_delta_time();
+
+ auto elapsed_time = duration_cast<milliseconds>(end_time - start_time).count();
+
+ // Assert that delta_time is close to the elapsed time
+ ASSERT_GE(delta_time, elapsed_time / 1000.0);
+ ASSERT_LE(delta_time, (elapsed_time + 2) / 1000.0);
+}
+