Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
TextureAtlasManager.cs
浏览该文件的文档.
1using System.Globalization;
2using Engine;
4
5namespace Game {
6 public static class TextureAtlasManager {
7 public static Dictionary<string, Subtexture> m_subtextures = [];
8 public static Texture2D AtlasTexture;
9
10 public static void Clear() {
11 m_subtextures.Clear();
12 }
13
14 public static void Initialize() {
15 Texture2D texture = ContentManager.Get<Texture2D>("Atlases/AtlasTexture");
16 string s = ContentManager.Get<string>("Atlases/Atlas");
17 LoadAtlases(texture, s);
18 }
19
20 public static void LoadAtlases(Texture2D AtlasTexture_, string Atlas) {
21 Clear();
22 AtlasTexture = AtlasTexture_;
23 LoadTextureAtlas(AtlasTexture, Atlas, "Textures/Atlas/");
24 }
25
26 public static Subtexture GetSubtexture(string name, bool throwOnNotFound) {
27 if (!m_subtextures.TryGetValue(name, out Subtexture value)) {
28 object value1 = ContentManager.Get(typeof(Texture2D), name, null, throwOnNotFound);
29 if (value1 == null) {
30 if (throwOnNotFound) {
31 throw new FileNotFoundException($"Required subtexture {name} not found in TextureAtlasManager.");
32 }
33 return null;
34 }
35 value = new Subtexture(value1 as Texture2D, Vector2.Zero, Vector2.One);
36 m_subtextures.Add(name, value);
37 }
38 return value;
39 }
40
41 public static Subtexture GetSubtexture(string name) => GetSubtexture(name, true);
42
43 public static void LoadTextureAtlas(Texture2D texture, string atlasDefinition, string prefix) {
44 string[] array = atlasDefinition.Split(['\n', '\r'], StringSplitOptions.RemoveEmptyEntries);
45 int num = 0;
46 while (true) {
47 if (num < array.Length) {
48 string[] array2 = array[num].Split([' '], StringSplitOptions.RemoveEmptyEntries);
49 if (array2.Length < 5) {
50 break;
51 }
52 string key = prefix + array2[0];
53 int num2 = int.Parse(array2[1], CultureInfo.InvariantCulture);
54 int num3 = int.Parse(array2[2], CultureInfo.InvariantCulture);
55 int num4 = int.Parse(array2[3], CultureInfo.InvariantCulture);
56 int num5 = int.Parse(array2[4], CultureInfo.InvariantCulture);
57 Vector2 topLeft = new(num2 / (float)texture.Width, num3 / (float)texture.Height);
58 Vector2 bottomRight = new((num2 + num4) / (float)texture.Width, (num3 + num5) / (float)texture.Height);
59 Subtexture value = new(texture, topLeft, bottomRight);
60 m_subtextures.Add(key, value);
61 num++;
62 continue;
63 }
64 return;
65 }
66 throw new InvalidOperationException("Invalid texture atlas definition.");
67 }
68 }
69}
static object Get(Type type, string name)
static void LoadAtlases(Texture2D AtlasTexture_, string Atlas)
static Dictionary< string, Subtexture > m_subtextures
static void LoadTextureAtlas(Texture2D texture, string atlasDefinition, string prefix)
static Subtexture GetSubtexture(string name)
static Subtexture GetSubtexture(string name, bool throwOnNotFound)
static readonly Vector2 Zero
static readonly Vector2 One