Skip to content

Commit 65ef2f9

Browse files
committed
refactor: Simplify loading internal textures
1 parent 3ae9a1f commit 65ef2f9

File tree

4 files changed

+27
-35
lines changed

4 files changed

+27
-35
lines changed

Source/Documentation/Manual/features-route.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ snow texture names are recognized: ``ORTSDefaultSnow.ace`` and
4343
``ORTSDefaultDMSnow.ace``, to be positioned within folder ``TERRTEX\SNOW`` of
4444
the concerned route. For the snow textures that are missing in the ``SNOW``
4545
subfolder, and only for them, ORTS uses such files to display snow, if they
46-
are present, instead of using file ``blank.bmp``.
46+
are present.
4747

4848
To have a minimum working snow texture set, the file ``microtex.ace`` must
4949
also be present in the ``SNOW`` subfolder.
-3.05 KB
Binary file not shown.

Source/RunActivity/RunActivity.csproj

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,6 @@
4545
<Content Include="Content\TrainForcesSprites.png">
4646
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
4747
</Content>
48-
<Content Include="Content\blank.bmp">
49-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
50-
</Content>
5148
<Content Include="Content\Clouds01.png">
5249
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
5350
</Content>

Source/RunActivity/Viewer3D/Materials.cs

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
using System.IO;
2525
using System.Linq;
2626
using System.Threading;
27-
using Microsoft.CodeAnalysis.VisualBasic.Syntax;
2827
using Microsoft.Xna.Framework;
2928
using Microsoft.Xna.Framework.Graphics;
3029
using Orts.Viewer3D.Common;
@@ -173,40 +172,37 @@ Texture2D invalid()
173172
}
174173
}
175174

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)
177177
{
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+
}
183182

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))
187190
{
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);
191193
}
192194

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))
194199
{
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);
210206
}
211207
}
212208

@@ -337,8 +333,7 @@ public SharedMaterialManager(Viewer viewer)
337333
DebugShader = new DebugShader(viewer.RenderProcess.GraphicsDevice);
338334
CabShader = new CabShader(viewer.RenderProcess.GraphicsDevice, Vector4.One, Vector4.One, Vector3.One, Vector3.One);
339335

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);
342337

343338
// Managing default snow textures
344339
var defaultSnowTexturePath = viewer.Simulator.RoutePath + @"\TERRTEX\SNOW\ORTSDefaultSnow.ace";

0 commit comments

Comments
 (0)