Skip to content

Commit

Permalink
Stop the UI treading on itself.
Browse files Browse the repository at this point in the history
  • Loading branch information
davidwhitney committed Apr 16, 2020
1 parent a666ed9 commit c33f4f0
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
23 changes: 22 additions & 1 deletion CoreBoy/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
using CoreBoy.gui;
Expand All @@ -17,7 +19,11 @@ static void Main(string[] args)
var cancellationTokenSource = new CancellationTokenSource();
var token = cancellationTokenSource.Token;

var emulator = new Emulator(args);
var arguments = new List<string>(args);

PromptForRom(arguments);

var emulator = new Emulator(arguments);
var ui = new WinFormsEmulatorSurface();

emulator.Controller = ui;
Expand All @@ -30,5 +36,20 @@ static void Main(string[] args)
emulator.Run(token);
Application.Run(ui);
}

private static void PromptForRom(List<string> arguments)
{
if (arguments.Any()) return;

using var openFileDialog = new OpenFileDialog
{
Filter = "Gameboy ROM (*.gb)|*.gb| All files(*.*) |*.*", FilterIndex = 0, RestoreDirectory = true
};

if (openFileDialog.ShowDialog() == DialogResult.OK)
{
arguments.Add(openFileDialog.FileName);
}
}
}
}
5 changes: 3 additions & 2 deletions CoreBoy/gui/Emulator.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using CoreBoy.controller;
Expand All @@ -21,10 +22,10 @@ public class Emulator: IRunnable

private readonly List<Thread> _runnables;

public Emulator(string[] args)
public Emulator(IEnumerable<string> args)
{
_runnables = new List<Thread>();
Options = ParseArgs(args);
Options = ParseArgs(args.ToArray());
}

private static GameboyOptions ParseArgs(string[] args)
Expand Down
20 changes: 18 additions & 2 deletions CoreBoy/gui/WinFormsEmulatorSurface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Threading;
using System.Windows.Forms;
using CoreBoy.controller;
using Button = CoreBoy.controller.Button;
Expand All @@ -14,6 +15,8 @@ public partial class WinFormsEmulatorSurface : Form, IController
private readonly PictureBox _pictureBox;
private readonly Dictionary<Keys, Button> _controls;

private readonly object _updateLock = new object();

public WinFormsEmulatorSurface()
{
InitializeComponent();
Expand Down Expand Up @@ -77,8 +80,21 @@ protected override void OnResize(EventArgs e)

public void UpdateDisplay(object _, byte[] frame)
{
using var memoryStream = new MemoryStream(frame);
_pictureBox.Image = Image.FromStream(memoryStream);
if (!Monitor.TryEnter(_updateLock)) return;

try
{
using var memoryStream = new MemoryStream(frame);
_pictureBox.Image = Image.FromStream(memoryStream);
}
catch
{
// YOLO
}
finally
{
Monitor.Exit(_updateLock);
}
}

protected override void OnFormClosed(FormClosedEventArgs e)
Expand Down

0 comments on commit c33f4f0

Please sign in to comment.