Gondwana Game Engine Development (master) - 2.7.0
Gondwana is a cross-platform 2.5D game and rendering engine written in C#/.NET 8. It provides fine-grained control over rendering, timing, and scene composition, with built-in support for parallax, z-ordering, pixel overhang, collision detection, and particle effects. Gondwana targets desktop and web platforms using SkiaSharp for graphics and pluggable audio backends for desktop and browser targets.
Loading...
Searching...
No Matches
FixedStepAccumulator.cs
1namespace Gondwana.Timers;
2
6internal sealed class FixedStepAccumulator
7{
8 private long _lastDriverTick;
9 private long _accumulatedTicks;
10
12 internal long SimulationTick { get; private set; }
13
16 internal void Reset(long tick)
17 {
18 _lastDriverTick = tick;
19 _accumulatedTicks = 0;
20 SimulationTick = tick;
21 }
22
32 internal FixedStepBatch Advance(long driverTick, int updatesPerSecond, int maxSteps)
33 {
34 if (updatesPerSecond <= 0)
35 throw new ArgumentOutOfRangeException(nameof(updatesPerSecond));
36
37 if (maxSteps <= 0)
38 throw new ArgumentOutOfRangeException(nameof(maxSteps));
39
40 long stepTicks = Math.Max(1, HighResTimer.TicksPerSecond / updatesPerSecond);
41
42 long elapsedTicks = driverTick > _lastDriverTick
43 ? driverTick - _lastDriverTick
44 : 0;
45
46 if (driverTick > _lastDriverTick)
47 _lastDriverTick = driverTick;
48
49 long maxAccumulatedTicks = stepTicks > long.MaxValue / maxSteps
50 ? long.MaxValue
51 : stepTicks * maxSteps;
52
53 _accumulatedTicks = elapsedTicks >= maxAccumulatedTicks - _accumulatedTicks
54 ? maxAccumulatedTicks
55 : _accumulatedTicks + elapsedTicks;
56
57 int stepCount = (int)Math.Min(maxSteps, _accumulatedTicks / stepTicks);
58 _accumulatedTicks -= stepCount * stepTicks;
59
60 long firstStepTick = SimulationTick + stepTicks;
61 SimulationTick += stepCount * stepTicks;
62
63 return new FixedStepBatch(firstStepTick, stepTicks, stepCount);
64 }
65}
66
71internal readonly record struct FixedStepBatch(long FirstStepTick, long StepTicks, int StepCount)
72{
76 internal long GetStepTick(int index)
77 {
78 if ((uint)index >= (uint)StepCount)
79 throw new ArgumentOutOfRangeException(nameof(index));
80
81 return FirstStepTick + (index * StepTicks);
82 }
83}