IAdditiveDrawer interface

This commit is contained in:
Dominic committed 2022-07-21 22:22:12 -05:00
1 parent afa86a4b34
commit d771a4e84f
5 files changed
+45 -36

No files matched your search

+1
View File
@@ -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;
+17
View File
@@ -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);
}
}
+2 -2
View File
@@ -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;
+21 -23
View File
@@ -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<Texture2D>).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<int> 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<MoonlordDeathDrama>("DrawWhite")))
@@ -668,20 +658,28 @@ namespace CalamityMod.ILEditing
cursor.EmitDelegate<Action>(() =>
{
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
@@ -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<int> drawCacheProjsBehindNPCsAndTiles, List<int> drawCacheProjsBehindNPCs, List<int> drawCacheProjsBehindProjectiles, List<int> drawCacheProjsOverWiresUI, List<int> 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);
}
}
}