Skip to content

Commit

Permalink
...do the same for examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
vddCore committed Jul 25, 2024
1 parent f5cb8d3 commit cd278b4
Show file tree
Hide file tree
Showing 83 changed files with 4,109 additions and 4,152 deletions.
398 changes: 198 additions & 200 deletions Chroma.Examples/ArbitraryGeometry/GameCore.cs

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion Chroma.Examples/ArbitraryGeometry/Program.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using ArbitraryGeometry;

new GameCore().Run();
using(var game = new GameCore())
game.Run();
168 changes: 83 additions & 85 deletions Chroma.Examples/Asynchronicity/GameCore.cs
Original file line number Diff line number Diff line change
@@ -1,127 +1,125 @@
using System;
namespace Asynchronicity;

using System;
using System.Drawing;
using System.Numerics;
using System.Threading.Tasks;
using Chroma;
using Chroma.ContentManagement;
using Chroma.Diagnostics.Logging;
using Chroma.Graphics;
using Chroma.Input;
using Chroma.Threading;
using Color = System.Drawing.Color;

namespace Asynchronicity
public class GameCore : Game
{
public class GameCore : Game
{
private static readonly Log _log = LogManager.GetForCurrentAssembly();
private static readonly Log _log = LogManager.GetForCurrentAssembly();

private RenderTarget _target;
private Task _longRunningTask;
private RenderTarget _target;
private Task _longRunningTask;

public GameCore() : base(new(false, false))
public GameCore() : base(new(false, false))
{
_longRunningTask = new Task(async () =>
{
_longRunningTask = new Task(async () =>
while (true)
{
while (true)
{
ProcessInSeparateTask();
await Task.Delay(10);
}
});
ProcessInSeparateTask();
await Task.Delay(10);
}
});

_longRunningTask.Start();
}
_longRunningTask.Start();
}

protected override void Draw(RenderContext context)
{
context.DrawString(
"Press <F1> to asynchronously dispose and recreate the render target\n" +
"causing an InvalidOperationException.\n\n" +
"Press <F2> to asynchronously queue the render target creation for execution on the main thread.\n" +
"Press <F3> to asynchronously queue render target destruction for execution on the main thread.",
new Vector2(8)
);
protected override void Draw(RenderContext context)
{
context.DrawString(
"Press <F1> to asynchronously dispose and recreate the render target\n" +
"causing an InvalidOperationException.\n\n" +
"Press <F2> to asynchronously queue the render target creation for execution on the main thread.\n" +
"Press <F3> to asynchronously queue render target destruction for execution on the main thread.",
new Vector2(8)
);

if (_target != null && !_target.Disposed)
if (_target != null && !_target.Disposed)
{
context.RenderTo(_target, (ctx, tgt) =>
{
context.RenderTo(_target, (ctx, tgt) =>
{
ctx.Rectangle(
ShapeMode.Fill,
new Vector2(16),
new Size(32, 32),
Color.Aqua
);
});
ctx.Rectangle(
ShapeMode.Fill,
new Vector2(16),
new Size(32, 32),
Color.Aqua
);
});

context.DrawTexture(_target, Vector2.Zero, Vector2.One, Vector2.Zero, 0);
}
context.DrawTexture(_target, Vector2.Zero, Vector2.One, Vector2.Zero, 0);
}
}

protected override void Update(float delta)
{
}
protected override void Update(float delta)
{
}

protected override void KeyPressed(KeyEventArgs e)
protected override void KeyPressed(KeyEventArgs e)
{
if (e.KeyCode == KeyCode.F1)
{
if (e.KeyCode == KeyCode.F1)
Task.Run(() =>
{
Task.Run(() =>
try
{
try
{
_target?.Dispose();
_target = new RenderTarget(Window.Size);
}
catch (InvalidOperationException ioe)
{
_log.Error($"Caught exception: {ioe.Message}");
}
});
}
else if (e.KeyCode == KeyCode.F2)
_target?.Dispose();
_target = new RenderTarget(Window.Size);
}
catch (InvalidOperationException ioe)
{
_log.Error($"Caught exception: {ioe.Message}");
}
});
}
else if (e.KeyCode == KeyCode.F2)
{
Task.Run(async () =>
{
Task.Run(async () =>
await Dispatcher.RunOnMainThread(() =>
{
await Dispatcher.RunOnMainThread(() =>
if (_target != null && !_target.Disposed)
{
if (_target != null && !_target.Disposed)
{
_target.Dispose();
}
_target.Dispose();
}

_target = new RenderTarget(Window.Size);
});
_target = new RenderTarget(Window.Size);
});
}
else if (e.KeyCode == KeyCode.F3)
});
}
else if (e.KeyCode == KeyCode.F3)
{
Task.Run(async () =>
{
Task.Run(async () =>
await Dispatcher.RunOnMainThread(() =>
{
await Dispatcher.RunOnMainThread(() =>
if (_target != null && !_target.Disposed)
{
if (_target != null && !_target.Disposed)
{
_target.Dispose();
}
});
_target.Dispose();
}
});
}
});
}
}

private void ProcessInSeparateTask()
private void ProcessInSeparateTask()
{
var target = Dispatcher.RunOnMainThread<RenderTarget>(() =>
{
var target = Dispatcher.RunOnMainThread<RenderTarget>(() =>
{
return new RenderTarget(10, 10);
}, true).Result;
return new RenderTarget(10, 10);
}, true).Result;

if (target != null)
{
_log.Info(target.ToString());
Dispatcher.RunOnMainThread(target.Dispose, true).GetAwaiter().GetResult();
}
if (target != null)
{
_log.Info(target.ToString());
Dispatcher.RunOnMainThread(target.Dispose, true).GetAwaiter().GetResult();
}
}
}
3 changes: 2 additions & 1 deletion Chroma.Examples/Asynchronicity/Program.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Asynchronicity;

new GameCore().Run();
using (var game = new GameCore())
game.Run();
135 changes: 67 additions & 68 deletions Chroma.Examples/AudioRecording/GameCore.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
namespace AudioRecording;

using System;
using System.IO;
using Chroma;
Expand All @@ -7,91 +9,88 @@
using Chroma.Graphics;
using Chroma.Input;

namespace AudioRecording
public class GameCore : Game
{
public class GameCore : Game
{
private AudioCapture _recording;
private MemoryStream _stream = new();
private AudioCapture _recording;
private MemoryStream _stream = new();

private Waveform _waveform;

private Waveform _waveform;
private byte[] _data;
private int _dataPointer;

public GameCore() : base(new(false, false))
{
}

private byte[] _data;
private int _dataPointer;
protected override void Draw(RenderContext context)
{
context.DrawString(
"Press <F1> to start recording.\n" +
"Press <F2> to stop the recording.\n" +
"Press <F3> to pause/resume the recording.\n" +
"Press <F4> to play the recorded sample back.",
new(8)
);

public GameCore() : base(new(false, false))
if (_recording != null)
{
Window.Title = $"{_recording.Status} | {_recording.TotalSize}";
}
}

protected override void Draw(RenderContext context)
protected override void KeyPressed(KeyEventArgs e)
{
if (e.KeyCode == KeyCode.F1)
{
context.DrawString(
"Press <F1> to start recording.\n" +
"Press <F2> to stop the recording.\n" +
"Press <F3> to pause/resume the recording.\n" +
"Press <F4> to play the recorded sample back.",
new(8)
);

if (_recording != null)
{
Window.Title = $"{_recording.Status} | {_recording.TotalSize}";
}
_recording.Dispose();

_recording = new StreamAudioCapture(_stream);
_recording.Start();
}
else if (e.KeyCode == KeyCode.F2)
{
if (_recording == null)
return;

protected override void KeyPressed(KeyEventArgs e)
_recording.Stop();
}
else if (e.KeyCode == KeyCode.F3)
{
if (e.KeyCode == KeyCode.F1)
{
if (_recording != null)
_recording.Dispose();

_recording = new StreamAudioCapture(_stream);
_recording.Start();
}
else if (e.KeyCode == KeyCode.F2)
{
if (_recording == null)
return;
if (_recording == null)
return;

if (_recording.Status == CaptureStatus.Paused)
_recording.Resume();
else if (_recording.Status == CaptureStatus.Recording)
_recording.Pause();
}
else if (e.KeyCode == KeyCode.F4)
{
if (_recording.Status != CaptureStatus.Stopped)
_recording.Stop();
}
else if (e.KeyCode == KeyCode.F3)
{
if (_recording == null)
return;

if (_recording.Status == CaptureStatus.Paused)
_recording.Resume();
else if (_recording.Status == CaptureStatus.Recording)
_recording.Pause();
}
else if (e.KeyCode == KeyCode.F4)
{
if (_recording.Status != CaptureStatus.Stopped)
_recording.Stop();

if (_waveform != null)
_waveform.Dispose();

_data = _stream.GetBuffer();

_waveform = new Waveform(
_recording.Format,
PlaybackDelegate,
_recording.ChannelMode
);

_waveform.Play();
}

if (_waveform != null)
_waveform.Dispose();

_data = _stream.GetBuffer();

_waveform = new Waveform(
_recording.Format,
PlaybackDelegate,
_recording.ChannelMode
);

_waveform.Play();
}
}

private void PlaybackDelegate(Span<byte> data, AudioFormat format)
private void PlaybackDelegate(Span<byte> data, AudioFormat format)
{
for (var i = 0; i < data.Length && _dataPointer < _data.Length; i++, _dataPointer++)
{
for (var i = 0; i < data.Length && _dataPointer < _data.Length; i++, _dataPointer++)
{
data[i] = _data[_dataPointer];
}
data[i] = _data[_dataPointer];
}
}
}
3 changes: 2 additions & 1 deletion Chroma.Examples/AudioRecording/Program.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using AudioRecording;

new GameCore().Run();
using (var game = new GameCore())
game.Run();
Loading

0 comments on commit cd278b4

Please sign in to comment.