Regenerate cached texture indices when the palette size changes.

This commit is contained in:
Paul Chote
2015-05-10 00:58:10 +01:00
parent 7eae157ad8
commit 889360d4c6
3 changed files with 37 additions and 12 deletions

View File

@@ -17,7 +17,6 @@ namespace OpenRA.Graphics
{ {
readonly IVertexBuffer<Vertex> vertexBuffer; readonly IVertexBuffer<Vertex> vertexBuffer;
readonly Vertex[] updateCellVertices = new Vertex[4]; readonly Vertex[] updateCellVertices = new Vertex[4];
readonly float terrainPaletteIndex;
readonly int rowStride; readonly int rowStride;
readonly WorldRenderer worldRenderer; readonly WorldRenderer worldRenderer;
@@ -25,6 +24,8 @@ namespace OpenRA.Graphics
readonly CellLayer<TerrainTile> mapTiles; readonly CellLayer<TerrainTile> mapTiles;
readonly Map map; readonly Map map;
float terrainPaletteIndex;
public TerrainRenderer(World world, WorldRenderer wr) public TerrainRenderer(World world, WorldRenderer wr)
{ {
worldRenderer = wr; worldRenderer = wr;
@@ -34,21 +35,18 @@ namespace OpenRA.Graphics
terrainPaletteIndex = wr.Palette("terrain").TextureIndex; terrainPaletteIndex = wr.Palette("terrain").TextureIndex;
rowStride = 4 * map.Bounds.Width; rowStride = 4 * map.Bounds.Width;
vertexBuffer = Game.Renderer.Device.CreateVertexBuffer(rowStride * map.Bounds.Height);
var vertices = new Vertex[rowStride * map.Bounds.Height]; UpdateMap();
vertexBuffer = Game.Renderer.Device.CreateVertexBuffer(vertices.Length);
var nv = 0;
foreach (var cell in map.Cells)
{
GenerateTileVertices(vertices, nv, cell);
nv += 4;
}
vertexBuffer.SetData(vertices, nv);
map.MapTiles.Value.CellEntryChanged += UpdateCell; map.MapTiles.Value.CellEntryChanged += UpdateCell;
map.MapHeight.Value.CellEntryChanged += UpdateCell; map.MapHeight.Value.CellEntryChanged += UpdateCell;
wr.PaletteInvalidated += () =>
{
terrainPaletteIndex = wr.Palette("terrain").TextureIndex;
UpdateMap();
};
} }
void GenerateTileVertices(Vertex[] vertices, int offset, CPos cell) void GenerateTileVertices(Vertex[] vertices, int offset, CPos cell)
@@ -58,6 +56,19 @@ namespace OpenRA.Graphics
Util.FastCreateQuad(vertices, pos, tile, terrainPaletteIndex, offset, tile.Size); Util.FastCreateQuad(vertices, pos, tile, terrainPaletteIndex, offset, tile.Size);
} }
void UpdateMap()
{
var nv = 0;
var vertices = new Vertex[rowStride * map.Bounds.Height];
foreach (var cell in map.Cells)
{
GenerateTileVertices(vertices, nv, cell);
nv += 4;
}
vertexBuffer.SetData(vertices, nv);
}
public void UpdateCell(CPos cell) public void UpdateCell(CPos cell)
{ {
var uv = cell.ToMPos(map.TileShape); var uv = cell.ToMPos(map.TileShape);

View File

@@ -25,6 +25,8 @@ namespace OpenRA.Graphics
public readonly Theater Theater; public readonly Theater Theater;
public Viewport Viewport { get; private set; } public Viewport Viewport { get; private set; }
public event Action PaletteInvalidated = null;
readonly HardwarePalette palette = new HardwarePalette(); readonly HardwarePalette palette = new HardwarePalette();
readonly Dictionary<string, PaletteReference> palettes = new Dictionary<string, PaletteReference>(); readonly Dictionary<string, PaletteReference> palettes = new Dictionary<string, PaletteReference>();
readonly TerrainRenderer terrainRenderer; readonly TerrainRenderer terrainRenderer;
@@ -70,7 +72,13 @@ namespace OpenRA.Graphics
if (allowOverwrite && palette.Contains(name)) if (allowOverwrite && palette.Contains(name))
ReplacePalette(name, pal); ReplacePalette(name, pal);
else else
{
var oldHeight = palette.Height;
palette.AddPalette(name, pal, allowModifiers); palette.AddPalette(name, pal, allowModifiers);
if (oldHeight != palette.Height && PaletteInvalidated != null)
PaletteInvalidated();
}
} }
public void ReplacePalette(string name, IPalette pal) public void ReplacePalette(string name, IPalette pal)

View File

@@ -172,6 +172,12 @@ namespace OpenRA.Mods.Common.Traits
fogPalette = wr.Palette(info.FogPalette); fogPalette = wr.Palette(info.FogPalette);
shroudPalette = wr.Palette(info.ShroudPalette); shroudPalette = wr.Palette(info.ShroudPalette);
wr.PaletteInvalidated += () =>
{
mapBorderShroudIsCached = false;
MarkCellsDirty(CellRegion.Expand(map.Cells, 1));
};
} }
Edges GetEdges(MPos uv, Func<MPos, bool> isVisible) Edges GetEdges(MPos uv, Func<MPos, bool> isVisible)