Skip to content

Commit

Permalink
Merge pull request #126 from kamiyaowl/issue-117
Browse files Browse the repository at this point in the history
TimersからTasksに置き換え
  • Loading branch information
kamiyaowl committed Jul 26, 2020
2 parents f700311 + aa44d01 commit 6de04b7
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 29 deletions.
1 change: 0 additions & 1 deletion BlazeSnes/BlazeSnes.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

<ItemGroup>
<PackageReference Include="Blazor.ContextMenu" Version="1.6.0" />
<PackageReference Include="Blazor.Extensions.Canvas" Version="1.0.0" />
<PackageReference Include="Blazored.LocalStorage" Version="3.0.0" />
<PackageReference Include="Blazorise" Version="0.9.1.2" />
<PackageReference Include="Blazorise.Charts" Version="0.9.1.2" />
Expand Down
63 changes: 38 additions & 25 deletions BlazeSnes/Pages/Index.razor
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
@page "/"
@inject Blazored.LocalStorage.ISyncLocalStorageService LocalStorage
@inject IJSRuntime JSRuntime
@using System.Threading
@using System.Diagnostics

<Jumbotron Margin="Margin.Is4.FromBottom" Background="Background.Primary">
<JumbotronTitle Size="JumbotronTitleSize.Is4">BlazeSnes</JumbotronTitle>
Expand Down Expand Up @@ -315,34 +317,19 @@

// Emulation関係
int counter = 0;
float actualFps = 0.0f;
byte[] frameBuffer;
System.Timers.Timer emulateTimer;
CancellationTokenSource emulationCancel;
Cartridge cartridge;

protected override async Task OnInitializedAsync() {
this.frameBuffer = new byte[BASE_WIDTH * BASE_HEIGHT * BYTE_PER_PIXEL]; // RGBA32
this.emulateTimer = new System.Timers.Timer(1.0 / FPS * 1000);
this.emulateTimer.Elapsed += OnEmulate;
this.emulateTimer.Start();

await base.OnInitializedAsync();
}

protected override async Task OnAfterRenderAsync(bool firstRender) {
await JSRuntime.InvokeAsync<object>("initEmulatorCanvas", null);

await base.OnAfterRenderAsync(firstRender);
}

private void OnEmulate(object sender, EventArgs e) {
var startMs = Environment.TickCount;
// TODO: Emulate here
var diffMs = Environment.TickCount - startMs;

// TODO: sound update
// TODO: 適当な頻度でcanvas更新を間引く
// TEST: とりあえず適当なFrameBufferのデータを作る
for (int j = 0; j < BASE_HEIGHT; j++) {
for (int i = 0; i < BASE_WIDTH; i++) {
Expand All @@ -353,15 +340,41 @@
frameBuffer[ptr + 3] = 0xff;
}
}
// GCに移動されないよう固定してJSに渡す。終わったら捨てる
var gch = System.Runtime.InteropServices.GCHandle.Alloc(frameBuffer, System.Runtime.InteropServices.GCHandleType.Pinned);
var pinnedAddr = gch.AddrOfPinnedObject();
var jsRuntime = JSRuntime as Microsoft.JSInterop.WebAssembly.WebAssemblyJSRuntime;
jsRuntime.InvokeUnmarshalled<IntPtr,string>("drawEmulatorCanvas", pinnedAddr);
gch.Free();

counter++;
// start emulation
this.emulationCancel = new CancellationTokenSource();
await OnEmulate(this.emulationCancel.Token);

await base.OnAfterRenderAsync(firstRender);
}

private async Task OnEmulate(CancellationToken cancelToken) {
for(this.counter = 0; !cancelToken.IsCancellationRequested; this.counter++) {
var sw = Stopwatch.StartNew();
// TODO: Emulate here
// TODO: sound update
// GCに移動されないよう固定してJSに渡す。終わったら捨てる
var gch = System.Runtime.InteropServices.GCHandle.Alloc(frameBuffer, System.Runtime.InteropServices.GCHandleType.Pinned);
var pinnedAddr = gch.AddrOfPinnedObject();
var jsRuntime = JSRuntime as Microsoft.JSInterop.WebAssembly.WebAssemblyJSRuntime;
jsRuntime.InvokeUnmarshalled<IntPtr,string>("drawEmulatorCanvas", pinnedAddr);
gch.Free();

// Emulation info
sw.Stop();
var elapsedMs = sw.ElapsedMilliseconds > 0 ? sw.ElapsedMilliseconds : 1;
this.actualFps = 1000.0f / (float)elapsedMs;
@* Console.WriteLine($"counter:{counter} elapsed:{elapsedMs} actualFps:{actualFps}"); *@

// FPS Control
var idealMs = 1000.0f / this.FPS;
if (idealMs > elapsedMs) {
var diffMs = idealMs - elapsedMs;
await Task.Delay((int)diffMs);
}
}
}

private void OnCanvasMouseMove(MouseEventArgs e) {
}

Expand Down
3 changes: 0 additions & 3 deletions BlazeSnes/_Imports.razor
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
@using Blazorise.TreeView
@using Blazorise.Components
@using BlazorContextMenu
@using Blazor.Extensions;
@using Blazor.Extensions.Canvas
@using Blazor.Extensions.Canvas.Canvas2D;
@using BlazeSnes
@using BlazeSnes.Layouts
@using BlazeSnes.Components
Expand Down

0 comments on commit 6de04b7

Please sign in to comment.