using Godot; using System; using System.Collections.Generic; public partial class TexLoader { private static TexLoader _instance; public static TexLoader Instance { get { if (_instance == null) { _instance = new TexLoader(); } return _instance; } } public class LoadedTexture { public ImageTexture tex; public int texWidth; public int texHeight; public IntPtr texPtr => (IntPtr)tex.GetRid().Id; } public Dictionary loadedTextures = new Dictionary(); public (IntPtr texPtr, int texWidth, int texHeight) GetPng(string path) { IntPtr texPtr; int texWidth, texHeight; if (loadedTextures.ContainsKey(path) && loadedTextures.TryGetValue(path, out LoadedTexture loadedTexture)) { texPtr = loadedTexture.texPtr; texWidth = loadedTexture.texWidth; texHeight = loadedTexture.texHeight; } else { texPtr = 0; texWidth = 0; texHeight = 0; } return (texPtr, texWidth, texHeight); } public bool LoadPng(string path) { GD.Print("Attempting to load png: " + path); if (loadedTextures.ContainsKey(path)) { return true; } try { LoadedTexture loadedTex; Image img = new Image(); var err = img.Load(path); if (err != Error.Ok) { // Provide a dummy texture instead as a fallback loadedTextures.Add(path, new LoadedTexture() { texWidth = 0, texHeight = 0, tex = new ImageTexture() }); return false; } // Create a Godot texture ImageTexture tex = ImageTexture.CreateFromImage(img); loadedTex = new LoadedTexture { tex = tex, texWidth = img.GetWidth(), texHeight = img.GetHeight() }; loadedTextures[path] = loadedTex; return true; } catch (Exception ex) { GD.PrintErr("Failed to load png: " + ex.Message); loadedTextures.Add(path, new LoadedTexture() { texWidth = 0, texHeight = 0, tex = new ImageTexture() }); return false; } } }