Move TerrainRenderer to a mod-defined trait.

This commit is contained in:
Paul Chote
2018-10-01 18:09:26 +01:00
committed by abcdefg30
parent fec9fe1ad4
commit a06cfb4004
9 changed files with 38 additions and 11 deletions

View File

@@ -1,73 +0,0 @@
#region Copyright & License Information
/*
* Copyright 2007-2018 The OpenRA Developers (see AUTHORS)
* 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
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version. For more
* information, see COPYING.
*/
#endregion
using System;
using System.Collections.Generic;
using OpenRA.Traits;
namespace OpenRA.Graphics
{
sealed class TerrainRenderer : IDisposable
{
readonly Map map;
readonly Dictionary<string, TerrainSpriteLayer> spriteLayers = new Dictionary<string, TerrainSpriteLayer>();
readonly Theater theater;
public TerrainRenderer(World world, WorldRenderer wr)
{
map = world.Map;
theater = wr.Theater;
foreach (var template in map.Rules.TileSet.Templates)
{
var palette = template.Value.Palette ?? TileSet.TerrainPaletteInternalName;
spriteLayers.GetOrAdd(palette, pal =>
new TerrainSpriteLayer(world, wr, theater.Sheet, BlendMode.Alpha, wr.Palette(palette), world.Type != WorldType.Editor));
}
foreach (var cell in map.AllCells)
UpdateCell(cell);
map.Tiles.CellEntryChanged += UpdateCell;
map.Height.CellEntryChanged += UpdateCell;
}
public void UpdateCell(CPos cell)
{
var tile = map.Tiles[cell];
var palette = TileSet.TerrainPaletteInternalName;
if (map.Rules.TileSet.Templates.ContainsKey(tile.Type))
palette = map.Rules.TileSet.Templates[tile.Type].Palette ?? palette;
var sprite = theater.TileSprite(tile);
foreach (var kv in spriteLayers)
kv.Value.Update(cell, palette == kv.Key ? sprite : null);
}
public void Draw(WorldRenderer wr, Viewport viewport)
{
foreach (var kv in spriteLayers.Values)
kv.Draw(wr.Viewport);
foreach (var r in wr.World.WorldActor.TraitsImplementing<IRenderOverlay>())
r.Render(wr);
}
public void Dispose()
{
map.Tiles.CellEntryChanged -= UpdateCell;
map.Height.CellEntryChanged -= UpdateCell;
foreach (var kv in spriteLayers.Values)
kv.Dispose();
}
}
}

View File

@@ -34,7 +34,7 @@ namespace OpenRA.Graphics
readonly HashSet<Actor> onScreenActors = new HashSet<Actor>();
readonly HardwarePalette palette = new HardwarePalette();
readonly Dictionary<string, PaletteReference> palettes = new Dictionary<string, PaletteReference>();
readonly TerrainRenderer terrainRenderer;
readonly IRenderTerrain terrainRenderer;
readonly Lazy<DebugVisualizations> debugVis;
readonly Func<string, PaletteReference> createPaletteReference;
readonly bool enableDepthBuffer;
@@ -62,7 +62,7 @@ namespace OpenRA.Graphics
palette.Initialize();
Theater = new Theater(world.Map.Rules.TileSet);
terrainRenderer = new TerrainRenderer(world, this);
terrainRenderer = world.WorldActor.TraitOrDefault<IRenderTerrain>();
debugVis = Exts.Lazy(() => world.WorldActor.TraitOrDefault<DebugVisualizations>());
}
@@ -181,7 +181,9 @@ namespace OpenRA.Graphics
if (enableDepthBuffer)
Game.Renderer.Context.EnableDepthBuffer();
terrainRenderer.Draw(this, Viewport);
if (terrainRenderer != null)
terrainRenderer.RenderTerrain(this, Viewport);
Game.Renderer.Flush();
for (var i = 0; i < renderables.Count; i++)
@@ -330,7 +332,6 @@ namespace OpenRA.Graphics
palette.Dispose();
Theater.Dispose();
terrainRenderer.Dispose();
}
}
}

View File

@@ -134,7 +134,6 @@
<Compile Include="Graphics\SpriteFont.cs" />
<Compile Include="Graphics\SpriteLoader.cs" />
<Compile Include="Graphics\SpriteRenderer.cs" />
<Compile Include="Graphics\TerrainRenderer.cs" />
<Compile Include="Graphics\Util.cs" />
<Compile Include="Graphics\Viewport.cs" />
<Compile Include="Graphics\WorldRenderer.cs" />

View File

@@ -374,12 +374,16 @@ namespace OpenRA.Traits
[RequireExplicitImplementation]
public interface INotifyBecomingIdle { void OnBecomingIdle(Actor self); }
[RequireExplicitImplementation]
public interface INotifyIdle { void TickIdle(Actor self); }
public interface IRenderAboveWorld { void RenderAboveWorld(Actor self, WorldRenderer wr); }
public interface IRenderShroud { void RenderShroud(Shroud shroud, WorldRenderer wr); }
[RequireExplicitImplementation]
public interface IRenderTerrain { void RenderTerrain(WorldRenderer wr, Viewport viewport); }
public interface IRenderAboveShroud
{
IEnumerable<IRenderable> RenderAboveShroud(Actor self, WorldRenderer wr);