2024-08-11 14:23:56 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using Godot;
|
2024-09-05 17:49:55 +00:00
|
|
|
using KeepersCompound.LGS;
|
2024-08-11 14:23:56 +00:00
|
|
|
|
|
|
|
namespace KeepersCompound.TMV;
|
|
|
|
|
2024-08-12 17:46:30 +00:00
|
|
|
public partial class TextureLoader
|
2024-08-11 14:23:56 +00:00
|
|
|
{
|
2024-08-24 16:10:08 +00:00
|
|
|
private readonly string _fmName;
|
2024-09-07 12:42:16 +00:00
|
|
|
private readonly List<Texture2D> _textureCache = new();
|
2024-08-11 14:23:56 +00:00
|
|
|
private readonly Dictionary<int, int> _idMap = new();
|
2024-08-29 18:09:53 +00:00
|
|
|
private readonly Dictionary<int, string> _idPathMap = new();
|
2024-08-11 14:23:56 +00:00
|
|
|
private readonly Dictionary<string, int> _pathMap = new();
|
|
|
|
|
2024-09-05 16:39:54 +00:00
|
|
|
public TextureLoader(string fmName)
|
2024-08-11 14:23:56 +00:00
|
|
|
{
|
2024-08-24 16:10:08 +00:00
|
|
|
_fmName = fmName;
|
2024-08-11 14:23:56 +00:00
|
|
|
LoadDefaultTexture();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void LoadDefaultTexture()
|
|
|
|
{
|
|
|
|
// TODO: This should be a resource loaded from RES
|
|
|
|
const string path = "user://textures/jorge.png";
|
|
|
|
var texture = ImageTexture.CreateFromImage(Image.LoadFromFile(path));
|
|
|
|
_textureCache.Add(texture);
|
|
|
|
}
|
|
|
|
|
2024-08-29 18:09:53 +00:00
|
|
|
public bool Register(int id, string path)
|
|
|
|
{
|
|
|
|
return _idPathMap.TryAdd(id, path);
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool Load(int id, string path)
|
2024-08-11 14:23:56 +00:00
|
|
|
{
|
2024-08-24 16:10:08 +00:00
|
|
|
var loaded = false;
|
2024-09-05 17:49:55 +00:00
|
|
|
var pathManager = Context.Instance.PathManager;
|
|
|
|
var (_, texPath) = pathManager.GetResourcePath(ResourceType.Texture, _fmName, path);
|
2024-08-24 16:10:08 +00:00
|
|
|
|
|
|
|
if (texPath != null)
|
2024-08-11 14:23:56 +00:00
|
|
|
{
|
2024-08-24 16:10:08 +00:00
|
|
|
var texture = LoadTexture(texPath);
|
|
|
|
if (texture != null)
|
|
|
|
{
|
|
|
|
_textureCache.Add(texture);
|
|
|
|
loaded = true;
|
|
|
|
}
|
2024-08-11 14:23:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var index = loaded ? _textureCache.Count - 1 : 0;
|
|
|
|
_idMap.TryAdd(id, index);
|
|
|
|
_pathMap.TryAdd(path, index);
|
|
|
|
return loaded;
|
|
|
|
}
|
|
|
|
|
2024-08-18 09:27:44 +00:00
|
|
|
public static ImageTexture LoadTexture(string path)
|
2024-08-12 17:46:30 +00:00
|
|
|
{
|
|
|
|
var ext = path.GetExtension().ToLower();
|
2024-08-24 16:10:08 +00:00
|
|
|
string[] validExtensions = { "png", "tga", "pcx", "gif" };
|
|
|
|
if (validExtensions.Contains(ext))
|
2024-08-12 17:46:30 +00:00
|
|
|
{
|
2024-08-24 16:10:08 +00:00
|
|
|
var texture = ext switch
|
|
|
|
{
|
|
|
|
"pcx" => LoadPcx(path),
|
|
|
|
"gif" => LoadGif(path),
|
|
|
|
_ => ImageTexture.CreateFromImage(Image.LoadFromFile(path)),
|
|
|
|
};
|
|
|
|
return texture;
|
|
|
|
}
|
|
|
|
return null;
|
2024-08-12 17:46:30 +00:00
|
|
|
}
|
|
|
|
|
2024-08-29 18:09:53 +00:00
|
|
|
// TODO: We should report load failures
|
2024-09-07 12:42:16 +00:00
|
|
|
public Texture2D Get(int id)
|
2024-08-11 14:23:56 +00:00
|
|
|
{
|
2024-08-29 18:09:53 +00:00
|
|
|
if (_idMap.TryGetValue(id, out int value))
|
2024-08-11 14:23:56 +00:00
|
|
|
{
|
2024-08-29 18:09:53 +00:00
|
|
|
return _textureCache[value];
|
2024-08-11 14:23:56 +00:00
|
|
|
}
|
2024-08-29 18:09:53 +00:00
|
|
|
|
|
|
|
if (_idPathMap.TryGetValue(id, out var path) && Load(id, path))
|
|
|
|
{
|
|
|
|
return _textureCache[_idMap[id]];
|
|
|
|
}
|
|
|
|
|
|
|
|
return _textureCache[0];
|
2024-08-11 14:23:56 +00:00
|
|
|
}
|
|
|
|
|
2024-08-29 18:09:53 +00:00
|
|
|
// TODO: Load by path :)
|
2024-09-07 12:42:16 +00:00
|
|
|
public Texture2D Get(string path)
|
2024-08-11 14:23:56 +00:00
|
|
|
{
|
|
|
|
if (!_pathMap.ContainsKey(path))
|
|
|
|
{
|
|
|
|
return _textureCache[0];
|
|
|
|
}
|
|
|
|
return _textureCache[_pathMap[path]];
|
|
|
|
}
|
|
|
|
}
|