From a486f730465e6b3df203e05f79a0bc86bb16652e Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Mon, 22 Apr 2024 20:58:12 +0200 Subject: [PATCH] sprite helper classes --- .../TanksServer/Graphics/DrawPowerUpsStep.cs | 29 ++--------- .../TanksServer/Graphics/DrawTanksStep.cs | 30 ++--------- tanks-backend/TanksServer/Graphics/Sprite.cs | 27 ++++++++++ .../TanksServer/Graphics/SpriteSheet.cs | 48 ++++++++++++++++++ tanks-backend/TanksServer/assets/tank.png | Bin 355 -> 493 bytes 5 files changed, 82 insertions(+), 52 deletions(-) create mode 100644 tanks-backend/TanksServer/Graphics/Sprite.cs create mode 100644 tanks-backend/TanksServer/Graphics/SpriteSheet.cs diff --git a/tanks-backend/TanksServer/Graphics/DrawPowerUpsStep.cs b/tanks-backend/TanksServer/Graphics/DrawPowerUpsStep.cs index dc898c7..c67a5e6 100644 --- a/tanks-backend/TanksServer/Graphics/DrawPowerUpsStep.cs +++ b/tanks-backend/TanksServer/Graphics/DrawPowerUpsStep.cs @@ -1,37 +1,14 @@ -using System.Diagnostics; -using SixLabors.ImageSharp; -using SixLabors.ImageSharp.PixelFormats; using TanksServer.GameLogic; namespace TanksServer.Graphics; -internal sealed class DrawPowerUpsStep : IDrawStep +internal sealed class DrawPowerUpsStep(MapEntityManager entityManager) : IDrawStep { - private readonly MapEntityManager _entityManager; - private readonly bool?[,] _explosiveSprite; - - public DrawPowerUpsStep(MapEntityManager entityManager) - { - _entityManager = entityManager; - - using var tankImage = Image.Load("assets/powerup_explosive.png"); - Debug.Assert(tankImage.Width == tankImage.Height && tankImage.Width == MapService.TileSize); - _explosiveSprite = new bool?[tankImage.Width, tankImage.Height]; - - var whitePixel = new Rgba32(byte.MaxValue, byte.MaxValue, byte.MaxValue, byte.MaxValue); - for (var y = 0; y < tankImage.Height; y++) - for (var x = 0; x < tankImage.Width; x++) - { - var pixelValue = tankImage[x, y]; - _explosiveSprite[x, y] = pixelValue.A == 0 - ? null - : pixelValue == whitePixel; - } - } + private readonly Sprite _explosiveSprite = Sprite.FromImageFile("assets/powerup_explosive.png"); public void Draw(GamePixelGrid pixels) { - foreach (var powerUp in _entityManager.PowerUps) + foreach (var powerUp in entityManager.PowerUps) { var position = powerUp.Bounds.TopLeft; diff --git a/tanks-backend/TanksServer/Graphics/DrawTanksStep.cs b/tanks-backend/TanksServer/Graphics/DrawTanksStep.cs index 98cd14f..9bd365e 100644 --- a/tanks-backend/TanksServer/Graphics/DrawTanksStep.cs +++ b/tanks-backend/TanksServer/Graphics/DrawTanksStep.cs @@ -1,12 +1,11 @@ -using SixLabors.ImageSharp; -using SixLabors.ImageSharp.PixelFormats; using TanksServer.GameLogic; namespace TanksServer.Graphics; internal sealed class DrawTanksStep(MapEntityManager entityManager) : IDrawStep { - private readonly bool[,] _tankSprite = LoadTankSprite(); + private readonly SpriteSheet _tankSprites = + SpriteSheet.FromImageFile("assets/tank.png", MapService.TileSize, MapService.TileSize); public void Draw(GamePixelGrid pixels) { @@ -17,7 +16,8 @@ internal sealed class DrawTanksStep(MapEntityManager entityManager) : IDrawStep for (byte dy = 0; dy < MapService.TileSize; dy++) for (byte dx = 0; dx < MapService.TileSize; dx++) { - if (!TankSpriteAt(dx, dy, tank.Orientation)) + var pixel = _tankSprites[tank.Orientation][dx, dy]; + if (!pixel.HasValue || !pixel.Value) continue; var (x, y) = tankPosition.GetPixelRelative(dx, dy); @@ -26,26 +26,4 @@ internal sealed class DrawTanksStep(MapEntityManager entityManager) : IDrawStep } } } - - private bool TankSpriteAt(int dx, int dy, int tankRotation) - { - var x = tankRotation % 4 * (MapService.TileSize + 1); - var y = (int)Math.Floor(tankRotation / 4d) * (MapService.TileSize + 1); - - return _tankSprite[x + dx, y + dy]; - } - - private static bool[,] LoadTankSprite() - { - using var tankImage = Image.Load("assets/tank.png"); - var tankSprite = new bool[tankImage.Width, tankImage.Height]; - - var whitePixel = new Rgba32(byte.MaxValue, byte.MaxValue, byte.MaxValue, byte.MaxValue); - - for (var y = 0; y < tankImage.Height; y++) - for (var x = 0; x < tankImage.Width; x++) - tankSprite[x, y] = tankImage[x, y] == whitePixel; - - return tankSprite; - } } diff --git a/tanks-backend/TanksServer/Graphics/Sprite.cs b/tanks-backend/TanksServer/Graphics/Sprite.cs new file mode 100644 index 0000000..e8bef11 --- /dev/null +++ b/tanks-backend/TanksServer/Graphics/Sprite.cs @@ -0,0 +1,27 @@ +using SixLabors.ImageSharp; +using SixLabors.ImageSharp.PixelFormats; + +namespace TanksServer.Graphics; + +internal sealed class Sprite(bool?[,] data) +{ + public static Sprite FromImageFile(string filePath) + { + using var image = Image.Load(filePath); + var data = new bool?[image.Width, image.Height]; + + var whitePixel = new Rgba32(byte.MaxValue, byte.MaxValue, byte.MaxValue, byte.MaxValue); + for (var y = 0; y < image.Height; y++) + for (var x = 0; x < image.Width; x++) + { + var pixelValue = image[x, y]; + data[x, y] = pixelValue.A == 0 + ? null + : pixelValue == whitePixel; + } + + return new Sprite(data); + } + + public bool? this[int x, int y] => data[x, y]; +} diff --git a/tanks-backend/TanksServer/Graphics/SpriteSheet.cs b/tanks-backend/TanksServer/Graphics/SpriteSheet.cs new file mode 100644 index 0000000..514cfb1 --- /dev/null +++ b/tanks-backend/TanksServer/Graphics/SpriteSheet.cs @@ -0,0 +1,48 @@ +using SixLabors.ImageSharp; +using SixLabors.ImageSharp.PixelFormats; + +namespace TanksServer.Graphics; + +internal sealed class SpriteSheet(Sprite[,] sprites) +{ + public static SpriteSheet FromImageFile(string filePath, int spriteWidth, int spriteHeight) + { + using var image = Image.Load(filePath); + + var spritesPerRow = image.Width / spriteWidth; + if (image.Width % spriteWidth != 0) + throw new InvalidOperationException("invalid sprite dimensions"); + + var spritesPerColumn = image.Height / spriteHeight; + if (image.Height % spriteHeight != 0) + throw new InvalidOperationException("invalid sprite dimensions"); + + var sprites = new Sprite[spritesPerRow, spritesPerColumn]; + + var whitePixel = new Rgba32(byte.MaxValue, byte.MaxValue, byte.MaxValue, byte.MaxValue); + for (var spriteY = 0; spriteY < spritesPerColumn; spriteY++) + 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 y = spriteY * spriteHeight + dy; + + var pixelValue = image[x, y]; + data[dx, dy] = pixelValue.A == 0 + ? null + : pixelValue == whitePixel; + } + + sprites[spriteX, spriteY] = new Sprite(data); + } + + return new SpriteSheet(sprites); + } + + public Sprite this[int x, int y] => sprites[x, y]; + + public Sprite this[int index] => sprites[index % sprites.GetLength(1), index / sprites.GetLength(1)]; +} diff --git a/tanks-backend/TanksServer/assets/tank.png b/tanks-backend/TanksServer/assets/tank.png index 9210ac136f39c4cdc12f4dc662d9d9bb97e9a335..f168e545eb584cf380fecb3fd225975d874c5a96 100644 GIT binary patch literal 493 zcmeAS@N?(olHy`uVBq!ia0y~yV9;P-U{K&-V_;xNSZl?=z`(##?Bp53!NI{%!;#X# zz`(e~)5S3)qIGS!_a+uaj>9W={-0Vod%0^mhs)bFIU5b@I4thmJ$vSu#JBr@GG|9- zI#++Y`_-_Z(Bx39J^X4{J6ZaX&dF!-$zI9cPeq41KR z@{KIxqU4_WhO$37UUtlM-ZS~~Jl{JvSKgO6Dj1m%^4I!*+7iia%}bh{)Re9iKptizjbVW zeCCtU=JXpo4{ILOT<6oGZg^$uI)ejcsnIMq`Hp6+R$I4dqi_zZdtgs(Xy??M8)Fk+ zCM^Hu#dgx;==+MOt2G<{da1oU5pKE1IXds|w4k|VZYe>fA^e*-?_W8Uxu0uNo|0Q$ zop$wRj(aacw;KDbyI_Bt!O~+BSIfli`^(+;gq}?ix{xU=Ty*XD297W#smr(Dv0RQX zmB090`Kso*@?W>opQJ7QoH*U&+s32^-+I?Hyjt)pFSPq&aMLHj&9dv>Ee*;oj}SGr z3e?{+=~P+p)8se4dTvbS`A=7e%W6kW{1C0bv%}ta^3|~EBHuoO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