Skip to content

Commit

Permalink
Extract spritesheet generation methods
Browse files Browse the repository at this point in the history
  • Loading branch information
tvasenin committed Nov 17, 2017
1 parent 6c3dcd9 commit cc74537
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions Pre2/AssetConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,36 +153,46 @@ private static void UnpackTrk(string resource)
File.WriteAllBytes(destFilename, data);
}

private static void GenerateSpriteSet(byte[] pal)
private static SpriteInfo GetSpritesheetInfo(SpriteData[] entries)
{
int spritesheetW = 0;
int spritesheetH = 0;
foreach (SpriteData entry in SpriteSetEntries)
SpriteInfo info = new SpriteInfo();
foreach (SpriteData entry in entries)
{
int localW = entry.X + entry.W;
int localH = entry.Y + entry.H;
if (spritesheetW < localW) { spritesheetW = localW; }
if (spritesheetH < localH) { spritesheetH = localH; }
if (info.W < localW) { info.W = localW; }
if (info.H < localH) { info.H = localH; }
}
return info;
}

byte[][] image = new byte[spritesheetH][];
for (var i = 0; i < image.Length; i++) { image[i] = new byte[spritesheetW]; }
private static byte[][] GenerateSpriteSheetImage(byte[][] sprites, SpriteInfo spriteSheetInfo, SpriteData[] entries)
{
byte[][] image = new byte[spriteSheetInfo.H][];
for (var i = 0; i < image.Length; i++) { image[i] = new byte[spriteSheetInfo.W]; }

for (var i = 0; i < SpriteSetEntries.Length; i++)
{
SpriteData entry = SpriteSetEntries[i];
byte[] sprite = SpriteImages[i];
SpriteData entry = entries[i];
byte[] sprite = sprites[i];
for (var spriteLine = 0; spriteLine < entry.H; spriteLine++)
{
int targetLine = entry.Y + spriteLine;
Array.Copy(sprite, spriteLine * entry.W, image[targetLine], entry.X, entry.W);
}
}
return image;
}

private static void GenerateSpriteSet(byte[] pal)
{
SpriteInfo spriteSheetInfo = GetSpritesheetInfo(SpriteSetEntries);
byte[][] image = GenerateSpriteSheetImage(SpriteImages, spriteSheetInfo, SpriteSetEntries);

using (FileStream outPng = File.Create(Path.Combine(CacheDir, "sprites.png")))
{
int numPaletteEntries = pal.Length / 3;
ImageInfo imiPng = new ImageInfo(spritesheetW, spritesheetH, 8, false, false, true);
ImageInfo imiPng = new ImageInfo(spriteSheetInfo.W, spriteSheetInfo.H, 8, false, false, true);
PngWriter pngw = new PngWriter(outPng, imiPng);
PngChunkPLTE palette = pngw.GetMetadata().CreatePLTEChunk();
FillPalette(palette, numPaletteEntries, pal);
Expand Down

0 comments on commit cc74537

Please sign in to comment.