Merge pull request #2952 from Mailaender/cursor-palette-refactor

Cursor Palette Refactor
This commit is contained in:
Matthias Mailänder
2013-04-06 01:15:32 -07:00
9 changed files with 51 additions and 37 deletions

View File

@@ -1,6 +1,6 @@
#region Copyright & License Information #region Copyright & License Information
/* /*
* Copyright 2007-2012 The OpenRA Developers (see AUTHORS) * Copyright 2007-2013 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made * This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License * available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information, * as published by the Free Software Foundation. For more information,
@@ -20,7 +20,7 @@ namespace OpenRA.Graphics
{ {
public static class CursorProvider public static class CursorProvider
{ {
public static HardwarePalette Palette; static HardwarePalette Palette;
static Dictionary<string, CursorSequence> cursors; static Dictionary<string, CursorSequence> cursors;
public static void Initialize(string[] sequenceFiles) public static void Initialize(string[] sequenceFiles)
@@ -32,22 +32,17 @@ namespace OpenRA.Graphics
if (sequences.NodesDict.ContainsKey("ShadowIndex")) if (sequences.NodesDict.ContainsKey("ShadowIndex"))
{ {
Array.Resize(ref ShadowIndex, ShadowIndex.Length + 1); Array.Resize(ref ShadowIndex, ShadowIndex.Length + 1);
ShadowIndex[ShadowIndex.Length - 1] = Convert.ToInt32(sequences.NodesDict["ShadowIndex"].Value); int.TryParse(sequences.NodesDict["ShadowIndex"].Value, out ShadowIndex[ShadowIndex.Length - 1]);
} }
var palettes = new Dictionary<string, Palette>();
foreach (var s in sequences.NodesDict["Palettes"].Nodes)
palettes.Add(s.Key, new Palette(FileSystem.Open(s.Value.Value), ShadowIndex));
Palette = new HardwarePalette(); Palette = new HardwarePalette();
foreach (var p in palettes) foreach (var p in sequences.NodesDict["Palettes"].Nodes)
Palette.AddPalette(p.Key, p.Value, false); Palette.AddPalette(p.Key, new Palette(FileSystem.Open(p.Value.Value), ShadowIndex), false);
// Generate initial palette texture
Palette.Update(new IPaletteModifier[] {});
foreach (var s in sequences.NodesDict["Cursors"].Nodes) foreach (var s in sequences.NodesDict["Cursors"].Nodes)
LoadSequencesForCursor(s.Key, s.Value); LoadSequencesForCursor(s.Key, s.Value);
Palette.Initialize();
} }
static void LoadSequencesForCursor(string cursorSrc, MiniYaml cursor) static void LoadSequencesForCursor(string cursorSrc, MiniYaml cursor)
@@ -65,9 +60,10 @@ namespace OpenRA.Graphics
public static void DrawCursor(Renderer renderer, string cursorName, int2 lastMousePos, int cursorFrame) public static void DrawCursor(Renderer renderer, string cursorName, int2 lastMousePos, int cursorFrame)
{ {
var cursorSequence = CursorProvider.GetCursorSequence(cursorName); var cursorSequence = GetCursorSequence(cursorName);
var cursorSprite = cursorSequence.GetSprite(cursorFrame); var cursorSprite = cursorSequence.GetSprite(cursorFrame);
renderer.SetPalette(Palette);
renderer.SpriteRenderer.DrawSprite(cursorSprite, renderer.SpriteRenderer.DrawSprite(cursorSprite,
lastMousePos - cursorSequence.Hotspot, lastMousePos - cursorSequence.Hotspot,
Palette.GetPaletteIndex(cursorSequence.Palette), Palette.GetPaletteIndex(cursorSequence.Palette),

View File

@@ -22,7 +22,7 @@ namespace OpenRA.Graphics
public const int MaxPalettes = 256; public const int MaxPalettes = 256;
int allocated = 0; int allocated = 0;
ITexture texture; public ITexture Texture { get; private set; }
Dictionary<string, Palette> palettes; Dictionary<string, Palette> palettes;
Dictionary<string, int> indices; Dictionary<string, int> indices;
Dictionary<string, bool> allowsMods; Dictionary<string, bool> allowsMods;
@@ -32,7 +32,7 @@ namespace OpenRA.Graphics
palettes = new Dictionary<string, Palette>(); palettes = new Dictionary<string, Palette>();
indices = new Dictionary<string, int>(); indices = new Dictionary<string, int>();
allowsMods = new Dictionary<string, bool>(); allowsMods = new Dictionary<string, bool>();
texture = Game.Renderer.Device.CreateTexture(); Texture = Game.Renderer.Device.CreateTexture();
} }
public Palette GetPalette(string name) public Palette GetPalette(string name)
@@ -62,7 +62,7 @@ namespace OpenRA.Graphics
} }
uint[,] data = new uint[MaxPalettes, 256]; uint[,] data = new uint[MaxPalettes, 256];
public void Update(IEnumerable<IPaletteModifier> paletteMods) public void ApplyModifiers(IEnumerable<IPaletteModifier> paletteMods)
{ {
var copy = palettes.ToDictionary(p => p.Key, p => new Palette(p.Value)); var copy = palettes.ToDictionary(p => p.Key, p => new Palette(p.Value));
var modifiable = copy.Where(p => allowsMods[p.Key]).ToDictionary(p => p.Key, p => p.Value); var modifiable = copy.Where(p => allowsMods[p.Key]).ToDictionary(p => p.Key, p => p.Value);
@@ -78,9 +78,12 @@ namespace OpenRA.Graphics
data[j,i] = c[i]; data[j,i] = c[i];
} }
// Doesn't work Texture.SetData(data);
texture.SetData(data); }
Game.Renderer.PaletteTexture = texture;
public void Initialize()
{
ApplyModifiers(new IPaletteModifier[] {});
} }
} }
} }

View File

@@ -78,7 +78,7 @@ namespace OpenRA.Graphics
DrawLine(new float2(r.Left, y), new float2(r.Right, y), color, color); DrawLine(new float2(r.Left, y), new float2(r.Right, y), color, color);
} }
public void SetShaderParams(ITexture palette, Size screen, float zoom, float2 scroll) public void SetViewportParams(Size screen, float zoom, float2 scroll)
{ {
shader.SetVec("Scroll", (int)scroll.X, (int)scroll.Y); shader.SetVec("Scroll", (int)scroll.X, (int)scroll.Y);
shader.SetVec("r1", zoom*2f/screen.Width, -zoom*2f/screen.Height); shader.SetVec("r1", zoom*2f/screen.Width, -zoom*2f/screen.Height);

View File

@@ -57,7 +57,7 @@ namespace OpenRA.Graphics
nv += 4; nv += 4;
} }
public void SetShaderParams(ITexture palette, Size screen, float zoom, float2 scroll) public void SetViewportParams(Size screen, float zoom, float2 scroll)
{ {
shader.SetVec("Scroll", (int)scroll.X, (int)scroll.Y); shader.SetVec("Scroll", (int)scroll.X, (int)scroll.Y);
shader.SetVec("r1", zoom*2f/screen.Width, -zoom*2f/screen.Height); shader.SetVec("r1", zoom*2f/screen.Width, -zoom*2f/screen.Height);

View File

@@ -34,8 +34,6 @@ namespace OpenRA.Graphics
public SpriteRenderer RgbaSpriteRenderer { get; private set; } public SpriteRenderer RgbaSpriteRenderer { get; private set; }
public SpriteRenderer SpriteRenderer { get; private set; } public SpriteRenderer SpriteRenderer { get; private set; }
public ITexture PaletteTexture;
Queue<IVertexBuffer<Vertex>> tempBuffers = new Queue<IVertexBuffer<Vertex>>(); Queue<IVertexBuffer<Vertex>> tempBuffers = new Queue<IVertexBuffer<Vertex>>();
public Dictionary<string, SpriteFont> Fonts; public Dictionary<string, SpriteFont> Fonts;
@@ -67,12 +65,26 @@ namespace OpenRA.Graphics
public void BeginFrame(float2 scroll, float zoom) public void BeginFrame(float2 scroll, float zoom)
{ {
device.Clear(); device.Clear();
WorldSpriteRenderer.SetShaderParams(PaletteTexture, Resolution, zoom, scroll); WorldSpriteRenderer.SetViewportParams(Resolution, zoom, scroll);
WorldLineRenderer.SetShaderParams(PaletteTexture, Resolution, zoom, scroll); SpriteRenderer.SetViewportParams(Resolution, 1f, float2.Zero);
WorldQuadRenderer.SetShaderParams(PaletteTexture, Resolution, zoom, scroll); RgbaSpriteRenderer.SetViewportParams(Resolution, 1f, float2.Zero);
SpriteRenderer.SetShaderParams(PaletteTexture, Resolution, 1f, float2.Zero); WorldLineRenderer.SetViewportParams(Resolution, zoom, scroll);
LineRenderer.SetShaderParams(PaletteTexture, Resolution, 1f, float2.Zero); WorldQuadRenderer.SetViewportParams(Resolution, zoom, scroll);
RgbaSpriteRenderer.SetShaderParams(PaletteTexture, Resolution, 1f, float2.Zero); LineRenderer.SetViewportParams(Resolution, 1f, float2.Zero);
}
ITexture currentPaletteTexture;
public void SetPalette(HardwarePalette palette)
{
if (palette.Texture == currentPaletteTexture)
return;
Flush();
currentPaletteTexture = palette.Texture;
RgbaSpriteRenderer.SetPalette(currentPaletteTexture);
SpriteRenderer.SetPalette(currentPaletteTexture);
WorldSpriteRenderer.SetPalette(currentPaletteTexture);
} }
public void EndFrame(IInputHandler inputHandler) public void EndFrame(IInputHandler inputHandler)

View File

@@ -87,9 +87,13 @@ namespace OpenRA.Graphics
shader.Render(() => renderer.DrawBatch(buffer, start, length, type)); shader.Render(() => renderer.DrawBatch(buffer, start, length, type));
} }
public void SetShaderParams(ITexture palette, Size screen, float zoom, float2 scroll) public void SetPalette(ITexture palette)
{ {
shader.SetTexture("Palette", palette); shader.SetTexture("Palette", palette);
}
public void SetViewportParams(Size screen, float zoom, float2 scroll)
{
shader.SetVec("Scroll", (int)scroll.X, (int)scroll.Y); shader.SetVec("Scroll", (int)scroll.X, (int)scroll.Y);
shader.SetVec("r1", zoom*2f/screen.Width, -zoom*2f/screen.Height); shader.SetVec("r1", zoom*2f/screen.Width, -zoom*2f/screen.Height);
shader.SetVec("r2", -1, 1); shader.SetVec("r2", -1, 1);

View File

@@ -127,7 +127,6 @@ namespace OpenRA.Graphics
using( new PerfSample("render_widgets") ) using( new PerfSample("render_widgets") )
{ {
Ui.Draw(); Ui.Draw();
var cursorName = Ui.Root.GetCursorOuter(Viewport.LastMousePos) ?? "default"; var cursorName = Ui.Root.GetCursorOuter(Viewport.LastMousePos) ?? "default";
CursorProvider.DrawCursor(renderer, cursorName, Viewport.LastMousePos, (int)cursorFrame); CursorProvider.DrawCursor(renderer, cursorName, Viewport.LastMousePos, (int)cursorFrame);
} }

View File

@@ -47,8 +47,7 @@ namespace OpenRA.Graphics
foreach (var pal in world.traitDict.ActorsWithTraitMultiple<IPalette>(world)) foreach (var pal in world.traitDict.ActorsWithTraitMultiple<IPalette>(world))
pal.Trait.InitPalette(this); pal.Trait.InitPalette(this);
// Generate initial palette texture palette.Initialize();
palette.Update(new IPaletteModifier[] {});
terrainRenderer = new TerrainRenderer(world, this); terrainRenderer = new TerrainRenderer(world, this);
shroudRenderer = new ShroudRenderer(world); shroudRenderer = new ShroudRenderer(world);
@@ -209,7 +208,8 @@ namespace OpenRA.Graphics
public void RefreshPalette() public void RefreshPalette()
{ {
palette.Update( world.WorldActor.TraitsImplementing<IPaletteModifier>() ); palette.ApplyModifiers(world.WorldActor.TraitsImplementing<IPaletteModifier>());
Game.Renderer.SetPalette(palette);
} }
// Conversion between world and screen coordinates // Conversion between world and screen coordinates