From 60451e9f3b265a8d72d687ccce7b1ca1898c8fd9 Mon Sep 17 00:00:00 2001 From: Tornado Tech <54727692+Tornado-Technology@users.noreply.github.com> Date: Sun, 14 Jul 2024 12:30:37 +1000 Subject: [PATCH] Rewrited PreloadTextures realisation --- .../Event/AudioLibraryInitializedEvent.cs | 5 +++++ .../OpenAL/ContextExtension.cs | 2 +- .../OpenAL/OpenALAudioManager.cs | 6 +++++- Hypercube.Client/Dependencies.cs | 2 +- .../Event/GraphicsLibraryInitializedEvent.cs | 5 +++++ .../Graphics/Rendering/Renderer.OpenGL.cs | 2 ++ .../Graphics/Rendering/Renderer.cs | 8 -------- .../Texture2DCreationSettings.cs | 4 ++-- .../Caching/ResourceCacher.Preload.cs | 18 +++++++++--------- .../Resources/Caching/ResourceCacher.cs | 18 +++++++++++++----- .../Resources/Caching/IResourceCacher.cs | 5 ----- 11 files changed, 43 insertions(+), 32 deletions(-) create mode 100644 Hypercube.Client/Audio/Event/AudioLibraryInitializedEvent.cs rename Hypercube.Client/Audio/{Realisations => Realisation}/OpenAL/ContextExtension.cs (68%) rename Hypercube.Client/Audio/{Realisations => Realisation}/OpenAL/OpenALAudioManager.cs (97%) create mode 100644 Hypercube.Client/Graphics/Event/GraphicsLibraryInitializedEvent.cs diff --git a/Hypercube.Client/Audio/Event/AudioLibraryInitializedEvent.cs b/Hypercube.Client/Audio/Event/AudioLibraryInitializedEvent.cs new file mode 100644 index 0000000..10c5ca4 --- /dev/null +++ b/Hypercube.Client/Audio/Event/AudioLibraryInitializedEvent.cs @@ -0,0 +1,5 @@ +using Hypercube.Shared.EventBus.Events; + +namespace Hypercube.Client.Audio.Event; + +public readonly record struct AudioLibraryInitializedEvent : IEventArgs; \ No newline at end of file diff --git a/Hypercube.Client/Audio/Realisations/OpenAL/ContextExtension.cs b/Hypercube.Client/Audio/Realisation/OpenAL/ContextExtension.cs similarity index 68% rename from Hypercube.Client/Audio/Realisations/OpenAL/ContextExtension.cs rename to Hypercube.Client/Audio/Realisation/OpenAL/ContextExtension.cs index 5648e79..d88daa3 100644 --- a/Hypercube.Client/Audio/Realisations/OpenAL/ContextExtension.cs +++ b/Hypercube.Client/Audio/Realisation/OpenAL/ContextExtension.cs @@ -1,4 +1,4 @@ -namespace Hypercube.Client.Audio.Realisations.OpenAL; +namespace Hypercube.Client.Audio.Realisation.OpenAL; public static class ContextExtension { diff --git a/Hypercube.Client/Audio/Realisations/OpenAL/OpenALAudioManager.cs b/Hypercube.Client/Audio/Realisation/OpenAL/OpenALAudioManager.cs similarity index 97% rename from Hypercube.Client/Audio/Realisations/OpenAL/OpenALAudioManager.cs rename to Hypercube.Client/Audio/Realisation/OpenAL/OpenALAudioManager.cs index 399b8b2..6b592c3 100644 --- a/Hypercube.Client/Audio/Realisations/OpenAL/OpenALAudioManager.cs +++ b/Hypercube.Client/Audio/Realisation/OpenAL/OpenALAudioManager.cs @@ -1,4 +1,5 @@ using System.Collections.Frozen; +using Hypercube.Client.Audio.Event; using Hypercube.Client.Audio.Loading; using Hypercube.Client.Utilities.Helpers; using Hypercube.Shared.Dependency; @@ -9,7 +10,7 @@ using Hypercube.Shared.Runtimes.Event; using OpenTK.Audio.OpenAL; -namespace Hypercube.Client.Audio.Realisations.OpenAL; +namespace Hypercube.Client.Audio.Realisation.OpenAL; /// /// For some reason, on my Windows 11 machine there is no openal32.dll @@ -57,6 +58,9 @@ public void Initialize() return; CreateContext(); + + _logger.EngineInfo("Initialized"); + _eventBus.Raise(new AudioLibraryInitializedEvent()); } private AudioStream CreateAudio(ResourcePath path, AudioSettings settings) diff --git a/Hypercube.Client/Dependencies.cs b/Hypercube.Client/Dependencies.cs index 9d4aa35..f7b81fd 100644 --- a/Hypercube.Client/Dependencies.cs +++ b/Hypercube.Client/Dependencies.cs @@ -1,6 +1,6 @@ using Hypercube.Client.Audio; using Hypercube.Client.Audio.Loading; -using Hypercube.Client.Audio.Realisations.OpenAL; +using Hypercube.Client.Audio.Realisation.OpenAL; using Hypercube.Client.Graphics.Drawing; using Hypercube.Client.Graphics.Rendering; using Hypercube.Client.Graphics.Texturing; diff --git a/Hypercube.Client/Graphics/Event/GraphicsLibraryInitializedEvent.cs b/Hypercube.Client/Graphics/Event/GraphicsLibraryInitializedEvent.cs new file mode 100644 index 0000000..5444069 --- /dev/null +++ b/Hypercube.Client/Graphics/Event/GraphicsLibraryInitializedEvent.cs @@ -0,0 +1,5 @@ +using Hypercube.Shared.EventBus.Events; + +namespace Hypercube.Client.Graphics.Event; + +public readonly record struct GraphicsLibraryInitializedEvent : IEventArgs; \ No newline at end of file diff --git a/Hypercube.Client/Graphics/Rendering/Renderer.OpenGL.cs b/Hypercube.Client/Graphics/Rendering/Renderer.OpenGL.cs index 930fd0f..0ded645 100644 --- a/Hypercube.Client/Graphics/Rendering/Renderer.OpenGL.cs +++ b/Hypercube.Client/Graphics/Rendering/Renderer.OpenGL.cs @@ -1,4 +1,5 @@ using System.Runtime.InteropServices; +using Hypercube.Client.Graphics.Event; using Hypercube.Shared.Logging; using OpenTK.Windowing.GraphicsLibraryFramework; using OpenToolkit.Graphics.OpenGL4; @@ -43,6 +44,7 @@ private void InitOpenGL() GL.ClearColor(0, 0, 0, 0); _loggerOpenGL.EngineInfo("Initialized"); + _eventBus.Raise(new GraphicsLibraryInitializedEvent()); } private void DebugMessageCallback(DebugSource source, DebugType type, int id, DebugSeverity severity, int length, IntPtr messagePointer, IntPtr userparam) diff --git a/Hypercube.Client/Graphics/Rendering/Renderer.cs b/Hypercube.Client/Graphics/Rendering/Renderer.cs index 949f829..e1e641e 100644 --- a/Hypercube.Client/Graphics/Rendering/Renderer.cs +++ b/Hypercube.Client/Graphics/Rendering/Renderer.cs @@ -1,13 +1,11 @@ using System.Collections.Frozen; using Hypercube.Client.Graphics.OpenGL; using Hypercube.Client.Graphics.Texturing; -using Hypercube.Client.Graphics.Texturing.Events; using Hypercube.Client.Graphics.Viewports; using Hypercube.Client.Graphics.Windows; using Hypercube.Client.Graphics.Windows.Manager; using Hypercube.Shared.Dependency; using Hypercube.Shared.EventBus; -using Hypercube.Shared.EventBus.Events; using Hypercube.Shared.Logging; using Hypercube.Shared.Resources.Caching; using Hypercube.Shared.Resources.Manager; @@ -103,13 +101,7 @@ private void OnStartup(ref RuntimeStartupEvent args) var windowIcons = _windowManager.LoadWindowIcons(_textureManager, _resourceManager, "/Icons").ToList(); _windowManager.SetWindowIcons(MainWindow, windowIcons); - InitOpenGL(); - - _resourceCacher.PreloadTextures(); - _resourceCacher.PreloadShaders(); - _resourceCacher.PreloadAudio(); - OnLoad(); } diff --git a/Hypercube.Client/Graphics/Texturing/TextureSettings/Texture2DCreationSettings.cs b/Hypercube.Client/Graphics/Texturing/TextureSettings/Texture2DCreationSettings.cs index 984d7bc..d0677b5 100644 --- a/Hypercube.Client/Graphics/Texturing/TextureSettings/Texture2DCreationSettings.cs +++ b/Hypercube.Client/Graphics/Texturing/TextureSettings/Texture2DCreationSettings.cs @@ -36,8 +36,8 @@ public Texture2DCreationSettings() { new TextureParameter(TextureParameterName.TextureWrapS, (int)TextureWrapMode.Repeat), new TextureParameter(TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat), - new TextureParameter(TextureParameterName.TextureMinFilter, (int)TextureMinFilter.LinearMipmapLinear), - new TextureParameter(TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear), + new TextureParameter(TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest), + new TextureParameter(TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest), }; PixelInternalFormat = PixelInternalFormat.Rgba; Level = 0; diff --git a/Hypercube.Client/Resources/Caching/ResourceCacher.Preload.cs b/Hypercube.Client/Resources/Caching/ResourceCacher.Preload.cs index c264105..b47e1d8 100644 --- a/Hypercube.Client/Resources/Caching/ResourceCacher.Preload.cs +++ b/Hypercube.Client/Resources/Caching/ResourceCacher.Preload.cs @@ -1,4 +1,6 @@ using System.Diagnostics; +using Hypercube.Client.Audio.Event; +using Hypercube.Client.Graphics.Event; using Hypercube.Shared.Dependency; using Hypercube.Shared.Logging; @@ -7,20 +9,18 @@ namespace Hypercube.Client.Resources.Caching; public partial class ResourceCacher { private readonly Logger _loggerPreload = LoggingManager.GetLogger("cache.preload"); - - public void PreloadTextures() + + private void OnAudioLibraryInitialized(ref AudioLibraryInitializedEvent ev) { - PreloadTextures(_container); + PreloadAudio(_container); } - public void PreloadShaders() + + private void OnGraphicsLibraryInitialized(ref GraphicsLibraryInitializedEvent ev) { + PreloadTextures(_container); PreloadShaders(_container); } - public void PreloadAudio() - { - PreloadAudio(_container); - } - + private void PreloadTextures(DependenciesContainer container) { _loggerPreload.EngineInfo("Preloading textures..."); diff --git a/Hypercube.Client/Resources/Caching/ResourceCacher.cs b/Hypercube.Client/Resources/Caching/ResourceCacher.cs index 15cc32b..fed2466 100644 --- a/Hypercube.Client/Resources/Caching/ResourceCacher.cs +++ b/Hypercube.Client/Resources/Caching/ResourceCacher.cs @@ -1,5 +1,8 @@ using System.Diagnostics.CodeAnalysis; +using Hypercube.Client.Audio.Event; +using Hypercube.Client.Graphics.Event; using Hypercube.Shared.Dependency; +using Hypercube.Shared.EventBus; using Hypercube.Shared.Logging; using Hypercube.Shared.Resources; using Hypercube.Shared.Resources.Caching; @@ -8,16 +11,21 @@ namespace Hypercube.Client.Resources.Caching; -public partial class ResourceCacher : IResourceCacher +public partial class ResourceCacher : IResourceCacher, IEventSubscriber, IPostInject { + [Dependency] private readonly IEventBus _eventBus = default!; [Dependency] private readonly IResourceManager _resourceManager = default!; - private Dictionary> _cachedResources = new(); - - private DependenciesContainer _container = DependencyManager.GetContainer(); - + private readonly Dictionary> _cachedResources = new(); + private readonly DependenciesContainer _container = DependencyManager.GetContainer(); private readonly Logger _logger = LoggingManager.GetLogger("cache"); + public void PostInject() + { + _eventBus.Subscribe(this, OnAudioLibraryInitialized); + _eventBus.Subscribe(this, OnGraphicsLibraryInitialized); + } + public T GetResource(ResourcePath path, bool useFallback = true) where T : Resource, new() { var typeDict = GetTypeDict(); diff --git a/Hypercube.Shared/Resources/Caching/IResourceCacher.cs b/Hypercube.Shared/Resources/Caching/IResourceCacher.cs index bdb2f76..08deb21 100644 --- a/Hypercube.Shared/Resources/Caching/IResourceCacher.cs +++ b/Hypercube.Shared/Resources/Caching/IResourceCacher.cs @@ -1,5 +1,4 @@ using System.Diagnostics.CodeAnalysis; -using Hypercube.Shared.Resources.Caching.Resource; namespace Hypercube.Shared.Resources.Caching; @@ -12,8 +11,4 @@ bool TryGetResource(ResourcePath path, [NotNullWhen(true)] out T? resource) void CacheResource(ResourcePath path, T resource) where T : Resource.Resource, new(); - - void PreloadTextures(); - void PreloadShaders(); - void PreloadAudio(); } \ No newline at end of file