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-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-19 20:16:07 +00:00
|
|
|
public TextureLoader()
|
2024-08-11 14:23:56 +00:00
|
|
|
{
|
2024-09-07 13:01:24 +00:00
|
|
|
LoadDefaultTextures();
|
2024-08-11 14:23:56 +00:00
|
|
|
}
|
|
|
|
|
2024-09-07 13:01:24 +00:00
|
|
|
private void LoadDefaultTextures()
|
2024-08-11 14:23:56 +00:00
|
|
|
{
|
2024-09-07 13:01:24 +00:00
|
|
|
_textureCache.Add(ResourceLoader.Load<CompressedTexture2D>("res://project/jorge.png"));
|
|
|
|
_textureCache.Add(ResourceLoader.Load<CompressedTexture2D>("res://project/sky.png"));
|
|
|
|
_idMap.Add(0, 0);
|
|
|
|
_idMap.Add(249, 1);
|
2024-08-11 14:23:56 +00:00
|
|
|
}
|
|
|
|
|
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-19 20:16:07 +00:00
|
|
|
var campaignResources = Context.Instance.CampaignResources;
|
|
|
|
var texPath = campaignResources.GetResourcePath(ResourceType.Texture, 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-09-17 16:07:07 +00:00
|
|
|
var image = ext switch
|
2024-08-24 16:10:08 +00:00
|
|
|
{
|
|
|
|
"pcx" => LoadPcx(path),
|
|
|
|
"gif" => LoadGif(path),
|
2024-09-17 16:07:07 +00:00
|
|
|
_ => Image.LoadFromFile(path),
|
2024-08-24 16:10:08 +00:00
|
|
|
};
|
2024-09-17 16:07:07 +00:00
|
|
|
image.GenerateMipmaps();
|
|
|
|
return ImageTexture.CreateFromImage(image);
|
2024-08-24 16:10:08 +00:00
|
|
|
}
|
|
|
|
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]];
|
|
|
|
}
|
|
|
|
}
|