Skip to content

Commit

Permalink
#43: Use FillRectangle to draw a pixel.
Browse files Browse the repository at this point in the history
  • Loading branch information
saku-kaarakainen committed Sep 10, 2016
1 parent 74e6036 commit 2ffd9af
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 7 deletions.
11 changes: 11 additions & 0 deletions WinBoyEmulator/GameBoy/Emulator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
using MMU = WinBoyEmulator.GameBoy.Memory.Memory;
using Screen = WinBoyEmulator.GameBoy.GPU.Screen;

using static WinBoyEmulator.GameBoy.WinBoyEvents;

namespace WinBoyEmulator.GameBoy
{
/// <summary>An interface between a From and Game Boy Emulator.</summary>
Expand All @@ -42,6 +44,12 @@ public class Emulator : IEmulator
private byte[] _game;
private string _gamePath;

/// <summary>Event handler for drawing the Screen to a form.</summary>
public DrawEventHandler DrawEventHandler { get; set; }

/// <summary>Grahics that is used to draw Sceen.</summary>
public Graphics Graphics { get; set; }

public string GamePath
{
get
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions WinBoyEmulator/GameBoy/IEmulator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,25 @@
// along with WinBoyEmulator. If not, see<http://www.gnu.org/licenses/>.
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
{
/// <summary>Interface IEmulator. class Emulator impments this.</summary>
public interface IEmulator
{
/// <summary>Event handler for drawing the Screen to a form.</summary>
DrawEventHandler DrawEventHandler { get; set; }

/// <summary>Grahics that is used to draw Sceen.</summary>
Graphics Graphics { get; set; }

/// <summary>Full path of a game.</summary>
string GamePath { get; set; }

Expand Down
2 changes: 1 addition & 1 deletion WinBoyEmulator/GameBoy/Toolbox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
21 changes: 21 additions & 0 deletions WinBoyEmulator/GameBoy/WinBoyEvents.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Event handler for drawing Screen to form.
/// </summary>
/// <param name="screen"></param>
/// <param name="e"></param>
public delegate void DrawEventHandler(Screen screen, PaintEventArgs e);
}
}
67 changes: 61 additions & 6 deletions WinBoyEmulator/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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));
Expand Down
30 changes: 30 additions & 0 deletions WinBoyEmulator/Pixel.cs
Original file line number Diff line number Diff line change
@@ -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);
}

}
2 changes: 2 additions & 0 deletions WinBoyEmulator/WinBoyEmulator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
<Compile Include="GameBoy\GPU\Screen.cs" />
<Compile Include="GameBoy\IEmulator.cs" />
<Compile Include="GameBoy\Toolbox.cs" />
<Compile Include="GameBoy\WinBoyEvents.cs" />
<Compile Include="MainForm.cs">
<SubType>Form</SubType>
</Compile>
Expand All @@ -81,6 +82,7 @@
<Compile Include="GameBoy\Memory\IMemory.cs" />
<Compile Include="GameBoy\Memory\Memory.cs" />
<Compile Include="GameBoy\Memory\Rom.cs" />
<Compile Include="Pixel.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<EmbeddedResource Include="MainForm.resx">
Expand Down

0 comments on commit 2ffd9af

Please sign in to comment.