diff options
author | Alexey 'Cluster' Avdyukhin <clusterrr@clusterrr.com> | 2022-10-25 17:16:48 +0300 |
---|---|---|
committer | Alexey 'Cluster' Avdyukhin <clusterrr@clusterrr.com> | 2022-10-25 17:16:48 +0300 |
commit | 2f99d859a6e19a17ad155b7d4522ce1cc388c2b2 (patch) | |
tree | 500a4594ba06e07abbf4b5cd806169559f1d1a53 /NesTiler/Tile.cs | |
parent | cb5930b4118b976f0134f9d2ca8e55332b976c6c (diff) |
Fixed sprites 8x16 processing
Diffstat (limited to 'NesTiler/Tile.cs')
-rw-r--r-- | NesTiler/Tile.cs | 31 |
1 files changed, 16 insertions, 15 deletions
diff --git a/NesTiler/Tile.cs b/NesTiler/Tile.cs index e20d81d..f77352a 100644 --- a/NesTiler/Tile.cs +++ b/NesTiler/Tile.cs @@ -7,34 +7,35 @@ namespace com.clusterrr.Famicom.NesTiler sealed record Tile : IEquatable<Tile>
{
public readonly byte[] Pixels;
- public readonly int Width;
+ public const int Width = 8;
public readonly int Height;
private int? hash;
private byte[] data = null;
- public Tile(byte[] data, int width, int height)
+ public Tile(byte[] data, int height)
{
- (Pixels, Width, Height) = (data, width, height);
+ (Pixels, Height) = (data, height);
}
- public byte[] GetAsTileData()
+ public byte[] GetAsPatternData()
{
if (data != null) return data;
- data = new byte[Width * Height / 8 * 2];
+ data = new byte[Height * 2]; // two bits per pixel
lock (data)
{
- int pixel = 0;
- byte bit = 7;
+ int pixel = 0; // total pixels counter
+ byte bit = 7; // bit number
for (int y = 0; y < Height; y++)
{
for (int x = 0; x < Width; x++)
{
- if ((Pixels[(y * Width) + x] & 1) != 0)
- data[(pixel / 64 * 2) + y] |= (byte)(1 << bit);
- if ((Pixels[(y * Width) + x] & 2) != 0)
- data[(pixel / 64 * 2) + y + 8] |= (byte)(1 << bit);
+ // for each pixel
+ if ((Pixels[(y * Width) + x] & 1) != 0) // check bit 0
+ data[y / 8 * 16 + (y % 8)] |= (byte)(1 << bit);
+ if ((Pixels[(y * Width) + x] & 2) != 0) // check bit 1
+ data[y / 8 * 16 + (y % 8) + 8] |= (byte)(1 << bit);
pixel++;
- bit = (byte)((byte)(bit - 1) % 8);
+ bit = (byte)((byte)(bit - 1) % 8); // decrease bit number, wrap around if need
}
}
}
@@ -43,8 +44,8 @@ namespace com.clusterrr.Famicom.NesTiler public bool Equals(Tile other)
{
- var data1 = GetAsTileData();
- var data2 = other.GetAsTileData();
+ var data1 = GetAsPatternData();
+ var data2 = other.GetAsPatternData();
return Enumerable.SequenceEqual(data1, data2);
}
@@ -52,7 +53,7 @@ namespace com.clusterrr.Famicom.NesTiler {
if (hash != null) return hash.Value;
var crc = new Crc32();
- crc.Append(GetAsTileData());
+ crc.Append(GetAsPatternData());
var hashBytes = crc.GetCurrentHash();
hash = BitConverter.ToInt32(hashBytes, 0);
return hash.Value;
|