forked from AerysBat/XNALara
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTextureManager.cs
148 lines (132 loc) · 5.35 KB
/
TextureManager.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework.Graphics;
namespace XNALara
{
public class TextureManager
{
private static TextureCreationParameters textureCreationParamsMipmaps =
new TextureCreationParameters(0, 0, 0, 1,
SurfaceFormat.Rgba32,
TextureUsage.AutoGenerateMipMap,
Color.White,
FilterOptions.None,
FilterOptions.None);
private static TextureCreationParameters textureCreationParamsNoMipmaps =
new TextureCreationParameters(0, 0, 0, 1,
SurfaceFormat.Rgba32,
TextureUsage.None,
Color.White,
FilterOptions.None,
FilterOptions.None);
private Game game;
private GraphicsDevice graphicsDevice;
private static Dictionary<string, Texture2D> textureDict = new Dictionary<string, Texture2D>();
private static Dictionary<Texture2D, string> textureDictInv = new Dictionary<Texture2D, string>();
private static Dictionary<Texture2D, int> textureRefs = new Dictionary<Texture2D, int>();
private static Dictionary<Texture2D, bool> textureTransparent = new Dictionary<Texture2D, bool>();
public TextureManager(Game game) {
this.game = game;
this.graphicsDevice = game.GraphicsDevice;
}
public Texture2D GetTexture(string filename, bool useMipmaps) {
Texture2D texture;
if (!textureDict.TryGetValue(filename, out texture)) {
TextureCreationParameters texParams =
useMipmaps || game.UseAlternativeReflection ?
textureCreationParamsMipmaps :
textureCreationParamsNoMipmaps;
try {
texture = Texture2D.FromFile(graphicsDevice, filename, texParams);
}
catch (Exception) {
return null;
}
textureDict[filename] = texture;
textureDictInv[texture] = filename;
}
if (texture != null) {
int refs;
if (!textureRefs.TryGetValue(texture, out refs)) {
refs = 0;
}
textureRefs[texture] = refs + 1;
}
return texture;
}
public Texture2D ReloadTexture(Texture2D texture) {
if (texture == null) {
return null;
}
string filename = textureDictInv[texture];
if (filename == null) {
return null;
}
Texture2D newTexture;
try {
newTexture = Texture2D.FromFile(graphicsDevice, filename, textureCreationParamsMipmaps);
}
catch (Exception) {
textureDict.Remove(filename);
textureDictInv.Remove(texture);
textureRefs.Remove(texture);
textureTransparent.Remove(texture);
texture.Dispose();
return null;
}
textureDict[filename] = newTexture;
textureDictInv.Remove(texture);
textureDictInv[newTexture] = filename;
int refs = textureRefs[texture];
textureRefs.Remove(texture);
textureRefs[newTexture] = refs;
textureTransparent.Remove(texture);
texture.Dispose();
return newTexture;
}
public void HandleMeshRemoved(Mesh mesh) {
foreach (Texture2D texture in mesh.Textures) {
if (texture == null) {
continue;
}
int refs;
if (textureRefs.TryGetValue(texture, out refs)) {
refs--;
if (refs > 0) {
textureRefs[texture] = refs;
}
else {
string filename = textureDictInv[texture];
textureDictInv.Remove(texture);
textureDict.Remove(filename);
textureRefs.Remove(texture);
textureTransparent.Remove(texture);
texture.Dispose();
}
}
}
}
public bool IsTransparent(Texture2D texture) {
if (texture == null) {
return false;
}
bool isTransparent;
if (textureTransparent.TryGetValue(texture, out isTransparent)) {
return isTransparent;
}
int width = texture.Width;
int height = texture.Height;
Color[] pixels = new Color[width * height];
texture.GetData<Color>(pixels);
isTransparent = false;
foreach (Color pixel in pixels) {
if (pixel.A < 255) {
isTransparent = true;
break;
}
}
textureTransparent[texture] = isTransparent;
return isTransparent;
}
}
}