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 #endregion
using System;
using System.Drawing; using System.Drawing;
namespace OpenRA.Graphics namespace OpenRA.Graphics
{ {
public class SheetBuilder public class SheetOverflowException : Exception
{ {
internal SheetBuilder(TextureChannel ch) public SheetOverflowException()
{ : base("Sprite sequence spans multiple sheets.\n"+
current = null; "This should be considered as a bug, but you "+
rowHeight = 0; "can increase the Graphics.SheetSize setting "+
channel = null; "to temporarily avoid the problem.") {}
initialChannel = ch;
} }
public Sprite Add(byte[] src, Size size) public class SheetBuilder
{ {
Sprite rect = Allocate(size); 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 = NewSheet();
channel = ch;
}
public Sprite Add(byte[] src, Size size, bool allowSheetOverflow)
{
Sprite rect = Allocate(size, allowSheetOverflow);
Util.FastCopyIntoChannel(rect, src); Util.FastCopyIntoChannel(rect, src);
return rect; 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]; byte[] data = new byte[size.Width * size.Height];
for (int i = 0; i < data.Length; i++) for (int i = 0; i < data.Length; i++)
data[i] = paletteIndex; data[i] = paletteIndex;
return Add(data, size); return Add(data, size, allowSheetOverflow);
} }
Sheet NewSheet() { return new Sheet(new Size( Renderer.SheetSize, Renderer.SheetSize ) ); } TextureChannel? NextChannel(TextureChannel t)
Sheet current = null;
int rowHeight = 0;
Point p;
TextureChannel? channel = null;
TextureChannel initialChannel;
TextureChannel? NextChannel(TextureChannel? t)
{ {
if (t == null) switch (t)
return initialChannel;
switch (t.Value)
{ {
case TextureChannel.Red: return TextureChannel.Green; case TextureChannel.Red: return TextureChannel.Green;
case TextureChannel.Green: return TextureChannel.Blue; case TextureChannel.Green: return TextureChannel.Blue;
case TextureChannel.Blue: return TextureChannel.Alpha; case TextureChannel.Blue: return TextureChannel.Alpha;
case TextureChannel.Alpha: return null; case TextureChannel.Alpha:
default: return null; 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) if (imageSize.Width + p.X > current.Size.Width)
{ {
p = new Point(0, p.Y + rowHeight); p = new Point(0, p.Y + rowHeight);
@@ -81,18 +77,23 @@ namespace OpenRA.Graphics
if (p.Y + imageSize.Height > current.Size.Height) if (p.Y + imageSize.Height > current.Size.Height)
{ {
var next = NextChannel(channel);
if (null == (channel = NextChannel(channel))) if (next == null)
{ {
current = NewSheet(); if (!allowSheetOverflow)
channel = NextChannel(channel); throw new SheetOverflowException();
current = new Sheet(new Size(Renderer.SheetSize, Renderer.SheetSize));
channel = TextureChannel.Red;
} }
else
channel = next.Value;
rowHeight = imageSize.Height; rowHeight = imageSize.Height;
p = new Point(0,0); 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(); current.MakeDirty();
p.X += imageSize.Width; p.X += imageSize.Width;

View File

@@ -96,9 +96,8 @@ namespace OpenRA.Graphics
face.LoadGlyph(index, LoadFlags.Default, LoadTarget.Normal); face.LoadGlyph(index, LoadFlags.Default, LoadTarget.Normal);
face.Glyph.RenderGlyph(RenderMode.Normal); face.Glyph.RenderGlyph(RenderMode.Normal);
var s = builder.Allocate( var size = new Size((int)face.Glyph.Metrics.Width >> 6, (int)face.Glyph.Metrics.Height >> 6);
new Size((int)face.Glyph.Metrics.Width >> 6, var s = builder.Allocate(size, true);
(int)face.Glyph.Metrics.Height >> 6));
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 => Game.modData.SheetBuilder.Add(a.Image, shp.Size)).ToArray(); return shp.Select(a => SheetBuilder.Add(a.Image, shp.Size, true)).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)).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; this.map = world.Map;
var tileSize = new Size(Game.CellSize, Game.CellSize); var tileSize = new Size(Game.CellSize, Game.CellSize);
var tileMapping = new Cache<TileReference<ushort,byte>, Sprite>(x => var tileMapping = new Cache<TileReference<ushort,byte>, Sprite>(
Game.modData.SheetBuilder.Add(world.TileSet.GetBytes(x), tileSize)); x => Game.modData.SheetBuilder.Add(world.TileSet.GetBytes(x), tileSize, false));
terrainSheet = tileMapping[map.MapTiles.Value[map.Bounds.Left, map.Bounds.Top]].sheet; terrainSheet = tileMapping[map.MapTiles.Value[map.Bounds.Left, map.Bounds.Top]].sheet;
var terrainPalette = wr.Palette("terrain").Index; var terrainPalette = wr.Palette("terrain").Index;
@@ -44,9 +44,6 @@ namespace OpenRA.Graphics
var tile = tileMapping[map.MapTiles.Value[i, j]]; var tile = tileMapping[map.MapTiles.Value[i, j]];
Util.FastCreateQuad(vertices, Game.CellSize * new float2(i, j), tile, terrainPalette, nv, tile.size); Util.FastCreateQuad(vertices, Game.CellSize * new float2(i, j), tile, terrainPalette, nv, tile.size);
nv += 4; 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); vertexBuffer = Game.Renderer.Device.CreateVertexBuffer(vertices.Length);

View File

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