Use PaletteReferences everywhere.

This commit is contained in:
Paul Chote
2013-06-13 00:07:47 +12:00
parent e5bcb88b0e
commit 7beef85a64
14 changed files with 56 additions and 42 deletions

View File

@@ -20,12 +20,23 @@ namespace OpenRA.Graphics
{
public static class CursorProvider
{
static HardwarePalette Palette;
static HardwarePalette palette;
static Dictionary<string, CursorSequence> cursors;
static Cache<string, PaletteReference> palettes;
static PaletteReference CreatePaletteReference(string name)
{
var pal = palette.GetPalette(name);
if (pal == null)
throw new InvalidOperationException("Palette `{0}` does not exist".F(name));
return new PaletteReference(name, palette.GetPaletteIndex(name), pal);
}
public static void Initialize(string[] sequenceFiles)
{
cursors = new Dictionary<string, CursorSequence>();
palettes = new Cache<string, PaletteReference>(CreatePaletteReference);
var sequences = new MiniYaml(null, sequenceFiles.Select(s => MiniYaml.FromFile(s)).Aggregate(MiniYaml.MergeLiberal));
int[] ShadowIndex = { };
@@ -35,14 +46,14 @@ namespace OpenRA.Graphics
int.TryParse(sequences.NodesDict["ShadowIndex"].Value, out ShadowIndex[ShadowIndex.Length - 1]);
}
Palette = new HardwarePalette();
palette = new HardwarePalette();
foreach (var p in sequences.NodesDict["Palettes"].Nodes)
Palette.AddPalette(p.Key, new Palette(FileSystem.Open(p.Value.Value), ShadowIndex), false);
palette.AddPalette(p.Key, new Palette(FileSystem.Open(p.Value.Value), ShadowIndex), false);
foreach (var s in sequences.NodesDict["Cursors"].Nodes)
LoadSequencesForCursor(s.Key, s.Value);
Palette.Initialize();
palette.Initialize();
}
static void LoadSequencesForCursor(string cursorSrc, MiniYaml cursor)
@@ -63,10 +74,10 @@ namespace OpenRA.Graphics
var cursorSequence = GetCursorSequence(cursorName);
var cursorSprite = cursorSequence.GetSprite(cursorFrame);
renderer.SetPalette(Palette);
renderer.SetPalette(palette);
renderer.SpriteRenderer.DrawSprite(cursorSprite,
lastMousePos - cursorSequence.Hotspot,
Palette.GetPaletteIndex(cursorSequence.Palette),
palettes[cursorSequence.Palette],
cursorSprite.size);
}

View File

@@ -82,7 +82,7 @@ namespace OpenRA.Graphics
public void Render(WorldRenderer wr)
{
sprite.DrawAt(wr.ScreenPxPosition(pos) - pxCenter, palette.Index, scale);
sprite.DrawAt(wr.ScreenPxPosition(pos) - pxCenter, palette, scale);
}
public void RenderDebugGeometry(WorldRenderer wr)

View File

@@ -185,14 +185,14 @@ namespace OpenRA.Graphics
{
s[starti, j].DrawAt(
Game.CellSize * new float2(starti, j),
pal.Index,
pal,
new float2(Game.CellSize * (i - starti), Game.CellSize));
starti = i + 1;
}
s[i, j].DrawAt(
Game.CellSize * new float2(i, j),
pal.Index);
pal);
starti = i + 1;
last = s[i, j];
}
@@ -200,7 +200,7 @@ namespace OpenRA.Graphics
if (starti < clip.Right)
s[starti, j].DrawAt(
Game.CellSize * new float2(starti, j),
pal.Index,
pal,
new float2(Game.CellSize * (clip.Right - starti), Game.CellSize));
}
}

View File

@@ -50,24 +50,19 @@ namespace OpenRA.Graphics
return textureCoords[k];
}
public void DrawAt(WorldRenderer wr, float2 location, string palette)
public void DrawAt(float2 location, PaletteReference pal)
{
Game.Renderer.WorldSpriteRenderer.DrawSprite(this, location, wr, palette, size);
Game.Renderer.WorldSpriteRenderer.DrawSprite(this, location, pal, size);
}
public void DrawAt(float2 location, int paletteIndex)
public void DrawAt(float2 location, PaletteReference pal, float scale)
{
Game.Renderer.WorldSpriteRenderer.DrawSprite(this, location, paletteIndex, size);
Game.Renderer.WorldSpriteRenderer.DrawSprite(this, location, pal, size*scale);
}
public void DrawAt(float2 location, int paletteIndex, float scale)
public void DrawAt(float2 location, PaletteReference pal, float2 size)
{
Game.Renderer.WorldSpriteRenderer.DrawSprite(this, location, paletteIndex, size*scale);
}
public void DrawAt(float2 location, int paletteIndex, float2 size)
{
Game.Renderer.WorldSpriteRenderer.DrawSprite(this, location, paletteIndex, size);
Game.Renderer.WorldSpriteRenderer.DrawSprite(this, location, pal, size);
}
}

View File

@@ -45,17 +45,17 @@ namespace OpenRA.Graphics
}
}
public void DrawSprite(Sprite s, float2 location, WorldRenderer wr, string palette)
public void DrawSprite(Sprite s, float2 location, PaletteReference pal)
{
DrawSprite(s, location, wr.Palette(palette).Index, s.size);
DrawSprite(s, location, pal.Index, s.size);
}
public void DrawSprite(Sprite s, float2 location, WorldRenderer wr, string palette, float2 size)
public void DrawSprite(Sprite s, float2 location, PaletteReference pal, float2 size)
{
DrawSprite(s, location, wr.Palette(palette).Index, size);
DrawSprite(s, location, pal.Index, size);
}
public void DrawSprite(Sprite s, float2 location, int paletteIndex, float2 size)
void DrawSprite(Sprite s, float2 location, int paletteIndex, float2 size)
{
Renderer.CurrentBatchRenderer = this;