diff options
| -rw-r--r-- | src/crepe/api/LoopManager.cpp | 5 | ||||
| -rw-r--r-- | src/crepe/api/LoopTimer.cpp | 35 | ||||
| -rw-r--r-- | src/crepe/api/LoopTimer.h | 12 | ||||
| -rw-r--r-- | src/example/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | src/example/gameloop.cpp | 7 | ||||
| -rw-r--r-- | src/test/loopTimerTest.cpp | 32 | 
6 files changed, 58 insertions, 34 deletions
| diff --git a/src/crepe/api/LoopManager.cpp b/src/crepe/api/LoopManager.cpp index 4a6d2cd..14e68c2 100644 --- a/src/crepe/api/LoopManager.cpp +++ b/src/crepe/api/LoopManager.cpp @@ -59,8 +59,13 @@ void LoopManager::loop() {  void LoopManager::setup() {  	this->game_running = true; +<<<<<<< HEAD +	LoopTimer::get_instance().start(); +	LoopTimer::get_instance().set_target_fps(200); +=======  	this->loop_timer->start();  	this->loop_timer->set_fps(60); +>>>>>>> wouter/gameloop-improvements  }  void LoopManager::render() { diff --git a/src/crepe/api/LoopTimer.cpp b/src/crepe/api/LoopTimer.cpp index d0a19d7..fe5544d 100644 --- a/src/crepe/api/LoopTimer.cpp +++ b/src/crepe/api/LoopTimer.cpp @@ -26,7 +26,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; @@ -40,13 +41,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; } @@ -55,22 +56,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) { -        auto remaining_time = this->frame_target_time - frame_duration; - -        // Sleep for most of the remaining time using SDLContext -        if (remaining_time > std::chrono::microseconds(2000)) { // 2ms threshold -            SDLContext::get_instance().delay( -                std::chrono::duration_cast<std::chrono::milliseconds>(remaining_time).count()); -        } - -        // Busy-wait for the last tiny remaining duration -        while (std::chrono::steady_clock::now() - current_frame_time < remaining_time) { -            // Busy wait -        } -    } - -    this->last_frame_time = std::chrono::steady_clock::now(); // Update 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()); +		} +	}  }  double LoopTimer::get_lag() const { diff --git a/src/crepe/api/LoopTimer.h b/src/crepe/api/LoopTimer.h index b20203d..e348628 100644 --- a/src/crepe/api/LoopTimer.h +++ b/src/crepe/api/LoopTimer.h @@ -29,7 +29,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). @@ -62,7 +62,6 @@ private:  	 * Initializes the timer to begin tracking frame times.  	 */  	void start(); -  	/**  	 * \brief Enforce the frame rate limit.  	 * @@ -110,8 +109,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 @@ -119,7 +120,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 @@ -128,6 +129,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()); +} |