-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResourceManager.cs
76 lines (60 loc) · 2.86 KB
/
ResourceManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using LiVerse.AppInfo;
using Microsoft.Xna.Framework.Graphics;
using SpriteFontPlus;
namespace LiVerse;
public static class ResourceManager {
public static string DefaultContentPath = Path.Combine(Environment.CurrentDirectory, "ApplicationData");
public static string DefaultContentUserDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"LiVerse");
public static string DefaultStoresPath = Path.Combine(DefaultContentUserDataPath, "Stores");
public static Info AppInfo;
//public static FontSystem GlobalFontSystem = new FontSystem();
public static Dictionary<string, SpriteFont> FontCache = new();
public static SpriteFont BakeFont(string fontName, int size) {
string fontPath = Path.Combine(DefaultContentPath, "Fonts", fontName + ".ttf");
if (fontPath.EndsWith(".ttf.ttf")) fontPath = fontPath.Replace(".ttf.ttf", ".ttf");
TtfFontBakerResult fontBakeResult = TtfFontBaker.Bake(File.ReadAllBytes(fontPath),
size, 1024, 1024, new[] {
CharacterRange.BasicLatin,
CharacterRange.Latin1Supplement,
CharacterRange.LatinExtendedA,
CharacterRange.Cyrillic
});
return fontBakeResult.CreateSpriteFont(LiVerseApp.Graphics!.GraphicsDevice);
}
/// <summary>
/// Gets font from FontCache, bake font if not found in cache
/// </summary>
public static SpriteFont GetFont(string fontName, int size) {
string fontKey = $"{fontName}:{size}";
if (FontCache.TryGetValue(fontKey, out SpriteFont? foundValue)) {
return foundValue;
}
SpriteFont bakeResult = BakeFont(fontName, size);
FontCache.Add(fontKey, bakeResult);
return bakeResult;
}
public static void LoadBaseResources() {
// Create Directories
Directory.CreateDirectory(DefaultContentUserDataPath);
Directory.CreateDirectory(DefaultStoresPath);
// Check if OpenSans font exists
string openSansFontPath = Path.Combine(DefaultContentPath, "Fonts", "NotoSans.ttf");
if (!File.Exists(openSansFontPath)) {
throw new FileNotFoundException("Could not find default font \"NotoSans.ttf\".");
}
}
// Load Sprite From File
public static Texture2D LoadTexture2DFromFile(string filePath, Action<byte[]> colorProcessor) {
if (!File.Exists(filePath)) {
throw new FileNotFoundException($"Could not find Texture, Path: {filePath}");
}
using (FileStream fileStream = new(filePath, FileMode.Open)) {
Texture2D ValToReturn = Texture2D.FromStream(LiVerseApp.Graphics!.GraphicsDevice, fileStream, colorProcessor);
return ValToReturn;
}
}
public static Texture2D LoadTexture2DFromFile(string filePath) {
return LoadTexture2DFromFile(filePath, DefaultColorProcessors.ZeroTransparentPixels);
}
}