aboutsummaryrefslogtreecommitdiff
path: root/src/test/LoopManagerTest.cpp
blob: af6cb1c0eab67aed17be6bda98b7a1f6606a5dd4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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);
}