-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
83 changed files
with
4,109 additions
and
4,152 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
Oops, something went wrong.