Merge pull request 'throw if RegisterDrawLayer called after adding renderer' (#5) from DrawLayerValidation into master
continuous-integration/drone/push Build is passing Details

pull/6/head
cosmonaut 2020-07-10 19:39:17 +00:00
commit bb2a678853
3 changed files with 54 additions and 6 deletions

View File

@ -0,0 +1,13 @@
using System;
namespace Encompass.Exceptions
{
public class IllegalDrawLayerException : Exception
{
public IllegalDrawLayerException(
string format,
params object[] args
) : base(string.Format(format, args)) { }
}
}

View File

@ -33,15 +33,13 @@ namespace Encompass
private readonly TrackingManager _trackingManager;
private readonly Dictionary<Type, HashSet<Engine>> _typeToReaders = new Dictionary<Type, HashSet<Engine>>();
private readonly HashSet<Engine> _senders = new HashSet<Engine>();
private readonly HashSet<Type> _componentTypesToPreload = new HashSet<Type>();
private readonly HashSet<Type> _messageTypes = new HashSet<Type>();
private readonly Dictionary<Type, int> _typeToIndex = new Dictionary<Type, int>();
private bool _rendererRegistered = false;
public WorldBuilder(int entityCapacity = 32768)
{
_entityCapacity = entityCapacity;
@ -177,6 +175,10 @@ namespace Encompass
/// <param name="layer">The draw layer to register.</param>
public void RegisterDrawLayer(int layer)
{
if (_rendererRegistered)
{
throw new IllegalDrawLayerException("Cannot register a draw layer after a Renderer has been registered.");
}
_drawLayerManager.RegisterDrawLayer(layer);
}
@ -189,6 +191,7 @@ namespace Encompass
renderer.AssignEntityManager(_entityManager);
renderer.AssignComponentManager(_componentManager);
_renderManager.RegisterOrderedRenderer<TComponent>(renderer.InternalRender);
_rendererRegistered = true;
return renderer;
}
@ -202,9 +205,8 @@ namespace Encompass
{
renderer.AssignEntityManager(_entityManager);
renderer.AssignComponentManager(_componentManager);
_renderManager.RegisterGeneralRendererWithLayer(renderer, layer);
_rendererRegistered = true;
return renderer;
}

View File

@ -558,5 +558,38 @@ namespace Tests
Assert.That(order.IndexOf(engineA), Is.LessThan(order.IndexOf(engineB)));
}
}
public class DrawLayerRegister
{
struct AComponent : IComponent, IDrawableComponent
{
public int Layer { get; set; }
}
class ARenderer : OrderedRenderer<AComponent>
{
public override void Render(Entity entity, in AComponent drawComponent) { }
}
[Test]
public void DrawLayerRegisterAfterOrderedRendererRegisterThrows()
{
var worldBuilder = new WorldBuilder();
var rendererA = worldBuilder.AddOrderedRenderer(new ARenderer());
Assert.Throws<IllegalDrawLayerException>(() => worldBuilder.RegisterDrawLayer(1));
}
[Test]
public void DrawLayerRegisterBeforeOrderedRendererDoesNotThrow()
{
var worldBuilder = new WorldBuilder();
Assert.DoesNotThrow(() => worldBuilder.RegisterDrawLayer(1));
Assert.DoesNotThrow(() => worldBuilder.AddOrderedRenderer(new ARenderer()));
}
}
}
}