Skip to content

Commit

Permalink
Asset management reimplementation (#11)
Browse files Browse the repository at this point in the history
* Removed all resources code, lul.

* Made Asset generic and sealed.

* Sketched most of the design.

* Fixed many things about FileSystemAssetSource.

* AssemblyResourcesAssetSource class

* Added BuiltInAssets asset source.

* Renames

* Immediate loading sketch.

* Autoloading asset readers, + documentation + fixes.

* Allowed readers to use asterisks to accept all extensions.

* Fixed 2 typos.

* Renamed Resources to Assets

* Asset.FromValue -> Assets.CreateUntracked

* Asset & Assets Documentation, renames.

* Fixed the rendering pipeline not requesting assets correctly.

* Assets.Exists, + documentation templates.

* Assets.TryGet + relative path overloads, internalized AssetLookup registration.

* Oops, fixes for the previous commit.

* Support for HJSON in materials and shaders.

* Asynchronous asset loading!

* Updated AudioSource to use asset references correctly.

* Many threading fixes, switched to ImageSharp in favor of DevIL.

* Fixed AudioSource clips not getting requested.

* Fixed random texture corruption.

* Unhardcoded reader registration with another kind of hardcode.
  • Loading branch information
Mirsario authored Oct 13, 2021
1 parent 819437c commit e0e10ed
Show file tree
Hide file tree
Showing 76 changed files with 1,411 additions and 1,213 deletions.
25 changes: 12 additions & 13 deletions Src/Audio/AudioClip.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
using System;
using System.Runtime.InteropServices;
using Dissonance.Engine.IO;
using Dissonance.Framework.Audio;

namespace Dissonance.Engine.Audio
{
public class AudioClip : Asset
public sealed class AudioClip : IDisposable
{
internal uint bufferId;

protected int channelsNum;
protected int bytesPerSample;
protected int sampleRate;
private int channelsNum;
private int bytesPerSample;
private int sampleRate;

public float LengthInSeconds { get; private set; }

internal uint BufferId { get; private set; }

public AudioClip()
{
bufferId = AL.GenBuffer();
BufferId = AL.GenBuffer();
}

public override void Dispose()
public void Dispose()
{
if (bufferId > 0) {
AL.DeleteBuffer(bufferId);
if (BufferId > 0) {
AL.DeleteBuffer(BufferId);

bufferId = 0;
BufferId = 0;
}
}

Expand All @@ -44,7 +43,7 @@ public unsafe void SetData<T>(T[] data, int channelsNum, int bytesPerSample, int
var format = GetSoundFormat(channelsNum, bytesPerSample);

fixed(T* ptr = data) {
AL.BufferData(bufferId, format, (IntPtr)ptr, data.Length * Marshal.SizeOf<T>(), sampleRate);
AL.BufferData(BufferId, format, (IntPtr)ptr, data.Length * Marshal.SizeOf<T>(), sampleRate);
}

AudioEngine.CheckALErrors();
Expand Down
15 changes: 13 additions & 2 deletions Src/Audio/Components/AudioSource.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
using Dissonance.Engine.IO;

namespace Dissonance.Engine.Audio
{
public struct AudioSource
{
internal enum PlaybackAction
{
None,
Play,
Pause,
Stop
}

internal uint sourceId = 0;
internal uint bufferId = 0;
internal bool wasLooped = false;
internal bool was2D = false;
internal PlaybackAction PendingAction = PlaybackAction.None;

public AudioClip Clip { get; set; } = null;
public Asset<AudioClip> Clip { get; set; } = null;
public float Volume { get; set; } = 1f;
public float Pitch { get; set; } = 1f;
public bool Loop { get; set; } = false;
Expand All @@ -16,7 +27,7 @@ public struct AudioSource
public float MaxDistance { get; set; } = 32f;
public float PlaybackOffset { get; set; } = 0f;

public AudioSource(AudioClip clip) : this()
public AudioSource(Asset<AudioClip> clip) : this()
{
Clip = clip;
}
Expand Down
78 changes: 45 additions & 33 deletions Src/Audio/Systems/AudioSourceSystem.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Dissonance.Framework.Audio;
using Dissonance.Engine.IO;
using Dissonance.Framework.Audio;

namespace Dissonance.Engine.Audio
{
Expand All @@ -23,6 +24,24 @@ protected internal override void Initialize()

private void Update()
{
foreach (var message in ReadMessages<StopAudioSourceMessage>()) {
if (message.Entity.Has<AudioSource>()) {
message.Entity.Get<AudioSource>().PendingAction = AudioSource.PlaybackAction.Stop;
}
}

foreach (var message in ReadMessages<PauseAudioSourceMessage>()) {
if (message.Entity.Has<AudioSource>()) {
message.Entity.Get<AudioSource>().PendingAction = AudioSource.PlaybackAction.Pause;
}
}

foreach (var message in ReadMessages<PlayAudioSourceMessage>()) {
if (message.Entity.Has<AudioSource>()) {
message.Entity.Get<AudioSource>().PendingAction = AudioSource.PlaybackAction.Play;
}
}

foreach (var entity in entities.ReadEntities()) {
ref var audioSource = ref entity.Get<AudioSource>();

Expand All @@ -31,15 +50,39 @@ private void Update()
AL.GenSource(out audioSource.sourceId);
}

// Load in clips
bool clipReady = audioSource.Clip != null && audioSource.Clip.TryGetOrRequestValue(out _);

// Update buffer.
uint newBufferId = audioSource.Clip?.bufferId ?? 0;
uint newBufferId = clipReady ? audioSource.Clip.Value.BufferId : 0;

if (audioSource.bufferId != newBufferId) {
AL.Source(audioSource.sourceId, SourceInt.Buffer, (int)newBufferId);

audioSource.bufferId = newBufferId;
}

if (audioSource.PendingAction != AudioSource.PlaybackAction.None) {
switch (audioSource.PendingAction) {
case AudioSource.PlaybackAction.Play:
if (audioSource.bufferId == 0) {
break;
}

AL.SourcePlay(audioSource.sourceId);
goto default;
case AudioSource.PlaybackAction.Pause:
AL.SourcePause(audioSource.sourceId);
goto default;
case AudioSource.PlaybackAction.Stop:
AL.SourceStop(audioSource.sourceId);
goto default;
default:
audioSource.PendingAction = 0;
break;
}
}

// Update volume.
AL.Source(audioSource.sourceId, SourceFloat.Gain, audioSource.Volume);

Expand Down Expand Up @@ -76,37 +119,6 @@ private void Update()
}

AudioEngine.CheckALErrors();

ReadMessages();
}

private void ReadMessages()
{
foreach (var stopMessage in ReadMessages<StopAudioSourceMessage>()) {
if (stopMessage.Entity.Has<AudioSource>()) {
var audioSource = stopMessage.Entity.Get<AudioSource>();

AL.SourceStop(audioSource.sourceId);
}
}

foreach (var pauseMessage in ReadMessages<PauseAudioSourceMessage>()) {
if (pauseMessage.Entity.Has<AudioSource>()) {
var audioSource = pauseMessage.Entity.Get<AudioSource>();

AL.SourcePause(audioSource.sourceId);
}
}

foreach (var playMessage in ReadMessages<PlayAudioSourceMessage>()) {
if (playMessage.Entity.Has<AudioSource>()) {
var audioSource = playMessage.Entity.Get<AudioSource>();

AL.SourcePlay(audioSource.sourceId);
}
}

AudioEngine.CheckALErrors();
}
}
}
4 changes: 2 additions & 2 deletions Src/BuiltInAssets/Shaders/Debug/Debug.program
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"Debug": {
"vertexShader": "Shaders/Debug/Debug.vert",
"fragmentShader": "Shaders/Debug/Debug.frag"
"vertexShader": "BuiltInAssets/Shaders/Debug/Debug.vert",
"fragmentShader": "BuiltInAssets/Shaders/Debug/Debug.frag"
}
}
Loading

0 comments on commit e0e10ed

Please sign in to comment.