Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/caching-manager' into caching-ma…
Browse files Browse the repository at this point in the history
…nager
  • Loading branch information
JerryImMouse committed Jul 14, 2024
2 parents 416c245 + 60451e9 commit 9fc5464
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 32 deletions.
5 changes: 5 additions & 0 deletions Hypercube.Client/Audio/Event/AudioLibraryInitializedEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using Hypercube.Shared.EventBus.Events;

namespace Hypercube.Client.Audio.Event;

public readonly record struct AudioLibraryInitializedEvent : IEventArgs;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Hypercube.Client.Audio.Realisations.OpenAL;
namespace Hypercube.Client.Audio.Realisation.OpenAL;

public static class ContextExtension
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

/// <remarks>
/// For some reason, on my Windows 11 machine there is no <c>openal32.dll</c>
Expand Down Expand Up @@ -57,6 +58,9 @@ public void Initialize()
return;

CreateContext();

_logger.EngineInfo("Initialized");
_eventBus.Raise(new AudioLibraryInitializedEvent());
}

private AudioStream CreateAudio(ResourcePath path, AudioSettings settings)
Expand Down
2 changes: 1 addition & 1 deletion Hypercube.Client/Dependencies.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using Hypercube.Shared.EventBus.Events;

namespace Hypercube.Client.Graphics.Event;

public readonly record struct GraphicsLibraryInitializedEvent : IEventArgs;
2 changes: 2 additions & 0 deletions Hypercube.Client/Graphics/Rendering/Renderer.OpenGL.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 0 additions & 8 deletions Hypercube.Client/Graphics/Rendering/Renderer.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
18 changes: 9 additions & 9 deletions Hypercube.Client/Resources/Caching/ResourceCacher.Preload.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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...");
Expand Down
18 changes: 13 additions & 5 deletions Hypercube.Client/Resources/Caching/ResourceCacher.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<Type, Dictionary<ResourcePath, Resource>> _cachedResources = new();

private DependenciesContainer _container = DependencyManager.GetContainer();

private readonly Dictionary<Type, Dictionary<ResourcePath, Resource>> _cachedResources = new();
private readonly DependenciesContainer _container = DependencyManager.GetContainer();
private readonly Logger _logger = LoggingManager.GetLogger("cache");

public void PostInject()
{
_eventBus.Subscribe<AudioLibraryInitializedEvent>(this, OnAudioLibraryInitialized);
_eventBus.Subscribe<GraphicsLibraryInitializedEvent>(this, OnGraphicsLibraryInitialized);
}

public T GetResource<T>(ResourcePath path, bool useFallback = true) where T : Resource, new()
{
var typeDict = GetTypeDict<T>();
Expand Down
5 changes: 0 additions & 5 deletions Hypercube.Shared/Resources/Caching/IResourceCacher.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Diagnostics.CodeAnalysis;
using Hypercube.Shared.Resources.Caching.Resource;

namespace Hypercube.Shared.Resources.Caching;

Expand All @@ -12,8 +11,4 @@ bool TryGetResource<T>(ResourcePath path, [NotNullWhen(true)] out T? resource)

void CacheResource<T>(ResourcePath path, T resource)
where T : Resource.Resource, new();

void PreloadTextures();
void PreloadShaders();
void PreloadAudio();
}

0 comments on commit 9fc5464

Please sign in to comment.