Load terrain from any SpriteSource.

This commit is contained in:
Paul Chote
2013-11-30 17:09:05 +13:00
parent bfd50b97e2
commit 314f819940
11 changed files with 54 additions and 56 deletions

View File

@@ -165,6 +165,7 @@
<Compile Include="Surface.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="TileSetRenderer.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\OpenRA.FileFormats\OpenRA.FileFormats.csproj">

View File

@@ -13,8 +13,9 @@ using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using OpenRA.FileFormats;
namespace OpenRA.FileFormats
namespace OpenRA.Editor
{
public class TileSetRenderer
{
@@ -22,34 +23,31 @@ namespace OpenRA.FileFormats
Dictionary<ushort, List<byte[]>> templates;
public Size TileSize;
List<byte[]> LoadTemplate(string filename, string[] exts, Cache<string, ISpriteFrame[]> r8cache, int[] frames)
List<byte[]> LoadTemplate(string filename, string[] exts, Dictionary<string, ISpriteSource> sourceCache, int[] frames)
{
if (exts.Contains(".R8") && FileSystem.Exists(filename + ".R8"))
ISpriteSource source;
if (!sourceCache.ContainsKey(filename))
{
var data = new List<byte[]>();
foreach (var f in frames)
data.Add(f >= 0 ? r8cache[filename][f].Data : null);
using (var s = FileSystem.OpenWithExts(filename, exts))
source = SpriteSource.LoadSpriteSource(s, filename);
return data;
if (source.CacheWhenLoadingTileset)
sourceCache.Add(filename, source);
}
else
source = sourceCache[filename];
if (frames != null)
{
var ret = new List<byte[]>();
var srcFrames = source.Frames.ToArray();
foreach (var i in frames)
ret.Add(srcFrames[i].Data);
return ret;
}
using (var s = FileSystem.OpenWithExts(filename, exts))
{
var type = SpriteSource.DetectSpriteType(s);
ISpriteSource source;
switch (type)
{
case SpriteType.TmpTD:
source = new TmpTDReader(s);
break;
case SpriteType.TmpRA:
source = new TmpRAReader(s);
break;
default:
throw new InvalidDataException(filename + " is not a valid terrain tile");
}
return source.Frames.Select(f => f.Data).ToList();
}
return source.Frames.Select(f => f.Data).ToList();
}
public TileSetRenderer(TileSet tileset, Size tileSize)
@@ -58,9 +56,9 @@ namespace OpenRA.FileFormats
this.TileSize = tileSize;
templates = new Dictionary<ushort, List<byte[]>>();
var r8cache = new Cache<string, ISpriteFrame[]>(s => new R8Reader(FileSystem.OpenWithExts(s, ".R8")).Frames.ToArray());
var sourceCache = new Dictionary<string, ISpriteSource>();
foreach (var t in TileSet.Templates)
templates.Add(t.Key, LoadTemplate(t.Value.Image, tileset.Extensions, r8cache, t.Value.Frames));
templates.Add(t.Key, LoadTemplate(t.Value.Image, tileset.Extensions, sourceCache, t.Value.Frames));
}
public Bitmap RenderTemplate(ushort id, Palette p)

View File

@@ -63,6 +63,7 @@ namespace OpenRA.FileFormats
{
readonly List<R8Image> frames = new List<R8Image>();
public IEnumerable<ISpriteFrame> Frames { get { return frames.Cast<ISpriteFrame>(); } }
public bool CacheWhenLoadingTileset { get { return true; } }
public readonly int ImageCount;
public R8Reader(Stream stream)

View File

@@ -88,6 +88,7 @@ namespace OpenRA.FileFormats
{
List<Frame> headers = new List<Frame>();
public IEnumerable<ISpriteFrame> Frames { get { return headers.Cast<ISpriteFrame>(); } }
public bool CacheWhenLoadingTileset { get { return false; } }
public ShpD2Reader(Stream s)
{

View File

@@ -58,6 +58,7 @@ namespace OpenRA.FileFormats
{
readonly List<ImageHeader> headers = new List<ImageHeader>();
public IEnumerable<ISpriteFrame> Frames { get { return headers.Cast<ISpriteFrame>(); } }
public bool CacheWhenLoadingTileset { get { return false; } }
public readonly Size Size;
int recurseDepth = 0;

View File

@@ -46,6 +46,7 @@ namespace OpenRA.FileFormats
{
readonly List<FrameHeader> frames = new List<FrameHeader>();
public IEnumerable<ISpriteFrame> Frames { get { return frames.Cast<ISpriteFrame>(); } }
public bool CacheWhenLoadingTileset { get { return false; } }
public ShpTSReader(Stream stream)
{

View File

@@ -26,6 +26,7 @@ namespace OpenRA.FileFormats
public interface ISpriteSource
{
IEnumerable<ISpriteFrame> Frames { get; }
bool CacheWhenLoadingTileset { get; }
}
public enum SpriteType { Unknown, ShpTD, ShpTS, ShpD2, TmpTD, TmpRA, R8 }

View File

@@ -19,6 +19,7 @@ namespace OpenRA.FileFormats
{
readonly List<TmpTile> tiles = new List<TmpTile>();
public IEnumerable<ISpriteFrame> Frames { get { return tiles.Cast<ISpriteFrame>(); } }
public bool CacheWhenLoadingTileset { get { return false; } }
public TmpRAReader(Stream s)
{

View File

@@ -38,6 +38,7 @@ namespace OpenRA.FileFormats
{
readonly List<TmpTile> tiles = new List<TmpTile>();
public IEnumerable<ISpriteFrame> Frames { get { return tiles.Cast<ISpriteFrame>(); } }
public bool CacheWhenLoadingTileset { get { return false; } }
public TmpTDReader(Stream s)
{

View File

@@ -141,7 +141,6 @@
<Compile Include="StreamExts.cs" />
<Compile Include="FileFormats\WavLoader.cs" />
<Compile Include="Graphics\R8Reader.cs" />
<Compile Include="Graphics\TileSetRenderer.cs" />
<Compile Include="Keycode.cs" />
<Compile Include="Hotkey.cs" />
<Compile Include="FileSystem\FileSystem.cs" />

View File

@@ -23,38 +23,31 @@ namespace OpenRA.Graphics
Dictionary<ushort, Sprite[]> templates;
Sprite missingTile;
Sprite[] LoadTemplate(string filename, string[] exts, Cache<string, ISpriteFrame[]> r8Cache, int[] frames)
Sprite[] LoadTemplate(string filename, string[] exts, Dictionary<string, ISpriteSource> sourceCache, int[] frames)
{
if (exts.Contains(".R8") && FileSystem.Exists(filename+".R8"))
ISpriteSource source;
if (!sourceCache.ContainsKey(filename))
{
return frames.Select(f =>
{
if (f < 0)
return null;
using (var s = FileSystem.OpenWithExts(filename, exts))
source = SpriteSource.LoadSpriteSource(s, filename);
var image = r8Cache[filename][f];
return sheetBuilder.Add(image.Data, new Size(image.Size.Width, image.Size.Height));
}).ToArray();
if (source.CacheWhenLoadingTileset)
sourceCache.Add(filename, source);
}
else
source = sourceCache[filename];
if (frames != null)
{
var ret = new List<Sprite>();
var srcFrames = source.Frames.ToArray();
foreach (var i in frames)
ret.Add(sheetBuilder.Add(srcFrames[i]));
return ret.ToArray();
}
using (var s = FileSystem.OpenWithExts(filename, exts))
{
var type = SpriteSource.DetectSpriteType(s);
ISpriteSource source;
switch (type)
{
case SpriteType.TmpTD:
source = new TmpTDReader(s);
break;
case SpriteType.TmpRA:
source = new TmpRAReader(s);
break;
default:
throw new InvalidDataException(filename + " is not a valid terrain tile");
}
return source.Frames.Select(f => sheetBuilder.Add(f)).ToArray();
}
return source.Frames.Select(f => sheetBuilder.Add(f)).ToArray();
}
public Theater(TileSet tileset)
@@ -69,11 +62,11 @@ namespace OpenRA.Graphics
return new Sheet(new Size(tileset.SheetSize, tileset.SheetSize));
};
var r8Cache = new Cache<string, ISpriteFrame[]>(s => new R8Reader(FileSystem.OpenWithExts(s, ".R8")).Frames.ToArray());
var sourceCache = new Dictionary<string, ISpriteSource>();
templates = new Dictionary<ushort, Sprite[]>();
sheetBuilder = new SheetBuilder(SheetType.Indexed, allocate);
foreach (var t in tileset.Templates)
templates.Add(t.Value.Id, LoadTemplate(t.Value.Image, tileset.Extensions, r8Cache, t.Value.Frames));
templates.Add(t.Value.Id, LoadTemplate(t.Value.Image, tileset.Extensions, sourceCache, t.Value.Frames));
// 1x1px transparent tile
missingTile = sheetBuilder.Add(new byte[1], new Size(1, 1));