Prevent incorrect usage of ModelRenderer.

Force BeginFrame/EndFrame to be called correctly, and prevent calls to RenderAsync when outside of a frame.
This commit is contained in:
RoosterDragon
2017-12-23 17:24:57 +00:00
committed by abcdefg30
parent 4c64a37e1d
commit e42e766bb6

View File

@@ -52,6 +52,7 @@ namespace OpenRA.Graphics
readonly List<Pair<Sheet, Action>> doRender = new List<Pair<Sheet, Action>>(); readonly List<Pair<Sheet, Action>> doRender = new List<Pair<Sheet, Action>>();
SheetBuilder sheetBuilderForFrame; SheetBuilder sheetBuilderForFrame;
bool isInFrame;
public ModelRenderer(Renderer renderer, IShader shader) public ModelRenderer(Renderer renderer, IShader shader)
{ {
@@ -83,6 +84,9 @@ namespace OpenRA.Graphics
float[] groundNormal, WRot lightSource, float[] lightAmbientColor, float[] lightDiffuseColor, float[] groundNormal, WRot lightSource, float[] lightAmbientColor, float[] lightDiffuseColor,
PaletteReference color, PaletteReference normals, PaletteReference shadowPalette) PaletteReference color, PaletteReference normals, PaletteReference shadowPalette)
{ {
if (!isInFrame)
throw new InvalidOperationException("BeginFrame has not been called. You cannot render until a frame has been started.");
// Correct for inverted y-axis // Correct for inverted y-axis
var scaleTransform = Util.ScaleMatrix(scale, scale, scale); var scaleTransform = Util.ScaleMatrix(scale, scale, scale);
@@ -279,6 +283,11 @@ namespace OpenRA.Graphics
public void BeginFrame() public void BeginFrame()
{ {
if (isInFrame)
throw new InvalidOperationException("BeginFrame has already been called. A new frame cannot be started until EndFrame has been called.");
isInFrame = true;
foreach (var kv in mappedBuffers) foreach (var kv in mappedBuffers)
unmappedBuffers.Push(kv); unmappedBuffers.Push(kv);
mappedBuffers.Clear(); mappedBuffers.Clear();
@@ -303,6 +312,10 @@ namespace OpenRA.Graphics
public void EndFrame() public void EndFrame()
{ {
if (!isInFrame)
throw new InvalidOperationException("BeginFrame has not been called. There is no frame to end.");
isInFrame = false;
sheetBuilderForFrame = null; sheetBuilderForFrame = null;
if (doRender.Count == 0) if (doRender.Count == 0)