Define a consistent interface for sprite loading. Fixes #4176.

This commit is contained in:
Paul Chote
2013-11-29 18:57:14 +13:00
parent 20a6c75ba4
commit f92ce8bf51
12 changed files with 323 additions and 183 deletions

View File

@@ -8,8 +8,10 @@
*/
#endregion
using System.Collections;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using OpenRA.FileFormats;
using OpenRA.Traits;
@@ -17,11 +19,11 @@ namespace OpenRA.Editor
{
static class RenderUtils
{
static Bitmap RenderShp(ShpReader shp, Palette p)
static Bitmap RenderShp(ISpriteSource shp, Palette p)
{
var frame = shp[0];
var frame = shp.Frames.First();
var bitmap = new Bitmap(shp.Width, shp.Height, PixelFormat.Format8bppIndexed);
var bitmap = new Bitmap(frame.Size.Width, frame.Size.Height, PixelFormat.Format8bppIndexed);
bitmap.Palette = p.AsSystemPalette();
@@ -33,9 +35,9 @@ namespace OpenRA.Editor
byte* q = (byte*)data.Scan0.ToPointer();
var stride2 = data.Stride;
for (var i = 0; i < shp.Width; i++)
for (var j = 0; j < shp.Height; j++)
q[j * stride2 + i] = frame.Image[i + shp.Width * j];
for (var i = 0; i < frame.Size.Width; i++)
for (var j = 0; j < frame.Size.Height; j++)
q[j * stride2 + i] = frame.Data[i + frame.Size.Width * j];
}
bitmap.UnlockBits(data);
@@ -78,10 +80,11 @@ namespace OpenRA.Editor
var image = info.SpriteNames[0];
using (var s = FileSystem.OpenWithExts(image, exts))
{
var shp = new ShpReader(s);
var frame = shp[shp.ImageCount - 1];
// TODO: Do this properly
var shp = new ShpReader(s) as ISpriteSource;
var frame = shp.Frames.Last();
var bitmap = new Bitmap(shp.Width, shp.Height, PixelFormat.Format8bppIndexed);
var bitmap = new Bitmap(frame.Size.Width, frame.Size.Height, PixelFormat.Format8bppIndexed);
bitmap.Palette = p.AsSystemPalette();
var data = bitmap.LockBits(bitmap.Bounds(),
ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
@@ -91,13 +94,13 @@ namespace OpenRA.Editor
byte* q = (byte*)data.Scan0.ToPointer();
var stride = data.Stride;
for (var i = 0; i < shp.Width; i++)
for (var j = 0; j < shp.Height; j++)
q[j * stride + i] = frame.Image[i + shp.Width * j];
for (var i = 0; i < frame.Size.Width; i++)
for (var j = 0; j < frame.Size.Height; j++)
q[j * stride + i] = frame.Data[i + frame.Size.Width * j];
}
bitmap.UnlockBits(data);
return new ResourceTemplate { Bitmap = bitmap, Info = info, Value = shp.ImageCount - 1 };
return new ResourceTemplate { Bitmap = bitmap, Info = info, Value = shp.Frames.Count() - 1 };
}
}
}