|
24 | 24 | using System.IO; |
25 | 25 | using System.Linq; |
26 | 26 | using System.Threading; |
27 | | -using Microsoft.CodeAnalysis.VisualBasic.Syntax; |
28 | 27 | using Microsoft.Xna.Framework; |
29 | 28 | using Microsoft.Xna.Framework.Graphics; |
30 | 29 | using Orts.Viewer3D.Common; |
@@ -173,40 +172,37 @@ Texture2D invalid() |
173 | 172 | } |
174 | 173 | } |
175 | 174 |
|
176 | | - public static Texture2D LoadInternal(GraphicsDevice graphicsDevice, string path) |
| 175 | + // Internal callers expect a new `Texture2D` for every load so we must also provide a new missing texture for each. |
| 176 | + internal static Texture2D GetInternalMissingTexture(GraphicsDevice graphicsDevice) |
177 | 177 | { |
178 | | - if (path == null || path == "") |
179 | | - return SharedMaterialManager.MissingTexture; |
180 | | - |
181 | | - path = path.ToLowerInvariant(); |
182 | | - var ext = Path.GetExtension(path); |
| 178 | + var texture = new Texture2D(graphicsDevice, 1, 1); |
| 179 | + texture.SetData(new[] { Color.Magenta }); |
| 180 | + return texture; |
| 181 | + } |
183 | 182 |
|
184 | | - if (ext == ".ace") |
185 | | - return Orts.Formats.Msts.AceFile.Texture2DFromFile(graphicsDevice, path); |
186 | | - else if (ext == ".dds" && File.Exists(path)) |
| 183 | + /// <summary> |
| 184 | + /// Loads an internal texture file; DO NOT use with game data, use <see cref="Get(string, bool)"/> instead. |
| 185 | + /// </summary> |
| 186 | + /// <returns>The <see cref="Texture2D"/> created from the given <paramref name="path"/> or a missing placeholder.</returns> |
| 187 | + public static Texture2D LoadInternal(GraphicsDevice graphicsDevice, string path) |
| 188 | + { |
| 189 | + if (!File.Exists(path)) |
187 | 190 | { |
188 | | - Texture2D ddsTexture; |
189 | | - DDSLib.DDSFromFile(path, graphicsDevice, true, out ddsTexture); |
190 | | - return ddsTexture; |
| 191 | + Trace.TraceError("Missing internal file: {0}", path); |
| 192 | + return GetInternalMissingTexture(graphicsDevice); |
191 | 193 | } |
192 | 194 |
|
193 | | - using (var stream = File.OpenRead(path)) |
| 195 | + // DO NOT add additional formats here without explicit approval |
| 196 | + // - BMP is used for ETCS/DMI |
| 197 | + // - PNG is used for everything else |
| 198 | + switch (Path.GetExtension(path)) |
194 | 199 | { |
195 | | - if (ext == ".gif" || ext == ".jpg" || ext == ".png") |
196 | | - return Texture2D.FromStream(graphicsDevice, stream); |
197 | | - else if (ext == ".bmp") |
198 | | - using (var image = System.Drawing.Image.FromStream(stream)) |
199 | | - { |
200 | | - using (var memoryStream = new MemoryStream()) |
201 | | - { |
202 | | - image.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png); |
203 | | - memoryStream.Seek(0, SeekOrigin.Begin); |
204 | | - return Texture2D.FromStream(graphicsDevice, memoryStream); |
205 | | - } |
206 | | - } |
207 | | - else |
208 | | - Trace.TraceWarning("Unsupported texture format: {0}", path); |
209 | | - return SharedMaterialManager.MissingTexture; |
| 200 | + case ".bmp": |
| 201 | + case ".png": |
| 202 | + return Texture2D.FromFile(graphicsDevice, path); |
| 203 | + default: |
| 204 | + Trace.TraceError("Unsupported internal file: {0}", path); |
| 205 | + return GetInternalMissingTexture(graphicsDevice); |
210 | 206 | } |
211 | 207 | } |
212 | 208 |
|
@@ -337,8 +333,7 @@ public SharedMaterialManager(Viewer viewer) |
337 | 333 | DebugShader = new DebugShader(viewer.RenderProcess.GraphicsDevice); |
338 | 334 | CabShader = new CabShader(viewer.RenderProcess.GraphicsDevice, Vector4.One, Vector4.One, Vector3.One, Vector3.One); |
339 | 335 |
|
340 | | - // TODO: This should happen on the loader thread. |
341 | | - MissingTexture = SharedTextureManager.LoadInternal(viewer.RenderProcess.GraphicsDevice, Path.Combine(viewer.ContentPath, "blank.bmp")); |
| 336 | + MissingTexture = SharedTextureManager.GetInternalMissingTexture(viewer.RenderProcess.GraphicsDevice); |
342 | 337 |
|
343 | 338 | // Managing default snow textures |
344 | 339 | var defaultSnowTexturePath = viewer.Simulator.RoutePath + @"\TERRTEX\SNOW\ORTSDefaultSnow.ace"; |
|
0 commit comments