Move ShpTS sprite loading into Mods.Common.

This commit is contained in:
Paul Chote
2014-10-05 15:34:56 +13:00
parent d658643b73
commit c798b306c4
7 changed files with 82 additions and 69 deletions

View File

@@ -1,103 +0,0 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using System.Drawing;
using System.IO;
using OpenRA.Graphics;
namespace OpenRA.FileFormats
{
public class ShpTSReader : ISpriteSource
{
class FrameHeader : ISpriteFrame
{
public Size Size { get; private set; }
public Size FrameSize { get; private set; }
public float2 Offset { get; private set; }
public byte[] Data { get; set; }
public bool DisableExportPadding { get { return false; } }
public readonly uint FileOffset;
public readonly byte Format;
public FrameHeader(Stream stream, Size frameSize)
{
var x = stream.ReadUInt16();
var y = stream.ReadUInt16();
var width = stream.ReadUInt16();
var height = stream.ReadUInt16();
// Note: the mixed Integer / fp division is intentional, and required for calculating the correct offset.
Offset = new float2(x + width / 2 - 0.5f * frameSize.Width, y + height / 2 - 0.5f * frameSize.Height);
Size = new Size(width, height);
FrameSize = frameSize;
Format = stream.ReadUInt8();
stream.Position += 11;
FileOffset = stream.ReadUInt32();
}
}
public IReadOnlyList<ISpriteFrame> Frames { get; private set; }
public ShpTSReader(Stream stream)
{
stream.ReadUInt16();
var width = stream.ReadUInt16();
var height = stream.ReadUInt16();
var size = new Size(width, height);
var frameCount = stream.ReadUInt16();
var frames = new FrameHeader[frameCount];
Frames = frames.AsReadOnly();
for (var i = 0; i < frames.Length; i++)
frames[i] = new FrameHeader(stream, size);
for (var i = 0; i < frameCount; i++)
{
var f = frames[i];
if (f.FileOffset == 0)
continue;
stream.Position = f.FileOffset;
var frameSize = f.Size.Width * f.Size.Height;
// Uncompressed
if (f.Format == 1 || f.Format == 0)
f.Data = stream.ReadBytes(frameSize);
// Uncompressed scanlines
else if (f.Format == 2)
{
f.Data = new byte[frameSize];
for (var j = 0; j < f.Size.Height; j++)
{
var length = stream.ReadUInt16() - 2;
var offset = f.Size.Width * j;
stream.ReadBytes(f.Data, offset, length);
}
}
// RLE-zero compressed scanlines
else if (f.Format == 3)
{
f.Data = new byte[frameSize];
for (var j = 0; j < f.Size.Height; j++)
{
var length = stream.ReadUInt16() - 2;
var offset = f.Size.Width * j;
Format2.DecodeInto(stream.ReadBytes(length), f.Data, offset);
}
}
}
}
}
}

View File

@@ -15,7 +15,7 @@ using OpenRA.FileFormats;
namespace OpenRA.Graphics
{
// TODO: Most of this should be moved into the format parsers themselves.
public enum SpriteType { Unknown, ShpTS, ShpD2, TmpTD, TmpRA, TmpTS, R8 }
public enum SpriteType { Unknown, ShpD2, TmpTD, TmpRA, TmpTS, R8 }
public static class SpriteSource
{
static bool IsTmpRA(Stream s)
@@ -66,43 +66,6 @@ namespace OpenRA.Graphics
return test == sx * sy / 2 + 52;
}
static bool IsShpTS(Stream s)
{
var start = s.Position;
// First word is zero
if (s.ReadUInt16() != 0)
{
s.Position = start;
return false;
}
// Sanity Check the image count
s.Position += 4;
var imageCount = s.ReadUInt16();
if (s.Position + 24 * imageCount > s.Length)
{
s.Position = start;
return false;
}
// Check the size and format flag
// Some files define bogus frames, so loop until we find a valid one
s.Position += 4;
ushort w, h, f = 0;
byte type;
do
{
w = s.ReadUInt16();
h = s.ReadUInt16();
type = s.ReadUInt8();
}
while (w == 0 && h == 0 && f++ < imageCount);
s.Position = start;
return type < 4;
}
static bool IsShpD2(Stream s)
{
var start = s.Position;
@@ -162,9 +125,6 @@ namespace OpenRA.Graphics
public static SpriteType DetectSpriteType(Stream s)
{
if (IsShpTS(s))
return SpriteType.ShpTS;
if (IsR8(s))
return SpriteType.R8;
@@ -188,8 +148,6 @@ namespace OpenRA.Graphics
var type = DetectSpriteType(s);
switch (type)
{
case SpriteType.ShpTS:
return new ShpTSReader(s);
case SpriteType.R8:
return new R8Reader(s);
case SpriteType.TmpRA:

View File

@@ -279,7 +279,6 @@
<Compile Include="FileFormats\PngLoader.cs" />
<Compile Include="FileFormats\R8Reader.cs" />
<Compile Include="FileFormats\ShpD2Reader.cs" />
<Compile Include="FileFormats\ShpTSReader.cs" />
<Compile Include="FileFormats\TmpRAReader.cs" />
<Compile Include="FileFormats\TmpTDReader.cs" />
<Compile Include="FileFormats\TmpTSReader.cs" />