Move TmpRA sprite loading into Mods.Common.

This commit is contained in:
Paul Chote
2014-10-05 15:49:34 +13:00
parent ca38193264
commit 997c79130a
8 changed files with 107 additions and 74 deletions

View File

@@ -1,51 +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 TmpRAReader : ISpriteSource
{
public IReadOnlyList<ISpriteFrame> Frames { get; private set; }
public TmpRAReader(Stream s)
{
var width = s.ReadUInt16();
var height = s.ReadUInt16();
var size = new Size(width, height);
s.Position += 12;
var imgStart = s.ReadUInt32();
s.Position += 8;
var indexEnd = s.ReadInt32();
s.Position += 4;
var indexStart = s.ReadInt32();
s.Position = indexStart;
var count = indexEnd - indexStart;
var tiles = new TmpTile[count];
Frames = tiles.AsReadOnly();
var tilesIndex = 0;
foreach (var b in s.ReadBytes(count))
{
if (b != 255)
{
s.Position = imgStart + b * width * height;
tiles[tilesIndex++] = new TmpTile(s.ReadBytes(width * height), size);
}
else
tiles[tilesIndex++] = new TmpTile(null, size);
}
}
}
}

View File

@@ -15,22 +15,9 @@ using OpenRA.FileFormats;
namespace OpenRA.Graphics
{
// TODO: Most of this should be moved into the format parsers themselves.
public enum SpriteType { Unknown, ShpD2, TmpTD, TmpRA, TmpTS }
public enum SpriteType { Unknown, ShpD2, TmpTD, TmpTS }
public static class SpriteSource
{
static bool IsTmpRA(Stream s)
{
var start = s.Position;
s.Position += 20;
var a = s.ReadUInt32();
s.Position += 2;
var b = s.ReadUInt16();
s.Position = start;
return a == 0 && b == 0x2c73;
}
static bool IsTmpTD(Stream s)
{
var start = s.Position;
@@ -106,9 +93,6 @@ namespace OpenRA.Graphics
public static SpriteType DetectSpriteType(Stream s)
{
if (IsTmpRA(s))
return SpriteType.TmpRA;
if (IsTmpTD(s))
return SpriteType.TmpTD;
@@ -126,8 +110,6 @@ namespace OpenRA.Graphics
var type = DetectSpriteType(s);
switch (type)
{
case SpriteType.TmpRA:
return new TmpRAReader(s);
case SpriteType.TmpTD:
return new TmpTDReader(s);
case SpriteType.TmpTS:

View File

@@ -278,7 +278,6 @@
<Compile Include="FileFormats\HvaReader.cs" />
<Compile Include="FileFormats\PngLoader.cs" />
<Compile Include="FileFormats\ShpD2Reader.cs" />
<Compile Include="FileFormats\TmpRAReader.cs" />
<Compile Include="FileFormats\TmpTDReader.cs" />
<Compile Include="FileFormats\TmpTSReader.cs" />
<Compile Include="FileFormats\VqaReader.cs" />

View File

@@ -115,6 +115,7 @@
<Compile Include="World\SmudgeLayer.cs" />
<Compile Include="SpriteLoaders\ShpTDLoader.cs" />
<Compile Include="SpriteLoaders\ShpTSLoader.cs" />
<Compile Include="SpriteLoaders\TmpRALoader.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>

View File

@@ -0,0 +1,102 @@
#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;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using OpenRA.FileFormats;
using OpenRA.Graphics;
namespace OpenRA.Mods.Common.SpriteLoaders
{
public class TmpRALoader : ISpriteLoader
{
class TmpRAFrame : ISpriteFrame
{
public Size Size { get; private set; }
public Size FrameSize { get; private set; }
public float2 Offset { get { return float2.Zero; } }
public byte[] Data { get; set; }
public bool DisableExportPadding { get { return false; } }
public TmpRAFrame(byte[] data, Size size)
{
FrameSize = size;
Data = data;
if (data == null)
Data = new byte[0];
else
Size = size;
}
}
bool IsTmpRA(Stream s)
{
var start = s.Position;
s.Position += 20;
var a = s.ReadUInt32();
s.Position += 2;
var b = s.ReadUInt16();
s.Position = start;
return a == 0 && b == 0x2c73;
}
TmpRAFrame[] ParseFrames(Stream s)
{
var start = s.Position;
var width = s.ReadUInt16();
var height = s.ReadUInt16();
var size = new Size(width, height);
s.Position += 12;
var imgStart = s.ReadUInt32();
s.Position += 8;
var indexEnd = s.ReadInt32();
s.Position += 4;
var indexStart = s.ReadInt32();
s.Position = indexStart;
var count = indexEnd - indexStart;
var tiles = new TmpRAFrame[count];
var tilesIndex = 0;
foreach (var b in s.ReadBytes(count))
{
if (b != 255)
{
s.Position = imgStart + b * width * height;
tiles[tilesIndex++] = new TmpRAFrame(s.ReadBytes(width * height), size);
}
else
tiles[tilesIndex++] = new TmpRAFrame(null, size);
}
s.Position = start;
return tiles;
}
public bool TryParseSprite(Stream s, out ISpriteFrame[] frames)
{
if (!IsTmpRA(s))
{
frames = null;
return false;
}
frames = ParseFrames(s);
return true;
}
}
}

View File

@@ -211,4 +211,4 @@ Missions:
SupportsMapsFrom: cnc
SpriteFormats: ShpTD, ShpTS
SpriteFormats: ShpTD, ShpTS, TmpRA

View File

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

View File

@@ -208,4 +208,4 @@ Missions:
SupportsMapsFrom: ra
SpriteFormats: ShpTD, ShpTS
SpriteFormats: ShpTD, TmpRA, ShpTS