Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion source/LibRender2/BaseRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -882,7 +882,7 @@ public void Reset()
{
if (!File.Exists(keys[i].Item1) || File.GetLastWriteTime(keys[i].Item1) != keys[i].Item3)
{
currentHost.StaticObjectCache.Remove(keys[i]);
currentHost.StaticObjectCache.TryRemove(keys[i], out _);
}
}
TextureManager.UnloadAllTextures(true);
Expand Down
6 changes: 4 additions & 2 deletions source/LibRender2/Menu/Menu.OptionType.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//Simplified BSD License (BSD-2-Clause)
//Simplified BSD License (BSD-2-Clause)
//
//Copyright (c) 2024, Maurizo M. Gavioli, The OpenBVE Project
//
Expand Down Expand Up @@ -46,6 +46,8 @@ public enum OptionType
/// <summary>Sets whether to automatically reload the current objects</summary>
AutoReloadObjects,
/// <summary>Sets the shadow quality</summary>
ShadowQuality
ShadowQuality,
/// <summary>Sets whether to use parallel route loading</summary>
ParallelRouteLoading
}
}
8 changes: 7 additions & 1 deletion source/LibRender2/Menu/MenuEntries/MenuOption.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//Simplified BSD License (BSD-2-Clause)
//Simplified BSD License (BSD-2-Clause)
//
//Copyright (c) 2024, Maurizo M. Gavioli, The OpenBVE Project
//
Expand Down Expand Up @@ -169,6 +169,9 @@ public MenuOption(AbstractMenu menu, OptionType type, string text, object[] entr
break;
}
return;
case OptionType.ParallelRouteLoading:
CurrentlySelectedOption = BaseMenu.CurrentOptions.ParallelRouteLoading ? 0 : 1;
return;
}
CurrentlySelectedOption = 0;
}
Expand Down Expand Up @@ -316,6 +319,9 @@ public void Flip()
}
BaseMenu.Renderer.InitializeShadows();
break;
case OptionType.ParallelRouteLoading:
BaseMenu.CurrentOptions.ParallelRouteLoading = !BaseMenu.CurrentOptions.ParallelRouteLoading;
break;

}

Expand Down
70 changes: 40 additions & 30 deletions source/LibRender2/Textures/TextureManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class TextureManager

/// <summary>Holds all currently registered textures.</summary>
public static Texture[] RegisteredTextures;
private static readonly object texturesLock = new object();
/// <summary>Holds cached texture origins</summary>
internal static Dictionary<TextureOrigin, Texture> textureCache = new Dictionary<TextureOrigin, Texture>();

Expand Down Expand Up @@ -106,37 +107,40 @@ public bool RegisterTexture(string path, TextureParameters parameters, out Textu
* Check if the texture is already registered.
* If so, return the existing handle.
* */
for (int i = 0; i < RegisteredTexturesCount; i++)
lock (texturesLock)
{
if (RegisteredTextures[i] != null)
for (int i = 0; i < RegisteredTexturesCount; i++)
{
try
if (RegisteredTextures[i] != null)
{
//The only exceptions thrown were these when it barfed
PathOrigin source = RegisteredTextures[i].Origin as PathOrigin;

if (source != null && source.Path.Equals(path, StringComparison.InvariantCultureIgnoreCase) && source.Parameters == parameters)
try
{
handle = RegisteredTextures[i];
return true;
//The only exceptions thrown were these when it barfed
PathOrigin source = RegisteredTextures[i].Origin as PathOrigin;

if (source != null && source.Path.Equals(path, StringComparison.InvariantCultureIgnoreCase) && source.Parameters == parameters)
{
handle = RegisteredTextures[i];
return true;
}
}
catch
{
// ignored
}
}
catch
{
// ignored
}

}

/*
* Register the texture and return the newly created handle.
* */
int idx = GetNextFreeTexture();
RegisteredTextures[idx] = new Texture(path, parameters, currentHost);
RegisteredTexturesCount++;
handle = RegisteredTextures[idx];
return true;
}

/*
* Register the texture and return the newly created handle.
* */
int idx = GetNextFreeTexture();
RegisteredTextures[idx] = new Texture(path, parameters, currentHost);
RegisteredTexturesCount++;
handle = RegisteredTextures[idx];
return true;
}

/// <summary>Registers a texture and returns a handle to the texture.</summary>
Expand All @@ -147,10 +151,13 @@ public Texture RegisterTexture(Texture texture)
/*
* Register the texture and return the newly created handle.
* */
int idx = GetNextFreeTexture();
RegisteredTextures[idx] = new Texture(texture);
RegisteredTexturesCount++;
return RegisteredTextures[idx];
lock (texturesLock)
{
int idx = GetNextFreeTexture();
RegisteredTextures[idx] = new Texture(texture);
RegisteredTexturesCount++;
return RegisteredTextures[idx];
}
}

/// <summary>Registers a texture and returns a handle to the texture.</summary>
Expand Down Expand Up @@ -178,10 +185,13 @@ public Texture RegisterTexture(Bitmap bitmap)
/*
* Register the texture and return the newly created handle.
* */
int idx = GetNextFreeTexture();
RegisteredTextures[idx] = new Texture(bitmap);
RegisteredTexturesCount++;
return RegisteredTextures[idx];
lock (texturesLock)
{
int idx = GetNextFreeTexture();
RegisteredTextures[idx] = new Texture(bitmap);
RegisteredTexturesCount++;
return RegisteredTextures[idx];
}
}


Expand Down
1 change: 1 addition & 0 deletions source/ObjectViewer/Graphics/NewRendererS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,7 @@ private void RenderOverlays(double timeElapsed)
OpenGlString.Draw(Fonts.SmallFont, $"Current frame rate: {FrameRate.ToString("0.0", culture)}fps", new Vector2(4, Screen.Height - 88), TextAlignment.TopLeft, Color128.White, true);
OpenGlString.Draw(Fonts.SmallFont, $"Total opaque faces: {opaqueFaces}", new Vector2(4, Screen.Height - 76), TextAlignment.TopLeft, Color128.White, true);
OpenGlString.Draw(Fonts.SmallFont, $"Total alpha faces: {alphaFaces}", new Vector2(4, Screen.Height - 64), TextAlignment.TopLeft, Color128.White, true);
OpenGlString.Draw(Fonts.SmallFont, $"Total Loading: {LoadingStats.TotalLoadingTime:0.0} ms", new Vector2(4 * scaleFactor, Screen.Height - 146), TextAlignment.TopLeft, Color128.Yellow, true);
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions source/ObjectViewer/Hosts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using OpenBveApi.Hosts;
using OpenBveApi.Interface;
using OpenBveApi.Math;
using OpenBveApi.Graphics;
using OpenBveApi.Objects;
using OpenBveApi.Routes;
using OpenBveApi.Textures;
Expand Down Expand Up @@ -286,13 +287,13 @@ public override bool LoadObject(string path, System.Text.Encoding Encoding, out

if (Object is StaticObject staticObject)
{
StaticObjectCache.Add(ValueTuple.Create(path.ToLowerInvariant(), false, File.GetLastWriteTime(path)), staticObject);
StaticObjectCache.TryAdd(ValueTuple.Create(path.ToLowerInvariant(), false, File.GetLastWriteTime(path)), staticObject);
return true;
}

if (Object is AnimatedObjectCollection aoc)
{
AnimatedObjectCollectionCache.Add(path.ToLowerInvariant(), aoc);
AnimatedObjectCollectionCache.TryAdd(path.ToLowerInvariant(), aoc);
}

return true;
Expand Down
7 changes: 6 additions & 1 deletion source/ObjectViewer/ProgramS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Forms;
Expand Down Expand Up @@ -321,6 +322,7 @@ internal static void MouseMovement()

internal static void RefreshObjects(bool autoReload = false)
{
Stopwatch sw = Stopwatch.StartNew();
LightingRelative = -1.0;

// Prune cache to allow actual reloading of modified files
Expand All @@ -338,7 +340,7 @@ internal static void RefreshObjects(bool autoReload = false)
}
foreach (var key in staticKeysToRemove)
{
CurrentHost.StaticObjectCache.Remove(key);
CurrentHost.StaticObjectCache.TryRemove(key, out _);
}
CurrentHost.AnimatedObjectCollectionCache.Clear();
// Let TextureManager check for texture changes
Expand Down Expand Up @@ -456,6 +458,9 @@ internal static void RefreshObjects(bool autoReload = false)
Renderer.GameWindow.Title = "Object Viewer";
}
LastReloadTime = DateTime.UtcNow;
sw.Stop();
LoadingStats.TotalLoadingTime = sw.Elapsed.TotalMilliseconds;
Interface.AddMessage(MessageType.Information, false, $"Object(s) loaded in {LoadingStats.TotalLoadingTime:0.0} ms");
UpdateWatchers();
}

Expand Down
23 changes: 23 additions & 0 deletions source/ObjectViewer/formOptions.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions source/ObjectViewer/formOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ private formOptions()
comboBoxBackwards.DataSource = Enum.GetValues(typeof(Key));
comboBoxBackwards.SelectedItem = Interface.CurrentOptions.CameraMoveBackward;
checkBoxAutoReload.Checked = Interface.CurrentOptions.AutoReloadObjects;
checkBoxParallel.Checked = Interface.CurrentOptions.ParallelRouteLoading;
}

private void InitializeSunSliders()
Expand Down Expand Up @@ -241,6 +242,7 @@ private void CloseButton_Click(object sender, EventArgs e)
Interface.CurrentOptions.CameraMoveForward = (Key)comboBoxForwards.SelectedItem;
Interface.CurrentOptions.CameraMoveBackward = (Key)comboBoxBackwards.SelectedItem;
Interface.CurrentOptions.AutoReloadObjects = checkBoxAutoReload.Checked;
Interface.CurrentOptions.ParallelRouteLoading = checkBoxParallel.Checked;

// Saving shadow settings
switch (comboBoxShadowResolution.SelectedIndex)
Expand Down
5 changes: 3 additions & 2 deletions source/OpenBVE/Game/Menu/Menu.SingleMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ public SingleMenu(AbstractMenu menu, MenuType menuType, int data = 0, double Max
Align = TextAlignment.TopLeft;
break;
case MenuType.Options:
Items = new MenuEntry[11];
Items = new MenuEntry[12];
Items[0] = new MenuCaption(menu, Translations.GetInterfaceString(HostApplication.OpenBve, new[] {"panel","options"}));
Items[1] = new MenuOption(menu, OptionType.ScreenResolution, Translations.GetInterfaceString(HostApplication.OpenBve, new[] {"options","resolution"}), Program.Renderer.Screen.AvailableResolutions.ToArray());
Items[2] = new MenuOption(menu, OptionType.FullScreen, Translations.GetInterfaceString(HostApplication.OpenBve, new[] {"options","display_mode_fullscreen"}), new[] { "true", "false" });
Expand All @@ -254,7 +254,8 @@ public SingleMenu(AbstractMenu menu, MenuType menuType, int data = 0, double Max
Translations.GetInterfaceString(HostApplication.OpenBve, new[] { "options", "shadows_resolution_high" }),
Translations.GetInterfaceString(HostApplication.OpenBve, new[] { "options", "shadows_resolution_ultra" })
});
Items[10] = new MenuCommand(menu, Translations.GetInterfaceString(HostApplication.OpenBve, new[] {"menu","back"}), MenuTag.MenuBack, 0);
Items[10] = new MenuOption(menu, OptionType.ParallelRouteLoading, "Parallel route loading", new[] { "true", "false" });
Items[11] = new MenuCommand(menu, Translations.GetInterfaceString(HostApplication.OpenBve, new[] {"menu","back"}), MenuTag.MenuBack, 0);
Align = TextAlignment.TopLeft;
break;
case MenuType.RouteList:
Expand Down
21 changes: 20 additions & 1 deletion source/OpenBVE/Game/RouteInfoOverlay.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using LibRender2;
using LibRender2;
using OpenBveApi.Colors;
using OpenTK.Graphics.OpenGL;
using OpenBveApi.Textures;
using OpenBveApi.Interface;
using OpenBveApi.Math;
using OpenBveApi.Graphics;

namespace OpenBve
{
Expand All @@ -20,6 +21,7 @@ private enum OverlayState
None = 0,
Map,
Gradient,
LoadingStats,
NumOf
}

Expand Down Expand Up @@ -97,6 +99,23 @@ public void Show()
Program.Renderer.Rectangle.Draw(null, new Vector2(Pos.X, gradientSize.Y / 2),
new Vector2(gradientPosWidth, gradientSize.Y / 2), gradientPosBar);
break;
case OverlayState.LoadingStats:
double x = 4.0;
double y = 4.0;
double scale = Program.Renderer.ScaleFactor.X;
Program.Renderer.OpenGlString.Draw(Program.Renderer.Fonts.SmallFont, "Loading Performance Stats:", new Vector2(x * scale, y * scale), TextAlignment.TopLeft, Color128.Yellow, true);
y += 20.0;
Program.Renderer.OpenGlString.Draw(Program.Renderer.Fonts.SmallFont, $"Route Parsing Time: {OpenBveApi.Interface.LoadingStats.RouteParseTime:0.0} ms", new Vector2(x * scale, y * scale), TextAlignment.TopLeft, Color128.White, true);
y += 20.0;
Program.Renderer.OpenGlString.Draw(Program.Renderer.Fonts.SmallFont, $"Object Preload Time: {OpenBveApi.Interface.LoadingStats.ObjectPreloadTime:0.0} ms", new Vector2(x * scale, y * scale), TextAlignment.TopLeft, Color128.White, true);
y += 20.0;
Program.Renderer.OpenGlString.Draw(Program.Renderer.Fonts.SmallFont, $"Unique Objects Found: {OpenBveApi.Interface.LoadingStats.ObjectsFound}", new Vector2(x * scale, y * scale), TextAlignment.TopLeft, Color128.White, true);
y += 20.0;
if (OpenBveApi.Interface.LoadingStats.TotalLoadingTime > 0)
{
Program.Renderer.OpenGlString.Draw(Program.Renderer.Fonts.SmallFont, $"Total Loading Time: {OpenBveApi.Interface.LoadingStats.TotalLoadingTime:0.0} ms", new Vector2(x * scale, y * scale), TextAlignment.TopLeft, Color128.White, true);
}
break;
}
}

Expand Down
6 changes: 3 additions & 3 deletions source/OpenBVE/System/Host.cs
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ public override bool LoadStaticObject(string path, System.Text.Encoding Encoding
{
staticObject.OptimizeObject(PreserveVertices, Interface.CurrentOptions.ObjectOptimizationBasicThreshold, Interface.CurrentOptions.ObjectOptimizationVertexCulling);
Object = staticObject;
StaticObjectCache.Add(ValueTuple.Create(path.ToLowerInvariant(), PreserveVertices, File.GetLastWriteTime(path)), Object);
StaticObjectCache.TryAdd(ValueTuple.Create(path.ToLowerInvariant(), PreserveVertices, File.GetLastWriteTime(path)), Object);
return true;
}

Expand Down Expand Up @@ -424,13 +424,13 @@ public override bool LoadObject(string path, System.Text.Encoding Encoding, out

if (Object is StaticObject staticObject)
{
StaticObjectCache.Add(ValueTuple.Create(path.ToLowerInvariant(), false, File.GetLastWriteTime(path)), staticObject);
StaticObjectCache.TryAdd(ValueTuple.Create(path.ToLowerInvariant(), false, File.GetLastWriteTime(path)), staticObject);
return true;
}

if (Object is AnimatedObjectCollection aoc)
{
AnimatedObjectCollectionCache.Add(path.ToLowerInvariant(), aoc);
AnimatedObjectCollectionCache.TryAdd(path.ToLowerInvariant(), aoc);
}

return true;
Expand Down
10 changes: 6 additions & 4 deletions source/OpenBVE/System/Interface.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections.Generic;
using System.Collections.Generic;
using System.Globalization;
using OpenBveApi;
using OpenBveApi.Interface;
Expand All @@ -9,9 +9,11 @@ internal static partial class Interface {
internal static void AddMessage(MessageType messageType, bool fileNotFound, string messageText) {
if (messageType == MessageType.Warning && !CurrentOptions.ShowWarningMessages) return;
if (messageType == MessageType.Error && !CurrentOptions.ShowErrorMessages) return;
LogMessages.Add(new LogMessage(messageType, fileNotFound, messageText));
Program.FileSystem.AppendToLogFile(messageText);

lock (LogMessages)
{
LogMessages.Add(new LogMessage(messageType, fileNotFound, messageText));
Program.FileSystem.AppendToLogFile(messageText);
}
}

/// <summary>Parses a string into OpenBVE's internal time representation (Seconds since midnight on the first day)</summary>
Expand Down
Loading