aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/crepe/api/LoopManager.cpp2
-rw-r--r--src/crepe/api/LoopTimer.cpp13
-rw-r--r--src/crepe/api/LoopTimer.h12
-rw-r--r--src/example/CMakeLists.txt1
-rw-r--r--src/example/gameloop.cpp7
-rw-r--r--src/test/loopTimerTest.cpp32
6 files changed, 46 insertions, 21 deletions
diff --git a/src/crepe/api/LoopManager.cpp b/src/crepe/api/LoopManager.cpp
index 7edf4d1..df09f7e 100644
--- a/src/crepe/api/LoopManager.cpp
+++ b/src/crepe/api/LoopManager.cpp
@@ -57,7 +57,7 @@ void LoopManager::loop() {
void LoopManager::setup() {
this->game_running = true;
LoopTimer::get_instance().start();
- LoopTimer::get_instance().set_fps(200);
+ LoopTimer::get_instance().set_target_fps(200);
}
void LoopManager::render() {
diff --git a/src/crepe/api/LoopTimer.cpp b/src/crepe/api/LoopTimer.cpp
index 15a0e3a..f94e24b 100644
--- a/src/crepe/api/LoopTimer.cpp
+++ b/src/crepe/api/LoopTimer.cpp
@@ -30,7 +30,8 @@ void LoopTimer::update() {
if (this->delta_time > this->maximum_delta_time) {
this->delta_time = this->maximum_delta_time;
}
-
+ this->actual_fps = 1.0 / this->delta_time.count();
+
this->delta_time *= this->game_scale;
this->elapsed_time += this->delta_time;
this->last_frame_time = current_frame_time;
@@ -44,13 +45,13 @@ void LoopTimer::advance_fixed_update() { this->elapsed_fixed_time += this->fixed
double LoopTimer::get_fixed_delta_time() const { return this->fixed_delta_time.count(); }
-void LoopTimer::set_fps(int fps) {
- this->fps = fps;
+void LoopTimer::set_target_fps(int fps) {
+ this->target_fps = fps;
// target time per frame in seconds
- this->frame_target_time = std::chrono::duration<double>(1.0) / fps;
+ this->frame_target_time = std::chrono::duration<double>(1.0) / target_fps;
}
-int LoopTimer::get_fps() const { return this->fps; }
+int LoopTimer::get_fps() const { return this->actual_fps; }
void LoopTimer::set_game_scale(double value) { this->game_scale = value; }
@@ -70,8 +71,6 @@ void LoopTimer::enforce_frame_rate() {
SDLContext::get_instance().delay(delay_time.count());
}
}
-
- this->last_frame_time = current_frame_time;
}
double LoopTimer::get_lag() const {
diff --git a/src/crepe/api/LoopTimer.h b/src/crepe/api/LoopTimer.h
index 9393439..0a48e20 100644
--- a/src/crepe/api/LoopTimer.h
+++ b/src/crepe/api/LoopTimer.h
@@ -35,7 +35,7 @@ public:
*
* \param fps The desired frames rendered per second.
*/
- void set_fps(int fps);
+ void set_target_fps(int fps);
/**
* \brief Get the current frames per second (FPS).
@@ -68,7 +68,6 @@ private:
* Initializes the timer to begin tracking frame times.
*/
void start();
-
/**
* \brief Enforce the frame rate limit.
*
@@ -121,8 +120,10 @@ private:
void advance_fixed_update();
private:
- //! Current frames per second
- int fps = 50;
+ //! Target frames per second
+ int target_fps = 50;
+ //! Actual frames per second
+ int actual_fps = 0;
//! Current game scale
double game_scale = 1;
//! Maximum delta time in seconds to avoid large jumps
@@ -130,7 +131,7 @@ private:
//! 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;
+ std::chrono::duration<double> frame_target_time = std::chrono::duration<double>(1.0) / target_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
@@ -139,6 +140,7 @@ private:
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/example/CMakeLists.txt b/src/example/CMakeLists.txt
index 560e2bc..85ec466 100644
--- a/src/example/CMakeLists.txt
+++ b/src/example/CMakeLists.txt
@@ -19,5 +19,4 @@ endfunction()
add_example(asset_manager)
add_example(savemgr)
add_example(rendering_particle)
-add_example(gameloop)
diff --git a/src/example/gameloop.cpp b/src/example/gameloop.cpp
deleted file mode 100644
index a676f20..0000000
--- a/src/example/gameloop.cpp
+++ /dev/null
@@ -1,7 +0,0 @@
-#include "crepe/api/LoopManager.h"
-using namespace crepe;
-int main() {
- LoopManager gameloop;
- gameloop.start();
- return 1;
-}
diff --git a/src/test/loopTimerTest.cpp b/src/test/loopTimerTest.cpp
new file mode 100644
index 0000000..a3b1646
--- /dev/null
+++ b/src/test/loopTimerTest.cpp
@@ -0,0 +1,32 @@
+#define private public
+#define protected public
+#include "api/LoopManager.h"
+#include "api/LoopTimer.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+
+using namespace std;
+using namespace std::chrono_literals;
+using namespace crepe;
+
+class LoopTimerTest : public ::testing::Test {
+public:
+LoopTimer loop_timer = LoopTimer::get_instance();
+protected:
+ void SetUp() override {
+ loop_timer.start();
+ }
+
+ void TearDown() override {
+
+ }
+};
+TEST_F(LoopTimerTest, TestDeltaTime) {
+ auto start_time = std::chrono::steady_clock::now();
+
+ loop_timer.update();
+ double delta_time = loop_timer.get_delta_time();
+
+ auto elapsed_time = std::chrono::steady_clock::now() - start_time;
+ EXPECT_LE(delta_time, std::chrono::duration<double>(elapsed_time).count());
+}