Skip to content

Commit

Permalink
refactor subsystems to allow scripting
Browse files Browse the repository at this point in the history
  • Loading branch information
LeNitrous committed Jul 28, 2023
1 parent de3a385 commit 8a9345a
Show file tree
Hide file tree
Showing 25 changed files with 741 additions and 389 deletions.
17 changes: 16 additions & 1 deletion source/Vignette.Desktop/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@
// Licensed under GPL 3.0 with SDK Exception. See LICENSE for details.

using Sekai;
using Sekai.GLFW;
using Sekai.OpenAL;
using Sekai.OpenGL;
using Vignette;

Host.Run<VignetteGame>(new HostOptions { Name = "Vignette" });
var options = new VignetteGameOptions();
options.UseOpenAL();
options.UseOpenGL();
options.UseScripts();

if (RuntimeInfo.IsDesktop)
{
options.UseGLFW();
options.Window.Title = "Vignette";
}

var game = new VignetteGame(options);
game.Run();
6 changes: 3 additions & 3 deletions source/Vignette.Desktop/Vignette.Desktop.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Sekai.OpenGL" Version="0.1.0-alpha.9" />
<PackageReference Include="Sekai.OpenAL" Version="0.1.0-alpha.9" />
<PackageReference Include="Sekai.Desktop" Version="0.1.0-alpha.9" />
<PackageReference Include="Sekai.GLFW" Version="0.1.0-alpha.10" />
<PackageReference Include="Sekai.OpenGL" Version="0.1.0-alpha.10" />
<PackageReference Include="Sekai.OpenAL" Version="0.1.0-alpha.10" />
</ItemGroup>

</Project>
25 changes: 18 additions & 7 deletions source/Vignette/Audio/AudioManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ internal AudioManager(AudioDevice device)
/// <returns>An audio controller.</returns>
public IAudioController GetController(AudioStream stream)
{
if (device is null)
{
throw new InvalidOperationException("The audio manager has not yet been initialized.");
}

return new StreamingAudioController(device.CreateSource(), stream, this);
}

Expand Down Expand Up @@ -61,6 +66,11 @@ public void Return(IAudioController controller)

AudioBuffer IObjectPool<AudioBuffer>.Get()
{
if (device is null)
{
throw new InvalidOperationException("The audio manager has not yet been initialized.");
}

if (!bufferPool.TryTake(out var buffer))
{
buffer = device.CreateBuffer();
Expand Down Expand Up @@ -102,13 +112,13 @@ public TimeSpan Position
private const int max_buffer_stream = 4;
private readonly AudioSource source;
private readonly AudioStream stream;
private readonly IObjectPool<AudioBuffer> bufferPool;
private readonly IObjectPool<AudioBuffer> buffer;

public StreamingAudioController(AudioSource source, AudioStream stream, IObjectPool<AudioBuffer> bufferPool)
public StreamingAudioController(AudioSource source, AudioStream stream, IObjectPool<AudioBuffer> buffer)
{
this.source = source;
this.stream = stream;
this.bufferPool = bufferPool;
this.buffer = buffer;
}

public void Play()
Expand All @@ -119,14 +129,15 @@ public void Play()

for (int i = 0; i < max_buffer_stream; i++)
{
var buffer = bufferPool.Get();
var b = buffer.Get();

if (!allocate(buffer))
if (!allocate(b))
{
buffer.Return(b);
break;
}

source.Enqueue(buffer);
source.Enqueue(b);
}
}

Expand Down Expand Up @@ -168,7 +179,7 @@ public void Dispose()

while(source.TryDequeue(out var buffer))
{
bufferPool.Return(buffer);
this.buffer.Return(buffer);
}

source.Dispose();
Expand Down
31 changes: 21 additions & 10 deletions source/Vignette/Behavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@
// Licensed under GPL 3.0 with SDK Exception. See LICENSE for details.

using System;
using Jint.Native;
using Vignette.Scripting;

namespace Vignette;

/// <summary>
/// A <see cref="Node"/> that processes itself per-frame.
/// A <see cref="Node"/> that has behavior.
/// </summary>
public abstract class Behavior : Node, IComparable<Behavior>
public class Behavior : Node, IComparable<Behavior>
{
/// <summary>
/// The processing order for this <see cref="Behavior"/>.
/// The processing order for this <see cref="Node"/>.
/// </summary>
[ScriptVisible]
public int Order
{
get => order;
Expand All @@ -29,8 +32,9 @@ public int Order
}

/// <summary>
/// Whether this <see cref="Behavior"/> should be enabled or not affecting <see cref="Update(TimeSpan)"/> calls.
/// Whether this <see cref="Node"/> should perform <see cref="Update(double)"/> calls.
/// </summary>
[ScriptVisible]
public bool Enabled
{
get => enabled;
Expand All @@ -57,31 +61,34 @@ public bool Enabled
public event EventHandler? EnabledChanged;

private int order;
private bool enabled = true;
private bool enabled = false;

/// <summary>
/// Called once in the update loop after the <see cref="Node"/> has entered the node graph.
/// Called once after entering a world.
/// </summary>
public virtual void Load()
{
Invoke(load);
}

/// <summary>
/// Called every frame to perform updates on this <see cref="Behavior"/>.
/// Called every frame.
/// </summary>
/// <param name="elapsed">The time elapsed between frames.</param>
/// <param name="elapsed">The elapsed time between frames.</param>
public virtual void Update(TimeSpan elapsed)
{
Invoke(update, elapsed.TotalSeconds);
}

/// <summary>
/// Called once in the update loop before the <see cref="Node"/> exits the node graph.
/// Called once before leaving a world.
/// </summary>
public virtual void Unload()
{
Invoke(unload);
}

public int CompareTo(Behavior? other)
int IComparable<Behavior>.CompareTo(Behavior? other)
{
if (other is null)
{
Expand All @@ -97,4 +104,8 @@ public int CompareTo(Behavior? other)

return Order.CompareTo(other.Order);
}

private static readonly JsValue load = new JsString("load");
private static readonly JsValue update = new JsString("update");
private static readonly JsValue unload = new JsString("unload");
}
17 changes: 4 additions & 13 deletions source/Vignette/Content/ContentManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,10 @@ public T Load<T>(ReadOnlySpan<byte> bytes)
continue;
}

try
if (typedLoader.TryLoad(bytes, out result))
{
result = typedLoader.Load(bytes);
break;
}
catch
{
}
}

if (result is null)
Expand All @@ -131,20 +127,15 @@ public T Load<T>(ReadOnlySpan<byte> bytes)
return result;
}

/// <summary>
/// Adds a content loader to the content manager.
/// </summary>
/// <param name="loader">The content loader to add.</param>
/// <param name="extensions">The file extensions supported by this loader.</param>
/// <exception cref="InvalidOperationException">Thrown when </exception>
internal void Add(IContentLoader loader, params string[] extensions)
private void add(IContentLoader loader, params string[] extensions)
{
foreach (string extension in extensions)
{
string ext = extension.StartsWith(ext_separator) ? extension : ext_separator + extension;
this.loaders.Add(loader);
this.extensions.Add(ext);
}

loaders.Add(loader);
}

private const char ext_separator = '.';
Expand Down
21 changes: 21 additions & 0 deletions source/Vignette/Content/IContentLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under GPL 3.0 with SDK Exception. See LICENSE for details.

using System;
using System.Diagnostics.CodeAnalysis;

namespace Vignette.Content;

Expand All @@ -25,4 +26,24 @@ public interface IContentLoader<T> : IContentLoader
/// <param name="bytes">The byte data to be read.</param>
/// <returns>The loaded content.</returns>
T Load(ReadOnlySpan<byte> bytes);

/// <summary>
/// Loads a <see cref="ReadOnlySpan{byte}"/> as <typeparamref name="T"/>.
/// </summary>
/// <param name="bytes">The byte data to be read.</param>
/// <param name="result">The loaded content.</param>
/// <returns><see langword="true"/> if the content has been loaded. Otherwise, <see langword="false"/>.</returns>
bool TryLoad(ReadOnlySpan<byte> bytes, [NotNullWhen(true)] out T? result)
{
try
{
result = Load(bytes);
return true;
}
catch
{
result = null;
return false;
}
}
}
46 changes: 40 additions & 6 deletions source/Vignette/Drawable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@
// Licensed under GPL 3.0 with SDK Exception. See LICENSE for details.

using System;
using System.Numerics;
using Jint.Native;
using Vignette.Graphics;
using Vignette.Scripting;

namespace Vignette;

/// <summary>
/// A <see cref="Node"/> that is capable of drawing.
/// A <see cref="Node"/> that can be drawn.
/// </summary>
public abstract class Drawable : Behavior
public class Drawable : Behavior, ISpatialObject
{
/// <summary>
/// Whether this <see cref="Drawable"/> should be drawn or not.
/// Whether this <see cref="Node"/> should perform <see cref="Draw()"/> calls.
/// </summary>
[ScriptVisible]
public bool Visible
{
get => visible;
Expand All @@ -29,18 +33,48 @@ public bool Visible
}
}

/// <summary>
/// The node's scaling.
/// </summary>
[ScriptVisible]
public Vector3 Scale { get; set; } = Vector3.One;

/// <summary>
/// The node's shearing.
/// </summary>
[ScriptVisible]
public Vector3 Shear
{
get => new(shear[0, 1], shear[0, 2], shear[1, 2]);
set
{
shear[0, 1] = value.X;
shear[0, 2] = value.Y;
shear[1, 2] = value.Z;
}
}

/// <summary>
/// Called when <see cref="Visible"/> has been changed.
/// </summary>
public event EventHandler? VisibleChanged;

private bool visible = true;
private bool visible = false;
private Matrix4x4 shear = Matrix4x4.Identity;

/// <summary>
/// Called every frame to perform drawing operations on this <see cref="Drawable"/>.
/// The node's matrix.
/// </summary>
/// <param name="context">The drawable rendering context.</param>
protected override Matrix4x4 Matrix => shear * Matrix4x4.CreateScale(Scale) * base.Matrix;

/// <summary>
/// Called when performing draw operations.
/// </summary>
/// <param name="context">The rendering context.</param>
public virtual void Draw(RenderContext context)
{
Invoke(draw, context);
}

private static readonly JsValue draw = new JsString("draw");
}
27 changes: 27 additions & 0 deletions source/Vignette/Graphics/IPointObject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) Cosyne
// Licensed under GPL 3.0 with SDK Exception. See LICENSE for details.

using System.Numerics;

namespace Vignette.Graphics;

/// <summary>
/// An object that has a position and rotation
/// </summary>
public interface IPointObject
{
/// <summary>
/// The point's position.
/// </summary>
Vector3 Position { get; }

/// <summary>
/// The point's rotation.
/// </summary>
Vector3 Rotation { get; }

/// <summary>
/// The point's matrix.
/// </summary>
Matrix4x4 Matrix { get; }
}
12 changes: 1 addition & 11 deletions source/Vignette/Graphics/IProjector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,8 @@ namespace Vignette.Graphics;
/// <summary>
/// An interface for objects providing clip space info.
/// </summary>
public interface IProjector
public interface IProjector : IPointObject
{
/// <summary>
/// The projector's position.
/// </summary>
Vector3 Position { get; }

/// <summary>
/// The projector's rotation.
/// </summary>
Vector3 Rotation { get; }

/// <summary>
/// The projector's view matrix.
/// </summary>
Expand Down
Loading

0 comments on commit 8a9345a

Please sign in to comment.