Allow SheetBuilder to throw an exception on sheet overflow.

This removes unnecessary checks from TerrainRenderer
and will be used in the voxel renderer.
This commit is contained in:
Paul Chote
2013-05-06 19:46:00 +12:00
parent c13c989fe8
commit 891158ca44
5 changed files with 44 additions and 47 deletions

View File

@@ -8,68 +8,64 @@
*/
#endregion
using System;
using System.Drawing;
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 class SheetBuilder
{
Sheet current;
int rowHeight = 0;
Point p;
TextureChannel channel;
Sheet NewSheet() { return new Sheet(new Size(Renderer.SheetSize, Renderer.SheetSize)); }
internal SheetBuilder(TextureChannel ch)
{
current = null;
rowHeight = 0;
channel = null;
initialChannel = ch;
current = NewSheet();
channel = ch;
}
public Sprite Add(byte[] src, Size size)
public Sprite Add(byte[] src, Size size, bool allowSheetOverflow)
{
Sprite rect = Allocate(size);
Sprite rect = Allocate(size, allowSheetOverflow);
Util.FastCopyIntoChannel(rect, src);
return rect;
}
public Sprite Add(Size size, byte paletteIndex)
public Sprite Add(Size size, byte paletteIndex, bool allowSheetOverflow)
{
byte[] data = new byte[size.Width * size.Height];
for (int i = 0; i < data.Length; i++)
data[i] = paletteIndex;
return Add(data, size);
return Add(data, size, allowSheetOverflow);
}
Sheet NewSheet() { return new Sheet(new Size( Renderer.SheetSize, Renderer.SheetSize ) ); }
Sheet current = null;
int rowHeight = 0;
Point p;
TextureChannel? channel = null;
TextureChannel initialChannel;
TextureChannel? NextChannel(TextureChannel? t)
TextureChannel? NextChannel(TextureChannel t)
{
if (t == null)
return initialChannel;
switch (t.Value)
switch (t)
{
case TextureChannel.Red: return TextureChannel.Green;
case TextureChannel.Green: return TextureChannel.Blue;
case TextureChannel.Blue: return TextureChannel.Alpha;
case TextureChannel.Alpha: return null;
case TextureChannel.Alpha:
default: return null;
}
}
public Sprite Allocate(Size imageSize)
public Sprite Allocate(Size imageSize, bool allowSheetOverflow)
{
if (current == null)
{
current = NewSheet();
channel = NextChannel(null);
}
if (imageSize.Width + p.X > current.Size.Width)
{
p = new Point(0, p.Y + rowHeight);
@@ -81,18 +77,23 @@ namespace OpenRA.Graphics
if (p.Y + imageSize.Height > current.Size.Height)
{
if (null == (channel = NextChannel(channel)))
var next = NextChannel(channel);
if (next == null)
{
current = NewSheet();
channel = NextChannel(channel);
if (!allowSheetOverflow)
throw new SheetOverflowException();
current = new Sheet(new Size(Renderer.SheetSize, Renderer.SheetSize));
channel = TextureChannel.Red;
}
else
channel = next.Value;
rowHeight = imageSize.Height;
p = new Point(0,0);
}
Sprite rect = new Sprite(current, new Rectangle(p, imageSize), channel.Value);
Sprite rect = new Sprite(current, new Rectangle(p, imageSize), channel);
current.MakeDirty();
p.X += imageSize.Width;

View File

@@ -96,9 +96,8 @@ namespace OpenRA.Graphics
face.LoadGlyph(index, LoadFlags.Default, LoadTarget.Normal);
face.Glyph.RenderGlyph(RenderMode.Normal);
var s = builder.Allocate(
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 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 => Game.modData.SheetBuilder.Add(a.Image, shp.Size)).ToArray();
return shp.Select(a => SheetBuilder.Add(a.Image, shp.Size, true)).ToArray();
}
else
{
var shp = new ShpReader(FileSystem.OpenWithExts(filename, exts));
return shp.Frames.Select(a => SheetBuilder.Add(a.Image, shp.Size)).ToArray();
return shp.Frames.Select(a => SheetBuilder.Add(a.Image, shp.Size, true)).ToArray();
}
}

View File

@@ -30,8 +30,8 @@ namespace OpenRA.Graphics
this.map = world.Map;
var tileSize = new Size(Game.CellSize, Game.CellSize);
var tileMapping = new Cache<TileReference<ushort,byte>, Sprite>(x =>
Game.modData.SheetBuilder.Add(world.TileSet.GetBytes(x), tileSize));
var tileMapping = new Cache<TileReference<ushort,byte>, Sprite>(
x => Game.modData.SheetBuilder.Add(world.TileSet.GetBytes(x), tileSize, false));
terrainSheet = tileMapping[map.MapTiles.Value[map.Bounds.Left, map.Bounds.Top]].sheet;
var terrainPalette = wr.Palette("terrain").Index;
@@ -44,9 +44,6 @@ namespace OpenRA.Graphics
var tile = tileMapping[map.MapTiles.Value[i, j]];
Util.FastCreateQuad(vertices, Game.CellSize * new float2(i, j), tile, terrainPalette, nv, tile.size);
nv += 4;
if (tileMapping[map.MapTiles.Value[i, j]].sheet != terrainSheet)
throw new InvalidOperationException("Terrain sprites span multiple sheets. Try increasing Game.Settings.Graphics.SheetSize.");
}
vertexBuffer = Game.Renderer.Device.CreateVertexBuffer(vertices.Length);

View File

@@ -99,9 +99,9 @@ namespace OpenRA.Mods.RA
if (cachedTileset != self.World.Map.Tileset)
{
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),
new Size(Game.CellSize, Game.CellSize)));
x => Game.modData.SheetBuilder.Add(self.World.TileSet.GetBytes(x), tileSize, true));
}
// Cache templates and tiles for the different states