another data view

This commit is contained in:
Vinzenz Schroeter 2024-04-10 22:39:33 +02:00
parent 85ae3e302c
commit 91ab911f9c
9 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

@ -2,7 +2,7 @@ using System.Collections;
namespace TanksServer.ServicePointDisplay;
internal sealed class FixedSizeBitFieldView(Memory<byte> data) : IList<bool>
internal sealed class FixedSizeBitRowView(Memory<byte> data) : IList<bool>
{
public int Count => data.Length * 8;
public bool IsReadOnly => false;

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++;