Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Adding separate high and low image and center point image generation …
…to SpriteSheetGen.
  • Loading branch information
MainMemory committed May 28, 2016
1 parent 270d212 commit ac9c86d
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 37 deletions.
88 changes: 88 additions & 0 deletions SonLVLAPI/Extensions.cs
Expand Up @@ -308,6 +308,94 @@ public static unsafe bool FastArrayEqual(this char[] arr1, char[] arr2)
return FastArrayEqualInternal(fp1, fp2, arr1.Length * 2);
}

private static unsafe bool FastArrayEqualInternal(void* fp1, ulong value, int length)
{
ulong* lp1 = (ulong*)fp1;
int longlen = length / 8;
for (int i = 0; i < longlen; i++)
if (*lp1++ != value) return false;
if ((length & 7) != 0)
{
byte* bp1 = (byte*)lp1;
if ((length & 4) == 4)
if (*(uint*)bp1 != (uint)value)
return false;
else
bp1 += 4;
if ((length & 2) == 2)
if (*(ushort*)bp1 != (ushort)value)
return false;
else
bp1 += 2;
if ((length & 1) == 1)
return *bp1 == (byte)value;
}
return true;
}

public static unsafe bool FastArrayEqual(this byte[] arr1, byte value)
{
ulong longval = (ulong)value;
longval |= longval << 8;
longval |= longval << 16;
longval |= longval << 32;
fixed (byte* fp1 = arr1)
return FastArrayEqualInternal(fp1, longval, arr1.Length);
}

public static unsafe bool FastArrayEqual(this sbyte[] arr1, sbyte value)
{
ulong longval = (ulong)(byte)value;
longval |= longval << 8;
longval |= longval << 16;
longval |= longval << 32;
fixed (sbyte* fp1 = arr1)
return FastArrayEqualInternal(fp1, longval, arr1.Length);
}

public static unsafe bool FastArrayEqual(this ushort[] arr1, ushort value)
{
ulong longval = (ulong)value;
longval |= longval << 16;
longval |= longval << 32;
fixed (ushort* fp1 = arr1)
return FastArrayEqualInternal(fp1, longval, arr1.Length * 2);
}

public static unsafe bool FastArrayEqual(this short[] arr1, short value)
{
ulong longval = (ulong)(ushort)value;
longval |= longval << 16;
longval |= longval << 32;
fixed (short* fp1 = arr1)
return FastArrayEqualInternal(fp1, longval, arr1.Length * 2);
}

public static unsafe bool FastArrayEqual(this uint[] arr1, uint value)
{
ulong longval = (ulong)value;
longval |= longval << 32;
fixed (uint* fp1 = arr1)
return FastArrayEqualInternal(fp1, longval, arr1.Length * 4);
}

public static unsafe bool FastArrayEqual(this int[] arr1, int value)
{
ulong longval = (ulong)(uint)value;
longval |= longval << 32;
fixed (int* fp1 = arr1)
return FastArrayEqualInternal(fp1, longval, arr1.Length * 4);
}

public static unsafe bool FastArrayEqual(this char[] arr1, char value)
{
ulong longval = (ulong)value;
longval |= longval << 16;
longval |= longval << 32;
fixed (char* fp1 = arr1)
return FastArrayEqualInternal(fp1, longval, arr1.Length * 2);
}

public static bool ListEqual<T>(this IList<T> lst1, IList<T> lst2)
{
if (lst1 == lst2) return true;
Expand Down
6 changes: 3 additions & 3 deletions SonLVLAPI/LevelData.cs
Expand Up @@ -2581,13 +2581,13 @@ public static Sprite[] MapFrameToBmp(byte[] art, MappingsFrame map, DPLCFrame dp
bottom = Math.Max(map[i].Y + (map[i].Height * 8), bottom);
}
Point offset = new Point(left, top);
Sprite[] spr = new Sprite[] { new Sprite(), new Sprite() };
BitmapBits[] img = new BitmapBits[] { new BitmapBits(right - left, bottom - top), new BitmapBits(right - left, bottom - top) };
for (int i = map.TileCount - 1; i >= 0; i--)
{
int pr = map[i].Tile.Priority ? 1 : 0;
spr[pr] = new Sprite(spr[pr], MapTileToBmp(art, map[i], startpal));
img[pr].DrawSprite(MapTileToBmp(art, map[i], startpal), -left, -top);
}
return spr;
return new Sprite[] { new Sprite(img[0], offset), new Sprite(img[1],offset) };
}

public static Sprite MapTileToBmp(byte[] art, MappingsTile map, int startpal)
Expand Down
106 changes: 72 additions & 34 deletions SpriteSheetGen/Program.cs
Expand Up @@ -86,8 +86,8 @@ static void Main(string[] args)
art = tiles.ToArray();
tiles.Clear();
byte[] tmp = null;
using (Bitmap bmp = new Bitmap(1, 1, PixelFormat.Format8bppIndexed))
palette = bmp.Palette;
using (Bitmap palbmp = new Bitmap(1, 1, PixelFormat.Format8bppIndexed))
palette = palbmp.Palette;
for (int i = 0; i < 256; i++)
palette.Entries[i] = Color.Black;
foreach (PaletteInfo palent in spriteInfo.Palette)
Expand Down Expand Up @@ -132,57 +132,76 @@ static void Main(string[] args)
}
else
dplc = null;
List<Sprite> sprites = new List<Sprite>(map.Count);
List<BitmapBits> spritesLow = new List<BitmapBits>(map.Count);
List<BitmapBits> spritesHigh = new List<BitmapBits>(map.Count);
List<BitmapBits> spritesMerged = new List<BitmapBits>(map.Count);
List<Point> offsets = new List<Point>(map.Count);
List<Point> centers = new List<Point>(map.Count);
for (int i = 0; i < map.Count; i++)
{
if (map[i].TileCount == 0)
{
//File.AppendAllText("log.txt", "Frame " + i + " empty.\r\n");
continue;
}
Sprite spr;
Sprite[] spr;
if (dplc != null)
spr = new Sprite(LevelData.MapFrameToBmp(art, map[i], dplc[i], spriteInfo.StartPalette));
spr = LevelData.MapFrameToBmp(art, map[i], dplc[i], spriteInfo.StartPalette);
else
spr = new Sprite(LevelData.MapFrameToBmp(art, map[i], spriteInfo.StartPalette));
for (int _x = 0; _x < spr.Width; _x++)
for (int _y = 0; _y < spr.Height; _y++)
if (spr.Image[_x, _y] != 0)
spr = LevelData.MapFrameToBmp(art, map[i], spriteInfo.StartPalette);
BitmapBits sprLow = spr[0].Image;
BitmapBits sprHigh = spr[1].Image;
BitmapBits sprMerged = new BitmapBits(sprLow);
sprMerged.DrawBitmapComposited(sprHigh, 0, 0);
centers.Add(new Point(-spr[0].X, -spr[0].Y));
for (int _x = 0; _x < sprMerged.Width; _x++)
for (int _y = 0; _y < sprMerged.Height; _y++)
if (sprMerged[_x, _y] != 0)
{
spr.Image = spr.Image.GetSection(_x, 0, spr.Width - _x, spr.Height);
sprMerged = sprMerged.GetSection(_x, 0, sprMerged.Width - _x, sprMerged.Height);
sprLow = sprLow.GetSection(_x, 0, sprLow.Width - _x, sprLow.Height);
sprHigh = sprHigh.GetSection(_x, 0, sprHigh.Width - _x, sprHigh.Height);
goto checkright;
}
checkright:
for (int _x = spr.Width - 1; _x >= 0; _x--)
for (int _y = 0; _y < spr.Height; _y++)
if (spr.Image[_x, _y] != 0)
for (int _x = sprMerged.Width - 1; _x >= 0; _x--)
for (int _y = 0; _y < sprMerged.Height; _y++)
if (sprMerged[_x, _y] != 0)
{
spr.Image = spr.Image.GetSection(0, 0, _x + 1, spr.Height);
sprMerged = sprMerged.GetSection(0, 0, _x + 1, sprMerged.Height);
sprLow = sprLow.GetSection(0, 0, _x + 1, sprLow.Height);
sprHigh = sprHigh.GetSection(0, 0, _x + 1, sprHigh.Height);
goto checktop;
}
checktop:
for (int _y = 0; _y < spr.Height; _y++)
for (int _x = 0; _x < spr.Width; _x++)
if (spr.Image[_x, _y] != 0)
for (int _y = 0; _y < sprMerged.Height; _y++)
for (int _x = 0; _x < sprMerged.Width; _x++)
if (sprMerged[_x, _y] != 0)
{
spr.Image = spr.Image.GetSection(0, _y, spr.Width, spr.Height - _y);
sprMerged = sprMerged.GetSection(0, _y, sprMerged.Width, sprMerged.Height - _y);
sprLow = sprLow.GetSection(0, _y, sprLow.Width, sprLow.Height - _y);
sprHigh = sprHigh.GetSection(0, _y, sprHigh.Width, sprHigh.Height - _y);
goto checkbottom;
}
checkbottom:
for (int _y = spr.Height - 1; _y >= 0; _y--)
for (int _x = 0; _x < spr.Width; _x++)
if (spr.Image[_x, _y] != 0)
for (int _y = sprMerged.Height - 1; _y >= 0; _y--)
for (int _x = 0; _x < sprMerged.Width; _x++)
if (sprMerged[_x, _y] != 0)
{
spr.Image = spr.Image.GetSection(0, 0, spr.Width, _y + 1);
sprMerged = sprMerged.GetSection(0, 0, sprMerged.Width, _y + 1);
sprLow = sprLow.GetSection(0, 0, sprLow.Width, _y + 1);
sprHigh = sprHigh.GetSection(0, 0, sprHigh.Width, _y + 1);
goto checkdone;
}
checkdone:
sprites.Add(spr);
spritesMerged.Add(sprMerged);
spritesLow.Add(sprLow);
spritesHigh.Add(sprHigh);
}
if (gridsize == 0)
{
for (int i = 0; i < sprites.Count; i++)
gridsize = Math.Max(gridsize, Math.Max(sprites[i].Width, sprites[i].Height));
for (int i = 0; i < spritesMerged.Count; i++)
gridsize = Math.Max(gridsize, Math.Max(spritesMerged[i].Width, spritesMerged[i].Height));
if (!fixedwidth)
width = (padding * 2 + gridsize) * columns;
}
Expand All @@ -191,9 +210,10 @@ static void Main(string[] args)
int height = 0;
int rowcnt = 0;
int rowheight = 0;
for (int i = 0; i < sprites.Count; i++)
for (int i = 0; i < spritesMerged.Count; i++)
{
Sprite spr = sprites[i];
BitmapBits spr = spritesMerged[i];
Point off;
if (gridsize == -1)
{
if (fixedwidth && x + spr.Width + padding > width)
Expand All @@ -202,7 +222,8 @@ static void Main(string[] args)
y += rowheight;
rowheight = 0;
}
spr.Offset = new System.Drawing.Point(x, y);
off = new System.Drawing.Point(x, y);
centers[i] = new Point(centers[i].X + off.X, centers[i].Y + off.Y);
if (!fixedwidth)
width = Math.Max(width, x + spr.Width + padding);
height = Math.Max(height, y + spr.Height + padding);
Expand All @@ -225,7 +246,8 @@ static void Main(string[] args)
x = padding;
y += gridsize + 2 * padding;
}
spr.Offset = new System.Drawing.Point(x + (gridsize - spr.Width) / 2, y + (gridsize - spr.Height) / 2);
off = new Point(x + (gridsize - spr.Width) / 2, y + (gridsize - spr.Height) / 2);
centers[i] = new Point(centers[i].X + off.X, centers[i].Y + off.Y);
height = Math.Max(height, y + gridsize + padding);
if (!fixedwidth && ++rowcnt == columns)
{
Expand All @@ -236,11 +258,20 @@ static void Main(string[] args)
else
x += gridsize + 2 * padding;
}
sprites[i] = spr;
offsets.Add(off);
}
BitmapBits bmp2 = new BitmapBits(width, height);
foreach (Sprite spr in sprites)
bmp2.DrawSprite(spr, 0, 0);
BitmapBits bmpMerged = new BitmapBits(width, height);
for (int i = 0; i < spritesMerged.Count; i++)
bmpMerged.DrawBitmap(spritesMerged[i], offsets[i]);
BitmapBits bmpLow = new BitmapBits(width, height);
for (int i = 0; i < spritesLow.Count; i++)
bmpLow.DrawBitmap(spritesLow[i], offsets[i]);
BitmapBits bmpHigh = new BitmapBits(width, height);
for (int i = 0; i < spritesHigh.Count; i++)
bmpHigh.DrawBitmap(spritesHigh[i], offsets[i]);
BitmapBits bmpCenter = new BitmapBits(width, height);
for (int i = 0; i < centers.Count; i++)
bmpCenter[centers[i].X, centers[i].Y] = 1;
Console.Write("Save as: ");
if (getopt.Optind + 1 >= args.Length)
filename = Console.ReadLine();
Expand All @@ -249,7 +280,14 @@ static void Main(string[] args)
filename = args[getopt.Optind + 1];
Console.WriteLine(filename);
}
bmp2.ToBitmap(palette).Save(filename);
filename = Path.GetFullPath(filename);
bmpMerged.ToBitmap(palette).Save(filename);
if (!bmpLow.Bits.FastArrayEqual(0) && !bmpHigh.Bits.FastArrayEqual(0))
{
bmpLow.ToBitmap(palette).Save(Path.Combine(Path.GetDirectoryName(filename), Path.GetFileNameWithoutExtension(filename) + "_low" + Path.GetExtension(filename)));
bmpHigh.ToBitmap(palette).Save(Path.Combine(Path.GetDirectoryName(filename), Path.GetFileNameWithoutExtension(filename) + "_high" + Path.GetExtension(filename)));
}
bmpCenter.ToBitmap1bpp(Color.Black, Color.White).Save(Path.Combine(Path.GetDirectoryName(filename), Path.GetFileNameWithoutExtension(filename) + "_center" + Path.GetExtension(filename)));
}
}

Expand Down

0 comments on commit ac9c86d

Please sign in to comment.