Move TmpTS sprite loading into Mods.TS.

This commit is contained in:
Paul Chote
2014-10-05 16:29:30 +13:00
parent 37cedd88a2
commit 533d044755
6 changed files with 117 additions and 107 deletions

View File

@@ -1,76 +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.Collections.Generic;
using System.Drawing;
using System.IO;
using OpenRA.Graphics;
namespace OpenRA.FileFormats
{
public class TmpTSTile : ISpriteFrame
{
public Size Size { get; private set; }
public Size FrameSize { get { return Size; } }
public float2 Offset { get { return float2.Zero; } }
public byte[] Data { get; set; }
public bool DisableExportPadding { get { return false; } }
public TmpTSTile(Stream s, Size size)
{
Size = size;
// Ignore tile header for now
s.Position += 52;
Data = new byte[size.Width * size.Height];
// Unpack tile data
var width = 4;
for (var i = 0; i < size.Height; i++)
{
var start = i * size.Width + (size.Width - width) / 2;
for (var j = 0; j < width; j++)
Data[start + j] = s.ReadUInt8();
width += (i < size.Height / 2 - 1 ? 1 : -1) * 4;
}
// Ignore Z-data for now
// Ignore extra data for now
}
}
public class TmpTSReader : ISpriteSource
{
public IReadOnlyList<ISpriteFrame> Frames { get; private set; }
public TmpTSReader(Stream s)
{
var templateWidth = s.ReadUInt32();
var templateHeight = s.ReadUInt32();
var tileWidth = s.ReadInt32();
var tileHeight = s.ReadInt32();
var size = new Size(tileWidth, tileHeight);
var offsets = new uint[templateWidth * templateHeight];
for (var i = 0; i < offsets.Length; i++)
offsets[i] = s.ReadUInt32();
var tiles = new List<TmpTSTile>();
for (var i = 0; i < offsets.Length; i++)
{
s.Position = offsets[i];
tiles.Add(new TmpTSTile(s, size));
}
Frames = tiles.ToArray().AsReadOnly();
}
}
}

View File

@@ -15,32 +15,9 @@ 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, TmpTS } public enum SpriteType { Unknown, ShpD2 }
public static class SpriteSource public static class SpriteSource
{ {
static bool IsTmpTS(Stream s)
{
var start = s.Position;
s.Position += 8;
var sx = s.ReadUInt32();
var sy = s.ReadUInt32();
// Find the first frame
var offset = s.ReadUInt32();
if (offset > s.Length - 52)
{
s.Position = start;
return false;
}
s.Position = offset + 12;
var test = s.ReadUInt32();
s.Position = start;
return test == sx * sy / 2 + 52;
}
static bool IsShpD2(Stream s) static bool IsShpD2(Stream s)
{ {
var start = s.Position; var start = s.Position;
@@ -81,9 +58,6 @@ namespace OpenRA.Graphics
public static SpriteType DetectSpriteType(Stream s) public static SpriteType DetectSpriteType(Stream s)
{ {
if (IsTmpTS(s))
return SpriteType.TmpTS;
if (IsShpD2(s)) if (IsShpD2(s))
return SpriteType.ShpD2; return SpriteType.ShpD2;
@@ -95,8 +69,6 @@ namespace OpenRA.Graphics
var type = DetectSpriteType(s); var type = DetectSpriteType(s);
switch (type) switch (type)
{ {
case SpriteType.TmpTS:
return new TmpTSReader(s);
case SpriteType.ShpD2: case SpriteType.ShpD2:
return new ShpD2Reader(s); return new ShpD2Reader(s);
case SpriteType.Unknown: case SpriteType.Unknown:

View File

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

View File

@@ -56,6 +56,7 @@
<Compile Include="TiberianSunRefinery.cs" /> <Compile Include="TiberianSunRefinery.cs" />
<Compile Include="Render\WithVoxelWalkerBody.cs" /> <Compile Include="Render\WithVoxelWalkerBody.cs" />
<Compile Include="Render\WithVoxelUnloadBody.cs" /> <Compile Include="Render\WithVoxelUnloadBody.cs" />
<Compile Include="SpriteLoaders\TmpTSLoader.cs" />
</ItemGroup> </ItemGroup>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets. Other similar extension points exist, see Microsoft.Common.targets.

View File

@@ -0,0 +1,114 @@
#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.TS.SpriteLoaders
{
public class TmpTSLoader : ISpriteLoader
{
class TmpTSFrame : ISpriteFrame
{
public Size Size { get; private set; }
public Size FrameSize { get { return Size; } }
public float2 Offset { get { return float2.Zero; } }
public byte[] Data { get; set; }
public bool DisableExportPadding { get { return false; } }
public TmpTSFrame(Stream s, Size size)
{
Size = size;
// Ignore tile header for now
s.Position += 52;
Data = new byte[size.Width * size.Height];
// Unpack tile data
var width = 4;
for (var i = 0; i < size.Height; i++)
{
var start = i * size.Width + (size.Width - width) / 2;
for (var j = 0; j < width; j++)
Data[start + j] = s.ReadUInt8();
width += (i < size.Height / 2 - 1 ? 1 : -1) * 4;
}
// Ignore Z-data for now
// Ignore extra data for now
}
}
bool IsTmpTS(Stream s)
{
var start = s.Position;
s.Position += 8;
var sx = s.ReadUInt32();
var sy = s.ReadUInt32();
// Find the first frame
var offset = s.ReadUInt32();
if (offset > s.Length - 52)
{
s.Position = start;
return false;
}
s.Position = offset + 12;
var test = s.ReadUInt32();
s.Position = start;
return test == sx * sy / 2 + 52;
}
TmpTSFrame[] ParseFrames(Stream s)
{
var start = s.Position;
var templateWidth = s.ReadUInt32();
var templateHeight = s.ReadUInt32();
var tileWidth = s.ReadInt32();
var tileHeight = s.ReadInt32();
var size = new Size(tileWidth, tileHeight);
var offsets = new uint[templateWidth * templateHeight];
for (var i = 0; i < offsets.Length; i++)
offsets[i] = s.ReadUInt32();
var tiles = new TmpTSFrame[offsets.Length];
for (var i = 0; i < offsets.Length; i++)
{
s.Position = offsets[i];
tiles[i] = new TmpTSFrame(s, size);
}
s.Position = start;
return tiles;
}
public bool TryParseSprite(Stream s, out ISpriteFrame[] frames)
{
if (!IsTmpTS(s))
{
frames = null;
return false;
}
frames = ParseFrames(s);
return true;
}
}
}

View File

@@ -230,4 +230,4 @@ LuaScripts:
SupportsMapsFrom: ts SupportsMapsFrom: ts
SpriteFormats: ShpTD, ShpTS SpriteFormats: ShpTS, TmpTS, ShpTD