Use the existing SpriteLoader cache for tilesets.
This commit is contained in:
@@ -22,7 +22,7 @@ namespace OpenRA.Editor
|
|||||||
{
|
{
|
||||||
public readonly int TileSize;
|
public readonly int TileSize;
|
||||||
public TileSet TileSet;
|
public TileSet TileSet;
|
||||||
Dictionary<ushort, List<byte[]>> templates;
|
Dictionary<ushort, byte[][]> templates;
|
||||||
|
|
||||||
// Extract a square tile that the editor can render
|
// Extract a square tile that the editor can render
|
||||||
byte[] ExtractSquareTile(ISpriteFrame frame)
|
byte[] ExtractSquareTile(ISpriteFrame frame)
|
||||||
@@ -44,42 +44,19 @@ namespace OpenRA.Editor
|
|||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
List<byte[]> LoadTemplate(string filename, string[] exts, Dictionary<string, ISpriteSource> sourceCache, int[] frames)
|
|
||||||
{
|
|
||||||
ISpriteSource source;
|
|
||||||
if (!sourceCache.ContainsKey(filename))
|
|
||||||
{
|
|
||||||
using (var s = GlobalFileSystem.OpenWithExts(filename, exts))
|
|
||||||
source = SpriteSource.LoadSpriteSource(s, filename);
|
|
||||||
|
|
||||||
if (source.CacheWhenLoadingTileset)
|
|
||||||
sourceCache.Add(filename, source);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
source = sourceCache[filename];
|
|
||||||
|
|
||||||
if (frames != null)
|
|
||||||
{
|
|
||||||
var ret = new List<byte[]>();
|
|
||||||
var srcFrames = source.Frames;
|
|
||||||
foreach (var i in frames)
|
|
||||||
ret.Add(ExtractSquareTile(srcFrames[i]));
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
return source.Frames.Select(f => ExtractSquareTile(f)).ToList();
|
|
||||||
}
|
|
||||||
|
|
||||||
public TileSetRenderer(TileSet tileset, Size tileSize)
|
public TileSetRenderer(TileSet tileset, Size tileSize)
|
||||||
{
|
{
|
||||||
this.TileSet = tileset;
|
this.TileSet = tileset;
|
||||||
this.TileSize = Math.Min(tileSize.Width, tileSize.Height);
|
this.TileSize = Math.Min(tileSize.Width, tileSize.Height);
|
||||||
|
|
||||||
templates = new Dictionary<ushort, List<byte[]>>();
|
templates = new Dictionary<ushort, byte[][]>();
|
||||||
var sourceCache = new Dictionary<string, ISpriteSource>();
|
var spriteLoader = new SpriteLoader(tileset.Extensions, null);
|
||||||
foreach (var t in TileSet.Templates)
|
foreach (var t in tileset.Templates)
|
||||||
templates.Add(t.Key, LoadTemplate(t.Value.Image, tileset.Extensions, sourceCache, t.Value.Frames));
|
{
|
||||||
|
var allFrames = spriteLoader.LoadAllFrames(t.Value.Image);
|
||||||
|
var frames = t.Value.Frames != null ? t.Value.Frames.Select(f => allFrames[f]).ToArray() : allFrames;
|
||||||
|
templates.Add(t.Value.Id, frames.Select(f => ExtractSquareTile(f)).ToArray());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Bitmap RenderTemplate(ushort id, IPalette p)
|
public Bitmap RenderTemplate(ushort id, IPalette p)
|
||||||
|
|||||||
@@ -62,7 +62,6 @@ namespace OpenRA.FileFormats
|
|||||||
}
|
}
|
||||||
|
|
||||||
public IReadOnlyList<ISpriteFrame> Frames { get; private set; }
|
public IReadOnlyList<ISpriteFrame> Frames { get; private set; }
|
||||||
public bool CacheWhenLoadingTileset { get { return true; } }
|
|
||||||
|
|
||||||
public readonly int ImageCount;
|
public readonly int ImageCount;
|
||||||
public R8Reader(Stream stream)
|
public R8Reader(Stream stream)
|
||||||
|
|||||||
@@ -84,7 +84,6 @@ namespace OpenRA.FileFormats
|
|||||||
}
|
}
|
||||||
|
|
||||||
public IReadOnlyList<ISpriteFrame> Frames { get; private set; }
|
public IReadOnlyList<ISpriteFrame> Frames { get; private set; }
|
||||||
public bool CacheWhenLoadingTileset { get { return false; } }
|
|
||||||
|
|
||||||
public ShpD2Reader(Stream s)
|
public ShpD2Reader(Stream s)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -60,7 +60,6 @@ namespace OpenRA.FileFormats
|
|||||||
}
|
}
|
||||||
|
|
||||||
public IReadOnlyList<ISpriteFrame> Frames { get; private set; }
|
public IReadOnlyList<ISpriteFrame> Frames { get; private set; }
|
||||||
public bool CacheWhenLoadingTileset { get { return false; } }
|
|
||||||
public readonly Size Size;
|
public readonly Size Size;
|
||||||
|
|
||||||
int recurseDepth = 0;
|
int recurseDepth = 0;
|
||||||
|
|||||||
@@ -45,7 +45,6 @@ namespace OpenRA.FileFormats
|
|||||||
}
|
}
|
||||||
|
|
||||||
public IReadOnlyList<ISpriteFrame> Frames { get; private set; }
|
public IReadOnlyList<ISpriteFrame> Frames { get; private set; }
|
||||||
public bool CacheWhenLoadingTileset { get { return false; } }
|
|
||||||
|
|
||||||
public ShpTSReader(Stream stream)
|
public ShpTSReader(Stream stream)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -17,7 +17,6 @@ namespace OpenRA.FileFormats
|
|||||||
public class TmpRAReader : ISpriteSource
|
public class TmpRAReader : ISpriteSource
|
||||||
{
|
{
|
||||||
public IReadOnlyList<ISpriteFrame> Frames { get; private set; }
|
public IReadOnlyList<ISpriteFrame> Frames { get; private set; }
|
||||||
public bool CacheWhenLoadingTileset { get { return false; } }
|
|
||||||
|
|
||||||
public TmpRAReader(Stream s)
|
public TmpRAReader(Stream s)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -36,7 +36,6 @@ namespace OpenRA.FileFormats
|
|||||||
public class TmpTDReader : ISpriteSource
|
public class TmpTDReader : ISpriteSource
|
||||||
{
|
{
|
||||||
public IReadOnlyList<ISpriteFrame> Frames { get; private set; }
|
public IReadOnlyList<ISpriteFrame> Frames { get; private set; }
|
||||||
public bool CacheWhenLoadingTileset { get { return false; } }
|
|
||||||
|
|
||||||
public TmpTDReader(Stream s)
|
public TmpTDReader(Stream s)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -50,7 +50,6 @@ namespace OpenRA.FileFormats
|
|||||||
public class TmpTSReader : ISpriteSource
|
public class TmpTSReader : ISpriteSource
|
||||||
{
|
{
|
||||||
public IReadOnlyList<ISpriteFrame> Frames { get; private set; }
|
public IReadOnlyList<ISpriteFrame> Frames { get; private set; }
|
||||||
public bool CacheWhenLoadingTileset { get { return false; } }
|
|
||||||
|
|
||||||
public TmpTSReader(Stream s)
|
public TmpTSReader(Stream s)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ namespace OpenRA.Graphics
|
|||||||
{
|
{
|
||||||
public readonly SheetBuilder SheetBuilder;
|
public readonly SheetBuilder SheetBuilder;
|
||||||
readonly Cache<string, Sprite[]> sprites;
|
readonly Cache<string, Sprite[]> sprites;
|
||||||
|
readonly Cache<string, ISpriteFrame[]> frames;
|
||||||
readonly string[] exts;
|
readonly string[] exts;
|
||||||
|
|
||||||
public SpriteLoader(string[] exts, SheetBuilder sheetBuilder)
|
public SpriteLoader(string[] exts, SheetBuilder sheetBuilder)
|
||||||
@@ -26,17 +27,24 @@ namespace OpenRA.Graphics
|
|||||||
|
|
||||||
// Include extension-less version
|
// Include extension-less version
|
||||||
this.exts = exts.Append("").ToArray();
|
this.exts = exts.Append("").ToArray();
|
||||||
sprites = new Cache<string, Sprite[]>(CacheSpriteFrames);
|
sprites = new Cache<string, Sprite[]>(CacheSprites);
|
||||||
|
frames = new Cache<string, ISpriteFrame[]>(CacheFrames);
|
||||||
}
|
}
|
||||||
|
|
||||||
Sprite[] CacheSpriteFrames(string filename)
|
Sprite[] CacheSprites(string filename)
|
||||||
|
{
|
||||||
|
return frames[filename].Select(a => SheetBuilder.Add(a))
|
||||||
|
.ToArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
ISpriteFrame[] CacheFrames(string filename)
|
||||||
{
|
{
|
||||||
using (var stream = GlobalFileSystem.OpenWithExts(filename, exts))
|
using (var stream = GlobalFileSystem.OpenWithExts(filename, exts))
|
||||||
return SpriteSource.LoadSpriteSource(stream, filename).Frames
|
return SpriteSource.LoadSpriteSource(stream, filename).Frames
|
||||||
.Select(a => SheetBuilder.Add(a))
|
|
||||||
.ToArray();
|
.ToArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Sprite[] LoadAllSprites(string filename) { return sprites[filename]; }
|
public Sprite[] LoadAllSprites(string filename) { return sprites[filename]; }
|
||||||
|
public ISpriteFrame[] LoadAllFrames(string filename) { return frames[filename]; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,7 +25,6 @@ namespace OpenRA.Graphics
|
|||||||
public interface ISpriteSource
|
public interface ISpriteSource
|
||||||
{
|
{
|
||||||
IReadOnlyList<ISpriteFrame> Frames { get; }
|
IReadOnlyList<ISpriteFrame> Frames { get; }
|
||||||
bool CacheWhenLoadingTileset { get; }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Most of this should be moved into the format parsers themselves.
|
// TODO: Most of this should be moved into the format parsers themselves.
|
||||||
|
|||||||
@@ -22,33 +22,6 @@ namespace OpenRA.Graphics
|
|||||||
Dictionary<ushort, Sprite[]> templates;
|
Dictionary<ushort, Sprite[]> templates;
|
||||||
Sprite missingTile;
|
Sprite missingTile;
|
||||||
|
|
||||||
Sprite[] LoadTemplate(string filename, string[] exts, Dictionary<string, ISpriteSource> sourceCache, int[] frames)
|
|
||||||
{
|
|
||||||
ISpriteSource source;
|
|
||||||
if (!sourceCache.ContainsKey(filename))
|
|
||||||
{
|
|
||||||
using (var s = GlobalFileSystem.OpenWithExts(filename, exts))
|
|
||||||
source = SpriteSource.LoadSpriteSource(s, filename);
|
|
||||||
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
|
|
||||||
return source.Frames.Select(f => sheetBuilder.Add(f)).ToArray();
|
|
||||||
}
|
|
||||||
|
|
||||||
public Theater(TileSet tileset)
|
public Theater(TileSet tileset)
|
||||||
{
|
{
|
||||||
var allocated = false;
|
var allocated = false;
|
||||||
@@ -61,11 +34,17 @@ namespace OpenRA.Graphics
|
|||||||
return new Sheet(new Size(tileset.SheetSize, tileset.SheetSize), true);
|
return new Sheet(new Size(tileset.SheetSize, tileset.SheetSize), true);
|
||||||
};
|
};
|
||||||
|
|
||||||
var sourceCache = new Dictionary<string, ISpriteSource>();
|
|
||||||
templates = new Dictionary<ushort, Sprite[]>();
|
|
||||||
sheetBuilder = new SheetBuilder(SheetType.Indexed, allocate);
|
sheetBuilder = new SheetBuilder(SheetType.Indexed, allocate);
|
||||||
|
templates = new Dictionary<ushort, Sprite[]>();
|
||||||
|
|
||||||
|
// We manage the SheetBuilder ourselves, to avoid loading all of the tileset images
|
||||||
|
var spriteLoader = new SpriteLoader(tileset.Extensions, null);
|
||||||
foreach (var t in tileset.Templates)
|
foreach (var t in tileset.Templates)
|
||||||
templates.Add(t.Value.Id, LoadTemplate(t.Value.Image, tileset.Extensions, sourceCache, t.Value.Frames));
|
{
|
||||||
|
var allFrames = spriteLoader.LoadAllFrames(t.Value.Image);
|
||||||
|
var frames = t.Value.Frames != null ? t.Value.Frames.Select(f => allFrames[f]).ToArray() : allFrames;
|
||||||
|
templates.Add(t.Value.Id, frames.Select(f => sheetBuilder.Add(f)).ToArray());
|
||||||
|
}
|
||||||
|
|
||||||
// 1x1px transparent tile
|
// 1x1px transparent tile
|
||||||
missingTile = sheetBuilder.Add(new byte[1], new Size(1, 1));
|
missingTile = sheetBuilder.Add(new byte[1], new Size(1, 1));
|
||||||
|
|||||||
Reference in New Issue
Block a user