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

@@ -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);

View File

@@ -563,6 +563,7 @@
<Compile Include="Traits\World\PaletteFromGimpOrJascFile.cs" />
<Compile Include="Traits\World\PaletteFromPng.cs" />
<Compile Include="Traits\World\PaletteFromRGBA.cs" />
<Compile Include="Traits\World\TerrainRenderer.cs" />
<Compile Include="Traits\World\ValidateOrder.cs" />
<Compile Include="Pathfinder\CellInfoLayerPool.cs" />
<Compile Include="Pathfinder\Constants.cs" />

View File

@@ -11,19 +11,32 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using OpenRA.Graphics;
using OpenRA.Traits;
namespace OpenRA.Graphics
namespace OpenRA.Mods.Common.Traits
{
sealed class TerrainRenderer : IDisposable
public class TerrainRendererInfo : ITraitInfo
{
public object Create(ActorInitializer init) { return new TerrainRenderer(init.World); }
}
public sealed class TerrainRenderer : IRenderTerrain, IWorldLoaded, INotifyActorDisposing
{
readonly Map map;
readonly Dictionary<string, TerrainSpriteLayer> spriteLayers = new Dictionary<string, TerrainSpriteLayer>();
readonly Theater theater;
Theater theater;
bool disposed;
public TerrainRenderer(World world, WorldRenderer wr)
public TerrainRenderer(World world)
{
map = world.Map;
}
void IWorldLoaded.WorldLoaded(World world, WorldRenderer wr)
{
theater = wr.Theater;
foreach (var template in map.Rules.TileSet.Templates)
@@ -52,7 +65,7 @@ namespace OpenRA.Graphics
kv.Value.Update(cell, palette == kv.Key ? sprite : null);
}
public void Draw(WorldRenderer wr, Viewport viewport)
void IRenderTerrain.RenderTerrain(WorldRenderer wr, Viewport viewport)
{
foreach (var kv in spriteLayers.Values)
kv.Draw(wr.Viewport);
@@ -61,13 +74,18 @@ namespace OpenRA.Graphics
r.Render(wr);
}
public void Dispose()
void INotifyActorDisposing.Disposing(Actor self)
{
if (disposed)
return;
map.Tiles.CellEntryChanged -= UpdateCell;
map.Height.CellEntryChanged -= UpdateCell;
foreach (var kv in spriteLayers.Values)
kv.Dispose();
disposed = true;
}
}
}

View File

@@ -7,6 +7,7 @@
VictoryMusic: win1
DefeatMusic: nod_map1
DebugVisualizations:
TerrainRenderer:
TerrainGeometryOverlay:
ShroudRenderer:
ShroudVariants: typea, typeb, typec, typed

View File

@@ -8,6 +8,7 @@
DefeatMusic: score
TerrainGeometryOverlay:
DebugVisualizations:
TerrainRenderer:
ShroudRenderer:
ShroudVariants: shrouda, shroudb, shroudc, shroudd
FogVariants: foga, fogb, fogc, fogd

View File

@@ -89,6 +89,7 @@
Locomotor@IMMOBILE:
Name: immobile
TerrainSpeeds:
TerrainRenderer:
ShroudRenderer:
FogVariants: shroud
Index: 255, 16, 32, 48, 64, 80, 96, 112, 128, 144, 160, 176, 192, 208, 224, 240, 20, 40, 56, 65, 97, 130, 148, 194, 24, 33, 66, 132, 28, 41, 67, 134, 1, 2, 4, 8, 3, 6, 12, 9, 7, 14, 13, 11, 5, 10, 15, 255

View File

@@ -6,6 +6,7 @@
MusicPlaylist:
VictoryMusic: score
DefeatMusic: maps
TerrainRenderer:
ShroudRenderer:
Index: 255, 16, 32, 48, 64, 80, 96, 112, 128, 144, 160, 176, 192, 208, 224, 240, 20, 40, 56, 65, 97, 130, 148, 194, 24, 33, 66, 132, 28, 41, 67, 134, 1, 2, 4, 8, 3, 6, 12, 9, 7, 14, 13, 11, 5, 10, 15, 255
UseExtendedIndex: true