Kumo
A hand-rolled game engine.
git clone https://git.sentomk.me/Kumo.git
loop_test.cpp (4011B) · raw
1 #include "runtime/loop.h" 2 3 #include <vector> 4 5 #include "check.h" 6 #include "core/clock.h" 7 #include "core/fixed.h" 8 #include "core/types.h" 9 #include "render/frame_buffer.h" 10 #include "runtime/game.h" 11 12 using kumo::Fixed; 13 using kumo::FrameBuffer; 14 using kumo::FrameReport; 15 using kumo::Game; 16 using kumo::Loop; 17 using kumo::LoopConfig; 18 using kumo::ManualClock; 19 using kumo::milliseconds; 20 using kumo::Nanoseconds; 21 using kumo::seconds; 22 using kumo::Tick; 23 24 namespace { 25 26 class RecordingGame final : public Game { 27 public: 28 void fixed_update(const Tick& tick) override { ticks.push_back(tick); } 29 30 void render(FrameBuffer& /*frame*/, Fixed alpha) const override { 31 ++render_count; 32 last_alpha = alpha; 33 } 34 35 std::vector<Tick> ticks; 36 mutable int render_count = 0; 37 mutable Fixed last_alpha; 38 }; 39 40 } // namespace 41 42 TEST(step_frame_runs_zero_one_and_many_ticks_with_sequential_indices) { 43 ManualClock clock(0); 44 Loop loop(clock, {milliseconds(10), milliseconds(100)}); 45 RecordingGame game; 46 FrameBuffer frame(8, 8); 47 48 // Shorter than one step: 0 updates, still renders once. 49 clock.advance(milliseconds(4)); 50 const FrameReport r0 = loop.step_frame(game, frame); 51 CHECK_EQ(r0.ticks_run, 0); 52 CHECK_EQ(r0.total_ticks, 0u); 53 CHECK_EQ(game.ticks.size(), 0u); 54 CHECK_EQ(game.render_count, 1); 55 56 // Crosses one step boundary (4 + 6 = 10 ms): tick 0. 57 clock.advance(milliseconds(6)); 58 const FrameReport r1 = loop.step_frame(game, frame); 59 CHECK_EQ(r1.ticks_run, 1); 60 CHECK_EQ(r1.total_ticks, 1u); 61 CHECK_EQ(game.ticks.size(), 1u); 62 CHECK_EQ(game.ticks[0].index, 0u); 63 CHECK_EQ(game.ticks[0].dt, milliseconds(10)); 64 65 // Long frame (25 ms): ticks 1 and 2, with 5 ms left over. 66 clock.advance(milliseconds(25)); 67 const FrameReport r2 = loop.step_frame(game, frame); 68 CHECK_EQ(r2.ticks_run, 2); 69 CHECK_EQ(r2.total_ticks, 3u); 70 CHECK_EQ(game.ticks.size(), 3u); 71 CHECK_EQ(game.ticks[1].index, 1u); 72 CHECK_EQ(game.ticks[2].index, 2u); 73 CHECK_EQ(loop.simulated_time(), milliseconds(30)); 74 } 75 76 TEST(step_frame_passes_fixed_alpha_to_render) { 77 ManualClock clock(0); 78 Loop loop(clock, {milliseconds(10), milliseconds(100)}); 79 RecordingGame game; 80 FrameBuffer frame(8, 8); 81 82 clock.advance(milliseconds(15)); 83 const FrameReport report = loop.step_frame(game, frame); 84 CHECK_EQ(report.alpha, Fixed::ratio(1, 2)); 85 CHECK_EQ(game.last_alpha, Fixed::ratio(1, 2)); 86 87 clock.advance(milliseconds(5)); 88 const FrameReport report2 = loop.step_frame(game, frame); 89 CHECK_EQ(report2.alpha, Fixed::from_int(0)); 90 CHECK_EQ(game.last_alpha, Fixed::from_int(0)); 91 } 92 93 TEST(step_frame_clamps_long_frames_to_max_frame) { 94 ManualClock clock(0); 95 Loop loop(clock, {milliseconds(10), milliseconds(50)}); 96 RecordingGame game; 97 FrameBuffer frame(8, 8); 98 99 clock.advance(seconds(3)); 100 const FrameReport report = loop.step_frame(game, frame); 101 CHECK_EQ(report.ticks_run, 5); 102 CHECK_EQ(game.ticks.size(), 5u); 103 for (size_t i = 0; i < game.ticks.size(); ++i) { 104 CHECK_EQ(game.ticks[i].index, static_cast<uint64_t>(i)); 105 } 106 } 107 108 TEST(step_ticks_advances_without_using_the_clock_or_rendering) { 109 ManualClock clock(0); 110 Loop loop(clock, {milliseconds(10), milliseconds(100)}); 111 RecordingGame game; 112 113 loop.step_ticks(game, 4); 114 CHECK_EQ(game.ticks.size(), 4u); 115 CHECK_EQ(game.render_count, 0); 116 CHECK_EQ(loop.total_ticks(), 4u); 117 CHECK_EQ(loop.simulated_time(), milliseconds(40)); 118 CHECK_EQ(game.ticks[0].index, 0u); 119 CHECK_EQ(game.ticks[3].index, 3u); 120 } 121 122 TEST(reset_timer_discards_elapsed_time) { 123 ManualClock clock(0); 124 Loop loop(clock, {milliseconds(10), milliseconds(100)}); 125 RecordingGame game; 126 FrameBuffer frame(8, 8); 127 128 clock.advance(seconds(2)); // e.g. slow loading pause 129 loop.reset_timer(); 130 clock.advance(milliseconds(10)); 131 132 const FrameReport report = loop.step_frame(game, frame); 133 CHECK_EQ(report.ticks_run, 1); 134 CHECK_EQ(game.ticks.size(), 1u); 135 } 136 137 TEST(tick_dt_seconds_matches_fixed_ratio) { 138 const Tick tick = {0, milliseconds(250)}; 139 CHECK_EQ(tick.dt_seconds(), Fixed::ratio(1, 4)); 140 }
