diff --git a/Backgrounds/SunkenSeaBurrowsBG.cs b/Backgrounds/SunkenSeaBurrowsBG.cs index 0d8b2e00d9..ee9a45fe41 100644 --- a/Backgrounds/SunkenSeaBurrowsBG.cs +++ b/Backgrounds/SunkenSeaBurrowsBG.cs @@ -6,6 +6,7 @@ using ReLogic.Content; using Terraria.ID; using CalamityMod.Graphics; using Terraria.Graphics.Shaders; +using Terraria.Graphics.Shaders; namespace CalamityMod.Backgrounds { diff --git a/CalamityMod.csproj b/CalamityMod.csproj index c37bd524b1..a6d337d349 100644 --- a/CalamityMod.csproj +++ b/CalamityMod.csproj @@ -1,6 +1,6 @@ - + latest @@ -8,7 +8,7 @@ - + - + - + - + diff --git a/Systems/TileMerging/TileBlendTexture_Baking.cs b/Systems/TileMerging/TileBlendTexture_Baking.cs index 96f2af8361..1209b8c1cc 100644 --- a/Systems/TileMerging/TileBlendTexture_Baking.cs +++ b/Systems/TileMerging/TileBlendTexture_Baking.cs @@ -16,7 +16,6 @@ namespace CalamityMod.Systems Main.QueueMainThreadAction(() => { BakeBlendTexture_Inner(texture); - // TODO: [SAFEACTION] Bandaid fix }); } diff --git a/Utilities/Daybreak/README.txt b/Utilities/Daybreak/README.txt new file mode 100644 index 0000000000..c66eb9288b --- /dev/null +++ b/Utilities/Daybreak/README.txt @@ -0,0 +1,5 @@ +Everything in this directory is adapted from DAYBREAK and jointly authored by +tomat and syrup. Explicit permission is given to use this code in Calamity +against the AGPL 3.0 license. + + diff --git a/Utilities/Daybreak/SpriteBatchParameters.cs b/Utilities/Daybreak/SpriteBatchParameters.cs new file mode 100644 index 0000000000..bd13e37c9b --- /dev/null +++ b/Utilities/Daybreak/SpriteBatchParameters.cs @@ -0,0 +1,93 @@ +#nullable enable + +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; + +namespace CalamityMod.Utilities.Daybreak; + +/// +/// Wraps the parameters of a initialization, +/// permitting values which may be replaced with +/// defaults when being converted to a snapshot. +/// +public struct SpriteBatchParameters +{ + /// + /// The sort mode. + /// + public SpriteSortMode? SortMode { get; set; } + + /// + /// The blend state. + /// + public BlendState? BlendState { get; set; } + + /// + /// The sampler state. + /// + public SamplerState? SamplerState { get; set; } + + /// + /// The depth stencil state. + /// + public DepthStencilState? DepthStencilState { get; set; } + + /// + /// The rasterizer state. + /// + public RasterizerState? RasterizerState { get; set; } + + /// + /// The custom effect, if applicable. + /// + public Effect? CustomEffect { get; set; } + + /// + /// The transformation matrix. + /// + public Matrix? TransformMatrix { get; set; } + + /// + /// Initializes a new set of parameters. + /// + public SpriteBatchParameters( + SpriteSortMode? sortMode = null, + BlendState? blendState = null, + SamplerState? samplerState = null, + DepthStencilState? depthStencilState = null, + RasterizerState? rasterizerState = null, + Effect? customEffect = null, + Matrix? transformMatrix = null + ) + { + SortMode = sortMode; + BlendState = blendState; + SamplerState = samplerState; + DepthStencilState = depthStencilState; + RasterizerState = rasterizerState; + CustomEffect = customEffect; + TransformMatrix = transformMatrix; + } + + /// + /// Creates a new , with + /// values being replaced with the values + /// provided by . + /// + /// + /// Default values to populate s with. + /// + /// The new snapshot. + public readonly SpriteBatchSnapshot ToSnapshot(SpriteBatchSnapshot defaultValues) + { + return new SpriteBatchSnapshot( + SortMode ?? defaultValues.SortMode, + BlendState ?? defaultValues.BlendState, + SamplerState ?? defaultValues.SamplerState, + DepthStencilState ?? defaultValues.DepthStencilState, + RasterizerState ?? defaultValues.RasterizerState, + CustomEffect ?? defaultValues.CustomEffect, + TransformMatrix ?? defaultValues.TransformMatrix + ); + } +} diff --git a/Utilities/Daybreak/SpriteBatchScope.cs b/Utilities/Daybreak/SpriteBatchScope.cs new file mode 100644 index 0000000000..98dc54f403 --- /dev/null +++ b/Utilities/Daybreak/SpriteBatchScope.cs @@ -0,0 +1,67 @@ +#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. +/// +public 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 (spriteBatch.beginCalled) + { + spriteBatch.End(); + } + + if (oldState.HasValue) + { + spriteBatch.Begin(oldState.Value); + } + } +} + +/// +/// Extensions to types for scopes. +/// +public static class SpriteBatchScopeExtensions +{ + /// + /// See . + /// + public static SpriteBatchScope Scope(this SpriteBatch @this) + { + return new SpriteBatchScope(@this); + } +} diff --git a/Utilities/Daybreak/SpriteBatchSnapshot.cs b/Utilities/Daybreak/SpriteBatchSnapshot.cs new file mode 100644 index 0000000000..ea68a99d5d --- /dev/null +++ b/Utilities/Daybreak/SpriteBatchSnapshot.cs @@ -0,0 +1,168 @@ +#nullable enable + +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; + +namespace CalamityMod.Utilities.Daybreak; + +/// +/// A "snapshot" of the current state of a . +///
+/// These values may be manipulated freely. +///
+/// +/// This API exists for making preservation of a 's +/// state trivial. +///
+/// The act of taking a snapshot through this object's constructor is pure +/// (that is, it has no side effects). It will not mutate the state of the +/// being analyzed. If you intend to modify the +/// , use the APIs provided in +/// . +///
+public struct SpriteBatchSnapshot +{ + /// + /// The sort mode. + /// + public SpriteSortMode SortMode { get; set; } + + /// + /// The blend state. + /// + public BlendState BlendState { get; set; } + + /// + /// The sampler state. + /// + public SamplerState SamplerState { get; set; } + + /// + /// The depth stencil state. + /// + public DepthStencilState DepthStencilState { get; set; } + + /// + /// The rasterizer state. + /// + public RasterizerState RasterizerState { get; set; } + + /// + /// The custom effect, if applicable. + /// + public Effect? CustomEffect { get; set; } + + /// + /// The transformation matrix. + /// + public Matrix TransformMatrix { get; set; } + + /// + /// Creates a new snapshot from raw + /// parameters. + /// + public SpriteBatchSnapshot( + SpriteSortMode sortMode, + BlendState blendState, + SamplerState samplerState, + DepthStencilState depthStencilState, + RasterizerState rasterizerState, + Effect? customEffect, + Matrix transformMatrix + ) + { + SortMode = sortMode; + BlendState = blendState; + SamplerState = samplerState; + DepthStencilState = depthStencilState; + RasterizerState = rasterizerState; + CustomEffect = customEffect; + TransformMatrix = transformMatrix; + } + + /// + /// Creates a new snapshot from the current + /// settings of the given . + /// + /// + /// The to take a snapshot of. + /// + public SpriteBatchSnapshot(SpriteBatch spriteBatch) + { + SortMode = spriteBatch.sortMode; + BlendState = spriteBatch.blendState; + SamplerState = spriteBatch.samplerState; + DepthStencilState = spriteBatch.depthStencilState; + RasterizerState = spriteBatch.rasterizerState; + CustomEffect = spriteBatch.customEffect; + TransformMatrix = spriteBatch.transformMatrix; + } + + /// + /// Initializes a set of parameters from this snapshot. + /// + public readonly SpriteBatchParameters ToParameters() + { + return new SpriteBatchParameters( + SortMode, + BlendState, + SamplerState, + DepthStencilState, + RasterizerState, + CustomEffect, + TransformMatrix + ); + } +} + +/// +/// Extensions to using +/// instances. +/// +public static class SpriteBatchSnapshotExtensions +{ + /// The . + extension(SpriteBatch sb) + { + /// + /// Takes a snapshot of the and then ends the + /// / + /// + /// The produced . + public void End(out SpriteBatchSnapshot ss) + { + ss = new SpriteBatchSnapshot(sb); + sb.End(); + } + + /// + /// Starts a with the parameters from the + /// given . + /// + /// The to use. + public void Begin(in SpriteBatchSnapshot ss) + { + sb.Begin( + ss.SortMode, + ss.BlendState, + ss.SamplerState, + ss.DepthStencilState, + ss.RasterizerState, + ss.CustomEffect, + ss.TransformMatrix + ); + } + + /// + /// Immediately ends and then starts the given + /// with the parameters from the given + /// . + /// + /// The to use. + public void Restart(in SpriteBatchSnapshot ss) + { + sb.End(); + sb.Begin(ss); + } + } +} diff --git a/Utilities/DrawingUtils.SpriteBatch.cs b/Utilities/DrawingUtils.SpriteBatch.cs index 2828d1a33a..96df255246 100644 --- a/Utilities/DrawingUtils.SpriteBatch.cs +++ b/Utilities/DrawingUtils.SpriteBatch.cs @@ -93,106 +93,5 @@ namespace CalamityMod return true; } } - - internal readonly record struct SpritebatchParameters( - BlendState BlendState, - SpriteSortMode SortMode, - DepthStencilState DepthStencilState, - SamplerState SamplerState, - RasterizerState RasterizerState, - Effect Effect, - Matrix TransformMatrix - ); - - internal static void End(this SpriteBatch spriteBatch, out SpritebatchParameters parameters) - { - spriteBatch.GetParameters(out parameters); - - spriteBatch.End(); - } - - internal static void GetParameters(this SpriteBatch spriteBatch, out SpritebatchParameters parameters) - { - parameters = new SpritebatchParameters( - spriteBatch.blendState, - spriteBatch.sortMode, - spriteBatch.depthStencilState, - spriteBatch.samplerState, - spriteBatch.rasterizerState, - spriteBatch.customEffect, - spriteBatch.transformMatrix - ); - } - - internal static void Begin(this SpriteBatch spriteBatch, in SpritebatchParameters parameters) - { - spriteBatch.Begin(parameters.SortMode, - parameters.BlendState, - parameters.SamplerState, - parameters.DepthStencilState, - parameters.RasterizerState, - parameters.Effect, - parameters.TransformMatrix); - } - - internal static void Restart(this SpriteBatch spriteBatch) - { - spriteBatch.End(out var sp); - spriteBatch.Begin(in sp); - } - - internal static void Restart(this SpriteBatch spriteBatch, in SpritebatchParameters parameters) - { - spriteBatch.End(); - spriteBatch.Begin(parameters); - } - - internal class SpritebatchScope : IDisposable - { - private readonly SpritebatchParameters _parameters; - private readonly SpriteBatch _sb; - /// - /// Takes in a spritebatch and gets from it without ending or otherwise mutating it. - /// - /// - public SpritebatchScope(SpriteBatch sb) - { - _sb = sb; - _sb.GetParameters(out _parameters); - } - /// - /// Takes in a spritebatch and gets from it before restarting it with the input . - /// - /// - /// - public SpritebatchScope(SpriteBatch sb, SpritebatchParameters parameters) - { - _sb = sb; - _sb.GetParameters(out _parameters); - _sb.Restart(parameters); - } - /// - /// SafeBegin equivalent; takes in a , gets from it, and then uses the and other parameters to start a new . - /// - /// - /// - /// - /// - /// - public SpritebatchScope(SpriteBatch sb, SpriteSortMode sortMode, BatchSetting settings, Effect effect, Matrix transformMatrix, bool end = false) - { - _sb = sb; - _sb.GetParameters(out _parameters); - if (end) { - _sb.End(); - } - _sb.Begin(sortMode, settings.blendState, settings.samplerState, settings.depthStencilState, settings.rasterizerState ?? Main.Rasterizer, effect, transformMatrix); - } - - public void Dispose() - { - _sb.Restart(_parameters); - } - } } }