-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cache sprite data for NPCs. Reduces garbage generated from detecting …
…the pixel below the mouse cursor.
- Loading branch information
1 parent
8df81f2
commit 232c38b
Showing
3 changed files
with
98 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
using AutomaticTypeMapper; | ||
using EOLib; | ||
using EOLib.Domain.NPC; | ||
using Microsoft.Xna.Framework; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace EndlessClient.Rendering.Sprites | ||
{ | ||
[AutoMappedType(IsSingleton = true)] | ||
public class NPCSpriteDataCache : INPCSpriteDataCache | ||
{ | ||
private readonly INPCSpriteSheet _npcSpriteSheet; | ||
private readonly Dictionary<int, Dictionary<NPCFrame, Memory<Color>>> _spriteData; | ||
|
||
public NPCSpriteDataCache(INPCSpriteSheet npcSpriteSheet) | ||
{ | ||
_npcSpriteSheet = npcSpriteSheet; | ||
_spriteData = new Dictionary<int, Dictionary<NPCFrame, Memory<Color>>>(); | ||
} | ||
|
||
public void Populate(int graphic) | ||
{ | ||
if (_spriteData.ContainsKey(graphic)) | ||
return; | ||
|
||
_spriteData[graphic] = new Dictionary<NPCFrame, Memory<Color>>(); | ||
|
||
foreach (NPCFrame frame in Enum.GetValues(typeof(NPCFrame))) | ||
{ | ||
var text = _npcSpriteSheet.GetNPCTexture(graphic, frame, EODirection.Down); | ||
var data = Array.Empty<Color>(); | ||
|
||
if (text != null) | ||
{ | ||
data = new Color[text.Width * text.Height]; | ||
text.GetData(data); | ||
} | ||
|
||
_spriteData[graphic][frame] = data; | ||
} | ||
|
||
} | ||
|
||
public ReadOnlySpan<Color> GetData(int graphic, NPCFrame frame) | ||
{ | ||
if (!_spriteData.ContainsKey(graphic)) | ||
{ | ||
Populate(graphic); | ||
} | ||
|
||
return _spriteData[graphic][frame].Span; | ||
} | ||
|
||
public bool IsBlankSprite(int graphic) | ||
{ | ||
if (!_spriteData.ContainsKey(graphic)) | ||
{ | ||
Populate(graphic); | ||
} | ||
|
||
return _spriteData[graphic][NPCFrame.Standing].Span.ToArray().Any(AlphaIsZero); | ||
} | ||
|
||
private static bool AlphaIsZero(Color input) => input.A == 0; | ||
} | ||
|
||
public interface INPCSpriteDataCache | ||
{ | ||
void Populate(int graphic); | ||
|
||
ReadOnlySpan<Color> GetData(int graphic, NPCFrame frame); | ||
|
||
bool IsBlankSprite(int graphic); | ||
} | ||
} |