do not send frames when nothing changes

This commit is contained in:
Vinzenz Schroeter 2024-04-12 19:54:16 +02:00
parent 89494ef495
commit de3d298475
5 changed files with 54 additions and 13 deletions

View file

@ -2,7 +2,7 @@ using System.Diagnostics;
namespace DisplayCommands;
public class ByteGrid(ushort width, ushort height)
public sealed class ByteGrid(ushort width, ushort height) : IEquatable<ByteGrid>
{
public ushort Height { get; } = height;
@ -12,7 +12,7 @@ public class ByteGrid(ushort width, ushort height)
public byte this[ushort x, ushort y]
{
get => Data.Span[ GetIndex(x, y)];
get => Data.Span[GetIndex(x, y)];
set => Data.Span[GetIndex(x, y)] = value;
}
@ -24,4 +24,16 @@ public class ByteGrid(ushort width, ushort height)
}
public void Clear() => Data.Span.Clear();
public bool Equals(ByteGrid? other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return Height == other.Height && Width == other.Width && Data.Span.SequenceEqual(other.Data.Span);
}
public override bool Equals(object? obj) => ReferenceEquals(this, obj) || (obj is ByteGrid other && Equals(other));
public override int GetHashCode() => HashCode.Combine(Height, Width, Data);
public static bool operator ==(ByteGrid? left, ByteGrid? right) => Equals(left, right);
public static bool operator !=(ByteGrid? left, ByteGrid? right) => !Equals(left, right);
}

View file

@ -3,7 +3,7 @@ using System.Text;
namespace DisplayCommands;
public sealed class Cp437Grid(ushort width, ushort height)
public sealed class Cp437Grid(ushort width, ushort height) : IEquatable<Cp437Grid>
{
private readonly ByteGrid _byteGrid = new(width, height);
@ -47,7 +47,6 @@ public sealed class Cp437Grid(ushort width, ushort height)
var consumed = _encoding.GetBytes(valuesStr, convertedStr);
Debug.Assert(consumed == 1);
return convertedStr[0];
}
private char ByteToChar(byte b)
@ -57,4 +56,16 @@ public sealed class Cp437Grid(ushort width, ushort height)
_encoding.GetChars(valueBytes, resultStr);
return resultStr[0];
}
public bool Equals(Cp437Grid? other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return Height == other.Height && Width == other.Width && _byteGrid.Equals(other._byteGrid);
}
public override bool Equals(object? obj) => ReferenceEquals(this, obj) || (obj is Cp437Grid other && Equals(other));
public override int GetHashCode() => HashCode.Combine(_byteGrid, Height, Width);
public static bool operator ==(Cp437Grid? left, Cp437Grid? right) => Equals(left, right);
public static bool operator !=(Cp437Grid? left, Cp437Grid? right) => !Equals(left, right);
}

View file

@ -2,7 +2,7 @@ using System.Diagnostics;
namespace DisplayCommands;
public sealed class PixelGrid(ushort width, ushort height)
public sealed class PixelGrid(ushort width, ushort height) : IEquatable<PixelGrid>
{
private readonly ByteGrid _byteGrid = new((ushort)(width / 8u), height);
@ -34,6 +34,18 @@ public sealed class PixelGrid(ushort width, ushort height)
public void Clear() => _byteGrid.Clear();
public bool Equals(PixelGrid? other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return Width == other.Width && Height == other.Height && _byteGrid.Equals(other._byteGrid);
}
public override bool Equals(object? obj) => ReferenceEquals(this, obj) || (obj is PixelGrid other && Equals(other));
public override int GetHashCode() => HashCode.Combine(_byteGrid, Width, Height);
public static bool operator ==(PixelGrid? left, PixelGrid? right) => Equals(left, right);
public static bool operator !=(PixelGrid? left, PixelGrid? right) => !Equals(left, right);
private (ushort byteIndex, byte bitInByteMask) GetIndexes(int x)
{
Debug.Assert(x < Width);

View file

@ -13,6 +13,7 @@ internal sealed class SendToServicePointDisplay : ITickStep
private readonly PlayerServer _players;
private readonly ILogger<SendToServicePointDisplay> _logger;
private readonly IDisplayConnection _displayConnection;
private PixelGrid? _lastSentFrame;
private DateTime _nextFailLog = DateTime.Now;
@ -50,7 +51,12 @@ internal sealed class SendToServicePointDisplay : ITickStep
try
{
await _displayConnection.SendCp437DataAsync(MapService.TilesPerRow, 0, _scoresBuffer);
await _displayConnection.SendBitmapLinearWindowAsync(0, 0, _lastFinishedFrameProvider.LastFrame);
var currentFrame = _lastFinishedFrameProvider.LastFrame;
if (_lastSentFrame == currentFrame)
return;
_lastSentFrame = currentFrame;
await _displayConnection.SendBitmapLinearWindowAsync(0, 0, _lastSentFrame);
}
catch (SocketException ex)
{

View file

@ -24,7 +24,7 @@
"MoveSpeed": 1.4,
"TurnSpeed": 0.4,
"ShootDelayMs": 400,
"BulletSpeed": 8
"BulletSpeed": 3
},
"Players": {
"SpawnDelayMs": 3000,