using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using ReLogic.Content;
using Terraria;
using Terraria.ModLoader;
namespace CalamityMod
{
public sealed class FramedMaskTexture
{
///
/// Cached Texture2D reference, null on server
///
public Texture2D Texture; // Leave this as field for performances sake
///
/// X axis's frame count, 0 on server
///
public int FrameXCount => _FrameXCount;
///
/// Y axis's frame count, 0 on server
///
public int FrameYCount => _FrameYCount;
///
/// Pixel width for each frame
///
public int FrameWidth => _FrameWidth;
///
/// Pixel height for each frame
///
public int FrameHeight => _FrameHeight;
///
/// Pixel Width of Texture, 0 on server
///
public int TextureWidth => _TextureWidth;
///
/// Pixel Height of Texture, 0 on server
///
public int TextureHeight => _TextureHeight;
private int _FrameWidth = 0, _FrameHeight = 0;
private int _FrameXCount = 0, _FrameYCount = 0;
private int _TextureWidth = 0, _TextureHeight = 0;
private readonly bool[] _HasMaskContent;
public FramedMaskTexture(string asset, int frameWidth, int frameHeight, bool pretendEveryFrameHaveGlow = false)
{
_FrameWidth = frameWidth;
_FrameHeight = frameHeight;
FramedGlowMaskSystem.OnUnload += Unload;
// Don't do anything further on server
if (Main.dedServ)
return;
Texture = ModContent.Request(asset, AssetRequestMode.ImmediateLoad).Value;
if (Texture is null)
return;
_TextureWidth = Texture.Width;
_TextureHeight = Texture.Height;
_FrameXCount = _TextureWidth / frameWidth;
_FrameYCount = _TextureHeight / frameHeight;
_HasMaskContent = new bool[FrameXCount * FrameYCount];
if (pretendEveryFrameHaveGlow)
{
for(int x = 0; x
{
var colData = Texture.GetColorsFromTexture();
// Interesting case, not sure if this will even happens
if (colData is null)
return;
Parallel.For(0, FrameXCount * FrameYCount, (i) =>
{
int xFrame = i % FrameXCount;
int yFrame = i / FrameXCount;
int xStart = xFrame * frameWidth;
int xEnd = Math.Min(xStart + frameWidth, _TextureWidth);
int yStart = yFrame * frameHeight;
int yEnd = Math.Min(yStart + frameHeight, _TextureHeight);
bool frameHasData = false;
for (int x = xStart; x < xEnd; x++)
{
if (frameHasData)
{
break;
}
for (int y = yStart; y < yEnd; y++)
{
Color col = colData[x, y];
if (col.A >= 1)
{
frameHasData = true;
break;
}
}
}
_HasMaskContent[xFrame + (yFrame * _FrameXCount)] = frameHasData;
});
});
}
}
public void Unload()
{
Texture = null;
_FrameWidth = 0;
_FrameHeight = 0;
_FrameXCount = 0;
_FrameYCount = 0;
}
public bool HasContentInFrameIndex(int xFrame, int yFrame)
{
if (Texture is null)
return false;
if (xFrame < 0 || xFrame >= _FrameXCount)
return false;
if (yFrame < 0 || yFrame >= _FrameYCount)
return false;
return _HasMaskContent[xFrame + (yFrame * _FrameXCount)];
}
public bool HasContentInFramePos(int xPos, int yPos)
{
int xFrame = xPos / _FrameWidth;
int yFrame = yPos / _FrameHeight;
if (xFrame < 0 || xFrame >= _FrameXCount)
return false;
if (yFrame < 0 || yFrame >= _FrameYCount)
return false;
return _HasMaskContent[xFrame + (yFrame * _FrameXCount)];
}
private sealed class FramedGlowMaskSystem : ModSystem
{
public static event Action OnUnload;
public override void Unload()
{
OnUnload?.Invoke();
OnUnload = null; // Clear cache completly
}
}
}
}