recrownedgtk/RecrownedAthenaeum/Render/ScissorBox.cs

53 lines
1.6 KiB
C#
Raw Normal View History

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace RecrownedAthenaeum.Render
{
/// <summary>
/// Box that crops off anything outside of the bounds.
/// </summary>
public class ScissorBox
{
ConsistentSpriteBatch spriteBatch;
Rectangle originalScissor;
RasterizerState originalRasterizerState;
RasterizerState scissorRasterizerState;
/// <summary>
/// Used to crop off anything that doesn't fit into the defined space.
/// </summary>
public ScissorBox()
{
scissorRasterizerState = new RasterizerState();
scissorRasterizerState.ScissorTestEnable = true;
}
/// <summary>
/// Starts spritebatch with scissoring enabled.
/// </summary>
/// <param name="rectangle"></param>
/// <param name="spriteBatch"></param>
public void Begin(Rectangle rectangle, ConsistentSpriteBatch spriteBatch)
{
this.spriteBatch = spriteBatch;
originalRasterizerState = spriteBatch.RasterizerState;
spriteBatch.Begin(rasterizerState: scissorRasterizerState);
originalScissor = spriteBatch.GraphicsDevice.ScissorRectangle;
spriteBatch.GraphicsDevice.ScissorRectangle = rectangle;
}
/// <summary>
/// Ends the scissoring and spritebatch.
/// </summary>
public void End()
{
spriteBatch.GraphicsDevice.ScissorRectangle = originalScissor;
spriteBatch.RasterizerState = originalRasterizerState;
spriteBatch.End();
}
}
}