Skip to content

Commit

Permalink
README's
Browse files Browse the repository at this point in the history
  • Loading branch information
davidwhitney committed Apr 23, 2020
1 parent 346dd05 commit cbd46a7
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 42 deletions.
21 changes: 1 addition & 20 deletions CoreBoy.Windows/Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;
using System.Threading;
using System.Windows.Forms;
using CoreBoy.gui;

namespace CoreBoy.Windows
{
Expand All @@ -10,28 +8,11 @@ public static class Program
[STAThread]
public static void Main(string[] args)
{
var cancellation = new CancellationTokenSource();
var arguments = GameboyOptions.Parse(args);

Application.SetCompatibleTextRenderingDefault(false);
Application.SetHighDpiMode(HighDpiMode.SystemAware);
Application.EnableVisualStyles();

var emulator = new Emulator(arguments);

if (!arguments.RomSpecified)
{
var (success, romPath) = WinFormsEmulatorSurface.PromptForRom();
arguments.Rom = success ? romPath : string.Empty;
}

var ui = new WinFormsEmulatorSurface();
ui.Closed += (_, e) => { cancellation.Cancel(); };
emulator.Controller = ui;
emulator.Display.OnFrameProduced += ui.UpdateDisplay;

emulator.Run(cancellation.Token);
Application.Run(ui);
Application.Run(new WinFormsEmulatorSurface());
}
}
}
4 changes: 2 additions & 2 deletions CoreBoy.Windows/WinFormsEmulatorSurface.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

106 changes: 99 additions & 7 deletions CoreBoy.Windows/WinFormsEmulatorSurface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,53 @@ namespace CoreBoy.Windows
public partial class WinFormsEmulatorSurface : Form, IController
{
private IButtonListener _listener;

private byte[] _lastFrame;
private readonly MenuStrip _menu;
private readonly PictureBox _pictureBox;
private readonly Dictionary<Keys, Button> _controls;

private readonly Emulator _emulator;
private readonly GameboyOptions _gameboyOptions;
private CancellationTokenSource _cancellation;

private readonly object _updateLock = new object();

public WinFormsEmulatorSurface()
{
InitializeComponent();

_pictureBox = new PictureBox
Controls.Add(_menu = new MenuStrip
{
Items =
{
new ToolStripMenuItem("Emulator")
{
DropDownItems =
{
new ToolStripMenuItem("Load ROM", null, (sender, args) => { StartEmulation(); }),
new ToolStripMenuItem("Pause", null, (sender, args) => { _emulator.TogglePause(); }),
new ToolStripMenuItem("Quit", null, (sender, args) => { Close(); })
}
},
new ToolStripMenuItem("Graphics")
{
DropDownItems =
{
new ToolStripMenuItem("Screenshot", null, (sender, args) => { Screenshot(); })
}
}
}
});

Controls.Add(_pictureBox = new PictureBox
{
Top = _menu.Height,
Width = BitmapDisplay.DisplayWidth * 5,
Height = BitmapDisplay.DisplayHeight * 5,
BackColor = Color.Black,
SizeMode = PictureBoxSizeMode.Zoom
};
});

_controls = new Dictionary<Keys, Button>
{
Expand All @@ -42,23 +73,83 @@ public WinFormsEmulatorSurface()
{Keys.Back, Button.Select}
};

Height = _menu.Height + _pictureBox.Height + 50;
Width = _pictureBox.Width;

_cancellation = new CancellationTokenSource();
_gameboyOptions = new GameboyOptions();
_emulator = new Emulator(_gameboyOptions);

ConnectEmulatorToPanel();
}

private void ConnectEmulatorToPanel()
{
_emulator.Controller = this;
_emulator.Display.OnFrameProduced += UpdateDisplay;

KeyDown += WinFormsEmulatorSurface_KeyDown;
KeyUp += WinFormsEmulatorSurface_KeyUp;
Controls.Add(_pictureBox);
Closed += (_, e) => { _cancellation.Cancel(); };
}

public static (bool, string) PromptForRom()
private void StartEmulation()
{
if (_emulator.Active)
{
_emulator.Stop(_cancellation);
_cancellation = new CancellationTokenSource();
_pictureBox.Image = null;
Thread.Sleep(100);
}

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

return openFileDialog.ShowDialog() == DialogResult.OK
? (true, openFileDialog.FileName)
var (success, romPath) = openFileDialog.ShowDialog() == DialogResult.OK
? (true, openFileDialog.FileName)
: (false, null);

if (success)
{
_gameboyOptions.Rom = romPath;
_emulator.Run(_cancellation.Token);
}
}

private void Screenshot()
{
_emulator.TogglePause();

using var sfd = new SaveFileDialog
{
Filter = "Bitmap (*.bmp)|*.bmp",
FilterIndex = 0,
RestoreDirectory = true
};

var (success, romPath) = sfd.ShowDialog() == DialogResult.OK
? (true, sfd.FileName)
: (false, null);

if (success)
{
try
{
Monitor.Enter(_updateLock);
File.WriteAllBytes(sfd.FileName, _lastFrame);
}
finally
{
Monitor.Exit(_updateLock);
}
}

_emulator.TogglePause();
}

private void WinFormsEmulatorSurface_KeyDown(object sender, KeyEventArgs e)
Expand Down Expand Up @@ -87,7 +178,7 @@ protected override void OnResize(EventArgs e)
if (_pictureBox == null) return;

_pictureBox.Width = Width;
_pictureBox.Height = Height;
_pictureBox.Height = Height - _menu.Height - 50;
}

public void UpdateDisplay(object _, byte[] frame)
Expand All @@ -96,6 +187,7 @@ public void UpdateDisplay(object _, byte[] frame)

try
{
_lastFrame = frame;
using var memoryStream = new MemoryStream(frame);
_pictureBox.Image = Image.FromStream(memoryStream);
}
Expand Down
48 changes: 35 additions & 13 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,33 +1,55 @@
# CoreBoy

A .NET Core port of Coffee-GB.

This is based on https://github.com/trekawek/coffee-gb which was an absolute joy to port.

A .NET Core Gameboy emulator that started life as a port of Coffee-GB (https://github.com/trekawek/coffee-gb).
MIT licensed, go nuts.

# Docs

Um, coming soon.
This-

Uses WinForms, so Windows only for the moment.
* Runs Gameboy and Gameboy Color games.
* Has a headless CLI mode
* Has a Windows-Only WinForms UI
* Can be used as a library in your own software

# Pre-Reqs

* .NET Core 3.1

# Usage

* Build it
* Run it on the command line
## Windows

Just run `CoreBoy.Windows` and load a ROM from the file menu!

## Mac / Linux

Command line:

```bash
> CoreBoy.exe rom.gb
dotnet CoreBoy.Cli -r myrom.rom --interactive
```

# WARNING
You can play on the command line!

GUI ...TBC

# Controls

LeftArrow = Left
RightArrow = Right
UpArrow = Up
DownArrow = Down
Z = A
X = B
Enter = Start
Backspace = Select

# Audio

Isn't working yet.

VERY VERY PRE-RELEASE.
# Resizing

It doesn't exit cleanly, it has hanging threads, it's not working properly yet.
But it's also kinda working, so, hey! Enjoy
Is currently buggy and slow, because it's just "whatever WinForms is doing" rather than explicitly scaled rendering.
I'll get around to it.

0 comments on commit cbd46a7

Please sign in to comment.