-
Notifications
You must be signed in to change notification settings - Fork 14
/
Renderer.cs
426 lines (351 loc) · 16.6 KB
/
Renderer.cs
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using System.Text;
using Rampastring.Tools;
using System.Globalization;
#if XNA
using System.Reflection;
#endif
namespace Rampastring.XNAUI;
public struct SpriteBatchSettings
{
public SpriteBatchSettings(SpriteSortMode ssm, BlendState bs, SamplerState ss, DepthStencilState dss, RasterizerState rs, Effect effect)
{
SpriteSortMode = ssm;
BlendState = bs;
SamplerState = ss;
DepthStencilState = dss;
RasterizerState = rs;
Effect = effect;
}
public readonly SpriteSortMode SpriteSortMode;
public readonly SamplerState SamplerState;
public readonly BlendState BlendState;
public readonly DepthStencilState DepthStencilState;
public readonly RasterizerState RasterizerState;
public readonly Effect Effect;
}
/// <summary>
/// Provides static methods for drawing.
/// </summary>
public static class Renderer
{
private static SpriteBatch spriteBatch;
private static List<SpriteFont> fonts;
private static Texture2D whitePixelTexture;
private static readonly LinkedList<SpriteBatchSettings> settingStack = new LinkedList<SpriteBatchSettings>();
internal static SpriteBatchSettings CurrentSettings;
public static SpriteBatchSettings GetCurrentSettings() => CurrentSettings;
public static void Initialize(GraphicsDevice gd, ContentManager content)
{
spriteBatch = new SpriteBatch(gd);
fonts = new List<SpriteFont>();
LoadFonts(content);
whitePixelTexture = AssetLoader.CreateTexture(Color.White, 1, 1);
}
/// <summary>
/// Clears all potentially existing loaded fonts and then loads fonts from asset loader directories.
/// </summary>
/// <param name="contentManager">A XNA/MonoGame ContentManager instance.</param>
public static void LoadFonts(ContentManager contentManager)
{
if (fonts == null)
fonts = new List<SpriteFont>();
else
fonts.Clear();
string originalContentRoot = contentManager.RootDirectory;
#if XNA
var contentManagerType = contentManager.GetType();
var rootDirectoryField = contentManagerType.GetField("rootDirectory", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.SetField | BindingFlags.GetField);
var fullRootDirectoryField = contentManager.GetType().GetField("fullRootDirectory", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.SetField | BindingFlags.GetField);
#endif
foreach (string searchPath in AssetLoader.AssetSearchPaths)
{
string newRootDirectory = SafePath.GetDirectory(searchPath).FullName;
while (true)
{
string sfName = string.Format(CultureInfo.InvariantCulture, "SpriteFont{0}", fonts.Count);
if (!SafePath.GetFile(searchPath, FormattableString.Invariant($"{sfName}.xnb")).Exists)
break;
#if !XNA
contentManager.RootDirectory = newRootDirectory;
#else
// XNA does not allow changing the value of RootDirectory after the
// content manager has been used. However, it has some internal fields
// we can modify through reflection to achieve the same.
// This would be a very bad solution when using a library that
// is updated regularly, but since XNA has been EOL for over a decade
// by this point, its internal logic is never going to change.
rootDirectoryField.SetValue(contentManager, newRootDirectory);
fullRootDirectoryField.SetValue(contentManager, newRootDirectory);
#endif
fonts.Add(contentManager.Load<SpriteFont>(sfName));
}
}
#if !XNA
contentManager.RootDirectory = originalContentRoot;
#else
rootDirectoryField.SetValue(contentManager, originalContentRoot);
fullRootDirectoryField.SetValue(contentManager, originalContentRoot);
#endif
}
/// <summary>
/// Allows direct access to the list of loaded fonts.
/// </summary>
public static List<SpriteFont> GetFontList() => fonts;
/// <summary>
/// Returns a version of the given string where all characters that don't
/// appear in the given font have been replaced with question marks.
/// </summary>
/// <param name="str">The string.</param>
/// <param name="fontIndex">The index of the font.</param>
public static string GetSafeString(string str, int fontIndex)
{
SpriteFont sf = fonts[fontIndex];
var sb = new StringBuilder(str);
for (int i = 0; i < str.Length; i++)
{
char c = str[i];
if (c != '\r' && c != '\n' && !sf.Characters.Contains(c))
sb.Replace(c, '?');
}
return sb.ToString();
}
/// <summary>
/// Returns a that has had its width limited to a specific number.
/// Characters that'd cross over the width have been cut.
/// </summary>
/// <param name="str">The string to limit.</param>
/// <param name="fontIndex">The index of the font to use.</param>
/// <param name="maxWidth">The maximum width of the string.</param>
/// <returns></returns>
public static string GetStringWithLimitedWidth(string str, int fontIndex, int maxWidth)
{
var sb = new StringBuilder(str);
var spriteFont = fonts[fontIndex];
while (spriteFont.MeasureString(sb.ToString()).X > maxWidth)
{
sb.Remove(sb.Length - 1, 1);
}
return sb.ToString();
}
public static TextParseReturnValue FixText(string text, int fontIndex, int width)
{
return TextParseReturnValue.FixText(fonts[fontIndex], width, text);
}
public static List<string> GetFixedTextLines(string text, int fontIndex, int width, bool splitWords = true, bool keepBlankLines = false)
{
return TextParseReturnValue.GetFixedTextLines(fonts[fontIndex], width, text, splitWords, keepBlankLines);
}
/// <summary>
/// Pushes new settings into the renderer's internal stack and applies them.
/// A call to <see cref="PushSettings(SpriteBatchSettings)"/> should always
/// be followed by <see cref="PopSettings"/> once drawing with the new settings is done.
/// </summary>
/// <param name="settings">The sprite batch settings.</param>
public static void PushSettings(SpriteBatchSettings settings)
{
EndDraw();
PushSettingsInternal();
CurrentSettings = settings;
BeginDrawInternal(CurrentSettings);
}
/// <summary>
/// Pops previous settings from the renderer's internal stack and applies them.
/// </summary>
public static void PopSettings()
{
EndDraw();
PopSettingsInternal();
BeginDrawInternal(CurrentSettings);
}
/// <summary>
/// Changes current rendering settings. This can be called between
/// <see cref="PushSettings(SpriteBatchSettings)"/> and <see cref="PopSettings"/>
/// when you want to draw something with new settings, but there's no reason
/// to save those settings.
/// </summary>
/// <param name="settings">The sprite batch settings.</param>
public static void ChangeSettings(SpriteBatchSettings settings)
{
EndDraw();
CurrentSettings = settings;
BeginDrawInternal(CurrentSettings);
}
/// <summary>
/// Prepares the renderer for drawing a batch of sprites.
/// </summary>
public static void BeginDraw()
{
BeginDrawInternal(CurrentSettings);
}
/// <summary>
/// Draws the currently queued batch of sprites.
/// </summary>
public static void EndDraw()
{
spriteBatch.End();
}
public static void PushRenderTarget(RenderTarget2D renderTarget) => RenderTargetStack.PushRenderTarget(renderTarget,
new SpriteBatchSettings(SpriteSortMode.Deferred, BlendState.AlphaBlend, null, null, null, null));
public static void PushRenderTargets(RenderTarget2D renderTarget, RenderTarget2D renderTarget2) =>
RenderTargetStack.PushRenderTargets(CurrentSettings, renderTarget, renderTarget2);
public static void PushRenderTargets(RenderTarget2D renderTarget, RenderTarget2D renderTarget2, RenderTarget2D renderTarget3) =>
RenderTargetStack.PushRenderTargets(CurrentSettings, renderTarget, renderTarget2, renderTarget3);
public static void PushRenderTargets(RenderTarget2D renderTarget, RenderTarget2D renderTarget2, RenderTarget2D renderTarget3, RenderTarget2D renderTarget4) =>
RenderTargetStack.PushRenderTargets(CurrentSettings, renderTarget, renderTarget2, renderTarget3, renderTarget4);
public static void PushRenderTarget(RenderTarget2D renderTarget, SpriteBatchSettings settings) => RenderTargetStack.PushRenderTarget(renderTarget, settings);
public static void PopRenderTarget() => RenderTargetStack.PopRenderTarget();
//BlendState blendState = new BlendState();
//blendState.AlphaDestinationBlend = Blend.One;
//blendState.ColorDestinationBlend = Blend.InverseSourceAlpha;
//blendState.AlphaSourceBlend = Blend.SourceAlpha;
//blendState.ColorSourceBlend = Blend.SourceAlpha;
internal static void BeginDrawInternal(SpriteBatchSettings settings) =>
BeginDrawInternal(settings.SpriteSortMode, settings.BlendState, settings.SamplerState, settings.DepthStencilState, settings.RasterizerState, settings.Effect);
internal static void BeginDrawInternal(SpriteSortMode ssm, BlendState bs, SamplerState ss, DepthStencilState dss, RasterizerState rs, Effect effect)
{
#if XNA
spriteBatch.Begin(ssm, bs, ss, DepthStencilState.Default, RasterizerState.CullNone);
#else
spriteBatch.Begin(ssm, bs, ss, dss, rs, effect);
#endif
}
internal static void PushSettingsInternal()
{
settingStack.AddFirst(CurrentSettings);
}
internal static void PopSettingsInternal()
{
CurrentSettings = settingStack.First.Value;
settingStack.RemoveFirst();
}
internal static void ClearStack()
{
settingStack.Clear();
}
#region Rendering code
public static void DrawTexture(Texture2D texture, Rectangle rectangle, Color color)
{
spriteBatch.Draw(texture, rectangle, color);
}
public static void DrawTexture(Texture2D texture, Rectangle sourceRectangle, Rectangle destinationRectangle, Color color)
{
spriteBatch.Draw(texture, destinationRectangle, sourceRectangle, color);
}
public static void DrawTexture(Texture2D texture, Rectangle sourceRectangle, Vector2 location, float rotation, Vector2 origin, Vector2 scale, Color color, float layerDepth = 0f)
{
spriteBatch.Draw(texture, location, sourceRectangle, color, rotation, origin, scale, SpriteEffects.None, layerDepth);
}
public static void DrawTexture(Texture2D texture, Rectangle destinationRectangle, Rectangle? sourceRectangle, Color color, float rotation, Vector2 origin, SpriteEffects effects, float layerDepth)
{
spriteBatch.Draw(texture, destinationRectangle, sourceRectangle, color, rotation, origin, effects, layerDepth);
}
public static void DrawTexture(Texture2D texture, Vector2 location, float rotation, Vector2 origin, Vector2 scale, Color color, float layerDepth = 0f)
{
spriteBatch.Draw(texture, location, null, color, rotation, origin, scale, SpriteEffects.None, layerDepth);
}
/// <summary>
/// Draws a circle's perimiter.
/// </summary>
/// <param name="position">The center point of the circle.</param>
/// <param name="radius">The radius of the circle.</param>
/// <param name="color">The color of the circle.</param>
/// <param name="precision">Defines how smooth the circle's perimiter is.
/// Larger values make the circle smoother, but have a larger effect on performance.</param>
/// <param name="thickness">The thickness of the perimiter.</param>
public static void DrawCircle(Vector2 position, float radius, Color color, int precision = 8, int thickness = 1)
{
float angle = 0f;
float increase = (float)Math.PI * 2f / precision;
Vector2 point = position + RMath.VectorFromLengthAndAngle(radius, angle);
for (int i = 0; i <= precision; i++)
{
Vector2 nextPoint = position + RMath.VectorFromLengthAndAngle(radius, angle);
DrawLine(point, nextPoint, color, thickness);
point = nextPoint;
angle += increase;
}
}
/// <summary>
/// Draws a circle where the circle's perimeter is dotted with a texture.
/// </summary>
/// <param name="position">The center point of the circle.</param>
/// <param name="radius">The radius of the circle.</param>
/// <param name="texture">The texture to dot the circle's perimiter with.</param>
/// <param name="color">The remap color of the texture.</param>
/// <param name="precision">How many times the texture is drawn on the perimiter.</param>
/// <param name="scale">The scale of the drawn texture compared to the size of the texture itself.</param>
/// <param name="layerDepth">The depth of the texture.</param>
public static void DrawCircleWithTexture(Vector2 position, float radius,
Texture2D texture, Color color, int precision = 8, float scale = 1f, float layerDepth = 0f)
{
float angle = 0f;
float increase = (float)Math.PI * 2f / precision;
Vector2 point = position + RMath.VectorFromLengthAndAngle(radius, angle);
for (int i = 0; i <= precision; i++)
{
DrawTexture(texture, point, 0f,
new Vector2(texture.Width / 2f, texture.Height / 2f),
new Vector2(scale, scale), color, layerDepth);
point = position + RMath.VectorFromLengthAndAngle(radius, angle);
angle += increase;
}
}
public static void DrawString(string text, int fontIndex, Vector2 location, Color color, float scale = 1.0f, float depth = 0f)
{
if (fontIndex >= fonts.Count)
throw new Exception("Invalid font index: " + fontIndex);
spriteBatch.DrawString(fonts[fontIndex], text, location, color, 0f, Vector2.Zero, scale, SpriteEffects.None, depth);
}
public static void DrawStringWithShadow(string text, int fontIndex, Vector2 location, Color color, float scale = 1.0f, float shadowDistance = 1.0f, float depth = 0f)
{
if (fontIndex >= fonts.Count)
throw new Exception("Invalid font index: " + fontIndex);
#if XNA
spriteBatch.DrawString(fonts[fontIndex], text,
new Vector2(location.X + shadowDistance, location.Y + shadowDistance),
new Color(0, 0, 0, color.A));
#else
spriteBatch.DrawString(fonts[fontIndex], text,
new Vector2(location.X + shadowDistance, location.Y + shadowDistance),
UISettings.ActiveSettings.TextShadowColor * (color.A / 255.0f),
0f, Vector2.Zero, scale, SpriteEffects.None, depth);
#endif
spriteBatch.DrawString(fonts[fontIndex], text, location, color, 0f, Vector2.Zero, scale, SpriteEffects.None, depth);
}
public static void DrawRectangle(Rectangle rect, Color color, int thickness = 1)
{
spriteBatch.Draw(whitePixelTexture, new Rectangle(rect.X, rect.Y, rect.Width, thickness), color);
spriteBatch.Draw(whitePixelTexture, new Rectangle(rect.X, rect.Y + thickness, thickness, rect.Height - thickness), color);
spriteBatch.Draw(whitePixelTexture, new Rectangle(rect.X + rect.Width - thickness, rect.Y, thickness, rect.Height), color);
spriteBatch.Draw(whitePixelTexture, new Rectangle(rect.X, rect.Y + rect.Height - thickness, rect.Width, thickness), color);
}
public static void FillRectangle(Rectangle rect, Color color)
{
spriteBatch.Draw(whitePixelTexture, rect, color);
}
public static Vector2 GetTextDimensions(string text, int fontIndex)
{
if (fontIndex >= fonts.Count)
throw new Exception("Invalid font index: " + fontIndex);
return fonts[fontIndex].MeasureString(text);
}
public static void DrawLine(Vector2 start, Vector2 end, Color color, int thickness = 1, float depth = 0f)
{
Vector2 line = end - start;
if (thickness > 1)
{
Vector2 offset = RMath.VectorFromLengthAndAngle(thickness / 2, RMath.AngleFromVector(line) - (float)Math.PI / 2.0f);
end += offset;
start += offset;
}
spriteBatch.Draw(whitePixelTexture,
new Rectangle((int)start.X, (int)start.Y, (int)line.Length(), thickness),
null, color, (float)Math.Atan2(line.Y, line.X), new Vector2(0, 0), SpriteEffects.None, depth);
}
#endregion
}