diff --git a/CalamityMod.cs b/CalamityMod.cs index afa5efb275..779a98907d 100644 --- a/CalamityMod.cs +++ b/CalamityMod.cs @@ -20,6 +20,7 @@ using CalamityMod.Items.Dyes.HairDye; using CalamityMod.Items.PermanentBoosters; using CalamityMod.Items.Pets; using CalamityMod.Items.VanillaArmorChanges; +using CalamityMod.Items.Weapons.Magic; using CalamityMod.Items.Weapons.Ranged; using CalamityMod.Localization; using CalamityMod.NPCs.AdultEidolonWyrm; diff --git a/DataStructures/IAdditiveDrawer.cs b/DataStructures/IAdditiveDrawer.cs new file mode 100644 index 0000000000..1081cf511b --- /dev/null +++ b/DataStructures/IAdditiveDrawer.cs @@ -0,0 +1,17 @@ +using Microsoft.Xna.Framework.Graphics; + +namespace CalamityMod.DataStructures +{ + + // Restarting the sprite batch for each different projectile is analogously equivalent to creating an entire new factory just to build a single car. + // It is inefficient and leads to performance problems as many projectiles do it. + // To mitigate this problem, a special interface exists for the purpose of deferring drawing of certain entities to a time in which they all may be + // subject to the same additive draw state before all of the spritebatch's contents are flushed. + // Manual drawing hooks should not be utilized when using this interface, such as ModNPC.PreDraw. + + // The implementation of this is almost identical to that of SLR but they're both so simple that it really shouldn't matter. + public interface IAdditiveDrawer + { + void AdditiveDraw(SpriteBatch spriteBatch); + } +} diff --git a/ILEditing/ILChangesLoading.cs b/ILEditing/ILChangesLoading.cs index b21c97043d..c553388b06 100644 --- a/ILEditing/ILChangesLoading.cs +++ b/ILEditing/ILChangesLoading.cs @@ -50,7 +50,7 @@ namespace CalamityMod.ILEditing IL.Terraria.Player.QuickHeal += ConditionallyReplaceManaSickness; IL.Terraria.Player.QuickMana += ConditionallyReplaceManaSickness; IL.Terraria.Player.ItemCheck_Inner += ConditionallyReplaceManaSickness; - IL.Terraria.Main.DoDraw += UseCustomDrawLayers; + IL.Terraria.Main.DoDraw += AdditiveDrawing; On.Terraria.Main.SortDrawCacheWorms += DrawFusableParticles; On.Terraria.Main.DrawInfernoRings += DrawForegroundParticles; On.Terraria.Main.SetDisplayMode += ResetRenderTargetSizes; @@ -164,7 +164,7 @@ namespace CalamityMod.ILEditing IL.Terraria.Player.QuickHeal -= ConditionallyReplaceManaSickness; IL.Terraria.Player.QuickMana -= ConditionallyReplaceManaSickness; IL.Terraria.Player.ItemCheck_Inner -= ConditionallyReplaceManaSickness; - IL.Terraria.Main.DoDraw -= UseCustomDrawLayers; + IL.Terraria.Main.DoDraw -= AdditiveDrawing; On.Terraria.Main.SortDrawCacheWorms -= DrawFusableParticles; On.Terraria.Main.DrawInfernoRings -= DrawForegroundParticles; On.Terraria.Main.SetDisplayMode -= ResetRenderTargetSizes; diff --git a/ILEditing/MechanicILChanges.cs b/ILEditing/MechanicILChanges.cs index c79990adca..0583ef2fa9 100644 --- a/ILEditing/MechanicILChanges.cs +++ b/ILEditing/MechanicILChanges.cs @@ -38,8 +38,8 @@ using Terraria.ModLoader; using Terraria.UI.Gamepad; using Terraria.Utilities; using Terraria.Graphics.Light; -using System.Collections.Generic; using Terraria.GameContent.Events; +using CalamityMod.DataStructures; namespace CalamityMod.ILEditing { @@ -58,16 +58,6 @@ namespace CalamityMod.ILEditing private static readonly MethodInfo textureGetValueMethod = typeof(Asset).GetMethod("get_Value", BindingFlags.Public | BindingFlags.Instance); - // Restarting the sprite batch for each different projectile is analogously equivalent to creating an entire new factory just to build a single car. - // It is inefficient and leads to performance problems as many projectiles do it. - // To mitigate this problem, a special draw cache exists for the purpose of deferring drawing of projectiles to a time in which all projectiles may be - // subject to the same additive draw state before all of the spritebatch's contents are flushed. - public static List ProjectileDrawCacheAdditiveBlending - { - get; - internal set; - } = new(Main.maxProjectiles); - #region Dash Fixes and Improvements private static void MakeShieldSlamIFramesConsistent(ILContext il) { @@ -659,7 +649,7 @@ namespace CalamityMod.ILEditing #endregion Fire Cursor Effect for the Calamity Accessory #region Custom Draw Layers - private static void UseCustomDrawLayers(ILContext il) + private static void AdditiveDrawing(ILContext il) { ILCursor cursor = new(il); if (!cursor.TryGotoNext(MoveType.After, i => i.MatchCall("DrawWhite"))) @@ -668,20 +658,28 @@ namespace CalamityMod.ILEditing cursor.EmitDelegate(() => { Main.spriteBatch.SetBlendState(BlendState.Additive); - for (int i = 0; i < ProjectileDrawCacheAdditiveBlending.Count; i++) + + // Draw Projectiles. + for (int i = 0; i < Main.maxProjectiles; i++) { - try - { - Main.instance.DrawProj(ProjectileDrawCacheAdditiveBlending[i]); - } - catch (Exception e) - { - TimeLogger.DrawException(e); - Main.projectile[ProjectileDrawCacheAdditiveBlending[i]].active = false; - } + if (!Main.projectile[i].active) + continue; + + if (Main.projectile[i].ModProjectile is IAdditiveDrawer d) + d.AdditiveDraw(Main.spriteBatch); } + + // Draw NPCs. + for (int i = 0; i < Main.maxNPCs; i++) + { + if (!Main.npc[i].active) + continue; + + if (Main.npc[i].ModNPC is IAdditiveDrawer d) + d.AdditiveDraw(Main.spriteBatch); + } + Main.spriteBatch.SetBlendState(BlendState.AlphaBlend); - ProjectileDrawCacheAdditiveBlending.Clear(); }); } #endregion Custom Draw Layers diff --git a/Projectiles/Summon/SmallAresArms/MinionPlasmaGas.cs b/Projectiles/Summon/SmallAresArms/MinionPlasmaGas.cs index 6322fe3e67..2610821b3b 100644 --- a/Projectiles/Summon/SmallAresArms/MinionPlasmaGas.cs +++ b/Projectiles/Summon/SmallAresArms/MinionPlasmaGas.cs @@ -1,15 +1,14 @@ -using CalamityMod.ILEditing; +using CalamityMod.DataStructures; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using System; -using System.Collections.Generic; using Terraria; using Terraria.GameContent; using Terraria.ModLoader; namespace CalamityMod.Projectiles.Summon.SmallAresArms { - public class MinionPlasmaGas : ModProjectile + public class MinionPlasmaGas : ModProjectile, IAdditiveDrawer { public ref float LightPower => ref Projectile.ai[0]; @@ -54,12 +53,7 @@ namespace CalamityMod.Projectiles.Summon.SmallAresArms Time++; } - public override void DrawBehind(int index, List drawCacheProjsBehindNPCsAndTiles, List drawCacheProjsBehindNPCs, List drawCacheProjsBehindProjectiles, List drawCacheProjsOverWiresUI, List overWiresUI) - { - ILChanges.ProjectileDrawCacheAdditiveBlending.Add(index); - } - - public override bool PreDraw(ref Color lightColor) + public void AdditiveDraw(SpriteBatch spriteBatch) { Texture2D texture = TextureAssets.Projectile[Projectile.type].Value; Vector2 origin = texture.Size() * 0.5f; @@ -67,8 +61,7 @@ namespace CalamityMod.Projectiles.Summon.SmallAresArms float opacity = Utils.GetLerpValue(0f, 0.08f, LightPower, true) * Projectile.Opacity * 0.6f; Color drawColor = new Color(141, 255, 105) * opacity; Vector2 scale = Projectile.Size / texture.Size() * Projectile.scale * 1.35f; - Main.spriteBatch.Draw(texture, drawPosition, null, drawColor, Projectile.rotation, origin, scale, SpriteEffects.None, 0f); - return false; + spriteBatch.Draw(texture, drawPosition, null, drawColor, Projectile.rotation, origin, scale, SpriteEffects.None, 0f); } } }