Move R8 sprite loading into Mods.D2k.

This commit is contained in:
Paul Chote
2014-10-05 15:42:14 +13:00
parent c798b306c4
commit ca38193264
5 changed files with 41 additions and 41 deletions

View File

@@ -15,7 +15,7 @@ using OpenRA.FileFormats;
namespace OpenRA.Graphics namespace OpenRA.Graphics
{ {
// TODO: Most of this should be moved into the format parsers themselves. // TODO: Most of this should be moved into the format parsers themselves.
public enum SpriteType { Unknown, ShpD2, TmpTD, TmpRA, TmpTS, R8 } public enum SpriteType { Unknown, ShpD2, TmpTD, TmpRA, TmpTS }
public static class SpriteSource public static class SpriteSource
{ {
static bool IsTmpRA(Stream s) static bool IsTmpRA(Stream s)
@@ -104,30 +104,8 @@ namespace OpenRA.Graphics
return b == 5 || b <= 3; return b == 5 || b <= 3;
} }
static bool IsR8(Stream s)
{
var start = s.Position;
// First byte is nonzero
if (s.ReadUInt8() == 0)
{
s.Position = start;
return false;
}
// Check the format of the first frame
s.Position = start + 25;
var d = s.ReadUInt8();
s.Position = start;
return d == 8;
}
public static SpriteType DetectSpriteType(Stream s) public static SpriteType DetectSpriteType(Stream s)
{ {
if (IsR8(s))
return SpriteType.R8;
if (IsTmpRA(s)) if (IsTmpRA(s))
return SpriteType.TmpRA; return SpriteType.TmpRA;
@@ -148,8 +126,6 @@ namespace OpenRA.Graphics
var type = DetectSpriteType(s); var type = DetectSpriteType(s);
switch (type) switch (type)
{ {
case SpriteType.R8:
return new R8Reader(s);
case SpriteType.TmpRA: case SpriteType.TmpRA:
return new TmpRAReader(s); return new TmpRAReader(s);
case SpriteType.TmpTD: case SpriteType.TmpTD:

View File

@@ -277,7 +277,6 @@
<Compile Include="FileFormats\XccLocalDatabase.cs" /> <Compile Include="FileFormats\XccLocalDatabase.cs" />
<Compile Include="FileFormats\HvaReader.cs" /> <Compile Include="FileFormats\HvaReader.cs" />
<Compile Include="FileFormats\PngLoader.cs" /> <Compile Include="FileFormats\PngLoader.cs" />
<Compile Include="FileFormats\R8Reader.cs" />
<Compile Include="FileFormats\ShpD2Reader.cs" /> <Compile Include="FileFormats\ShpD2Reader.cs" />
<Compile Include="FileFormats\TmpRAReader.cs" /> <Compile Include="FileFormats\TmpRAReader.cs" />
<Compile Include="FileFormats\TmpTDReader.cs" /> <Compile Include="FileFormats\TmpTDReader.cs" />

View File

@@ -84,6 +84,7 @@
<Compile Include="Widgets\SlidingContainerWidget.cs" /> <Compile Include="Widgets\SlidingContainerWidget.cs" />
<Compile Include="Widgets\Logic\IngameChromeLogic.cs" /> <Compile Include="Widgets\Logic\IngameChromeLogic.cs" />
<Compile Include="ChooseBuildTabOnSelect.cs" /> <Compile Include="ChooseBuildTabOnSelect.cs" />
<Compile Include="R8Loader.cs" />
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<PropertyGroup> <PropertyGroup>

View File

@@ -8,16 +8,18 @@
*/ */
#endregion #endregion
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using System.IO; using System.IO;
using System.Linq;
using OpenRA.Graphics; using OpenRA.Graphics;
namespace OpenRA.FileFormats namespace OpenRA.Mods.D2k.SpriteLoaders
{ {
public class R8Reader : ISpriteSource public class R8Loader : ISpriteLoader
{ {
class R8Image : ISpriteFrame class R8Frame : ISpriteFrame
{ {
public Size Size { get; private set; } public Size Size { get; private set; }
public Size FrameSize { get; private set; } public Size FrameSize { get; private set; }
@@ -25,7 +27,7 @@ namespace OpenRA.FileFormats
public byte[] Data { get; set; } public byte[] Data { get; set; }
public bool DisableExportPadding { get { return true; } } public bool DisableExportPadding { get { return true; } }
public R8Image(Stream s) public R8Frame(Stream s)
{ {
// Scan forward until we find some data // Scan forward until we find some data
var type = s.ReadUInt8(); var type = s.ReadUInt8();
@@ -62,19 +64,41 @@ namespace OpenRA.FileFormats
} }
} }
public IReadOnlyList<ISpriteFrame> Frames { get; private set; } bool IsR8(Stream s)
{
var start = s.Position;
public readonly int ImageCount; // First byte is nonzero
public R8Reader(Stream stream) if (s.ReadUInt8() == 0)
{ {
var frames = new List<R8Image>(); s.Position = start;
while (stream.Position < stream.Length) return false;
{
frames.Add(new R8Image(stream));
ImageCount++;
} }
Frames = frames.ToArray().AsReadOnly(); // Check the format of the first frame
s.Position = start + 25;
var d = s.ReadUInt8();
s.Position = start;
return d == 8;
}
public bool TryParseSprite(Stream s, out ISpriteFrame[] frames)
{
if (!IsR8(s))
{
frames = null;
return false;
}
var start = s.Position;
var tmp = new List<R8Frame>();
while (s.Position < s.Length)
tmp.Add(new R8Frame(s));
s.Position = start;
frames = tmp.ToArray();
return true;
} }
} }
} }

View File

@@ -189,4 +189,4 @@ LuaScripts:
SupportsMapsFrom: d2k SupportsMapsFrom: d2k
SpriteFormats: ShpTD SpriteFormats: R8, ShpTD