Allow SheetBuilder to generate 1/2/4 channel sheets.

This makes the SpriteFont RBGA sprite hack explicit,
and adds a DualIndexed option to be used by the voxel
renderer.
This commit is contained in:
Paul Chote
2013-05-06 20:48:09 +12:00
parent 28d4df355d
commit 4ebe547a05
4 changed files with 34 additions and 24 deletions

View File

@@ -22,31 +22,39 @@ namespace OpenRA.Graphics
"to temporarily avoid the problem.") {} "to temporarily avoid the problem.") {}
} }
public enum SheetType
{
Indexed = 1,
DualIndexed = 2,
BGRA = 4,
}
public class SheetBuilder public class SheetBuilder
{ {
Sheet current; Sheet current;
TextureChannel channel;
SheetType type;
int rowHeight = 0; int rowHeight = 0;
Point p; Point p;
TextureChannel channel;
Sheet NewSheet() { return new Sheet(new Size(Renderer.SheetSize, Renderer.SheetSize)); }
internal SheetBuilder(TextureChannel ch) internal SheetBuilder(SheetType t)
{ {
current = NewSheet(); current = new Sheet(new Size(Renderer.SheetSize, Renderer.SheetSize));;
channel = ch; channel = TextureChannel.Red;
type = t;
} }
public Sprite Add(byte[] src, Size size, bool allowSheetOverflow) public Sprite Add(byte[] src, Size size, bool allowSheetOverflow)
{ {
Sprite rect = Allocate(size, allowSheetOverflow); var rect = Allocate(size, allowSheetOverflow);
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, bool allowSheetOverflow)
{ {
byte[] data = new byte[size.Width * size.Height]; var data = new byte[size.Width * size.Height];
for (int 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, allowSheetOverflow);
@@ -54,14 +62,11 @@ namespace OpenRA.Graphics
TextureChannel? NextChannel(TextureChannel t) TextureChannel? NextChannel(TextureChannel t)
{ {
switch (t) var nextChannel = (int)t + (int)type;
{ if (nextChannel > (int)TextureChannel.Alpha)
case TextureChannel.Red: return TextureChannel.Green; return null;
case TextureChannel.Green: return TextureChannel.Blue;
case TextureChannel.Blue: return TextureChannel.Alpha; return (TextureChannel)nextChannel;
case TextureChannel.Alpha:
default: return null;
}
} }
public Sprite Allocate(Size imageSize, bool allowSheetOverflow) public Sprite Allocate(Size imageSize, bool allowSheetOverflow)
@@ -93,11 +98,13 @@ namespace OpenRA.Graphics
p = new Point(0,0); p = new Point(0,0);
} }
Sprite rect = new Sprite(current, new Rectangle(p, imageSize), channel); var rect = new Sprite(current, new Rectangle(p, imageSize), channel);
current.MakeDirty(); current.MakeDirty();
p.X += imageSize.Width; p.X += imageSize.Width;
return rect; return rect;
} }
public Sheet Current { get { return current; } }
} }
} }

View File

@@ -31,8 +31,10 @@ namespace OpenRA.Graphics
glyphs = new Cache<Pair<char, Color>, GlyphInfo>(CreateGlyph, glyphs = new Cache<Pair<char, Color>, GlyphInfo>(CreateGlyph,
Pair<char,Color>.EqualityComparer); Pair<char,Color>.EqualityComparer);
// setup a 1-channel SheetBuilder for our private use // setup a SheetBuilder for our private use
if (builder == null) builder = new SheetBuilder(TextureChannel.Alpha); // TODO: SheetBuilder state is leaked between mod switches
if (builder == null)
builder = new SheetBuilder(SheetType.BGRA);
PrecacheColor(Color.White); PrecacheColor(Color.White);
PrecacheColor(Color.Red); PrecacheColor(Color.Red);
@@ -46,7 +48,7 @@ namespace OpenRA.Graphics
throw new InvalidOperationException(); throw new InvalidOperationException();
} }
public void DrawText (string text, float2 location, Color c) public void DrawText(string text, float2 location, Color c)
{ {
location.Y += size; // baseline vs top location.Y += size; // baseline vs top
@@ -84,7 +86,7 @@ namespace OpenRA.Graphics
public int2 Measure(string text) public int2 Measure(string text)
{ {
return new int2((int)text.Split( '\n' ).Max( s => s.Sum(a => glyphs[Pair.New(a, Color.White)].Advance)), text.Split('\n').Count()*size); return new int2((int)text.Split('\n').Max(s => s.Sum(a => glyphs[Pair.New(a, Color.White)].Advance)), text.Split('\n').Count()*size);
} }
Cache<Pair<char,Color>, GlyphInfo> glyphs; Cache<Pair<char,Color>, GlyphInfo> glyphs;

View File

@@ -32,12 +32,13 @@ namespace OpenRA.Graphics
static readonly int[] channelMasks = { 2, 1, 0, 3 }; // yes, our channel order is nuts. static readonly int[] channelMasks = { 2, 1, 0, 3 }; // yes, our channel order is nuts.
public static void FastCopyIntoChannel(Sprite dest, byte[] src) public static void FastCopyIntoChannel(Sprite dest, byte[] src) { FastCopyIntoChannel(dest, 0, src); }
public static void FastCopyIntoChannel(Sprite dest, int channelOffset, byte[] src)
{ {
var data = dest.sheet.Data; var data = dest.sheet.Data;
var srcStride = dest.bounds.Width; var srcStride = dest.bounds.Width;
var destStride = dest.sheet.Size.Width * 4; var destStride = dest.sheet.Size.Width * 4;
var destOffset = destStride * dest.bounds.Top + dest.bounds.Left * 4 + channelMasks[(int)dest.channel]; var destOffset = destStride * dest.bounds.Top + dest.bounds.Left * 4 + channelMasks[(int)dest.channel + channelOffset];
var destSkip = destStride - 4 * srcStride; var destSkip = destStride - 4 * srcStride;
var height = dest.bounds.Height; var height = dest.bounds.Height;

View File

@@ -53,7 +53,7 @@ namespace OpenRA
ChromeMetrics.Initialize(Manifest.ChromeMetrics); ChromeMetrics.Initialize(Manifest.ChromeMetrics);
ChromeProvider.Initialize(Manifest.Chrome); ChromeProvider.Initialize(Manifest.Chrome);
SheetBuilder = new SheetBuilder(TextureChannel.Red); SheetBuilder = new SheetBuilder(SheetType.Indexed);
SpriteLoader = new SpriteLoader(new string[] { ".shp" }, SheetBuilder); SpriteLoader = new SpriteLoader(new string[] { ".shp" }, SheetBuilder);
CursorProvider.Initialize(Manifest.Cursors); CursorProvider.Initialize(Manifest.Cursors);
} }