Skip to content

Commit

Permalink
patch: frame utils
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasteles committed Jun 1, 2024
1 parent 965135b commit a79ef46
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 271 deletions.
3 changes: 0 additions & 3 deletions src/Backdash.Utils/Backdash.Utils.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,4 @@
<PackageTags>network, endpoint, multiplayer, json, input</PackageTags>
<NoWarn>CS1591</NoWarn>
</PropertyGroup>
<ItemGroup>
<Folder Include="GamePad\Serializers\" />
</ItemGroup>
</Project>
15 changes: 0 additions & 15 deletions src/Backdash.Utils/GamePad/Extensions.cs

This file was deleted.

30 changes: 0 additions & 30 deletions src/Backdash.Utils/GamePad/PadButtonInputs.cs

This file was deleted.

215 changes: 0 additions & 215 deletions src/Backdash.Utils/GamePad/PadButtonsInputEditor.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/Backdash/Backends/SyncTestBackend.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ public void AdvanceFrame()
#endif
Checksum: lastSaved.Checksum
));
if (frame - lastVerified != checkDistance.Value)
if (frame - lastVerified != checkDistance.FrameValue)
return;
// We've gone far enough ahead and should now start replaying frames.
// Load the last verified frame and set the rollback flag to true.
Expand Down
5 changes: 5 additions & 0 deletions src/Backdash/Data/Frame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@ public bool TryFormat(
/// <inheritdoc />
public static FrameSpan operator +(Frame left, FrameSpan right) => right + left.Number;

/// <summary>
/// Returns the absolute value of a Frame.
/// </summary>
public static Frame Abs(in Frame frame) => new(Math.Abs(frame.Number));

/// <summary>
/// Clamps frame value to a range
/// </summary>
Expand Down
60 changes: 56 additions & 4 deletions src/Backdash/Data/FrameSpan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace Backdash.Data;
/// <summary>Returns max frame span value</summary>
public static readonly FrameSpan MaxValue = new(int.MaxValue);

/// <summary>Returns the <see cref="int"/> count of frames in the current frame span <see cref="Frame"/>.</summary>
/// <summary>Returns the <see cref="int"/> count of frames in the current frame span <see cref="Data.Frame"/>.</summary>
public readonly int FrameCount = 0;

/// <summary>
Expand All @@ -50,8 +50,38 @@ namespace Backdash.Data;
/// <summary>Returns the time value for the current frame span in <see cref="TimeSpan"/>.</summary>
public TimeSpan Duration(short fps = DefaultFramesPerSecond) => GetDuration(FrameCount, fps);

/// <summary>Returns the value for the current frame span as a <see cref="Frame"/>.</summary>
public Frame Value => new(FrameCount);
/// <summary>Returns the value for the current frame span as a <see cref="Data.Frame"/>.</summary>
public Frame FrameValue => new(FrameCount);

/// <summary>
/// Returns frame at the time position in milliseconds
/// </summary>
public Frame GetFrameAtMilliSecond(double millis, short fps = DefaultFramesPerSecond)
{
var span = FromMilliseconds(millis, fps);
if (span.FrameCount > FrameCount)
throw new InvalidOperationException("Out of range frame time");

return span.FrameValue;
}

/// <summary>
/// Returns frame at the time position in seconds
/// </summary>
public Frame GetFrameAtSecond(double seconds, short fps = DefaultFramesPerSecond)
{
var span = FromSeconds(seconds, fps);
if (span.FrameCount > FrameCount)
throw new InvalidOperationException("Out of range frame time");

return span.FrameValue;
}

/// <summary>
/// Returns frame at the timespan position
/// </summary>
public Frame GetFrameAt(TimeSpan duration, short fps = DefaultFramesPerSecond) =>
GetFrameAtMilliSecond(duration.TotalMilliseconds, fps);

/// <inheritdoc />
public int CompareTo(FrameSpan other) => FrameCount.CompareTo(other.FrameCount);
Expand All @@ -72,7 +102,7 @@ public bool TryFormat(
{
bytesWritten = 0;
Utf8StringWriter writer = new(in utf8Destination, ref bytesWritten);
if (!writer.Write(FrameCount, format)) return false;
if (!writer.Write(FrameCount, format, provider)) return false;
if (!writer.Write(" frames"u8)) return false;
return true;
}
Expand Down Expand Up @@ -153,4 +183,26 @@ public static TimeSpan GetDuration(int frameCount, short fps = DefaultFramesPerS

/// <inheritdoc />
public static FrameSpan operator -(FrameSpan left, FrameSpan right) => new(left.FrameCount - right.FrameCount);

/// <summary>
/// Returns the absolute value of a Frame.
/// </summary>
public static FrameSpan Abs(in FrameSpan frame) => new(Math.Abs(frame.FrameCount));

/// <summary>
/// Clamps frame value to a range
/// </summary>
public static FrameSpan Clamp(in FrameSpan frame, int min, int max) => new(Math.Clamp(frame.FrameCount, min, max));

/// <summary>
/// Clamps frame value to a range
/// </summary>
public static FrameSpan Clamp(in FrameSpan frame, in FrameSpan min, in FrameSpan max) =>
Clamp(in frame, min.FrameCount, max.FrameCount);

/// <summary>
/// Clamps frame value to a range
/// </summary>
public static FrameSpan Clamp(in FrameSpan frame, in Frame min, in Frame max) =>
Clamp(in frame, min.Number, max.Number);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using Backdash.GamePad;
using Backdash.Serialization;
using Backdash.Tests.TestUtils.Types;

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using System.Runtime.InteropServices;
using Backdash.GamePad;

// ReSharper disable InconsistentNaming
namespace Backdash.GamePad;
namespace Backdash.Tests.TestUtils.Types;

[Serializable]
[StructLayout(LayoutKind.Sequential, Pack = 1)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using Backdash.GamePad;
using Backdash.Serialization;
using Backdash.Serialization.Buffer;

Expand Down

0 comments on commit a79ef46

Please sign in to comment.