wip move to new uniffi language binding

This commit is contained in:
Vinzenz Schroeter 2024-11-12 18:27:04 +01:00
commit 53cbdd8440
30 changed files with 212 additions and 188 deletions

View file

@ -1,5 +1,6 @@
 
Microsoft Visual Studio Solution File, Format Version 12.00 Microsoft Visual Studio Solution File, Format Version 12.00
#
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TanksServer", "TanksServer\TanksServer.csproj", "{D88BF376-47A4-4C72-ADD1-983F9285C351}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TanksServer", "TanksServer\TanksServer.csproj", "{D88BF376-47A4-4C72-ADD1-983F9285C351}"
EndProject EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "shared", "shared", "{12DB7D48-1BB2-488B-B4D9-4126087D2F8C}" Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "shared", "shared", "{12DB7D48-1BB2-488B-B4D9-4126087D2F8C}"
@ -9,7 +10,19 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "shared", "shared", "{12DB7D
..\.envrc = ..\.envrc ..\.envrc = ..\.envrc
EndProjectSection EndProjectSection
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ServicePoint", "servicepoint/crates/servicepoint_binding_cs/ServicePoint/ServicePoint.csproj", "{DFCC69ED-E02B-4631-8A23-5D394BA01E03}" Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "servicepoint", "servicepoint", "{A10FB29A-9078-4E90-9CE1-E6C2B5209E19}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "crates", "crates", "{656A7CBA-9445-41CC-B1AF-A6897AAC9F17}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "servicepoint_binding_uniffi", "servicepoint_binding_uniffi", "{5FD9FAD7-07BA-4DF9-8C84-15A9558373F1}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "libraries", "libraries", "{6082A0DC-5345-48C8-BA2E-667754A2F0E9}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "csharp", "csharp", "{D8A3290B-5DFB-43C6-99EE-56AB5F53F468}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ServicePoint", "servicepoint\crates\servicepoint_binding_uniffi\libraries\csharp\ServicePoint\ServicePoint.csproj", "{D1DDCD0D-6152-45E6-B673-DD78C466BDC3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ServicePoint.Tests", "servicepoint\crates\servicepoint_binding_uniffi\libraries\csharp\ServicePoint.Tests\ServicePoint.Tests.csproj", "{EF42D6B7-70B1-490B-BB5F-5A44D1309A7C}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -21,9 +34,21 @@ Global
{D88BF376-47A4-4C72-ADD1-983F9285C351}.Debug|Any CPU.Build.0 = Debug|Any CPU {D88BF376-47A4-4C72-ADD1-983F9285C351}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D88BF376-47A4-4C72-ADD1-983F9285C351}.Release|Any CPU.ActiveCfg = Release|Any CPU {D88BF376-47A4-4C72-ADD1-983F9285C351}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D88BF376-47A4-4C72-ADD1-983F9285C351}.Release|Any CPU.Build.0 = Release|Any CPU {D88BF376-47A4-4C72-ADD1-983F9285C351}.Release|Any CPU.Build.0 = Release|Any CPU
{DFCC69ED-E02B-4631-8A23-5D394BA01E03}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {D1DDCD0D-6152-45E6-B673-DD78C466BDC3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DFCC69ED-E02B-4631-8A23-5D394BA01E03}.Debug|Any CPU.Build.0 = Debug|Any CPU {D1DDCD0D-6152-45E6-B673-DD78C466BDC3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DFCC69ED-E02B-4631-8A23-5D394BA01E03}.Release|Any CPU.ActiveCfg = Release|Any CPU {D1DDCD0D-6152-45E6-B673-DD78C466BDC3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DFCC69ED-E02B-4631-8A23-5D394BA01E03}.Release|Any CPU.Build.0 = Release|Any CPU {D1DDCD0D-6152-45E6-B673-DD78C466BDC3}.Release|Any CPU.Build.0 = Release|Any CPU
{EF42D6B7-70B1-490B-BB5F-5A44D1309A7C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EF42D6B7-70B1-490B-BB5F-5A44D1309A7C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EF42D6B7-70B1-490B-BB5F-5A44D1309A7C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EF42D6B7-70B1-490B-BB5F-5A44D1309A7C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{656A7CBA-9445-41CC-B1AF-A6897AAC9F17} = {A10FB29A-9078-4E90-9CE1-E6C2B5209E19}
{5FD9FAD7-07BA-4DF9-8C84-15A9558373F1} = {656A7CBA-9445-41CC-B1AF-A6897AAC9F17}
{6082A0DC-5345-48C8-BA2E-667754A2F0E9} = {5FD9FAD7-07BA-4DF9-8C84-15A9558373F1}
{D8A3290B-5DFB-43C6-99EE-56AB5F53F468} = {6082A0DC-5345-48C8-BA2E-667754A2F0E9}
{D1DDCD0D-6152-45E6-B673-DD78C466BDC3} = {D8A3290B-5DFB-43C6-99EE-56AB5F53F468}
{EF42D6B7-70B1-490B-BB5F-5A44D1309A7C} = {D8A3290B-5DFB-43C6-99EE-56AB5F53F468}
EndGlobalSection EndGlobalSection
EndGlobal EndGlobal

View file

@ -1,3 +1,4 @@
using System.Diagnostics.CodeAnalysis;
using System.IO; using System.IO;
using System.Text; using System.Text;
using System.Text.Json; using System.Text.Json;
@ -7,7 +8,6 @@ using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.HttpResults; using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Diagnostics.HealthChecks; using Microsoft.Extensions.Diagnostics.HealthChecks;
using ServicePoint;
using TanksServer.GameLogic; using TanksServer.GameLogic;
using TanksServer.Interactivity; using TanksServer.Interactivity;
@ -22,6 +22,8 @@ internal sealed class Endpoints(
Connection displayConnection Connection displayConnection
) )
{ {
[RequiresUnreferencedCode("Calls Microsoft.AspNetCore.Builder.EndpointRouteBuilderExtensions.MapPost(String, Delegate)")]
[RequiresDynamicCode("Calls Microsoft.AspNetCore.Builder.EndpointRouteBuilderExtensions.MapPost(String, Delegate)")]
public void Map(WebApplication app) public void Map(WebApplication app)
{ {
app.MapPost("/player", PostPlayer); app.MapPost("/player", PostPlayer);
@ -31,7 +33,7 @@ internal sealed class Endpoints(
app.Map("/controls", ConnectControlsAsync); app.Map("/controls", ConnectControlsAsync);
app.MapGet("/map", () => mapService.MapNames); app.MapGet("/map", () => mapService.MapNames);
app.MapPost("/map", PostMap); app.MapPost("/map", PostMap);
app.MapPost("/resetDisplay", () => displayConnection.Send(Command.HardReset().IntoPacket())); app.MapPost("/resetDisplay", () => displayConnection.Send(Command.HardReset()));
app.MapGet("/map/{name}", GetMapByName); app.MapGet("/map/{name}", GetMapByName);
app.MapHealthChecks("/health", new HealthCheckOptions app.MapHealthChecks("/health", new HealthCheckOptions
@ -117,7 +119,7 @@ internal sealed class Endpoints(
if (!mapService.TryGetPreview(name, out var preview)) if (!mapService.TryGetPreview(name, out var preview))
return TypedResults.NotFound(); return TypedResults.NotFound();
var mapInfo = new MapInfo(prototype.Name, prototype.GetType().Name, preview.Data.ToArray()); var mapInfo = new MapInfo(prototype.Name, prototype.GetType().Name, preview.CopyRaw());
return TypedResults.Ok(mapInfo); return TypedResults.Ok(mapInfo);
} }

View file

@ -88,13 +88,13 @@ internal sealed class CollideBullets : ITickStep
pixel = pixel.GetPixelRelative(-4, -4); pixel = pixel.GetPixelRelative(-4, -4);
for (short dx = 0; dx < _explosiveSprite.Width; dx++) for (short dx = 0; dx < _explosiveSprite.Width; dx++)
for (short dy = 0; dy < _explosiveSprite.Height; dy++) for (short dy = 0; dy < _explosiveSprite.Height; dy++)
{ {
if (!_explosiveSprite[dx, dy].HasValue) if (!_explosiveSprite[dx, dy].HasValue)
continue; continue;
Core(pixel.GetPixelRelative(dx, dy)); Core(pixel.GetPixelRelative(dx, dy));
} }
return; return;

View file

@ -10,22 +10,22 @@ internal sealed class EmptyTileFinder(
var maxMinDistance = 0d; var maxMinDistance = 0d;
TilePosition spawnTile = default; TilePosition spawnTile = default;
for (ushort x = 1; x < MapService.TilesPerRow - 1; x++) for (ushort x = 1; x < MapService.TilesPerRow - 1; x++)
for (ushort y = 1; y < MapService.TilesPerColumn - 1; y++) for (ushort y = 1; y < MapService.TilesPerColumn - 1; y++)
{ {
var tile = new TilePosition(x, y); var tile = new TilePosition(x, y);
if (mapService.Current.IsWall(tile)) if (mapService.Current.IsWall(tile))
continue; continue;
var tilePixelCenter = tile.GetCenter().ToFloatPosition(); var tilePixelCenter = tile.GetCenter().ToFloatPosition();
var minDistance = entityManager.AllEntities var minDistance = entityManager.AllEntities
.Select(entity => entity.Position.Distance(tilePixelCenter)) .Select(entity => entity.Position.Distance(tilePixelCenter))
.Aggregate(double.MaxValue, Math.Min); .Aggregate(double.MaxValue, Math.Min);
if (minDistance <= maxMinDistance) if (minDistance <= maxMinDistance)
continue; continue;
maxMinDistance = minDistance; maxMinDistance = minDistance;
spawnTile = tile; spawnTile = tile;
} }
return spawnTile; return spawnTile;
} }

View file

@ -4,7 +4,7 @@ internal sealed class Map(string name, bool[,] walls)
{ {
public string Name => name; public string Name => name;
public bool IsWall(int x, int y) => walls[x, y]; public bool IsWall(ulong x, ulong y) => walls[x, y];
public bool IsWall(PixelPosition position) => walls[position.X, position.Y]; public bool IsWall(PixelPosition position) => walls[position.X, position.Y];
@ -12,12 +12,12 @@ internal sealed class Map(string name, bool[,] walls)
{ {
var pixel = position.ToPixelPosition(); var pixel = position.ToPixelPosition();
for (short dx = 0; dx < MapService.TileSize; dx++) for (long dx = 0; dx < (long)MapService.TileSize; dx++)
for (short dy = 0; dy < MapService.TileSize; dy++) for (long dy = 0; dy < (long)MapService.TileSize; dy++)
{ {
if (IsWall(pixel.GetPixelRelative(dx, dy))) if (IsWall(pixel.GetPixelRelative(dx, dy)))
return true; return true;
} }
return false; return false;
} }

View file

@ -37,7 +37,7 @@ internal sealed class MapEntityManager(
{ {
Rotation = Random.Shared.NextDouble(), Rotation = Random.Shared.NextDouble(),
MaxBullets = _rules.MagazineSize, MaxBullets = _rules.MagazineSize,
BulletStats =new BulletStats(_rules.BulletSpeed, 0, false, false) BulletStats = new BulletStats(_rules.BulletSpeed, 0, false, false)
}; };
_playerTanks[player] = tank; _playerTanks[player] = tank;
logger.LogInformation("Tank added for player {}", player.Name); logger.LogInformation("Tank added for player {}", player.Name);

View file

@ -1,18 +1,17 @@
using System.Diagnostics; using System.Diagnostics;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using System.IO; using System.IO;
using ServicePoint;
using TanksServer.Graphics; using TanksServer.Graphics;
namespace TanksServer.GameLogic; namespace TanksServer.GameLogic;
internal sealed class MapService internal sealed class MapService
{ {
public const ushort TilesPerRow = 44; public const ulong TilesPerRow = 44;
public const ushort TilesPerColumn = 20; public const ulong TilesPerColumn = 20;
public const ushort TileSize = 8; public const ulong TileSize = 8;
public const ushort PixelsPerRow = TilesPerRow * TileSize; public const ulong PixelsPerRow = TilesPerRow * TileSize;
public const ushort PixelsPerColumn = TilesPerColumn * TileSize; public const ulong PixelsPerColumn = TilesPerColumn * TileSize;
private readonly ConcurrentDictionary<string, MapPrototype> _mapPrototypes = new(); private readonly ConcurrentDictionary<string, MapPrototype> _mapPrototypes = new();
private readonly ConcurrentDictionary<string, Bitmap> _mapPreviews = new(); private readonly ConcurrentDictionary<string, Bitmap> _mapPreviews = new();

View file

@ -68,13 +68,13 @@ internal sealed class MoveTanks(
{ {
var (topLeft, _) = newPosition.GetBoundsForCenter(MapService.TileSize); var (topLeft, _) = newPosition.GetBoundsForCenter(MapService.TileSize);
for (short y = 0; y < MapService.TileSize; y++) for (ulong y = 0; y < MapService.TileSize; y++)
for (short x = 0; x < MapService.TileSize; x++) for (ulong x = 0; x < MapService.TileSize; x++)
{ {
var pixelToCheck = topLeft.GetPixelRelative(x, y); var pixelToCheck = topLeft.GetPixelRelative((long)x, (long)y);
if (map.Current.IsWall(pixelToCheck)) if (map.Current.IsWall(pixelToCheck))
return true; return true;
} }
return false; return false;
} }

View file

@ -11,7 +11,7 @@ internal sealed class SpriteMapPrototype : MapPrototype
public SpriteMapPrototype(string name, Sprite sprite) public SpriteMapPrototype(string name, Sprite sprite)
{ {
if (sprite.Width != MapService.PixelsPerRow || sprite.Height != MapService.PixelsPerColumn) if ((ulong)sprite.Width != MapService.PixelsPerRow || (ulong)sprite.Height != MapService.PixelsPerColumn)
throw new FileLoadException($"invalid image size in file {Name}"); throw new FileLoadException($"invalid image size in file {Name}");
Name = name; Name = name;

View file

@ -8,7 +8,7 @@ internal sealed class TextMapPrototype : MapPrototype
public TextMapPrototype(string name, string text) public TextMapPrototype(string name, string text)
{ {
if (text.Length != MapService.TilesPerColumn * MapService.TilesPerRow) if ((ulong)text.Length != MapService.TilesPerColumn * MapService.TilesPerRow)
throw new ArgumentException($"cannot load map {name}: invalid length"); throw new ArgumentException($"cannot load map {name}: invalid length");
Name = name; Name = name;
Text = text; Text = text;
@ -19,20 +19,20 @@ internal sealed class TextMapPrototype : MapPrototype
{ {
var walls = new bool[MapService.PixelsPerRow, MapService.PixelsPerColumn]; var walls = new bool[MapService.PixelsPerRow, MapService.PixelsPerColumn];
for (ushort tileX = 0; tileX < MapService.TilesPerRow; tileX++) for (ulong tileX = 0; tileX < MapService.TilesPerRow; tileX++)
for (ushort tileY = 0; tileY < MapService.TilesPerColumn; tileY++) for (ulong tileY = 0; tileY < MapService.TilesPerColumn; tileY++)
{
var tile = new TilePosition(tileX, tileY);
if (Text[tileX + tileY * MapService.TilesPerRow] != '#')
continue;
for (byte pixelInTileX = 0; pixelInTileX < MapService.TileSize; pixelInTileX++)
for (byte pixelInTileY = 0; pixelInTileY < MapService.TileSize; pixelInTileY++)
{ {
var (x, y) = tile.ToPixelPosition().GetPixelRelative(pixelInTileX, pixelInTileY); var tile = new TilePosition(tileX, tileY);
walls[x, y] = true; if (Text[(int)(tileX + tileY * MapService.TilesPerRow)] != '#')
continue;
for (byte pixelInTileX = 0; pixelInTileX < MapService.TileSize; pixelInTileX++)
for (byte pixelInTileY = 0; pixelInTileY < MapService.TileSize; pixelInTileY++)
{
var (x, y) = tile.ToPixelPosition().GetPixelRelative(pixelInTileX, pixelInTileY);
walls[x, y] = true;
}
} }
}
return new Map(Name, walls); return new Map(Name, walls);
} }

View file

@ -7,3 +7,4 @@ global using System.Threading.Tasks;
global using Microsoft.Extensions.Logging; global using Microsoft.Extensions.Logging;
global using Microsoft.Extensions.Options; global using Microsoft.Extensions.Options;
global using TanksServer.Models; global using TanksServer.Models;
global using ServicePoint;

View file

@ -1,4 +1,3 @@
using ServicePoint;
using TanksServer.GameLogic; using TanksServer.GameLogic;
namespace TanksServer.Graphics; namespace TanksServer.Graphics;
@ -9,24 +8,24 @@ internal sealed class DrawMapStep(MapService map) : IDrawStep
private static void Draw(GamePixelGrid pixels, Map map) private static void Draw(GamePixelGrid pixels, Map map)
{ {
for (ushort y = 0; y < MapService.PixelsPerColumn; y++) for (ulong y = 0; y < MapService.PixelsPerColumn; y++)
for (ushort x = 0; x < MapService.PixelsPerRow; x++) for (ulong x = 0; x < MapService.PixelsPerRow; x++)
{ {
if (!map.IsWall(x, y)) if (!map.IsWall(x, y))
continue; continue;
pixels[x, y].EntityType = GamePixelEntityType.Wall; pixels[x, y].EntityType = GamePixelEntityType.Wall;
} }
} }
public static void Draw(Bitmap pixels, Map map) public static void Draw(Bitmap pixels, Map map)
{ {
for (ushort y = 0; y < MapService.PixelsPerColumn; y++) for (ulong y = 0; y < MapService.PixelsPerColumn; y++)
for (ushort x = 0; x < MapService.PixelsPerRow; x++) for (ulong x = 0; x < MapService.PixelsPerRow; x++)
{ {
if (!map.IsWall(x, y)) if (!map.IsWall(x, y))
continue; continue;
pixels[x, y] = true; pixels.Set(x, y, true);
} }
} }
} }

View file

@ -30,16 +30,16 @@ internal sealed class DrawPowerUpsStep(MapEntityManager entityManager) : IDrawSt
private static void DrawPowerUp(GamePixelGrid pixels, Sprite sprite, PixelPosition position) private static void DrawPowerUp(GamePixelGrid pixels, Sprite sprite, PixelPosition position)
{ {
for (byte dy = 0; dy < MapService.TileSize; dy++) for (byte dy = 0; dy < MapService.TileSize; dy++)
for (byte dx = 0; dx < MapService.TileSize; dx++) for (byte dx = 0; dx < MapService.TileSize; dx++)
{ {
var pixelState = sprite[dx, dy]; var pixelState = sprite[dx, dy];
if (!pixelState.HasValue) if (!pixelState.HasValue)
continue; continue;
var (x, y) = position.GetPixelRelative(dx, dy); var (x, y) = position.GetPixelRelative(dx, dy);
pixels[x, y].EntityType = pixelState.Value pixels[x, y].EntityType = pixelState.Value
? GamePixelEntityType.PowerUp ? GamePixelEntityType.PowerUp
: null; : null;
} }
} }
} }

View file

@ -5,7 +5,7 @@ namespace TanksServer.Graphics;
internal sealed class DrawTanksStep(MapEntityManager entityManager) : IDrawStep internal sealed class DrawTanksStep(MapEntityManager entityManager) : IDrawStep
{ {
private readonly SpriteSheet _tankSprites = private readonly SpriteSheet _tankSprites =
SpriteSheet.FromImageFile("assets/tank.png", MapService.TileSize, MapService.TileSize); SpriteSheet.FromImageFile("assets/tank.png", (int)MapService.TileSize, (int)MapService.TileSize);
public void Draw(GamePixelGrid pixels) public void Draw(GamePixelGrid pixels)
{ {
@ -14,16 +14,16 @@ internal sealed class DrawTanksStep(MapEntityManager entityManager) : IDrawStep
var tankPosition = tank.Bounds.TopLeft; var tankPosition = tank.Bounds.TopLeft;
for (byte dy = 0; dy < MapService.TileSize; dy++) for (byte dy = 0; dy < MapService.TileSize; dy++)
for (byte dx = 0; dx < MapService.TileSize; dx++) for (byte dx = 0; dx < MapService.TileSize; dx++)
{ {
var pixel = _tankSprites[tank.Orientation][dx, dy]; var pixel = _tankSprites[tank.Orientation][dx, dy];
if (!pixel.HasValue || !pixel.Value) if (!pixel.HasValue || !pixel.Value)
continue; continue;
var (x, y) = tankPosition.GetPixelRelative(dx, dy); var (x, y) = tankPosition.GetPixelRelative(dx, dy);
pixels[x, y].EntityType = GamePixelEntityType.Tank; pixels[x, y].EntityType = GamePixelEntityType.Tank;
pixels[x, y].BelongsTo = tank.Owner; pixels[x, y].BelongsTo = tank.Owner;
} }
} }
} }
} }

View file

@ -5,27 +5,27 @@ namespace TanksServer.Graphics;
internal sealed class GamePixelGrid : IEnumerable<GamePixel> internal sealed class GamePixelGrid : IEnumerable<GamePixel>
{ {
public int Width { get; } public ulong Width { get; }
public int Height { get; } public ulong Height { get; }
private readonly GamePixel[,] _pixels; private readonly GamePixel[,] _pixels;
public GamePixelGrid(int width, int height) public GamePixelGrid(ulong width, ulong height)
{ {
Width = width; Width = width;
Height = height; Height = height;
_pixels = new GamePixel[width, height]; _pixels = new GamePixel[width, height];
for (var y = 0; y < height; y++) for (ulong y = 0; y < height; y++)
for (var x = 0; x < width; x++) for (ulong x = 0; x < width; x++)
this[x, y] = new GamePixel(); this[x, y] = new GamePixel();
} }
public GamePixel this[int x, int y] public GamePixel this[ulong x, ulong y]
{ {
get get
{ {
Debug.Assert(y * Width + x < _pixels.Length); Debug.Assert(y * Width + x < (ulong)_pixels.Length);
return _pixels[x, y]; return _pixels[x, y];
} }
set => _pixels[x, y] = value; set => _pixels[x, y] = value;
@ -41,8 +41,8 @@ internal sealed class GamePixelGrid : IEnumerable<GamePixel>
public IEnumerator<GamePixel> GetEnumerator() public IEnumerator<GamePixel> GetEnumerator()
{ {
for (var y = 0; y < Height; y++) for (ulong y = 0; y < Height; y++)
for (var x = 0; x < Width; x++) for (ulong x = 0; x < Width; x++)
yield return this[x, y]; yield return this[x, y];
} }
} }

View file

@ -20,7 +20,7 @@ internal sealed class GeneratePixelsTickStep(
public async ValueTask TickAsync(TimeSpan _) public async ValueTask TickAsync(TimeSpan _)
{ {
Draw(_gamePixelGrid, _observerPixelGrid); Draw(_gamePixelGrid, _observerPixelGrid);
if (_observerPixelGrid.Data.SequenceEqual(_lastObserverPixelGrid.Data)) if (_observerPixelGrid.Equals(_lastObserverPixelGrid))
return; return;
await _consumers.Select(c => c.OnFrameDoneAsync(_gamePixelGrid, _observerPixelGrid)) await _consumers.Select(c => c.OnFrameDoneAsync(_gamePixelGrid, _observerPixelGrid))
@ -37,12 +37,12 @@ internal sealed class GeneratePixelsTickStep(
step.Draw(gamePixelGrid); step.Draw(gamePixelGrid);
observerPixelGrid.Fill(false); observerPixelGrid.Fill(false);
for (var y = 0; y < MapService.PixelsPerColumn; y++) for (ulong y = 0; y < MapService.PixelsPerColumn; y++)
for (var x = 0; x < MapService.PixelsPerRow; x++) for (ulong x = 0; x < MapService.PixelsPerRow; x++)
{ {
if (gamePixelGrid[x, y].EntityType.HasValue) if (gamePixelGrid[x, y].EntityType.HasValue)
observerPixelGrid[(ushort)x, (ushort)y] = true; observerPixelGrid.Set(x, y, true);
} }
} }
public void Dispose() public void Dispose()

View file

@ -12,13 +12,13 @@ internal sealed class Sprite(bool?[,] data)
var whitePixel = new Rgba32(byte.MaxValue, byte.MaxValue, byte.MaxValue, byte.MaxValue); var whitePixel = new Rgba32(byte.MaxValue, byte.MaxValue, byte.MaxValue, byte.MaxValue);
for (var y = 0; y < image.Height; y++) for (var y = 0; y < image.Height; y++)
for (var x = 0; x < image.Width; x++) for (var x = 0; x < image.Width; x++)
{ {
var pixelValue = image[x, y]; var pixelValue = image[x, y];
data[x, y] = pixelValue.A == 0 data[x, y] = pixelValue.A == 0
? null ? null
: pixelValue == whitePixel; : pixelValue == whitePixel;
} }
return new Sprite(data); return new Sprite(data);
} }
@ -34,8 +34,8 @@ internal sealed class Sprite(bool?[,] data)
var result = new bool[Width, Height]; var result = new bool[Width, Height];
for (var y = 0; y < Height; y++) for (var y = 0; y < Height; y++)
for (var x = 0; x < Width; x++) for (var x = 0; x < Width; x++)
result[x, y] = this[x, y] ?? false; result[x, y] = this[x, y] ?? false;
return result; return result;
} }

View file

@ -21,24 +21,24 @@ internal sealed class SpriteSheet(Sprite[,] sprites)
var whitePixel = new Rgba32(byte.MaxValue, byte.MaxValue, byte.MaxValue, byte.MaxValue); var whitePixel = new Rgba32(byte.MaxValue, byte.MaxValue, byte.MaxValue, byte.MaxValue);
for (var spriteY = 0; spriteY < spritesPerColumn; spriteY++) for (var spriteY = 0; spriteY < spritesPerColumn; spriteY++)
for (var spriteX = 0; spriteX < spritesPerRow; spriteX++) for (var spriteX = 0; spriteX < spritesPerRow; spriteX++)
{
var data = new bool?[spriteWidth, spriteHeight];
for (var dy = 0; dy < spriteHeight; dy++)
for (var dx = 0; dx < spriteWidth; dx++)
{ {
var x = spriteX * spriteWidth + dx; var data = new bool?[spriteWidth, spriteHeight];
var y = spriteY * spriteHeight + dy; for (var dy = 0; dy < spriteHeight; dy++)
for (var dx = 0; dx < spriteWidth; dx++)
{
var x = spriteX * spriteWidth + dx;
var y = spriteY * spriteHeight + dy;
var pixelValue = image[x, y]; var pixelValue = image[x, y];
data[dx, dy] = pixelValue.A == 0 data[dx, dy] = pixelValue.A == 0
? null ? null
: pixelValue == whitePixel; : pixelValue == whitePixel;
}
sprites[spriteX, spriteY] = new Sprite(data);
} }
sprites[spriteX, spriteY] = new Sprite(data);
}
return new SpriteSheet(sprites); return new SpriteSheet(sprites);
} }

View file

@ -2,13 +2,13 @@ using System.Buffers;
namespace TanksServer.Interactivity; namespace TanksServer.Interactivity;
internal sealed class BufferPool: MemoryPool<byte> internal sealed class BufferPool : MemoryPool<byte>
{ {
private readonly MemoryPool<byte> _actualPool = Shared; private readonly MemoryPool<byte> _actualPool = Shared;
public override int MaxBufferSize => int.MaxValue; public override int MaxBufferSize => int.MaxValue;
protected override void Dispose(bool disposing) {} protected override void Dispose(bool disposing) { }
public override IMemoryOwner<byte> Rent(int minBufferSize = -1) public override IMemoryOwner<byte> Rent(int minBufferSize = -1)
{ {
@ -16,7 +16,7 @@ internal sealed class BufferPool: MemoryPool<byte>
return new BufferPoolMemoryOwner(_actualPool.Rent(minBufferSize), minBufferSize); return new BufferPoolMemoryOwner(_actualPool.Rent(minBufferSize), minBufferSize);
} }
private sealed class BufferPoolMemoryOwner(IMemoryOwner<byte> actualOwner, int wantedSize): IMemoryOwner<byte> private sealed class BufferPoolMemoryOwner(IMemoryOwner<byte> actualOwner, int wantedSize) : IMemoryOwner<byte>
{ {
public Memory<byte> Memory { get; } = actualOwner.Memory[..wantedSize]; public Memory<byte> Memory { get; } = actualOwner.Memory[..wantedSize];

View file

@ -36,7 +36,7 @@ internal sealed class ClientScreenServerConnection
private Package BuildNextPackage(Bitmap pixels, GamePixelGrid gamePixelGrid) private Package BuildNextPackage(Bitmap pixels, GamePixelGrid gamePixelGrid)
{ {
var pixelsData = pixels.Data; var pixelsData = pixels.CopyRaw();
var nextPixels = _bufferPool.Rent(pixelsData.Length); var nextPixels = _bufferPool.Rent(pixelsData.Length);
pixelsData.CopyTo(nextPixels.Memory.Span); pixelsData.CopyTo(nextPixels.Memory.Span);

Some files were not shown because too many files have changed in this diff Show more