another data view

This commit is contained in:
Vinzenz Schroeter 2024-04-10 22:39:33 +02:00
commit 91ab911f9c
4 changed files with 44 additions and 23 deletions

View file

@ -0,0 +1,23 @@
using System.Diagnostics;
namespace TanksServer.ServicePointDisplay;
internal sealed class FixedSizeBitGridView(Memory<byte> data, int columns, int rows)
{
private readonly FixedSizeBitRowView _bits = new(data);
public bool this[PixelPosition position]
{
get => _bits[ToPixelIndex(position)];
set => _bits[ToPixelIndex(position)] = value;
}
private int ToPixelIndex(PixelPosition position)
{
Debug.Assert(position.X < columns);
Debug.Assert(position.Y < rows);
var index = position.Y * columns + position.X;
ArgumentOutOfRangeException.ThrowIfNegative(index, nameof(position));
return index;
}
}

View file

@ -0,0 +1,82 @@
using System.Collections;
namespace TanksServer.ServicePointDisplay;
internal sealed class FixedSizeBitRowView(Memory<byte> data) : IList<bool>
{
public int Count => data.Length * 8;
public bool IsReadOnly => false;
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
public IEnumerator<bool> GetEnumerator()
{
return Enumerable().GetEnumerator();
IEnumerable<bool> Enumerable()
{
for (var i = 0; i < Count; i++)
yield return this[i];
}
}
public void Clear()
{
var span = data.Span;
for (var i = 0; i < data.Length; i++)
span[i] = 0;
}
public void CopyTo(bool[] array, int arrayIndex)
{
for (var i = 0; i < Count && i + arrayIndex < array.Length; i++)
array[i + arrayIndex] = this[i];
}
private (int byteIndex, int bitInByteIndex) GetIndexes(int bitIndex)
{
var byteIndex = bitIndex / 8;
var bitInByteIndex = 7 - bitIndex % 8;
if (byteIndex >= data.Length)
{
throw new ArgumentOutOfRangeException(nameof(bitIndex),
$"accessing this bit field at position {bitIndex} would result in an access to byte " +
$"{byteIndex} but byte length is {data.Length}");
}
return (byteIndex, bitInByteIndex);
}
public bool this[int bitIndex]
{
get
{
var (byteIndex, bitInByteIndex) = GetIndexes(bitIndex);
var bitInByteMask = (byte)(1 << bitInByteIndex);
return (data.Span[byteIndex] & bitInByteMask) != 0;
}
set
{
var (byteIndex, bitInByteIndex) = GetIndexes(bitIndex);
var bitInByteMask = (byte)(1 << bitInByteIndex);
if (value)
{
data.Span[byteIndex] |= bitInByteMask;
}
else
{
var withoutBitMask = (byte)(ushort.MaxValue ^ bitInByteMask);
data.Span[byteIndex] &= withoutBitMask;
}
}
}
public void Add(bool item) => throw new NotSupportedException();
public bool Contains(bool item) => throw new NotSupportedException();
public bool Remove(bool item) => throw new NotSupportedException();
public int IndexOf(bool item) => throw new NotSupportedException();
public void Insert(int index, bool item) => throw new NotSupportedException();
public void RemoveAt(int index) => throw new NotSupportedException();
}

View file

@ -1,20 +1,22 @@
using TanksServer.GameLogic;
namespace TanksServer.ServicePointDisplay;
internal sealed class PixelDisplayBufferView : DisplayBufferView
{
private PixelDisplayBufferView(byte[] data) : base(data)
private PixelDisplayBufferView(byte[] data, int columns, int pixelRows) : base(data)
{
Pixels = new FixedSizeBitFieldView(Data.AsMemory(10));
Pixels = new FixedSizeBitGridView(Data.AsMemory(10), columns, pixelRows);
}
// ReSharper disable once CollectionNeverQueried.Global (setting values in collection updates underlying byte array)
public FixedSizeBitFieldView Pixels { get; }
public FixedSizeBitGridView Pixels { get; }
public static PixelDisplayBufferView New(ushort x, ushort y, ushort widthInTiles, ushort pixelRows)
{
// 10 bytes header, one byte per tile row (with one bit each pixel) after that
var size = 10 + widthInTiles * pixelRows;
return new PixelDisplayBufferView(new byte[size])
return new PixelDisplayBufferView(new byte[size], widthInTiles * MapService.TileSize, pixelRows)
{
Mode = 19,
TileX = x,

View file

@ -90,10 +90,10 @@ internal sealed class SendToServicePointDisplay : ITickStep, IDisposable
foreach (var p in playersToDisplay)
{
var score = p.Kills.ToString();
var nameLength = ScoresWidth - score.Length;
var nameLength = Math.Min(p.Name.Length, ScoresWidth - score.Length - 1);
var name = p.Name[..nameLength];
var spaces = new string(' ', nameLength - name.Length + 1);
var spaces = new string(' ', ScoresWidth - score.Length - nameLength);
_scoresBuffer.Rows[row] = name + spaces + score;
row++;