Skip to content

Commit

Permalink
SFML.Net 3
Browse files Browse the repository at this point in the history
  • Loading branch information
Marioalexsan committed Sep 20, 2024
1 parent 2999135 commit b07c9ca
Show file tree
Hide file tree
Showing 50 changed files with 2,257 additions and 999 deletions.
21 changes: 20 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,22 @@ indent_style = space
indent_size = 4
insert_final_newline = false
trim_trailing_whitespace = true
csharp_using_directive_placement = outside_namespace:silent
csharp_prefer_simple_using_statement = true:suggestion
csharp_prefer_braces = true:warning
csharp_style_namespace_declarations = file_scoped:silent
csharp_style_prefer_method_group_conversion = true:silent
csharp_style_prefer_top_level_statements = false:warning
csharp_style_prefer_primary_constructors = true:suggestion
csharp_style_expression_bodied_methods = true:warning
csharp_style_expression_bodied_constructors = true:warning
csharp_style_expression_bodied_operators = true:warning
csharp_style_expression_bodied_properties = true:warning
csharp_style_expression_bodied_indexers = true:warning
csharp_style_expression_bodied_accessors = true:warning
csharp_style_expression_bodied_lambdas = true:silent
csharp_style_expression_bodied_local_functions = false:silent
csharp_indent_labels = no_change

#########################
# File Extension Settings
Expand Down Expand Up @@ -350,4 +366,7 @@ dotnet_diagnostic.IDE0130.severity = suggestion
dotnet_diagnostic.IDE0060.severity = suggestion

# CA1805: Do not initialize unnecessarily
dotnet_diagnostic.CA1805.severity = warning
dotnet_diagnostic.CA1805.severity = warning
dotnet_style_prefer_simplified_boolean_expressions = true:suggestion
dotnet_style_operator_placement_when_wrapping = beginning_of_line
tab_width = 4
19 changes: 19 additions & 0 deletions SFML.sln
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
.editorconfig = .editorconfig
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{0A84335E-59C1-4969-9F5F-4D42DC7F2BFC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SFML.System.Test", "test\SFML.System.Test\SFML.System.Test.csproj", "{90D86010-580C-4D2A-8AD1-C18CD982C5A0}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -88,10 +92,25 @@ Global
{88DD6B5D-3013-4737-A77C-EC2563FCED38}.Release|x64.Build.0 = Release|x64
{88DD6B5D-3013-4737-A77C-EC2563FCED38}.Release|x86.ActiveCfg = Release|x86
{88DD6B5D-3013-4737-A77C-EC2563FCED38}.Release|x86.Build.0 = Release|x86
{90D86010-580C-4D2A-8AD1-C18CD982C5A0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{90D86010-580C-4D2A-8AD1-C18CD982C5A0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{90D86010-580C-4D2A-8AD1-C18CD982C5A0}.Debug|x64.ActiveCfg = Debug|x64
{90D86010-580C-4D2A-8AD1-C18CD982C5A0}.Debug|x64.Build.0 = Debug|x64
{90D86010-580C-4D2A-8AD1-C18CD982C5A0}.Debug|x86.ActiveCfg = Debug|x86
{90D86010-580C-4D2A-8AD1-C18CD982C5A0}.Debug|x86.Build.0 = Debug|x86
{90D86010-580C-4D2A-8AD1-C18CD982C5A0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{90D86010-580C-4D2A-8AD1-C18CD982C5A0}.Release|Any CPU.Build.0 = Release|Any CPU
{90D86010-580C-4D2A-8AD1-C18CD982C5A0}.Release|x64.ActiveCfg = Release|x64
{90D86010-580C-4D2A-8AD1-C18CD982C5A0}.Release|x64.Build.0 = Release|x64
{90D86010-580C-4D2A-8AD1-C18CD982C5A0}.Release|x86.ActiveCfg = Release|x86
{90D86010-580C-4D2A-8AD1-C18CD982C5A0}.Release|x86.Build.0 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{90D86010-580C-4D2A-8AD1-C18CD982C5A0} = {0A84335E-59C1-4969-9F5F-4D42DC7F2BFC}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {384AFDD8-7EEF-462A-B496-B5F71D0EEF1D}
EndGlobalSection
Expand Down
50 changes: 50 additions & 0 deletions src/SFML.Audio/Cone.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System.Runtime.InteropServices;
using SFML.System;

namespace SFML.Audio
{
////////////////////////////////////////////////////////////
/// <summary>
/// Structure defining the properties of a directional cone
/// <para/>
/// Sounds will play at gain 1 when they are positioned
/// within the inner angle of the cone. Sounds will play
/// at outerGain when they are positioned outside the
/// outer angle of the cone. The gain declines linearly
/// from 1 to outerGain as the sound moves from the inner
/// angle to the outer angle.
/// </summary>
////////////////////////////////////////////////////////////
public struct Cone
{
/// <summary>Inner angle</summary>
public Angle InnerAngle;

/// <summary>Outer angle</summary>
public Angle OuterAngle;

/// <summary>Outer angle</summary>
public float OuterGain;

[StructLayout(LayoutKind.Sequential)]
internal struct MarshalData
{
public float InnerAngleDegrees;
public float OuterAngleDegrees;
public float OuterGain;
}

// Return a marshalled version of the instance, that can directly be passed to the C API
internal MarshalData Marshal()
{
var data = new MarshalData
{
InnerAngleDegrees = InnerAngle.Degrees,
OuterAngleDegrees = OuterAngle.Degrees,
OuterGain = OuterGain
};

return data;
}
}
}
24 changes: 24 additions & 0 deletions src/SFML.Audio/Listener.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Runtime.InteropServices;
using System.Security;
using SFML.System;
Expand Down Expand Up @@ -52,6 +53,29 @@ public static Vector3f Direction
set => sfListener_setDirection(value);
}

////////////////////////////////////////////////////////////
/// <summary>
/// The velocity of the listener in the scene (default is (0, 0, -1))
/// </summary>
////////////////////////////////////////////////////////////
public static Vector3f Velocity
{
get => throw new NotImplementedException("TODO Implement when CSFML is ready.");
set => throw new NotImplementedException("TODO Implement when CSFML is ready.");
}

////////////////////////////////////////////////////////////
/// <summary>
/// The cone defines how directional attenuation is applied.
/// The default cone of a sound is {2 * PI, 2 * PI, 1}.
/// </summary>
////////////////////////////////////////////////////////////
public static Cone Cone
{
get => throw new NotImplementedException("TODO Implement when CSFML is ready.");
set => throw new NotImplementedException("TODO Implement when CSFML is ready.");
}

////////////////////////////////////////////////////////////
/// <summary>
/// The up vector is the vector that points upward from the
Expand Down
19 changes: 15 additions & 4 deletions src/SFML.Audio/Music.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Security;
Expand Down Expand Up @@ -132,6 +133,16 @@ public Music(byte[] bytes) :
////////////////////////////////////////////////////////////
public uint SampleRate => sfMusic_getSampleRate(CPointer);

////////////////////////////////////////////////////////////
/// <summary>
/// Get the map of position in sample frame to sound channel
/// <para/>
/// This is used to map a sample in the sample stream to a
/// position during spatialisation.
/// </summary>
////////////////////////////////////////////////////////////
public IEnumerable<SoundChannel> ChannelMap => throw new NotImplementedException("TODO CSFML implementation.");

////////////////////////////////////////////////////////////
/// <summary>
/// Number of channels (1 = mono, 2 = stereo)
Expand Down Expand Up @@ -165,8 +176,8 @@ public Music(byte[] bytes) :
////////////////////////////////////////////////////////////
public bool Loop
{
get => sfMusic_getLoop(CPointer);
set => sfMusic_setLoop(CPointer, value);
get => sfMusic_isLooping(CPointer);
set => sfMusic_setLooping(CPointer, value);
}

////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -421,7 +432,7 @@ public TimeSpan(Time offset, Time length)
private static extern void sfMusic_setPitch(IntPtr music, float pitch);

[DllImport(CSFML.Audio, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
private static extern void sfMusic_setLoop(IntPtr music, bool loop);
private static extern void sfMusic_setLooping(IntPtr music, bool loop);

[DllImport(CSFML.Audio, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
private static extern void sfMusic_setVolume(IntPtr music, float volume);
Expand All @@ -442,7 +453,7 @@ public TimeSpan(Time offset, Time length)
private static extern void sfMusic_setPlayingOffset(IntPtr music, Time timeOffset);

[DllImport(CSFML.Audio, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
private static extern bool sfMusic_getLoop(IntPtr music);
private static extern bool sfMusic_isLooping(IntPtr music);

[DllImport(CSFML.Audio, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
private static extern float sfMusic_getPitch(IntPtr music);
Expand Down
22 changes: 6 additions & 16 deletions src/SFML.Audio/Sound.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,14 @@ public enum SoundStatus
////////////////////////////////////////////////////////////
public class Sound : ObjectBase
{
////////////////////////////////////////////////////////////
/// <summary>
/// Default constructor (invalid sound)
/// </summary>
////////////////////////////////////////////////////////////
public Sound() :
base(sfSound_create())
{
}

////////////////////////////////////////////////////////////
/// <summary>
/// Construct the sound with a buffer
/// </summary>
/// <param name="buffer">Sound buffer containing the audio data to play with the sound</param>
////////////////////////////////////////////////////////////
public Sound(SoundBuffer buffer) :
base(sfSound_create()) => SoundBuffer = buffer;
base(sfSound_create(buffer.CPointer)) => SoundBuffer = buffer;

////////////////////////////////////////////////////////////
/// <summary>
Expand Down Expand Up @@ -125,8 +115,8 @@ public SoundBuffer SoundBuffer
////////////////////////////////////////////////////////////
public bool Loop
{
get => sfSound_getLoop(CPointer);
set => sfSound_setLoop(CPointer, value);
get => sfSound_isLooping(CPointer);
set => sfSound_setLooping(CPointer, value);
}

////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -282,7 +272,7 @@ public override string ToString()

#region Imports
[DllImport(CSFML.Audio, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
private static extern IntPtr sfSound_create();
private static extern IntPtr sfSound_create(IntPtr soundBuffer);

[DllImport(CSFML.Audio, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
private static extern IntPtr sfSound_copy(IntPtr sound);
Expand All @@ -303,10 +293,10 @@ public override string ToString()
private static extern void sfSound_setBuffer(IntPtr sound, IntPtr buffer);

[DllImport(CSFML.Audio, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
private static extern void sfSound_setLoop(IntPtr sound, bool loop);
private static extern void sfSound_setLooping(IntPtr sound, bool loop);

[DllImport(CSFML.Audio, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
private static extern bool sfSound_getLoop(IntPtr sound);
private static extern bool sfSound_isLooping(IntPtr sound);

[DllImport(CSFML.Audio, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
private static extern SoundStatus sfSound_getStatus(IntPtr sound);
Expand Down
21 changes: 18 additions & 3 deletions src/SFML.Audio/SoundBuffer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Security;
Expand Down Expand Up @@ -93,16 +94,20 @@ public SoundBuffer(byte[] bytes) :
/// <param name="samples">Array of samples</param>
/// <param name="channelCount">Channel count</param>
/// <param name="sampleRate">Sample rate</param>
/// <param name="channelMapData">Map of position in sample frame to sound channel</param>
/// <exception cref="LoadingFailedException" />
////////////////////////////////////////////////////////////
public SoundBuffer(short[] samples, uint channelCount, uint sampleRate) :
public SoundBuffer(short[] samples, uint channelCount, uint sampleRate, SoundChannel[] channelMapData) :
base(IntPtr.Zero)
{
unsafe
{
fixed (short* samplesPtr = samples)
{
CPointer = sfSoundBuffer_createFromSamples(samplesPtr, (uint)samples.Length, channelCount, sampleRate);
fixed (SoundChannel* channels = channelMapData)
{
CPointer = sfSoundBuffer_createFromSamples(samplesPtr, (uint)samples.Length, channelCount, sampleRate, channels, (UIntPtr)channelMapData.Length);
}
}
}

Expand Down Expand Up @@ -178,6 +183,16 @@ public short[] Samples
}
}

////////////////////////////////////////////////////////////
/// <summary>
/// Get the map of position in sample frame to sound channel
/// <para/>
/// This is used to map a sample in the sample stream to a
/// position during spatialisation.
/// </summary>
////////////////////////////////////////////////////////////
public IEnumerable<SoundChannel> ChannelMap => throw new NotImplementedException("TODO CSFML implementation.");

////////////////////////////////////////////////////////////
/// <summary>
/// Provide a string describing the object
Expand Down Expand Up @@ -216,7 +231,7 @@ public override string ToString()
private static extern unsafe IntPtr sfSoundBuffer_createFromMemory(IntPtr data, UIntPtr size);

[DllImport(CSFML.Audio, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
private static extern unsafe IntPtr sfSoundBuffer_createFromSamples(short* samples, ulong sampleCount, uint channelsCount, uint sampleRate);
private static extern unsafe IntPtr sfSoundBuffer_createFromSamples(short* samples, ulong sampleCount, uint channelsCount, uint sampleRate, SoundChannel* channelMapData, UIntPtr channelMapSize);

[DllImport(CSFML.Audio, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
private static extern IntPtr sfSoundBuffer_copy(IntPtr soundBuffer);
Expand Down
3 changes: 2 additions & 1 deletion src/SFML.Audio/SoundBufferRecorder.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;

namespace SFML.Audio
Expand Down Expand Up @@ -70,7 +71,7 @@ protected override bool OnProcessSamples(short[] samples)
/// Called when the current capture stops
/// </summary>
////////////////////////////////////////////////////////////
protected override void OnStop() => SoundBuffer = new SoundBuffer(_samplesArray.ToArray(), 1, SampleRate);
protected override void OnStop() => SoundBuffer = new SoundBuffer(_samplesArray.ToArray(), 1, SampleRate, Array.Empty<SoundChannel>()); // TODO pass in a proper array?

private readonly List<short> _samplesArray = new List<short>();
}
Expand Down
40 changes: 40 additions & 0 deletions src/SFML.Audio/SoundChannel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
namespace SFML.Audio
{
////////////////////////////////////////////////////////////
/// <summary>
/// Types of sound channels that can be read/written from sound buffers/files
/// <para/>
/// In multi-channel audio, each sound channel can be
/// assigned a position. The position of the channel is
/// used to determine where to place a sound when it
/// is spatialised. Assigning an incorrect sound channel
/// will result in multi-channel audio being positioned
/// incorrectly when using spatialisation.
/// </summary>
////////////////////////////////////////////////////////////
#pragma warning disable CS1591 // TODO: add documentation when available
public enum SoundChannel
{
Unspecified,
Mono,
FrontLeft,
FrontRight,
FrontCenter,
FrontLeftOfCenter,
FrontRightOfCenter,
LowFrequencyEffects,
BackLeft,
BackRight,
BackCenter,
SideLeft,
SideRight,
TopCenter,
TopFrontLeft,
TopFrontRight,
TopFrontCenter,
TopBackLeft,
TopBackRight,
TopBackCenter
}
#pragma warning restore CS1591
}
Loading

0 comments on commit b07c9ca

Please sign in to comment.