21 public static void CreateCheckers()
25 Checkers =
new Texture2D(GameMain.Instance.GraphicsDevice, w, h);
27 Color[] data =
new Color[w * h];
29 for (
int y = 0; y < h; y++)
31 for (
int x = 0; x < w; x++)
33 data[y * w + x] = (x < w / 2) ^ (y < h / 2) ? Color.White : Color.Black;
37 Checkers.SetData(0,
new Rectangle(0, 0, w, h), data, 0, w * h);
39 public static void InitStatic()
43 CUI.OnDispose += () =>
69 public static Texture2D BackupTexture => GUI.WhiteTexture;
70 public Dictionary<string, Texture2D> LoadedTextures =
new();
71 public void DisposeAllTextures()
73 foreach (Texture2D texture
in LoadedTextures.Values)
75 if (texture == BackupTexture)
continue;
80 public string NormalizePath(
string path)
85 public CUISprite GetSprite(
string path, Rectangle? sourceRect =
null, CUISpriteDrawMode? drawMode =
null, SpriteEffects? effects =
null)
87 CUISprite sprite =
new CUISprite(GetTexture(path))
92 if (sourceRect.HasValue) sprite.SourceRect = sourceRect.Value;
93 if (drawMode.HasValue) sprite.DrawMode = drawMode.Value;
94 if (effects.HasValue) sprite.Effects = effects.Value;
104 return GetSprite(
CUI.CUITexturePath,
new Rectangle(x * 32, y * 32, 32, 32), drawMode, effects);
107 private Texture2D TryLoad(
string path)
109 Texture2D texture =
null;
112 if (File.Exists(path))
114 using (FileStream fs = File.OpenRead(path))
116 texture = Texture2D.FromStream(GameMain.Instance.GraphicsDevice, fs);
124 public Texture2D GetTexture(
string path)
126 if (LoadedTextures ==
null)
128 CUI.Error($
"LoadedTextures was null");
129 return BackupTexture;
132 if (LoadedTextures.ContainsKey(path))
return LoadedTextures[path];
134 Texture2D loaded =
null;
136 if (CUI.AssetsPath !=
null) loaded ??= TryLoad(Path.Combine(CUI.AssetsPath, path));
137 if (CUI.PGNAssets !=
null) loaded ??= TryLoad(Path.Combine(CUI.PGNAssets, path));
138 loaded ??= TryLoad(path);
141 CUI.Warning($
"Coudn't find {path} texture, setting it to backup texture");
142 loaded ??= BackupTexture;
143 if (CUI.PGNAssets ==
null)
145 CUI.Warning($
"Note: It was loaded before CUI.PGNAssets was set");
149 LoadedTextures[path] = loaded;
153 public void Dispose()
155 DisposeAllTextures();
157 LoadedTextures.Clear();
158 LoadedTextures =
null;
Class for loading textures without duplicates Also disposes all loaded textures automatically.
static Texture2D Checkers
Path to additional PNGs, it can be set in CUI.
CUISprite GetCUISprite(int x, int y, CUISpriteDrawMode? drawMode=null, SpriteEffects? effects=null)
32x32 square on (x,y) position from CUI.png