#nullable enable
using System;
using Microsoft.Xna.Framework.Graphics;
namespace CalamityMod.Utilities.Daybreak;
///
/// Starts a and caches its old start parameters
/// if it was begun to restore it on disposal.
///
internal readonly struct SpriteBatchScope : IDisposable
{
private readonly SpriteBatch spriteBatch;
private readonly SpriteBatchSnapshot? oldState;
///
/// Initializes a new scope. If the has
/// already begun, its current parameters are saved and the
/// is ended; the original parameters will
/// then be reapplied on disposal.
///
/// The .
public SpriteBatchScope(SpriteBatch spriteBatch)
{
this.spriteBatch = spriteBatch;
if (!spriteBatch.beginCalled)
{
return;
}
spriteBatch.End(out var old);
oldState = old;
}
///
/// Ends the and starts it with the old
/// parameters if it has already begun prior.
///
public void Dispose()
{
if (oldState.HasValue)
{
spriteBatch.Begin(oldState.Value);
}
}
}
///
/// Extensions to types for scopes.
///
internal static class SpriteBatchScopeExtensions
{
///
/// See .
///
public static SpriteBatchScope Scope(this SpriteBatch @this)
{
return new SpriteBatchScope(@this);
}
}