using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; namespace MLEM.Graphics { /// /// A sprite batch context is a set of information for a to use, which encapsulates all of the information usually passed directly to SpriteBatch.Begin. /// To use a sprite batch context effectively, the extension methods in should be used. /// public struct SpriteBatchContext { /// /// The drawing order for sprite and text drawing. /// public SpriteSortMode SortMode; /// /// State of the blending. /// public BlendState BlendState; /// /// State of the sampler. /// public SamplerState SamplerState; /// /// State of the depth-stencil buffer. /// public DepthStencilState DepthStencilState; /// /// State of the rasterization. /// public RasterizerState RasterizerState; /// /// A custom to override the default sprite effect. /// public Effect Effect; /// /// An optional matrix used to transform the sprite geometry. /// public Matrix TransformMatrix; /// /// Creates a new sprite batch context with the given parameters. /// /// The drawing order for sprite and text drawing. by default. /// State of the blending. Uses if null. /// State of the sampler. Uses if null. /// State of the depth-stencil buffer. Uses if null. /// State of the rasterization. Uses if null. /// A custom to override the default sprite effect. /// An optional matrix used to transform the sprite geometry. Uses if null. public SpriteBatchContext(SpriteSortMode sortMode = SpriteSortMode.Deferred, BlendState blendState = null, SamplerState samplerState = null, DepthStencilState depthStencilState = null, RasterizerState rasterizerState = null, Effect effect = null, Matrix? transformMatrix = null) { this.SortMode = sortMode; this.BlendState = blendState ?? BlendState.AlphaBlend; this.SamplerState = samplerState ?? SamplerState.LinearClamp; this.DepthStencilState = depthStencilState ?? DepthStencilState.None; this.RasterizerState = rasterizerState ?? RasterizerState.CullCounterClockwise; this.Effect = effect; this.TransformMatrix = transformMatrix ?? Matrix.Identity; } /// /// Creates a new sprite batch context from the passed 's current information. /// This can be useful to retrieve some information about drawing right after a call has occured. /// /// The graphics device to query data from. /// The drawing order for sprite and text drawing. by default. /// A custom to override the default sprite effect. /// An optional matrix used to transform the sprite geometry. Uses if null. /// A new sprite batch context from the 's current information. public static SpriteBatchContext Current(GraphicsDevice device, SpriteSortMode sortMode = SpriteSortMode.Deferred, Effect effect = null, Matrix? transformMatrix = null) { return new SpriteBatchContext(sortMode, device.BlendState, device.SamplerStates[0], device.DepthStencilState, device.RasterizerState, effect, transformMatrix); } } /// /// A set of extensions for . /// public static class SpriteBatchContextExtensions { /// /// Begins a new sprite and text batch with the specified /// /// The sprite batch to use for drawing. /// The sprite batch context to use. public static void Begin(this SpriteBatch batch, SpriteBatchContext context) { batch.Begin(context.SortMode, context.BlendState, context.SamplerState, context.DepthStencilState, context.RasterizerState, context.Effect, context.TransformMatrix); } /// /// Draws the given batch's content onto the 's current render target (or the back buffer) with the given settings. /// Note that this method should not be called while a regular is currently active. /// /// The static sprite batch to use for drawing. /// The sprite batch context to use. public static void Draw(this StaticSpriteBatch batch, SpriteBatchContext context) { batch.Draw(context.BlendState, context.SamplerState, context.DepthStencilState, context.RasterizerState, context.Effect, context.TransformMatrix); } } }