Gondwana Game Engine Development (master) - 2.6.1
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 if (maxSteps <= 0)
37 throw new ArgumentOutOfRangeException(nameof(maxSteps));
38
39 long stepTicks = Math.Max(1, HighResTimer.TicksPerSecond / updatesPerSecond);
40 long elapsedTicks = driverTick > _lastDriverTick
41 ? driverTick - _lastDriverTick
42 : 0;
43
44 if (driverTick > _lastDriverTick)
45 _lastDriverTick = driverTick;
46
47 long maxAccumulatedTicks = stepTicks > long.MaxValue / maxSteps
48 ? long.MaxValue
49 : stepTicks * maxSteps;
50
51 _accumulatedTicks = elapsedTicks >= maxAccumulatedTicks - _accumulatedTicks
52 ? maxAccumulatedTicks
53 : _accumulatedTicks + elapsedTicks;
54
55 int stepCount = (int)Math.Min(maxSteps, _accumulatedTicks / stepTicks);
56 _accumulatedTicks -= stepCount * stepTicks;
57
58 long firstStepTick = SimulationTick + stepTicks;
59 SimulationTick += stepCount * stepTicks;
60
61 return new FixedStepBatch(firstStepTick, stepTicks, stepCount);
62 }
63}
64
69internal readonly record struct FixedStepBatch(long FirstStepTick, long StepTicks, int StepCount)
70{
74 internal long GetStepTick(int index)
75 {
76 if ((uint)index >= (uint)StepCount)
77 throw new ArgumentOutOfRangeException(nameof(index));
78
79 return FirstStepTick + (index * StepTicks);
80 }
81}