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

View File

@@ -99,7 +99,7 @@ namespace OpenRA.Graphics
face.Glyph.RenderGlyph(RenderMode.Normal);
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
{

View File

@@ -35,12 +35,12 @@ namespace OpenRA.Graphics
if (ImageCount == 0)
{
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
{
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.map = world.Map;
// TODO: Use a fixed sheet size specified in the tileset yaml
sheetBuilder = new SheetBuilder(SheetType.Indexed);
var allocated = false;
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 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 vertices = new Vertex[4 * map.Bounds.Height * map.Bounds.Width];

View File

@@ -41,6 +41,20 @@ namespace OpenRA.Graphics
int totalVertexCount;
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()
{
voxels = new Cache<Pair<string,string>, Voxel>(LoadFile);
@@ -48,7 +62,7 @@ namespace OpenRA.Graphics
totalVertexCount = 0;
cachedVertexCount = 0;
sheetBuilder = new SheetBuilder(SheetType.DualIndexed);
sheetBuilder = CreateSheetBuilder();
}
static float[] channelSelect = { 0.75f, 0.25f, -0.25f, -0.75f };
@@ -67,7 +81,7 @@ namespace OpenRA.Graphics
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, 1, normals);
s.sheet.MakeDirty();
@@ -164,7 +178,7 @@ namespace OpenRA.Graphics
{
// Sheet overflow - allocate a new sheet and try once more
Log.Write("debug", "Voxel sheet overflow! Generating new sheet");
sheetBuilder = new SheetBuilder(SheetType.DualIndexed);
sheetBuilder = CreateSheetBuilder();
v = GenerateSlicePlanes(l).SelectMany(x => x).ToArray();
}

View File

@@ -101,7 +101,7 @@ namespace OpenRA.Mods.RA
cachedTileset = self.World.Map.Tileset;
var tileSize = new Size(Game.CellSize, Game.CellSize);
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