From 2ffd9af611ab6919bc28c63c2d90fe7469bb629f Mon Sep 17 00:00:00 2001 From: saku-kaarakainen Date: Sun, 11 Sep 2016 00:50:33 +0300 Subject: [PATCH] #43: Use FillRectangle to draw a pixel. --- WinBoyEmulator/GameBoy/Emulator.cs | 11 +++++ WinBoyEmulator/GameBoy/IEmulator.cs | 9 ++++ WinBoyEmulator/GameBoy/Toolbox.cs | 2 +- WinBoyEmulator/GameBoy/WinBoyEvents.cs | 21 ++++++++ WinBoyEmulator/MainForm.cs | 67 +++++++++++++++++++++++--- WinBoyEmulator/Pixel.cs | 30 ++++++++++++ WinBoyEmulator/WinBoyEmulator.csproj | 2 + 7 files changed, 135 insertions(+), 7 deletions(-) create mode 100644 WinBoyEmulator/GameBoy/WinBoyEvents.cs create mode 100644 WinBoyEmulator/Pixel.cs diff --git a/WinBoyEmulator/GameBoy/Emulator.cs b/WinBoyEmulator/GameBoy/Emulator.cs index 273e2d8..88d1261 100644 --- a/WinBoyEmulator/GameBoy/Emulator.cs +++ b/WinBoyEmulator/GameBoy/Emulator.cs @@ -31,6 +31,8 @@ using MMU = WinBoyEmulator.GameBoy.Memory.Memory; using Screen = WinBoyEmulator.GameBoy.GPU.Screen; +using static WinBoyEmulator.GameBoy.WinBoyEvents; + namespace WinBoyEmulator.GameBoy { /// An interface between a From and Game Boy Emulator. @@ -42,6 +44,12 @@ public class Emulator : IEmulator private byte[] _game; private string _gamePath; + /// Event handler for drawing the Screen to a form. + public DrawEventHandler DrawEventHandler { get; set; } + + /// Grahics that is used to draw Sceen. + public Graphics Graphics { get; set; } + public string GamePath { get @@ -72,7 +80,10 @@ private void _readGameFile(string filename) private void _render() { + var clipRect = new Rectangle(); + var e = new PaintEventArgs(Graphics, clipRect); + DrawEventHandler(_toolbox.RandomizeScreen(), e); } private void _gameCycle(object sender, ElapsedEventArgs e) diff --git a/WinBoyEmulator/GameBoy/IEmulator.cs b/WinBoyEmulator/GameBoy/IEmulator.cs index dd1b604..8919d41 100644 --- a/WinBoyEmulator/GameBoy/IEmulator.cs +++ b/WinBoyEmulator/GameBoy/IEmulator.cs @@ -14,16 +14,25 @@ // along with WinBoyEmulator. If not, see. using System; using System.Collections.Generic; +using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; +using static WinBoyEmulator.GameBoy.WinBoyEvents; + namespace WinBoyEmulator.GameBoy { /// Interface IEmulator. class Emulator impments this. public interface IEmulator { + /// Event handler for drawing the Screen to a form. + DrawEventHandler DrawEventHandler { get; set; } + + /// Grahics that is used to draw Sceen. + Graphics Graphics { get; set; } + /// Full path of a game. string GamePath { get; set; } diff --git a/WinBoyEmulator/GameBoy/Toolbox.cs b/WinBoyEmulator/GameBoy/Toolbox.cs index 8e20f52..16e7989 100644 --- a/WinBoyEmulator/GameBoy/Toolbox.cs +++ b/WinBoyEmulator/GameBoy/Toolbox.cs @@ -55,7 +55,7 @@ public Screen RandomizeScreen() { for(var i = 0; i < _screen.Data.GetLength(0); i++) { - for(var j = 0; j < _screen.Data.GetLength(1); i++) + for(var j = 0; j < _screen.Data.GetLength(1); j++) { var max = Configuration.Colors.Palette.Length - 1; var value = _random.Next(max); diff --git a/WinBoyEmulator/GameBoy/WinBoyEvents.cs b/WinBoyEmulator/GameBoy/WinBoyEvents.cs new file mode 100644 index 0000000..c822e74 --- /dev/null +++ b/WinBoyEmulator/GameBoy/WinBoyEvents.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +using Screen = WinBoyEmulator.GameBoy.GPU.Screen; + +namespace WinBoyEmulator.GameBoy +{ + public class WinBoyEvents + { + /// + /// Event handler for drawing Screen to form. + /// + /// + /// + public delegate void DrawEventHandler(Screen screen, PaintEventArgs e); + } +} diff --git a/WinBoyEmulator/MainForm.cs b/WinBoyEmulator/MainForm.cs index e66cf4d..8524660 100644 --- a/WinBoyEmulator/MainForm.cs +++ b/WinBoyEmulator/MainForm.cs @@ -33,23 +33,78 @@ namespace WinBoyEmulator { public partial class MainForm : Form { - private static readonly object _syncRoot = new object(); + private static readonly object _syncDraw = new object(); private const string _sourceCodeUrl = "https://github.com/saku-kaarakainen/WinBoyEmulator/"; + private const int gbWidth = GameBoy.Configuration.Screen.Width; + private const int gbHeight = GameBoy.Configuration.Screen.Height; + private static int gbcLength = GameBoy.Configuration.Colors.Palette.Length; + + private Rectangle[,] _rectangles; + private Pixel _pixel; private Emulator _emulator; private LogWriter _logWriter; + public MainForm() { InitializeComponent(); } private void MainForm_Load(object sender, EventArgs e) { _logWriter = new LogWriter(GetType()); - - // Check Issues #30 and #31 - // Check #31 - _emulator = new Emulator { GamePath = "C:\\temp\\game.gb" }; + _pixel = new Pixel + { + Top = _menuStripMain.Top + _menuStripMain.Height, + Left = _menuStripMain.Left + _menuStripMain.Width, + Width = 1, + Height = 1 + }; + _emulator = new Emulator + { + // Check issues #30 and #31 + GamePath = "C:\\temp\\game.gb", + Graphics = CreateGraphics() + }; + _emulator.DrawEventHandler += _draw; _emulator.StartEmulation(); } + private Brush GetBrushById(int colorCode) + { + // Not the most flexible solution... + switch (colorCode) + { + case 0: return Brushes.White; + case 1: return Brushes.Silver; + case 2: return Brushes.Gray; + case 3: return Brushes.Black; + default: + var sColor = nameof(colorCode); + throw new ArgumentOutOfRangeException(sColor, $"{sColor} must be between 0 and 3."); + } + } + + private void _draw(Screen screen, PaintEventArgs e) + { + using (var graphics = CreateGraphics()) + { + for (var i = 0; i < screen.Data.GetLength(0); i++) + { + for (var j = 0; j < screen.Data.GetLength(1); j++) + { + var brush = GetBrushById(screen.Data[i, j]); + var x = _pixel.Top + i; + var y = _pixel.Left + j; + + Console.WriteLine($"x = {x}"); + Console.WriteLine($"y = {y}"); + Console.WriteLine($"screen.Data[i, j] = {screen.Data[i, j]}"); + + graphics.FillRectangle(brush, x, y, _pixel.Width, _pixel.Height); + //graphics.FillRectangle(brush, _rectangles[i, j]); + } + } + } + } + #region _Click private void _toolStripMenuItemAbout_Click(object sender, EventArgs e) { @@ -62,7 +117,7 @@ private void _closeEmulatorToolStripMenuItem_Click(object sender, EventArgs e) } private void _toolStripMenuItemOpen_Click(object sender, EventArgs e) => _openFileDialogMain.ShowDialog(); - + private void _toolStripMenuItemClose_Click(object sender, EventArgs e) => Close(); private void _toolStripMenuItemSourceCode_Click(object sender, EventArgs e) => Process.Start(new ProcessStartInfo(_sourceCodeUrl)); diff --git a/WinBoyEmulator/Pixel.cs b/WinBoyEmulator/Pixel.cs new file mode 100644 index 0000000..bb85ea0 --- /dev/null +++ b/WinBoyEmulator/Pixel.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace WinBoyEmulator +{ + public class Pixel + { + public int Top { get; set; } + public int Left { get; set; } + public int Width { get; set; } + public int Height { get; set; } + + public Pixel() /* : this(0, 0, 1, 1) */ { } + + public Pixel(int top, int left, int width, int height) + { + Top = top; + Left = left; + Width = width; + Height = height; + } + + public Rectangle ToRectangle() => new Rectangle(Top, Left, Width, Height); + } + +} diff --git a/WinBoyEmulator/WinBoyEmulator.csproj b/WinBoyEmulator/WinBoyEmulator.csproj index 0b75ec2..7400c1c 100644 --- a/WinBoyEmulator/WinBoyEmulator.csproj +++ b/WinBoyEmulator/WinBoyEmulator.csproj @@ -71,6 +71,7 @@ + Form @@ -81,6 +82,7 @@ +