Changes ISpriteSource.Frames to be of type IReadOnlyList<ISpriteFrame>.
- Updated implementations to return a ReadOnlyList around an array (to reduce wasted memory from exposing lists or lazy enumerators around lists). - Protect non-public ISpriteFrame classes by making them inner classes to prevent casting. - Added an AsReadOnly extension method for lists.
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2013 The OpenRA Developers (see AUTHORS)
|
||||
* 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,
|
||||
@@ -15,63 +15,66 @@ using OpenRA.Graphics;
|
||||
|
||||
namespace OpenRA.FileFormats
|
||||
{
|
||||
class R8Image : ISpriteFrame
|
||||
{
|
||||
public Size Size { get; private set; }
|
||||
public Size FrameSize { get; private set; }
|
||||
public float2 Offset { get; private set; }
|
||||
public byte[] Data { get; set; }
|
||||
|
||||
public R8Image(Stream s)
|
||||
{
|
||||
// Scan forward until we find some data
|
||||
var type = s.ReadUInt8();
|
||||
while (type == 0)
|
||||
type = s.ReadUInt8();
|
||||
|
||||
var width = s.ReadInt32();
|
||||
var height = s.ReadInt32();
|
||||
var x = s.ReadInt32();
|
||||
var y = s.ReadInt32();
|
||||
|
||||
Size = new Size(width, height);
|
||||
Offset = new int2(width/2 - x, height/2 - y);
|
||||
|
||||
/*var imageOffset = */s.ReadInt32();
|
||||
var paletteOffset = s.ReadInt32();
|
||||
var bpp = s.ReadUInt8();
|
||||
if (bpp != 8)
|
||||
throw new InvalidDataException("Error: {0} bits per pixel are not supported.".F(bpp));
|
||||
|
||||
var frameHeight = s.ReadUInt8();
|
||||
var frameWidth = s.ReadUInt8();
|
||||
FrameSize = new Size(frameWidth, frameHeight);
|
||||
|
||||
// Skip alignment byte
|
||||
s.ReadUInt8();
|
||||
|
||||
Data = s.ReadBytes(width*height);
|
||||
|
||||
// Ignore palette
|
||||
if (type == 1 && paletteOffset != 0)
|
||||
s.Seek(520, SeekOrigin.Current);
|
||||
}
|
||||
}
|
||||
|
||||
public class R8Reader : ISpriteSource
|
||||
{
|
||||
readonly List<ISpriteFrame> frames = new List<ISpriteFrame>();
|
||||
public IEnumerable<ISpriteFrame> Frames { get { return frames; } }
|
||||
class R8Image : ISpriteFrame
|
||||
{
|
||||
public Size Size { get; private set; }
|
||||
public Size FrameSize { get; private set; }
|
||||
public float2 Offset { get; private set; }
|
||||
public byte[] Data { get; set; }
|
||||
|
||||
public R8Image(Stream s)
|
||||
{
|
||||
// Scan forward until we find some data
|
||||
var type = s.ReadUInt8();
|
||||
while (type == 0)
|
||||
type = s.ReadUInt8();
|
||||
|
||||
var width = s.ReadInt32();
|
||||
var height = s.ReadInt32();
|
||||
var x = s.ReadInt32();
|
||||
var y = s.ReadInt32();
|
||||
|
||||
Size = new Size(width, height);
|
||||
Offset = new int2(width / 2 - x, height / 2 - y);
|
||||
|
||||
/*var imageOffset = */
|
||||
s.ReadInt32();
|
||||
var paletteOffset = s.ReadInt32();
|
||||
var bpp = s.ReadUInt8();
|
||||
if (bpp != 8)
|
||||
throw new InvalidDataException("Error: {0} bits per pixel are not supported.".F(bpp));
|
||||
|
||||
var frameHeight = s.ReadUInt8();
|
||||
var frameWidth = s.ReadUInt8();
|
||||
FrameSize = new Size(frameWidth, frameHeight);
|
||||
|
||||
// Skip alignment byte
|
||||
s.ReadUInt8();
|
||||
|
||||
Data = s.ReadBytes(width * height);
|
||||
|
||||
// Ignore palette
|
||||
if (type == 1 && paletteOffset != 0)
|
||||
s.Seek(520, SeekOrigin.Current);
|
||||
}
|
||||
}
|
||||
|
||||
public IReadOnlyList<ISpriteFrame> Frames { get; private set; }
|
||||
public bool CacheWhenLoadingTileset { get { return true; } }
|
||||
|
||||
public readonly int ImageCount;
|
||||
public R8Reader(Stream stream)
|
||||
{
|
||||
var frames = new List<R8Image>();
|
||||
while (stream.Position < stream.Length)
|
||||
{
|
||||
frames.Add(new R8Image(stream));
|
||||
ImageCount++;
|
||||
}
|
||||
|
||||
Frames = frames.ToArray().AsReadOnly();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user