| | | 1 | | //======================================================================= |
| | | 2 | | // TrailblazerClock.cs |
| | | 3 | | //======================================================================= |
| | | 4 | | // MIT License, Copyright (c) 2024-present David Oravsky (mrdav30) |
| | | 5 | | // See LICENSE file in the project root for full license information. |
| | | 6 | | //======================================================================= |
| | | 7 | | |
| | | 8 | | using System; |
| | | 9 | | using System.Runtime.CompilerServices; |
| | | 10 | | using FixedMathSharp; |
| | | 11 | | |
| | | 12 | | namespace Trailblazer; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Stores deterministic fixed-step timing state for one Trailblazer runtime owner. |
| | | 16 | | /// </summary> |
| | | 17 | | internal sealed class TrailblazerClock |
| | | 18 | | { |
| | | 19 | | internal const int DefaultFrameRate = 32; |
| | | 20 | | |
| | 989 | 21 | | private int _frameRate = DefaultFrameRate; |
| | | 22 | | |
| | 989 | 23 | | private Fixed64 _deltaTime = Fixed64.One / (Fixed64)DefaultFrameRate; |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Gets the fixed simulation frame rate. |
| | | 27 | | /// </summary> |
| | 100 | 28 | | public int FrameRate => _frameRate; |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Gets the fixed time step for each simulation frame. |
| | | 32 | | /// </summary> |
| | 6411 | 33 | | public Fixed64 DeltaTime => _deltaTime; |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Gets the reciprocal of the current fixed time step. |
| | | 37 | | /// </summary> |
| | 4670 | 38 | | public Fixed64 InvDeltaTime => Fixed64.One / _deltaTime; |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Gets the number of simulated frames. |
| | | 42 | | /// </summary> |
| | | 43 | | public int FrameCount { get; private set; } |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Gets the total simulated time in seconds. |
| | | 47 | | /// </summary> |
| | | 48 | | public Fixed64 TotalTime { get; private set; } |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Gets the accumulated visualization time since the last late-simulate reset. |
| | | 52 | | /// </summary> |
| | | 53 | | public Fixed64 AccumulatedTime { get; private set; } |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// Gets whether the next visualization step should reset accumulated time. |
| | | 57 | | /// </summary> |
| | | 58 | | public bool ResetAccumulation { get; private set; } |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// Gets the accumulated visualization time expressed in simulation frames. |
| | | 62 | | /// </summary> |
| | | 63 | | public Fixed64 ExpectedAccumulation { get; private set; } |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// Advances the fixed simulation frame. |
| | | 67 | | /// </summary> |
| | | 68 | | public void Simulate() |
| | | 69 | | { |
| | 12185 | 70 | | FrameCount++; |
| | 12185 | 71 | | TotalTime += _deltaTime; |
| | 12185 | 72 | | } |
| | | 73 | | |
| | | 74 | | /// <summary> |
| | | 75 | | /// Marks visualization accumulation for reset after a fixed simulation frame completes. |
| | | 76 | | /// </summary> |
| | | 77 | | public void LateSimulate() |
| | | 78 | | { |
| | 2 | 79 | | ResetAccumulation = true; |
| | 2 | 80 | | } |
| | | 81 | | |
| | | 82 | | /// <summary> |
| | | 83 | | /// Advances deterministic visualization accumulation by one fixed step. |
| | | 84 | | /// </summary> |
| | | 85 | | public void Visualize() |
| | | 86 | | { |
| | 5 | 87 | | if (ResetAccumulation) |
| | | 88 | | { |
| | 2 | 89 | | AccumulatedTime = Fixed64.Zero; |
| | 2 | 90 | | ResetAccumulation = false; |
| | | 91 | | } |
| | | 92 | | |
| | 5 | 93 | | AccumulatedTime += _deltaTime; |
| | 5 | 94 | | ExpectedAccumulation = AccumulatedTime / _deltaTime; |
| | 5 | 95 | | } |
| | | 96 | | |
| | | 97 | | /// <summary> |
| | | 98 | | /// Resets elapsed timing state while preserving the configured frame rate. |
| | | 99 | | /// </summary> |
| | | 100 | | public void Reset() |
| | | 101 | | { |
| | 22 | 102 | | FrameCount = 0; |
| | 22 | 103 | | TotalTime = Fixed64.Zero; |
| | 22 | 104 | | AccumulatedTime = Fixed64.Zero; |
| | 22 | 105 | | ExpectedAccumulation = Fixed64.Zero; |
| | 22 | 106 | | ResetAccumulation = false; |
| | 22 | 107 | | } |
| | | 108 | | |
| | | 109 | | /// <summary> |
| | | 110 | | /// Updates the fixed simulation frame rate. |
| | | 111 | | /// </summary> |
| | | 112 | | /// <param name="frameRate">The new frame rate. Must be greater than zero.</param> |
| | | 113 | | public void SetFrameRate(int frameRate) |
| | | 114 | | { |
| | 11 | 115 | | SwiftThrowHelper.ThrowIfArgumentOutOfRange( |
| | 11 | 116 | | frameRate <= 0, |
| | 11 | 117 | | actualValue: frameRate, |
| | 11 | 118 | | paramName: nameof(frameRate), |
| | 11 | 119 | | message: "Frame rate must be greater than zero."); |
| | | 120 | | |
| | 9 | 121 | | _frameRate = frameRate; |
| | 9 | 122 | | _deltaTime = Fixed64.One / (Fixed64)_frameRate; |
| | 9 | 123 | | } |
| | | 124 | | |
| | | 125 | | /// <summary> |
| | | 126 | | /// Calculates the frame index containing the specified fixed-point timestamp. |
| | | 127 | | /// </summary> |
| | | 128 | | /// <param name="timestamp">The timestamp to resolve.</param> |
| | | 129 | | /// <returns>The zero-based frame index for the timestamp.</returns> |
| | | 130 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 131 | | public int GetFrameFromTime(Fixed64 timestamp) |
| | | 132 | | { |
| | 1 | 133 | | return (timestamp * InvDeltaTime).FloorToInt(); |
| | | 134 | | } |
| | | 135 | | } |