Skip to content

Commit

Permalink
Add faster rendering, wider compatibility and save on close.
Browse files Browse the repository at this point in the history
* Faster rending: Only updates certain pixels instead of rendering the bitmap as a whole
* Wider compatibility: Target platform changed to .NET Framework 4.0 (Windows XP supported)
* Save on close: Checks the history and checks for a save when the window is closed.
  • Loading branch information
itisrazza committed Sep 2, 2019
1 parent 14659a1 commit 90e2c00
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 105 deletions.
6 changes: 3 additions & 3 deletions VGAPainter/App.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
</configuration>
</configuration>
77 changes: 39 additions & 38 deletions VGAPainter/MainForm.Designer.cs

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

102 changes: 81 additions & 21 deletions VGAPainter/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,10 @@ public partial class MainForm : Form
private int bmWidth = 16, bmHeight = 16;
private byte[] bmData = new byte[256];

// canvas properties
// canvas
private Bitmap canvas;
private int zoom = 5;
private int offsetX = 0;
private int offsetY = 0;


// mouse actions
private bool mouseDraw = false;
private DrawMode drawMode;
Expand Down Expand Up @@ -65,7 +64,7 @@ public MainForm()
}

// a few UI things
Redraw();
FullRedraw();
UpdateStackButtons();
}

Expand Down Expand Up @@ -209,7 +208,7 @@ private void ShowPalette_Click(object sender, EventArgs e)
undoStack.Clear();
redoStack.Clear();

Redraw();
FullRedraw();
}

#endregion
Expand Down Expand Up @@ -251,17 +250,28 @@ private Bitmap RenderBitmap(int scale, bool grid)
return bm;
}

/// <summary>
/// Redraws the canvas from scratch
/// </summary>
private void FullRedraw()
{
canvas = RenderBitmap(zoom, showGrid.Checked);
Redraw();
}

/// <summary>
/// Redraws the canvas
/// </summary>
private void Redraw()
{
canvasBox.Image = RenderBitmap(zoom, showGrid.Checked);
{
canvasBox.Image = canvas;
canvasBox.Refresh();
canvasBox.Update();
}

private void RedrawUITrigger(object sender, EventArgs e)
{
Redraw();
FullRedraw();
}

#endregion
Expand All @@ -276,19 +286,19 @@ private void ViewToolStripMenuItem_Click(object sender, EventArgs e)
private void ZoomIn_Click(object sender, EventArgs e)
{
zoom++;
Redraw();
FullRedraw();
}

private void ZoomOut_Click(object sender, EventArgs e)
{
if (zoom > 1) zoom--;
Redraw();
FullRedraw();
}

private void ZoomReset_Click(object sender, EventArgs e)
{
zoom = Int32.Parse((sender as ToolStripItem).Tag as string);
Redraw();
FullRedraw();
}

#endregion
Expand Down Expand Up @@ -333,6 +343,10 @@ private void SaveImage_Click(object sender, EventArgs e)
{
// get the filename
var result = saveTgr.ShowDialog(this);
if (result == DialogResult.Cancel && e is FormClosingEventArgs)
{
(e as FormClosingEventArgs).Cancel = true;
}
if (result != DialogResult.OK) return;

// save the image
Expand Down Expand Up @@ -378,17 +392,18 @@ private void DoMouse(int mouseX, int mouseY)
// don't do anything out of scope
if (mouseX >= bmWidth || mouseY >= bmHeight) return;

// pre-calc image offset
// precalc offset and boundary check
int offset = mouseY * bmWidth + mouseX;
if (offset < 0 || offset >= bmData.Length) return;

// pixel changes to commit to history
var changes = new HashSet<PixelChange>();

switch (drawMode)
{
case DrawMode.Pixel:
var oldColor = bmData[offset];
if (color == oldColor) break; // do nothing if the color is the same
bmData[offset] = color;
undoStack.Push(new HistoryEvent(new PixelChange(offset, oldColor, color)));
var change = DrawPixel(mouseX, mouseY, color);
if (change != null) changes.Add(change);
redoStack.Clear();
break;
case DrawMode.Picker:
Expand All @@ -398,8 +413,13 @@ private void DoMouse(int mouseX, int mouseY)
throw new NotImplementedException("Draw mode " + drawMode.ToString() + " not implemented.");
}

Redraw();
// undo/redo stack things
if (changes.Count > 0)
undoStack.Push(new HistoryEvent(changes));
UpdateStackButtons();

// trigger canvasBox redraw
Redraw();
}

private void NewToolStripMenuItem_Click(object sender, EventArgs e)
Expand All @@ -412,7 +432,7 @@ private void NewToolStripMenuItem_Click(object sender, EventArgs e)
bmHeight = (int)newDialog.bmHeight.Value;
bmData = new byte[bmWidth * bmHeight];

Redraw();
FullRedraw();
}

private void CanvasBox_MouseDown(object sender, MouseEventArgs e)
Expand Down Expand Up @@ -440,6 +460,29 @@ private void CanvasBox_MouseLeave(object sender, EventArgs e)

#endregion

private PixelChange DrawPixel(int x, int y, byte color)
{
// precalc offset and check boundary
int offset = y * bmWidth + x;
if (offset < 0 || offset >= bmData.Length) return null;

// don't do anything if the pixel is the same
if (bmData[offset] == color) return null;

// change the pixel and note the change
var change = new PixelChange(offset, bmData[offset], color);
bmData[offset] = color;

// update the bitmap
var gfx = Graphics.FromImage(canvas);
gfx.FillRectangle(new SolidBrush(vgaPalette[color]),
x * zoom, y * zoom,
zoom - (showGrid.Checked ? 1 : 0),
zoom - (showGrid.Checked ? 1 : 0));

return change;
}

private void AboutToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show(this, "VGAPainter by thegreatrazz\n" +
Expand All @@ -466,7 +509,9 @@ private void UndoAction_Click(object sender, EventArgs e)
// undo it
foreach (var change in action.changes)
{
bmData[change.Offset] = change.OldColor;
int x = change.Offset % bmWidth;
int y = change.Offset / bmWidth;
DrawPixel(x, y, change.OldColor);
}

// put it on the redo stack
Expand All @@ -484,7 +529,9 @@ private void RedoAction_Click(object sender, EventArgs e)
// undo it
foreach (var change in action.changes)
{
bmData[change.Offset] = change.NewColor;
int x = change.Offset % bmWidth;
int y = change.Offset / bmWidth;
DrawPixel(x, y, change.NewColor);
}

// put it on the redo stack
Expand All @@ -494,6 +541,19 @@ private void RedoAction_Click(object sender, EventArgs e)
UpdateStackButtons();
}

private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (undoStack.Count > 0)
{
var result = MessageBox.Show("Would you like to save your work before quitting?", "Save and Quit", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);
if (result == DialogResult.Cancel) e.Cancel = true;
if (result == DialogResult.Yes)
{
SaveImage_Click(sender, e);
}
}
}

private void DrawTool_Select(object sender, EventArgs e)
{
foreach (var el in menuEdit.DropDownItems)
Expand Down
4 changes: 2 additions & 2 deletions VGAPainter/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.0.2.0")]
[assembly: AssemblyFileVersion("0.0.2.0")]
[assembly: AssemblyVersion("0.0.3.0")]
[assembly: AssemblyFileVersion("0.0.3.0")]
Loading

0 comments on commit 90e2c00

Please sign in to comment.