Merge pull request #8649 from pchote/resource-layer

Render resources using TerrainSpriteLayers
This commit is contained in:
Pavel Penev
2015-07-06 22:54:29 +03:00
3 changed files with 116 additions and 68 deletions

View File

@@ -19,6 +19,9 @@ namespace OpenRA.Graphics
{ {
public sealed class TerrainSpriteLayer : IDisposable public sealed class TerrainSpriteLayer : IDisposable
{ {
public readonly Sheet Sheet;
public readonly BlendMode BlendMode;
readonly Sprite emptySprite; readonly Sprite emptySprite;
readonly IVertexBuffer<Vertex> vertexBuffer; readonly IVertexBuffer<Vertex> vertexBuffer;
@@ -30,17 +33,14 @@ namespace OpenRA.Graphics
readonly WorldRenderer worldRenderer; readonly WorldRenderer worldRenderer;
readonly Map map; readonly Map map;
readonly Sheet sheet;
readonly BlendMode blendMode;
float paletteIndex; float paletteIndex;
public TerrainSpriteLayer(World world, WorldRenderer wr, Sheet sheet, BlendMode blendMode, PaletteReference palette, bool restrictToBounds) public TerrainSpriteLayer(World world, WorldRenderer wr, Sheet sheet, BlendMode blendMode, PaletteReference palette, bool restrictToBounds)
{ {
worldRenderer = wr; worldRenderer = wr;
this.restrictToBounds = restrictToBounds; this.restrictToBounds = restrictToBounds;
this.sheet = sheet; Sheet = sheet;
this.blendMode = blendMode; BlendMode = blendMode;
paletteIndex = palette.TextureIndex; paletteIndex = palette.TextureIndex;
map = world.Map; map = world.Map;
@@ -69,7 +69,8 @@ namespace OpenRA.Graphics
public void Update(CPos cell, Sprite sprite) public void Update(CPos cell, Sprite sprite)
{ {
var pos = worldRenderer.ScreenPosition(map.CenterOfCell(cell)) + sprite.Offset - 0.5f * sprite.Size; var pos = sprite == null ? float2.Zero :
worldRenderer.ScreenPosition(map.CenterOfCell(cell)) + sprite.Offset - 0.5f * sprite.Size;
Update(cell.ToMPos(map.TileShape), sprite, pos); Update(cell.ToMPos(map.TileShape), sprite, pos);
} }
@@ -77,10 +78,10 @@ namespace OpenRA.Graphics
{ {
if (sprite != null) if (sprite != null)
{ {
if (sprite.Sheet != sheet) if (sprite.Sheet != Sheet)
throw new InvalidDataException("Attempted to add sprite from a different sheet"); throw new InvalidDataException("Attempted to add sprite from a different sheet");
if (sprite.BlendMode != blendMode) if (sprite.BlendMode != BlendMode)
throw new InvalidDataException("Attempted to add sprite with a different blend mode"); throw new InvalidDataException("Attempted to add sprite with a different blend mode");
} }
else else
@@ -122,7 +123,7 @@ namespace OpenRA.Graphics
Game.Renderer.WorldSpriteRenderer.DrawVertexBuffer( Game.Renderer.WorldSpriteRenderer.DrawVertexBuffer(
vertexBuffer, rowStride * firstRow, rowStride * (lastRow - firstRow), vertexBuffer, rowStride * firstRow, rowStride * (lastRow - firstRow),
PrimitiveType.QuadList, sheet, blendMode); PrimitiveType.QuadList, Sheet, BlendMode);
Game.Renderer.Flush(); Game.Renderer.Flush();
} }

View File

@@ -10,6 +10,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Linq; using System.Linq;
using OpenRA.Graphics; using OpenRA.Graphics;
using OpenRA.Traits; using OpenRA.Traits;
@@ -17,33 +18,51 @@ using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits namespace OpenRA.Mods.Common.Traits
{ {
[Desc("Attach this to the world actor.", "Order of the layers defines the Z sorting.")] [Desc("Attach this to the world actor.", "Order of the layers defines the Z sorting.")]
public class ResourceLayerInfo : TraitInfo<ResourceLayer>, Requires<ResourceTypeInfo> { } public class ResourceLayerInfo : ITraitInfo, Requires<ResourceTypeInfo>, Requires<BuildingInfluenceInfo>
{
public virtual object Create(ActorInitializer init) { return new ResourceLayer(init.Self); }
}
public class ResourceLayer : IRenderOverlay, IWorldLoaded, ITickRender public class ResourceLayer : IRenderOverlay, IWorldLoaded, ITickRender, INotifyActorDisposing
{ {
static readonly CellContents EmptyCell = new CellContents(); static readonly CellContents EmptyCell = new CellContents();
World world; readonly World world;
readonly BuildingInfluence buildingInfluence;
readonly HashSet<CPos> dirty = new HashSet<CPos>();
readonly Dictionary<PaletteReference, TerrainSpriteLayer> spriteLayers = new Dictionary<PaletteReference, TerrainSpriteLayer>();
BuildingInfluence buildingInfluence; protected readonly CellLayer<CellContents> Content;
protected readonly CellLayer<CellContents> RenderContent;
protected CellLayer<CellContents> content; public ResourceLayer(Actor self)
protected CellLayer<CellContents> render; {
List<CPos> dirty; world = self.World;
buildingInfluence = self.Trait<BuildingInfluence>();
Content = new CellLayer<CellContents>(world.Map);
RenderContent = new CellLayer<CellContents>(world.Map);
RenderContent.CellEntryChanged += UpdateSpriteLayers;
}
void UpdateSpriteLayers(CPos cell)
{
var resource = RenderContent[cell];
foreach (var kv in spriteLayers)
{
// resource.Type is meaningless (and may be null) if resource.Sprite is null
if (resource.Sprite != null && resource.Type.Palette == kv.Key)
kv.Value.Update(cell, resource.Sprite);
else
kv.Value.Update(cell, null);
}
}
public void Render(WorldRenderer wr) public void Render(WorldRenderer wr)
{ {
var shroudObscured = world.ShroudObscuresTest; foreach (var kv in spriteLayers.Values)
foreach (var uv in wr.Viewport.VisibleCellsInsideBounds.MapCoords) kv.Draw(wr.Viewport);
{
if (shroudObscured(uv))
continue;
var c = render[uv];
if (c.Sprite != null)
new SpriteRenderable(c.Sprite, wr.World.Map.CenterOfCell(uv.ToCPos(world.Map)),
WVec.Zero, -511, c.Type.Palette, 1f, true).Render(wr); // TODO ZOffset is ignored
}
} }
int GetAdjacentCellsWith(ResourceType t, CPos cell) int GetAdjacentCellsWith(ResourceType t, CPos cell)
@@ -54,7 +73,7 @@ namespace OpenRA.Mods.Common.Traits
for (var v = -1; v < 2; v++) for (var v = -1; v < 2; v++)
{ {
var c = cell + new CVec(u, v); var c = cell + new CVec(u, v);
if (content.Contains(c) && content[c].Type == t) if (Content.Contains(c) && Content[c].Type == t)
++sum; ++sum;
} }
} }
@@ -64,14 +83,6 @@ namespace OpenRA.Mods.Common.Traits
public void WorldLoaded(World w, WorldRenderer wr) public void WorldLoaded(World w, WorldRenderer wr)
{ {
this.world = w;
buildingInfluence = world.WorldActor.Trait<BuildingInfluence>();
content = new CellLayer<CellContents>(w.Map);
render = new CellLayer<CellContents>(w.Map);
dirty = new List<CPos>();
var resources = w.WorldActor.TraitsImplementing<ResourceType>() var resources = w.WorldActor.TraitsImplementing<ResourceType>()
.ToDictionary(r => r.Info.ResourceType, r => r); .ToDictionary(r => r.Info.ResourceType, r => r);
@@ -84,30 +95,51 @@ namespace OpenRA.Mods.Common.Traits
if (!AllowResourceAt(t, cell)) if (!AllowResourceAt(t, cell))
continue; continue;
content[cell] = CreateResourceCell(t, cell); Content[cell] = CreateResourceCell(t, cell);
} }
// Set initial density based on the number of neighboring resources // Set initial density based on the number of neighboring resources
foreach (var cell in w.Map.AllCells) foreach (var cell in w.Map.AllCells)
{ {
var type = content[cell].Type; var type = Content[cell].Type;
if (type != null) if (type != null)
{ {
// Adjacent includes the current cell, so is always >= 1 // Adjacent includes the current cell, so is always >= 1
var adjacent = GetAdjacentCellsWith(type, cell); var adjacent = GetAdjacentCellsWith(type, cell);
var density = int2.Lerp(0, type.Info.MaxDensity, adjacent, 9); var density = int2.Lerp(0, type.Info.MaxDensity, adjacent, 9);
var temp = content[cell]; var temp = Content[cell];
temp.Density = Math.Max(density, 1); temp.Density = Math.Max(density, 1);
render[cell] = content[cell] = temp; Content[cell] = temp;
UpdateRenderedSprite(cell); dirty.Add(cell);
} }
} }
// Build the sprite layer dictionary for rendering resources
// All resources that have the same palette must also share a sheet and blend mode
foreach (var r in resources)
{
var layer = spriteLayers.GetOrAdd(r.Value.Palette, pal =>
{
var first = r.Value.Variants.First().Value.First();
return new TerrainSpriteLayer(w, wr, first.Sheet, first.BlendMode, pal, wr.World.Type != WorldType.Editor);
});
// Validate that sprites are compatible with this layer
var sheet = layer.Sheet;
if (r.Value.Variants.Any(kv => kv.Value.Any(s => s.Sheet != sheet)))
throw new InvalidDataException("Resource sprites span multiple sheets. Try loading their sequences earlier.");
var blendMode = layer.BlendMode;
if (r.Value.Variants.Any(kv => kv.Value.Any(s => s.BlendMode != blendMode)))
throw new InvalidDataException("Resource sprites specify different blend modes. "
+ "Try using different palettes for resource types that use different blend modes.");
}
} }
protected virtual void UpdateRenderedSprite(CPos cell) protected virtual void UpdateRenderedSprite(CPos cell)
{ {
var t = render[cell]; var t = RenderContent[cell];
if (t.Density > 0) if (t.Density > 0)
{ {
var sprites = t.Type.Variants[t.Variant]; var sprites = t.Type.Variants[t.Variant];
@@ -117,7 +149,7 @@ namespace OpenRA.Mods.Common.Traits
else else
t.Sprite = null; t.Sprite = null;
render[cell] = t; RenderContent[cell] = t;
} }
protected virtual string ChooseRandomVariant(ResourceType t) protected virtual string ChooseRandomVariant(ResourceType t)
@@ -132,7 +164,7 @@ namespace OpenRA.Mods.Common.Traits
{ {
if (!self.World.FogObscures(c)) if (!self.World.FogObscures(c))
{ {
render[c] = content[c]; RenderContent[c] = Content[c];
UpdateRenderedSprite(c); UpdateRenderedSprite(c);
remove.Add(c); remove.Add(c);
} }
@@ -187,7 +219,7 @@ namespace OpenRA.Mods.Common.Traits
public void AddResource(ResourceType t, CPos p, int n) public void AddResource(ResourceType t, CPos p, int n)
{ {
var cell = content[p]; var cell = Content[p];
if (cell.Type == null) if (cell.Type == null)
cell = CreateResourceCell(t, p); cell = CreateResourceCell(t, p);
@@ -195,33 +227,31 @@ namespace OpenRA.Mods.Common.Traits
return; return;
cell.Density = Math.Min(cell.Type.Info.MaxDensity, cell.Density + n); cell.Density = Math.Min(cell.Type.Info.MaxDensity, cell.Density + n);
content[p] = cell; Content[p] = cell;
if (!dirty.Contains(p)) dirty.Add(p);
dirty.Add(p);
} }
public bool IsFull(CPos cell) public bool IsFull(CPos cell)
{ {
return content[cell].Density == content[cell].Type.Info.MaxDensity; return Content[cell].Density == Content[cell].Type.Info.MaxDensity;
} }
public ResourceType Harvest(CPos cell) public ResourceType Harvest(CPos cell)
{ {
var c = content[cell]; var c = Content[cell];
if (c.Type == null) if (c.Type == null)
return null; return null;
if (--c.Density < 0) if (--c.Density < 0)
{ {
content[cell] = EmptyCell; Content[cell] = EmptyCell;
world.Map.CustomTerrain[cell] = byte.MaxValue; world.Map.CustomTerrain[cell] = byte.MaxValue;
} }
else else
content[cell] = c; Content[cell] = c;
if (!dirty.Contains(cell)) dirty.Add(cell);
dirty.Add(cell);
return c.Type; return c.Type;
} }
@@ -229,26 +259,37 @@ namespace OpenRA.Mods.Common.Traits
public void Destroy(CPos cell) public void Destroy(CPos cell)
{ {
// Don't break other users of CustomTerrain if there are no resources // Don't break other users of CustomTerrain if there are no resources
if (content[cell].Type == null) if (Content[cell].Type == null)
return; return;
// Clear cell // Clear cell
content[cell] = EmptyCell; Content[cell] = EmptyCell;
world.Map.CustomTerrain[cell] = byte.MaxValue; world.Map.CustomTerrain[cell] = byte.MaxValue;
if (!dirty.Contains(cell)) dirty.Add(cell);
dirty.Add(cell);
} }
public ResourceType GetResource(CPos cell) { return content[cell].Type; } public ResourceType GetResource(CPos cell) { return Content[cell].Type; }
public ResourceType GetRenderedResource(CPos cell) { return render[cell].Type; } public ResourceType GetRenderedResource(CPos cell) { return RenderContent[cell].Type; }
public int GetResourceDensity(CPos cell) { return content[cell].Density; } public int GetResourceDensity(CPos cell) { return Content[cell].Density; }
public int GetMaxResourceDensity(CPos cell) public int GetMaxResourceDensity(CPos cell)
{ {
if (content[cell].Type == null) if (Content[cell].Type == null)
return 0; return 0;
return content[cell].Type.Info.MaxDensity; return Content[cell].Type.Info.MaxDensity;
}
bool disposed;
public void Disposing(Actor self)
{
if (disposed)
return;
foreach (var kv in spriteLayers.Values)
kv.Dispose();
disposed = true;
} }
public struct CellContents public struct CellContents

View File

@@ -17,7 +17,10 @@ using OpenRA.Traits;
namespace OpenRA.Mods.D2k.Traits namespace OpenRA.Mods.D2k.Traits
{ {
[Desc("Used to render spice with round borders.")] [Desc("Used to render spice with round borders.")]
public class D2kResourceLayerInfo : TraitInfo<D2kResourceLayer> { } public class D2kResourceLayerInfo : ResourceLayerInfo
{
public override object Create(ActorInitializer init) { return new D2kResourceLayer(init.Self); }
}
public class D2kResourceLayer : ResourceLayer public class D2kResourceLayer : ResourceLayer
{ {
@@ -96,9 +99,12 @@ namespace OpenRA.Mods.D2k.Traits
{ ClearSides.Bottom | ClearSides.TopLeft | ClearSides.BottomLeft | ClearSides.BottomRight, 49 }, { ClearSides.Bottom | ClearSides.TopLeft | ClearSides.BottomLeft | ClearSides.BottomRight, 49 },
}; };
public D2kResourceLayer(Actor self)
: base(self) { }
bool CellContains(CPos c, ResourceType t) bool CellContains(CPos c, ResourceType t)
{ {
return render.Contains(c) && render[c].Type == t; return RenderContent.Contains(c) && RenderContent[c].Type == t;
} }
ClearSides FindClearSides(ResourceType t, CPos p) ClearSides FindClearSides(ResourceType t, CPos p)
@@ -133,10 +139,10 @@ namespace OpenRA.Mods.D2k.Traits
void UpdateRenderedTileInner(CPos p) void UpdateRenderedTileInner(CPos p)
{ {
if (!render.Contains(p)) if (!RenderContent.Contains(p))
return; return;
var t = render[p]; var t = RenderContent[p];
if (t.Density > 0) if (t.Density > 0)
{ {
var clear = FindClearSides(t.Type, p); var clear = FindClearSides(t.Type, p);
@@ -156,7 +162,7 @@ namespace OpenRA.Mods.D2k.Traits
else else
t.Sprite = null; t.Sprite = null;
render[p] = t; RenderContent[p] = t;
} }
protected override void UpdateRenderedSprite(CPos p) protected override void UpdateRenderedSprite(CPos p)