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 zcmV0r$uK7MD46>u`zxKINaUM%*Gh@-~4Ht zS!qLi_TBX{d`fHKSRt4B==u;}g&;bub|8Mo-Hv^6T(+pkM!2#J08XqB$$UhxancbZ z*lII!V~n3M9r+09h?zsYlhd9}+}Ww$7{w7;U{w7(|7N5awu7XDLatgIViD|CaykM2 zJDXvU}Co$But}wuMWm^Q<4#jAzCa$8f6Kn-XQH*_6iIv&0U18~9r}|0;$wBv_(yhB@V-Ih|Fc1C1UbyN?s3E$MpX}=1RZX!l*ek*&k2;S&bwKGhv z&_CM%IY_b!gprHir$@Y1&14eLX&n=C*2l02Rw5eH+wKI@Uv(eR=OfiA&UgCSXXIw3 z^I?}X_OW8%_KmE7>Y(~=Rg2M6g7Oiw9j@-BQf+rw6*W3hFSL@%c2nhJ>`g940yl5v jt5+Q>T9NQuFS&?6Gn3U;mlXE#00000NkvXXu0mjfLNeuJ literal 355 zcmV-p0i6DcP)N-I}PV^kM65X)YyEfd~@khkQR{&A#J#P~hiZ*t7jv^WZq=qL?AL zLd4#Sx}Q*}0dCRzq=O@FeA5Ygy^xnKMDmfO0T;H__0u!!f(en)HH3tUNeAnJg9NSu zh5JeGiv+a-3rs4j0e=B+8(3ScLH2XhNt+hC?d7O(Peg z85;mQX5HLcgVjrDVVQ-=TBg*&Mh_QVsMi*=n#{j9#WgQ_Z;YOGD_-89q#6}c`57l` zk4m1Ur|k>kh1-|fwXES5XC&XmeBXHKVrTpaFaWRY10{hZ_TT^j002ovPDHLkV1lKg Bn{xmF