Added texture cache and sprite origins.
This commit is contained in:
26
AlientAttack.MonoGame/Textures/TextureCache.cs
Normal file
26
AlientAttack.MonoGame/Textures/TextureCache.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using Microsoft.Xna.Framework.Content;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace AlienAttack.MonoGame.Textures;
|
||||
|
||||
public sealed class TextureCache
|
||||
{
|
||||
private readonly Dictionary<string, Texture2D> _cache = [];
|
||||
private ContentManager _content = default!;
|
||||
|
||||
public void Initialize(ContentManager content) => _content = content;
|
||||
|
||||
public Texture2D Get(string assetName)
|
||||
{
|
||||
if (_cache.TryGetValue(assetName, out var texture))
|
||||
return texture;
|
||||
|
||||
texture = _content.Load<Texture2D>(assetName);
|
||||
_cache[assetName] = texture;
|
||||
|
||||
return texture;
|
||||
}
|
||||
|
||||
public void Clear() => _cache.Clear();
|
||||
}
|
||||
Reference in New Issue
Block a user