Load terrain from any SpriteSource.
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
@@ -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 }
|
||||
|
||||
@@ -1,110 +0,0 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2013 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.Drawing.Imaging;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
|
||||
namespace OpenRA.FileFormats
|
||||
{
|
||||
public class TileSetRenderer
|
||||
{
|
||||
public TileSet TileSet;
|
||||
Dictionary<ushort, List<byte[]>> templates;
|
||||
public Size TileSize;
|
||||
|
||||
List<byte[]> LoadTemplate(string filename, string[] exts, Cache<string, ISpriteFrame[]> r8cache, int[] frames)
|
||||
{
|
||||
if (exts.Contains(".R8") && FileSystem.Exists(filename + ".R8"))
|
||||
{
|
||||
var data = new List<byte[]>();
|
||||
foreach (var f in frames)
|
||||
data.Add(f >= 0 ? r8cache[filename][f].Data : null);
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
public TileSetRenderer(TileSet tileset, Size tileSize)
|
||||
{
|
||||
this.TileSet = tileset;
|
||||
this.TileSize = tileSize;
|
||||
|
||||
templates = new Dictionary<ushort, List<byte[]>>();
|
||||
var r8cache = new Cache<string, ISpriteFrame[]>(s => new R8Reader(FileSystem.OpenWithExts(s, ".R8")).Frames.ToArray());
|
||||
foreach (var t in TileSet.Templates)
|
||||
templates.Add(t.Key, LoadTemplate(t.Value.Image, tileset.Extensions, r8cache, t.Value.Frames));
|
||||
}
|
||||
|
||||
public Bitmap RenderTemplate(ushort id, Palette p)
|
||||
{
|
||||
var template = TileSet.Templates[id];
|
||||
var templateData = templates[id];
|
||||
|
||||
var bitmap = new Bitmap(TileSize.Width * template.Size.X, TileSize.Height * template.Size.Y,
|
||||
PixelFormat.Format8bppIndexed);
|
||||
|
||||
bitmap.Palette = p.AsSystemPalette();
|
||||
|
||||
var data = bitmap.LockBits(bitmap.Bounds(),
|
||||
ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
|
||||
|
||||
unsafe
|
||||
{
|
||||
var q = (byte*)data.Scan0.ToPointer();
|
||||
var stride = data.Stride;
|
||||
|
||||
for (var u = 0; u < template.Size.X; u++)
|
||||
for (var v = 0; v < template.Size.Y; v++)
|
||||
if (templateData[u + v * template.Size.X] != null)
|
||||
{
|
||||
var rawImage = templateData[u + v * template.Size.X];
|
||||
for (var i = 0; i < TileSize.Width; i++)
|
||||
for (var j = 0; j < TileSize.Height; j++)
|
||||
q[(v * TileSize.Width + j) * stride + u * TileSize.Width + i] = rawImage[i + TileSize.Width * j];
|
||||
}
|
||||
else
|
||||
{
|
||||
for (var i = 0; i < TileSize.Width; i++)
|
||||
for (var j = 0; j < TileSize.Height; j++)
|
||||
q[(v * TileSize.Width + j) * stride + u * TileSize.Width + i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
bitmap.UnlockBits(data);
|
||||
return bitmap;
|
||||
}
|
||||
|
||||
public List<byte[]> Data(ushort id)
|
||||
{
|
||||
return templates[id];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
@@ -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" />
|
||||
|
||||
Reference in New Issue
Block a user