Skip to content

Commit

Permalink
Don't release planes with a plane master (OpenDreamProject#1599)
Browse files Browse the repository at this point in the history
* Don't release planes with a plane master

* Properly dispose of the plane's render target
  • Loading branch information
wixoaGit authored Jan 1, 2024
1 parent 94ceaba commit f63978a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
27 changes: 13 additions & 14 deletions OpenDreamClient/Rendering/DreamPlane.cs
Original file line number Diff line number Diff line change
@@ -1,37 +1,36 @@
using OpenDreamShared.Dream;
using Robust.Client.Graphics;
using Robust.Client.Graphics;
using Robust.Shared.Utility;

namespace OpenDreamClient.Rendering;

internal sealed class DreamPlane {
public IRenderTexture RenderTarget => _temporaryRenderTarget ?? _mainRenderTarget;
internal sealed class DreamPlane(IRenderTexture mainRenderTarget) : IDisposable {
public IRenderTexture RenderTarget => _temporaryRenderTarget ?? mainRenderTarget;
public RendererMetaData? Master;

public readonly List<Action<Vector2i>> IconDrawActions = new();
public readonly List<Action<Vector2i>> MouseMapDrawActions = new();

private IRenderTexture _mainRenderTarget;
private IRenderTexture? _temporaryRenderTarget;

public DreamPlane(IRenderTexture renderTarget) {
_mainRenderTarget = renderTarget;
}

public void Clear() {
Master = null;
IconDrawActions.Clear();
MouseMapDrawActions.Clear();
_temporaryRenderTarget = null;
}

public void Dispose() {
mainRenderTarget.Dispose();
Clear();
}

/// <summary>
/// Sets this plane's main render target<br/>
/// Persists through calls to <see cref="Clear()"/>
/// </summary>
public void SetMainRenderTarget(IRenderTexture renderTarget) {
_mainRenderTarget.Dispose();
_mainRenderTarget = renderTarget;
mainRenderTarget.Dispose();
mainRenderTarget = renderTarget;
}

/// <summary>
Expand All @@ -47,17 +46,17 @@ public void SetTemporaryRenderTarget(IRenderTexture renderTarget) {
/// </summary>
public void Draw(DreamViewOverlay overlay, DrawingHandleWorld handle) {
// Draw all icons
handle.RenderInRenderTarget(_mainRenderTarget, () => {
handle.RenderInRenderTarget(mainRenderTarget, () => {
foreach (Action<Vector2i> iconAction in IconDrawActions)
iconAction(_mainRenderTarget.Size);
iconAction(mainRenderTarget.Size);
}, new Color());

if (_temporaryRenderTarget != null) {
// Draw again, but with the color applied
handle.RenderInRenderTarget(_temporaryRenderTarget, () => {
handle.UseShader(overlay.GetBlendAndColorShader(Master, useOverlayMode: true));
handle.SetTransform(overlay.CreateRenderTargetFlipMatrix(_temporaryRenderTarget.Size, Vector2.Zero));
handle.DrawTextureRect(_mainRenderTarget.Texture, new Box2(Vector2.Zero, _mainRenderTarget.Size));
handle.DrawTextureRect(mainRenderTarget.Texture, new Box2(Vector2.Zero, mainRenderTarget.Size));
handle.SetTransform(Matrix3.Identity);
handle.UseShader(null);
}, new Color());
Expand Down
4 changes: 3 additions & 1 deletion OpenDreamClient/Rendering/DreamViewOverlay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,8 @@ private void ClearPlanes() {
var plane = pair.Value;

// We can remove the plane if there was nothing on it last frame
if (plane.IconDrawActions.Count == 0 && plane.MouseMapDrawActions.Count == 0) {
if (plane.IconDrawActions.Count == 0 && plane.MouseMapDrawActions.Count == 0 && plane.Master == null) {
plane.Dispose();
_planes.Remove(pair.Key);
continue;
}
Expand All @@ -625,6 +626,7 @@ private DreamPlane GetPlane(int planeIndex, Vector2i viewportSize) {

plane = new(renderTarget);
_planes.Add(planeIndex, plane);
_sawmill.Info($"Created plane {planeIndex}");
return plane;
}

Expand Down

0 comments on commit f63978a

Please sign in to comment.