Generalize SheetBuilder overflow behavior.

This commit is contained in:
Paul Chote
2013-06-07 17:06:16 +12:00
parent cd268c11ee
commit 0e1c12131a
6 changed files with 57 additions and 28 deletions

View File

@@ -15,11 +15,8 @@ namespace OpenRA.Graphics
{ {
public class SheetOverflowException : Exception public class SheetOverflowException : Exception
{ {
public SheetOverflowException() public SheetOverflowException(string message)
: base("Sprite sequence spans multiple sheets.\n"+ : base(message) {}
"This should be considered as a bug, but you "+
"can increase the Graphics.SheetSize setting "+
"to temporarily avoid the problem.") {}
} }
public enum SheetType public enum SheetType
@@ -36,28 +33,38 @@ namespace OpenRA.Graphics
SheetType type; SheetType type;
int rowHeight = 0; int rowHeight = 0;
Point p; Point p;
Func<Sheet> allocateSheet;
internal SheetBuilder(SheetType t) public static Sheet AllocateSheet()
{ {
current = new Sheet(new Size(Renderer.SheetSize, Renderer.SheetSize));; return new Sheet(new Size(Renderer.SheetSize, Renderer.SheetSize));;
channel = TextureChannel.Red;
type = t;
} }
public Sprite Add(byte[] src, Size size, bool allowSheetOverflow) internal SheetBuilder(SheetType t)
: this(t, AllocateSheet) {}
internal SheetBuilder(SheetType t, Func<Sheet> allocateSheet)
{ {
var rect = Allocate(size, allowSheetOverflow); channel = TextureChannel.Red;
type = t;
current = allocateSheet();
this.allocateSheet = allocateSheet;
}
public Sprite Add(byte[] src, Size size)
{
var rect = Allocate(size);
Util.FastCopyIntoChannel(rect, src); Util.FastCopyIntoChannel(rect, src);
return rect; return rect;
} }
public Sprite Add(Size size, byte paletteIndex, bool allowSheetOverflow) public Sprite Add(Size size, byte paletteIndex)
{ {
var data = new byte[size.Width * size.Height]; var data = new byte[size.Width * size.Height];
for (var i = 0; i < data.Length; i++) for (var i = 0; i < data.Length; i++)
data[i] = paletteIndex; data[i] = paletteIndex;
return Add(data, size, allowSheetOverflow); return Add(data, size);
} }
TextureChannel? NextChannel(TextureChannel t) TextureChannel? NextChannel(TextureChannel t)
@@ -69,7 +76,7 @@ namespace OpenRA.Graphics
return (TextureChannel)nextChannel; return (TextureChannel)nextChannel;
} }
public Sprite Allocate(Size imageSize, bool allowSheetOverflow) public Sprite Allocate(Size imageSize)
{ {
if (imageSize.Width + p.X > current.Size.Width) if (imageSize.Width + p.X > current.Size.Width)
{ {
@@ -85,10 +92,7 @@ namespace OpenRA.Graphics
var next = NextChannel(channel); var next = NextChannel(channel);
if (next == null) if (next == null)
{ {
if (!allowSheetOverflow) current = allocateSheet();
throw new SheetOverflowException();
current = new Sheet(new Size(Renderer.SheetSize, Renderer.SheetSize));
channel = TextureChannel.Red; channel = TextureChannel.Red;
} }
else else

View File

@@ -99,7 +99,7 @@ namespace OpenRA.Graphics
face.Glyph.RenderGlyph(RenderMode.Normal); face.Glyph.RenderGlyph(RenderMode.Normal);
var size = new Size((int)face.Glyph.Metrics.Width >> 6, (int)face.Glyph.Metrics.Height >> 6); var size = new Size((int)face.Glyph.Metrics.Width >> 6, (int)face.Glyph.Metrics.Height >> 6);
var s = builder.Allocate(size, true); var s = builder.Allocate(size);
var g = new GlyphInfo var g = new GlyphInfo
{ {

View File

@@ -35,12 +35,12 @@ namespace OpenRA.Graphics
if (ImageCount == 0) if (ImageCount == 0)
{ {
var shp = new ShpTSReader(FileSystem.OpenWithExts(filename, exts)); var shp = new ShpTSReader(FileSystem.OpenWithExts(filename, exts));
return shp.Select(a => SheetBuilder.Add(a.Image, shp.Size, true)).ToArray(); return shp.Select(a => SheetBuilder.Add(a.Image, shp.Size)).ToArray();
} }
else else
{ {
var shp = new ShpReader(FileSystem.OpenWithExts(filename, exts)); var shp = new ShpReader(FileSystem.OpenWithExts(filename, exts));
return shp.Frames.Select(a => SheetBuilder.Add(a.Image, shp.Size, true)).ToArray(); return shp.Frames.Select(a => SheetBuilder.Add(a.Image, shp.Size)).ToArray();
} }
} }

View File

@@ -29,11 +29,22 @@ namespace OpenRA.Graphics
this.world = world; this.world = world;
this.map = world.Map; this.map = world.Map;
// TODO: Use a fixed sheet size specified in the tileset yaml var allocated = false;
sheetBuilder = new SheetBuilder(SheetType.Indexed); Func<Sheet> allocate = () =>
{
if (allocated)
throw new SheetOverflowException("Terrain sheet overflow");
allocated = true;
// TODO: Use a fixed sheet size specified in the tileset yaml
return SheetBuilder.AllocateSheet();
};
sheetBuilder = new SheetBuilder(SheetType.Indexed, allocate);
var tileSize = new Size(Game.CellSize, Game.CellSize); var tileSize = new Size(Game.CellSize, Game.CellSize);
var tileMapping = new Cache<TileReference<ushort,byte>, Sprite>( var tileMapping = new Cache<TileReference<ushort,byte>, Sprite>(
x => sheetBuilder.Add(world.TileSet.GetBytes(x), tileSize, false)); x => sheetBuilder.Add(world.TileSet.GetBytes(x), tileSize));
var terrainPalette = wr.Palette("terrain").Index; var terrainPalette = wr.Palette("terrain").Index;
var vertices = new Vertex[4 * map.Bounds.Height * map.Bounds.Width]; var vertices = new Vertex[4 * map.Bounds.Height * map.Bounds.Width];

View File

@@ -41,6 +41,20 @@ namespace OpenRA.Graphics
int totalVertexCount; int totalVertexCount;
int cachedVertexCount; int cachedVertexCount;
SheetBuilder CreateSheetBuilder()
{
var allocated = false;
Func<Sheet> allocate = () =>
{
if (allocated)
throw new SheetOverflowException("");
allocated = true;
return SheetBuilder.AllocateSheet();
};
return new SheetBuilder(SheetType.DualIndexed, allocate);
}
public VoxelLoader() public VoxelLoader()
{ {
voxels = new Cache<Pair<string,string>, Voxel>(LoadFile); voxels = new Cache<Pair<string,string>, Voxel>(LoadFile);
@@ -48,7 +62,7 @@ namespace OpenRA.Graphics
totalVertexCount = 0; totalVertexCount = 0;
cachedVertexCount = 0; cachedVertexCount = 0;
sheetBuilder = new SheetBuilder(SheetType.DualIndexed); sheetBuilder = CreateSheetBuilder();
} }
static float[] channelSelect = { 0.75f, 0.25f, -0.25f, -0.75f }; static float[] channelSelect = { 0.75f, 0.25f, -0.25f, -0.75f };
@@ -67,7 +81,7 @@ namespace OpenRA.Graphics
c++; c++;
} }
Sprite s = sheetBuilder.Allocate(new Size(su, sv), false); Sprite s = sheetBuilder.Allocate(new Size(su, sv));
Util.FastCopyIntoChannel(s, 0, colors); Util.FastCopyIntoChannel(s, 0, colors);
Util.FastCopyIntoChannel(s, 1, normals); Util.FastCopyIntoChannel(s, 1, normals);
s.sheet.MakeDirty(); s.sheet.MakeDirty();
@@ -164,7 +178,7 @@ namespace OpenRA.Graphics
{ {
// Sheet overflow - allocate a new sheet and try once more // Sheet overflow - allocate a new sheet and try once more
Log.Write("debug", "Voxel sheet overflow! Generating new sheet"); Log.Write("debug", "Voxel sheet overflow! Generating new sheet");
sheetBuilder = new SheetBuilder(SheetType.DualIndexed); sheetBuilder = CreateSheetBuilder();
v = GenerateSlicePlanes(l).SelectMany(x => x).ToArray(); v = GenerateSlicePlanes(l).SelectMany(x => x).ToArray();
} }

View File

@@ -101,7 +101,7 @@ namespace OpenRA.Mods.RA
cachedTileset = self.World.Map.Tileset; cachedTileset = self.World.Map.Tileset;
var tileSize = new Size(Game.CellSize, Game.CellSize); var tileSize = new Size(Game.CellSize, Game.CellSize);
sprites = new Cache<TileReference<ushort,byte>, Sprite>( sprites = new Cache<TileReference<ushort,byte>, Sprite>(
x => Game.modData.SheetBuilder.Add(self.World.TileSet.GetBytes(x), tileSize, true)); x => Game.modData.SheetBuilder.Add(self.World.TileSet.GetBytes(x), tileSize));
} }
// Cache templates and tiles for the different states // Cache templates and tiles for the different states