-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Splitting GUI from Headless emulator. Cramming in stupid ascii art re…
…ndering.
- Loading branch information
1 parent
6c3cd14
commit 629075c
Showing
11 changed files
with
299 additions
and
78 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<None Remove="mario.gb" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Content Include="mario.gb"> | ||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
</Content> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\CoreBoy\CoreBoy.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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 |
---|---|---|
@@ -0,0 +1,99 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using System.Windows.Forms; | ||
using CoreBoy.controller; | ||
using CoreBoy.gui; | ||
using Button = CoreBoy.controller.Button; | ||
|
||
namespace CoreBoy.Cli | ||
{ | ||
public class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
var cancellation = new CancellationTokenSource(); | ||
var arguments = GameboyOptions.Parse(args); | ||
var emulator = new Emulator(arguments); | ||
|
||
if (!arguments.RomSpecified) | ||
{ | ||
GameboyOptions.PrintUsage(Console.Out); | ||
Console.Out.Flush(); | ||
Environment.Exit(1); | ||
} | ||
|
||
var ui = new CommandLineInteractivity(); | ||
|
||
emulator.Controller = ui; | ||
emulator.Display.OnFrameProduced += ui.UpdateDisplay; | ||
emulator.Run(cancellation.Token); | ||
|
||
//var inputLoop = new Thread(ui.ProcessInput); | ||
//inputLoop.Start(); | ||
|
||
ui.ProcessInput(); | ||
|
||
|
||
cancellation.Cancel(); | ||
} | ||
} | ||
|
||
public class CommandLineInteractivity : IController | ||
{ | ||
private IButtonListener _listener; | ||
private readonly Dictionary<ConsoleKey, Button> _controls; | ||
|
||
public CommandLineInteractivity() | ||
{ | ||
Console.Clear(); | ||
Console.SetCursorPosition(0, 0); | ||
Console.WindowHeight = 92; | ||
|
||
_controls = new Dictionary<ConsoleKey, Button> | ||
{ | ||
{ConsoleKey.LeftArrow, Button.Left}, | ||
{ConsoleKey.RightArrow, Button.Right}, | ||
{ConsoleKey.UpArrow, Button.Up}, | ||
{ConsoleKey.DownArrow, Button.Down}, | ||
{ConsoleKey.Z, Button.A}, | ||
{ConsoleKey.X, Button.B}, | ||
{ConsoleKey.Enter, Button.Start}, | ||
{ConsoleKey.Backspace, Button.Select} | ||
}; | ||
} | ||
|
||
public void SetButtonListener(IButtonListener listener) => _listener = listener; | ||
|
||
public void ProcessInput() | ||
{ | ||
Button lastButton = null; | ||
var input = Console.ReadKey(true); | ||
while (input.Key != ConsoleKey.Escape) | ||
{ | ||
var button = _controls.ContainsKey(input.Key) ? _controls[input.Key] : null; | ||
|
||
if (button != null) | ||
{ | ||
if (lastButton != null) | ||
{ | ||
_listener?.OnButtonRelease(lastButton); | ||
|
||
} | ||
|
||
_listener?.OnButtonPress(button); | ||
lastButton = button; | ||
} | ||
|
||
input = Console.ReadKey(true); | ||
} | ||
} | ||
|
||
public void UpdateDisplay(object sender, byte[] framedata) | ||
{ | ||
var frame = SillyAsciiArtCreator.GenerateArt(framedata); | ||
Console.SetCursorPosition(0, 0); | ||
Console.WriteLine(frame); | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,85 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using SixLabors.ImageSharp; | ||
using SixLabors.ImageSharp.PixelFormats; | ||
using SixLabors.ImageSharp.Processing; | ||
|
||
namespace CoreBoy.Cli | ||
{ | ||
public class SillyAsciiArtCreator | ||
{ | ||
private static readonly Dictionary<float, string> Map; | ||
|
||
static SillyAsciiArtCreator() | ||
{ | ||
Map = new Dictionary<float, string> | ||
{ | ||
{200, " "}, | ||
{190, " "}, | ||
{180, " "}, | ||
{170, " "}, | ||
{160, " "}, | ||
{150, "."}, | ||
{140, "o"}, | ||
{130, "O"}, | ||
{120, "+"}, | ||
{110, "#"}, | ||
{100, "@"}, | ||
{080, "%"}, | ||
{060, "░"}, | ||
{040, "▒"}, | ||
{020, "▓"}, | ||
{000, "█"}, | ||
}; | ||
} | ||
|
||
public static string GenerateArt(byte[] jpg) | ||
{ | ||
var image = Image.Load(jpg); | ||
image.Mutate(x => x.Resize(106, 72)); | ||
|
||
var map = Map.OrderBy(kvp => kvp.Key).ToList(); | ||
var sb = new StringBuilder(); | ||
|
||
for (var y = 0; y < image.Height; y++) | ||
{ | ||
for (var x = 0; x < image.Width; x++) | ||
{ | ||
var pixel = image[x, y]; | ||
var currentChar = MapToAscii(map, pixel); | ||
|
||
sb.Append(currentChar); | ||
} | ||
|
||
sb.Append(Environment.NewLine); | ||
} | ||
|
||
image.Dispose(); | ||
|
||
return PostProcessOutput(sb); | ||
} | ||
|
||
private static string MapToAscii(IEnumerable<KeyValuePair<float, string>> map, Rgba32 pixel) | ||
{ | ||
var currentChar = ""; | ||
foreach (var (key, value) in map) | ||
{ | ||
if (key <= pixel.R) | ||
{ | ||
currentChar = value; | ||
} | ||
} | ||
return currentChar; | ||
} | ||
|
||
private static string PostProcessOutput(StringBuilder sb) | ||
{ | ||
var output = sb.ToString(); | ||
sb.Clear(); | ||
output = output.Substring(0, output.Length - Environment.NewLine.Length); | ||
return output; | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop"> | ||
|
||
<PropertyGroup> | ||
<OutputType>WinExe</OutputType> | ||
<LangVersion>8.0</LangVersion> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
<RootNamespace>CoreBoy.Windows</RootNamespace> | ||
<AssemblyName>CoreBoy.Windows</AssemblyName> | ||
<StartupObject>CoreBoy.Windows.Program</StartupObject> | ||
<UseWindowsForms>true</UseWindowsForms> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<None Remove="mario.gb" /> | ||
<None Remove="tetris.gb" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Content Include="mario.gb"> | ||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
</Content> | ||
<Content Include="tetris.gb"> | ||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
</Content> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\CoreBoy\CoreBoy.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Compile Update="WinFormsEmulatorSurface.cs"> | ||
<SubType>Form</SubType> | ||
</Compile> | ||
</ItemGroup> | ||
|
||
</Project> |
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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Windows.Forms; | ||
using CoreBoy.gui; | ||
|
||
namespace CoreBoy.Windows | ||
{ | ||
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); | ||
} | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...y/gui/WinFormsEmulatorSurface.Designer.cs → ...ndows/WinFormsEmulatorSurface.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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
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
Oops, something went wrong.