-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScreenTargetHandler.cs
More file actions
249 lines (198 loc) · 6.19 KB
/
ScreenTargetHandler.cs
File metadata and controls
249 lines (198 loc) · 6.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// source: https://github.com/ProjectStarlight/StarlightRiver
using System;
using System.Collections.Generic;
using System.Threading;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Terraria;
using Terraria.ModLoader;
namespace BookLibrary;
public class ScreenTarget
{
/// <summary>
/// What gets rendered to this screen target. Spritebatch is automatically started and RT automatically set, you only need to write the code for what you are rendering.
/// </summary>
public Action<SpriteBatch> drawFunct;
/// <summary>
/// If this render target should be rendered. Make sure this it as restrictive as possible to prevent uneccisary rendering work.
/// </summary>
public Func<bool> activeFunct;
/// <summary>
/// Optional function that runs when the screen is resized. Returns the size the render target should be. Return null to prevent resizing.
/// </summary>
public Func<Vector2, Vector2?> onResize;
/// <summary>
/// Where this render target should fall in the order of rendering. Important if you want to render something to chain into another RT.
/// </summary>
public float order;
/// <summary>
/// If this screen target should draw in the game menu. Be careful with these as lots of things are different in the menu!
/// </summary>
public bool allowOnMenu;
public RenderTarget2D RenderTarget { get; set; }
public ScreenTarget(Action<SpriteBatch> draw, Func<bool> active, float order, Func<Vector2, Vector2?> onResize = null)
{
if (Main.dedServ)
return;
drawFunct = draw;
activeFunct = active;
this.order = order;
this.onResize = onResize;
Vector2? initialDims = onResize is null ? new Vector2(Main.screenWidth, Main.screenHeight) : onResize(new Vector2(Main.screenWidth, Main.screenHeight));
Main.QueueMainThreadAction(() => RenderTarget = new RenderTarget2D(Main.instance.GraphicsDevice, (int)initialDims?.X, (int)initialDims?.Y, false, SurfaceFormat.Color, DepthFormat.None, 0, RenderTargetUsage.PreserveContents));
ScreenTargetHandler.AddTarget(this);
}
/// <summary>
/// Foribly resize a target to a new size
/// </summary>
/// <param name="size"></param>
public void ForceResize(Vector2 size)
{
if (Main.dedServ)
return;
RenderTarget.Dispose();
RenderTarget = new RenderTarget2D(Main.instance.GraphicsDevice, (int)size.X, (int)size.Y, false, SurfaceFormat.Color, DepthFormat.None, 0, RenderTargetUsage.PreserveContents);
}
}
internal class ScreenTargetHandler : ModSystem
{
public static List<ScreenTarget> targets = new();
public static Semaphore targetSem = new(1, 1);
private static int firstResizeTime = 0;
private static bool wasIngame;
public float Priority => 1;
public override void Load()
{
if (!Main.dedServ)
{
On_Main.CheckMonoliths += RenderScreens;
Main.OnResolutionChanged += ResizeScreens;
On_Main.UpdateMenu += MenuUpdate;
}
}
public override void Unload()
{
if (!Main.dedServ)
{
On_Main.CheckMonoliths -= RenderScreens;
Main.OnResolutionChanged -= ResizeScreens;
Main.QueueMainThreadAction(() => {
if (targets != null)
{
targets.ForEach(n => n.RenderTarget?.Dispose());
targets.Clear();
targets = null;
}
else
{
Mod.Logger.Warn("Screen targets was null, all ScreenTargets may not have been released! (leaking VRAM!)");
}
});
}
}
/// <summary>
/// Registers a new screen target and orders it into the list. Called automatically by the constructor of ScreenTarget!
/// </summary>
/// <param name="toAdd"></param>
public static void AddTarget(ScreenTarget toAdd)
{
targetSem.WaitOne();
targets.Add(toAdd);
targets.Sort((a, b) => a.order.CompareTo(b.order));
targetSem.Release();
}
/// <summary>
/// Removes a screen target from the targets list. Should not normally need to be used.
/// </summary>
/// <param name="toRemove"></param>
public static void RemoveTarget(ScreenTarget toRemove)
{
targetSem.WaitOne();
targets.Remove(toRemove);
targets.Sort((a, b) => a.order - b.order > 0 ? 1 : -1);
targetSem.Release();
}
public static void ResizeScreens(Vector2 obj)
{
if (Main.dedServ)
return;
targetSem.WaitOne();
targets.ForEach(n => {
if (!Main.gameMenu || n.allowOnMenu)
{
Vector2? size = obj;
if (n.onResize != null)
size = n.onResize(obj);
if (Main.gameMenu)
{
float menuScalingFactor = (float)size.Value.Y / 900f;
if (menuScalingFactor < 1f)
menuScalingFactor = 1f;
if (Main.SettingDontScaleMainMenuUp)
menuScalingFactor = 1f;
size /= menuScalingFactor;
}
if (size != null)
{
n.RenderTarget?.Dispose();
n.RenderTarget = new RenderTarget2D(Main.instance.GraphicsDevice, (int)size?.X, (int)size?.Y, false, SurfaceFormat.Color, DepthFormat.None, 0, RenderTargetUsage.PreserveContents);
}
}
});
targetSem.Release();
}
private void RenderScreens(On_Main.orig_CheckMonoliths orig)
{
orig();
if (Main.dedServ)
return;
RenderTargetBinding[] bindings = Main.graphics.GraphicsDevice.GetRenderTargets();
targetSem.WaitOne();
foreach (ScreenTarget target in targets)
{
if (Main.gameMenu && !target.allowOnMenu)
continue;
if (target.drawFunct is null) //allows for RTs which dont draw in the default loop, like the lighting tile buffers
continue;
if (target.activeFunct())
{
Main.spriteBatch.Begin(default, default, Main.DefaultSamplerState, default, RasterizerState.CullNone, default);
Main.graphics.GraphicsDevice.SetRenderTarget(target.RenderTarget);
Main.graphics.GraphicsDevice.Clear(Color.Transparent);
target.drawFunct(Main.spriteBatch);
Main.spriteBatch.End();
}
}
Main.graphics.GraphicsDevice.SetRenderTargets(bindings);
targetSem.Release();
}
public override void PostUpdateEverything()
{
if (!wasIngame)
{
firstResizeTime = 0;
wasIngame = true;
}
else
{
firstResizeTime++;
}
if (firstResizeTime == 20)
ResizeScreens(new Vector2(Main.screenWidth, Main.screenHeight));
}
private void MenuUpdate(On_Main.orig_UpdateMenu orig)
{
if (wasIngame)
{
firstResizeTime = 0;
wasIngame = false;
}
else
{
firstResizeTime++;
}
if (firstResizeTime == 20)
ResizeScreens(new Vector2(Main.screenWidth, Main.screenHeight));
orig();
}
}