Define a consistent interface for sprite loading. Fixes #4176.

This commit is contained in:
Paul Chote
2013-11-29 18:57:14 +13:00
parent 20a6c75ba4
commit f92ce8bf51
12 changed files with 323 additions and 183 deletions

View File

@@ -15,17 +15,17 @@ using System.Linq;
namespace OpenRA.FileFormats
{
public class TSImageHeader
class FrameHeader : ISpriteFrame
{
public readonly Size Size;
public readonly float2 Offset;
public Size Size { get; private set; }
public Size FrameSize { get; private set; }
public float2 Offset { get; private set; }
public byte[] Data { get; set; }
public readonly uint FileOffset;
public readonly byte Format;
public byte[] Image;
public TSImageHeader(Stream stream, Size frameSize)
public FrameHeader(Stream stream, Size frameSize)
{
var x = stream.ReadUInt16();
var y = stream.ReadUInt16();
@@ -34,6 +34,7 @@ namespace OpenRA.FileFormats
Offset = new float2(x + 0.5f * (width - frameSize.Width), y + 0.5f * (height - frameSize.Height));
Size = new Size(width, height);
FrameSize = frameSize;
Format = stream.ReadUInt8();
stream.Position += 11;
@@ -41,13 +42,13 @@ namespace OpenRA.FileFormats
}
}
public class ShpTSReader
public class ShpTSReader : ISpriteSource
{
public readonly int ImageCount;
public readonly Size Size;
readonly List<TSImageHeader> frames = new List<TSImageHeader>();
public IEnumerable<TSImageHeader> Frames { get { return frames; } }
readonly List<FrameHeader> frames = new List<FrameHeader>();
public IEnumerable<ISpriteFrame> Frames { get { return frames.Cast<ISpriteFrame>(); } }
public ShpTSReader(Stream stream)
{
@@ -58,7 +59,7 @@ namespace OpenRA.FileFormats
ImageCount = stream.ReadUInt16();
for (var i = 0; i < ImageCount; i++)
frames.Add(new TSImageHeader(stream, Size));
frames.Add(new FrameHeader(stream, Size));
for (var i = 0; i < ImageCount; i++)
{
@@ -70,23 +71,23 @@ namespace OpenRA.FileFormats
// Uncompressed
if (f.Format == 1 || f.Format == 0)
f.Image = stream.ReadBytes(f.Size.Width * f.Size.Height);
f.Data = stream.ReadBytes(f.Size.Width * f.Size.Height);
// Uncompressed scanlines
else if (f.Format == 2)
{
f.Image = new byte[f.Size.Width * f.Size.Height];
f.Data = new byte[f.Size.Width * f.Size.Height];
for (var j = 0; j < f.Size.Height; j++)
{
var length = stream.ReadUInt16() - 2;
stream.Read(f.Image, f.Size.Width * j, length);
stream.Read(f.Data, f.Size.Width * j, length);
}
}
// RLE-zero compressed scanlines
else if (f.Format == 3)
{
f.Image = new byte[f.Size.Width * f.Size.Height];
f.Data = new byte[f.Size.Width * f.Size.Height];
for (var j = 0; j < f.Size.Height; j++)
{
@@ -103,7 +104,7 @@ namespace OpenRA.FileFormats
length--;
}
else
f.Image[k++] = b;
f.Data[k++] = b;
}
}
}