Skip to content

Commit

Permalink
Merge pull request #22 from thiagomvas/1.7-fixes
Browse files Browse the repository at this point in the history
1.7 fixes
  • Loading branch information
thiagomvas authored Jun 3, 2024
2 parents 4e9271d + d35bfb5 commit f53b576
Show file tree
Hide file tree
Showing 22 changed files with 351 additions and 176 deletions.
2 changes: 1 addition & 1 deletion Basalt.Core/Basalt.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>1.6.2</Version>
<Version>1.7.0</Version>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<PackageIcon>BasaltLogoBg.png</PackageIcon>
<PackageReadmeFile>README.md</PackageReadmeFile>
Expand Down
2 changes: 1 addition & 1 deletion Basalt.Core/Common/Abstractions/Sound/AudioType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ public enum AudioType
/// <summary>
/// Represents a music audio type.
/// </summary>
Music
Music,
}
}
20 changes: 3 additions & 17 deletions Basalt.Core/Common/Abstractions/Sound/ISoundSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,13 @@ namespace Basalt.Core.Common.Abstractions.Sound
/// </summary>
public interface ISoundSystem : IEngineComponent
{
/// <summary>
/// Loads an audio resource from the specified file.
/// </summary>
/// <param name="filename">The path to the audio file.</param>
/// <param name="type">The type of audio.</param>
void LoadAudio(string filename, AudioType type);

/// <summary>
/// Plays a loaded audio resource.
/// </summary>
/// <param name="filename">The filename of the audio resource to play.</param>
/// <param name="audioCacheKey">The filename of the audio resource to play.</param>
/// <param name="type">The type of audio.</param>
void PlayAudio(string filename, AudioType type);
void PlayAudio(string audioCacheKey, AudioType type);

/// <summary>
/// Pauses the currently playing audio.
Expand All @@ -39,19 +33,11 @@ public interface ISoundSystem : IEngineComponent
/// <param name="type">The type of audio.</param>
void StopAudio(AudioType type);

/// <summary>
/// Unloads an audio resource.
/// </summary>
/// <param name="filename">The filename of the audio resource to unload.</param>
/// <param name="type">The type of audio.</param>
void UnloadAudio(string filename, AudioType type);

/// <summary>
/// Sets the volume for the specified audio type.
/// </summary>
/// <param name="volume">The volume level.</param>
/// <param name="type">The type of audio.</param>
void SetVolume(float volume, AudioType type);
void SetVolume(float volume);

/// <summary>
/// Checks if music is currently playing.
Expand Down
2 changes: 1 addition & 1 deletion Basalt.Raylib/Basalt.Raylib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Version>1.6.2</Version>
<Version>1.7.0</Version>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<PackageIcon>BasaltLogoBg.png</PackageIcon>
<PackageReadmeFile>README.md</PackageReadmeFile>
Expand Down
16 changes: 10 additions & 6 deletions Basalt.Raylib/Components/ModelRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace Basalt.Raylib.Components
/// </summary>
public class ModelRenderer : Component
{
private string _modelCacheKey;
/// <summary>
/// Gets or sets the size of the model.
/// </summary>
Expand All @@ -32,12 +33,15 @@ public class ModelRenderer : Component
/// <summary>
/// Gets or sets the cache key for the model.
/// </summary>
public string ModelCacheKey { get; set; }

/// <summary>
/// Gets or sets the cache key for the lighting shader.
/// </summary>
public string LightingShaderCacheKey { get; set; }
public string ModelCacheKey
{
get => _modelCacheKey;
set
{
_modelCacheKey = value;
init = false;
}
}

/// <summary>
/// Gets or sets the scale of the model.
Expand Down
147 changes: 132 additions & 15 deletions Basalt.Raylib/Graphics/RaylibCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public unsafe static class ResourceCacheExtensions
private static object shaderLock = new object();

private static object textureLock = new object();
private static object audioLock = new object();
/// <summary>
/// The queue that stores the model load requests.
/// </summary>
Expand All @@ -28,10 +29,12 @@ public unsafe static class ResourceCacheExtensions
private static List<KeyValuePair<string, ShaderLoadRequest>> shaderLoadQueue = new();

private static List<KeyValuePair<string, TextureLoadRequest>> textureLoadQueue = new();
private static List<KeyValuePair<string, AudioLoadRequest>> audioLoadQueue = new();

private static List<string> modelKeys = new();
private static List<string> shaderKeys = new();
private static List<string> textureKeys = new();
private static List<string> audioKeys = new();

/// <summary>
/// Loads the models and shaders from the load queues.
Expand Down Expand Up @@ -64,6 +67,15 @@ internal static void LoadQueued(this ResourceCache cache)
}
textureLoadQueue.Clear();
}

lock (audioLock)
{
foreach (var req in audioLoadQueue)
{
loadAudio(req.Value);
}
audioLoadQueue.Clear();
}
}

/// <summary>
Expand All @@ -90,8 +102,6 @@ public static void LoadModel(this ResourceCache cache, string modelName, string
}
}

Model model = Raylib_cs.Raylib.LoadModel(modelPath);

lock (modelLock)
{
loadModel(new(modelName, modelPath, shaderCacheKey));
Expand All @@ -106,26 +116,44 @@ public static void LoadModel(this ResourceCache cache, string modelName, string
/// <param name="vertexShaderPath">The path to the vertex shader file.</param>
public static void LoadShader(this ResourceCache cache, string shaderName, string fragmentShaderPath, string vertexShaderPath)
{
var request = new ShaderLoadRequest()
{
shaderName = shaderName,
fragmentShaderPath = fragmentShaderPath,
vertexShaderPath = vertexShaderPath
};
if (!Raylib_cs.Raylib.IsWindowReady())
{
var request = new ShaderLoadRequest()
{
shaderName = shaderName,
fragmentShaderPath = fragmentShaderPath,
vertexShaderPath = vertexShaderPath
};
lock (shaderLock)
{
shaderLoadQueue.Add(new(shaderName, request));
return;
}
}

Shader shader = Raylib_cs.Raylib.LoadShader(vertexShaderPath, fragmentShaderPath);

lock (shaderLock)
{
ResourceCache.CacheResource(shaderName, shader);
loadShader(request);
}
}
public static void LoadAudio(this ResourceCache cache, string audioName, string audioPath, AudioLoadRequest.Type loadType)
{
if (!Raylib_cs.Raylib.IsWindowReady())
{
var request = new AudioLoadRequest()
{
audioName = audioName,
audioPath = audioPath,
loadType = loadType
};
lock (shaderLock)
{
audioLoadQueue.Add(new(audioName, request));
return;
}
}
lock (audioLock)
{
loadAudio(new(audioName, audioPath, loadType));
}
}

Expand All @@ -145,8 +173,10 @@ public static void LoadTexture(this ResourceCache cache, string textureName, str
}
}

Texture2D texture = Raylib_cs.Raylib.LoadTexture(texturePath);
ResourceCache.CacheResource(textureName, texture);
lock (textureLock)
{
loadTexture(new(textureName, texturePath));
}
}

/// <summary>
Expand Down Expand Up @@ -205,6 +235,31 @@ public static void LoadTexture(this ResourceCache cache, string textureName, str
}
}
}

public static Music? GetMusic(this ResourceCache cache, string musicName)
{
if (ResourceCache.GetResource<Music>(musicName) is Music music)
{
return music;
}
else
{
return null;
}
}

public static Raylib_cs.Sound? GetSound(this ResourceCache cache, string soundName)
{
if (ResourceCache.GetResource<Raylib_cs.Sound>(soundName) is Raylib_cs.Sound sound)
{
return sound;
}
else
{
return null;
}
}

/// <summary>
/// Caches a model with the specified name.
/// </summary>
Expand Down Expand Up @@ -241,6 +296,17 @@ public static void CacheTexture(this ResourceCache cache, string textureName, Te
}
}

public static void CacheMusic(this ResourceCache cache, string musicName, Music music)
{
ResourceCache.CacheResource(musicName, music);
}

public static void CacheSound(this ResourceCache cache, string soundName, Raylib_cs.Sound sound)
{
ResourceCache.CacheResource(soundName, sound);
}


/// <summary>
/// Loads a model based on the specified request.
/// </summary>
Expand Down Expand Up @@ -280,6 +346,23 @@ private static void loadTexture(TextureLoadRequest req)
textureKeys.Add(req.textureName);
}

private static void loadAudio(AudioLoadRequest req)
{
switch (req.loadType)
{
case AudioLoadRequest.Type.Music:
Music music = Raylib_cs.Raylib.LoadMusicStream(req.audioPath);
ResourceCache.CacheResource(req.audioName, music);
break;
case AudioLoadRequest.Type.Sound:
Raylib_cs.Sound sound = Raylib_cs.Raylib.LoadSound(req.audioPath);
ResourceCache.CacheResource(req.audioName, sound);
break;
}
Engine.Instance.Logger.LogInformation($"Loaded audio: {req.audioName}");
audioKeys.Add(req.audioName);
}

/// <summary>
/// Unloads all cached Raylib models and shaders.
/// </summary>
Expand Down Expand Up @@ -312,6 +395,22 @@ public static void UnloadRaylib(this ResourceCache cache)
}
textureKeys.Clear();
}

lock (audioLock)
{
foreach (var key in audioKeys)
{
if (ResourceCache.TryGetResource(key, out Music m))
{
Raylib_cs.Raylib.UnloadMusicStream(m);
}
else if (ResourceCache.TryGetResource(key, out Raylib_cs.Sound s))
{
Raylib_cs.Raylib.UnloadSound(s);
}
}
audioKeys.Clear();
}
}

internal static void LoadRaylibPrimitives(this ResourceCache cache)
Expand All @@ -334,7 +433,6 @@ internal static void LoadRaylibPrimitives(this ResourceCache cache)
torus.Materials[0].Shader = shader;
knot.Materials[0].Shader = shader;
}

ResourceCache.CacheResource("cube", cube);
ResourceCache.CacheResource("sphere", sphere);
ResourceCache.CacheResource("plane", plane);
Expand Down Expand Up @@ -389,5 +487,24 @@ public TextureLoadRequest(string textureName, string texturePath)
this.texturePath = texturePath;
}
}

public struct AudioLoadRequest
{
public string audioName;
public string audioPath;
public Type loadType;
public enum Type
{
Music,
Sound,
}

public AudioLoadRequest(string audioName, string audioPath, Type loadType)
{
this.audioName = audioName;
this.audioPath = audioPath;
this.loadType = loadType;
}
}
}
}
Loading

0 comments on commit f53b576

Please sign in to comment.