using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace MrGameEng.Host;
///
/// Minimal overlay renderer handed to : fills rectangles
/// in normalized screen coordinates (0..1 on both axes) over the rendered scene.
///
public sealed class TransitionRenderer : IDisposable
{
private readonly GraphicsDevice _device;
private readonly BasicEffect _effect;
private readonly VertexPositionColor[] _vertices = new VertexPositionColor[6];
/// Disposes the GPU effect. Called by on shutdown.
public void Dispose() => _effect.Dispose();
internal TransitionRenderer(GraphicsDevice device)
{
_device = device;
_effect = new BasicEffect(device)
{
VertexColorEnabled = true,
TextureEnabled = false,
World = Matrix.Identity,
View = Matrix.Identity,
Projection = Matrix.CreateOrthographicOffCenter(0f, 1f, 1f, 0f, 0f, 1f),
};
}
///
/// Fills a rectangle given in normalized screen coordinates with
/// at the given (0 = invisible, 1 = solid).
///
public void Fill(float x, float y, float width, float height, Color color, float opacity = 1f)
{
var alpha = (byte)(Math.Clamp(opacity, 0f, 1f) * color.A);
var premultiplied = Color.FromNonPremultiplied(color.R, color.G, color.B, alpha);
var topLeft = new Vector3(x, y, 0f);
var topRight = new Vector3(x + width, y, 0f);
var bottomLeft = new Vector3(x, y + height, 0f);
var bottomRight = new Vector3(x + width, y + height, 0f);
_vertices[0] = new VertexPositionColor(topLeft, premultiplied);
_vertices[1] = new VertexPositionColor(topRight, premultiplied);
_vertices[2] = new VertexPositionColor(bottomLeft, premultiplied);
_vertices[3] = new VertexPositionColor(bottomLeft, premultiplied);
_vertices[4] = new VertexPositionColor(topRight, premultiplied);
_vertices[5] = new VertexPositionColor(bottomRight, premultiplied);
_device.BlendState = BlendState.AlphaBlend;
_device.DepthStencilState = DepthStencilState.None;
_device.RasterizerState = RasterizerState.CullNone;
foreach (var pass in _effect.CurrentTechnique.Passes)
{
pass.Apply();
_device.DrawUserPrimitives(PrimitiveType.TriangleList, _vertices, 0, 2);
}
}
}