Skip to content

Commit

Permalink
first release version
Browse files Browse the repository at this point in the history
  • Loading branch information
MatejGomboc committed Jan 3, 2018
1 parent a2422a7 commit f345446
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 9 deletions.
7 changes: 6 additions & 1 deletion mandelbrot/mandelbrot-plotter/MainForm.Designer.cs

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

80 changes: 77 additions & 3 deletions mandelbrot/mandelbrot-plotter/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,23 @@
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace mandelbrot_plotter
{
public partial class MainForm : Form
{
private float min_x = -2.0f;
private float max_x = 1.0f;
private float min_y = -1.0f;
private float max_y = 1.0f;
private uint max_iterations = 40;

public MainForm()
{
InitializeComponent();

MandelbrotDLL.init_opencl();
if (!MandelbrotDLL.init_opencl()) throw new Exception(Marshal.PtrToStringAnsi(MandelbrotDLL.get_status_message(), MandelbrotDLL.get_status_message_len()));
}

private void MainForm_Load(object sender, EventArgs e)
Expand All @@ -24,7 +31,8 @@ private void MainForm_Load(object sender, EventArgs e)
// Lock the bitmap's bits.
BitmapData bmpData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, b.PixelFormat);

MandelbrotDLL.get_image_opencl(bmpData.Scan0, (uint)b.Width, (uint)b.Height, -2.0f, 1.0f, -1.0f, 1.0f, 40);
if (!MandelbrotDLL.get_image_opencl(bmpData.Scan0, (uint)b.Width, (uint)b.Height, min_x, max_x, min_y, max_y, max_iterations))
throw new Exception(Marshal.PtrToStringAnsi(MandelbrotDLL.get_status_message(), MandelbrotDLL.get_status_message_len()));

// Unlock the bits.
b.UnlockBits(bmpData);
Expand All @@ -44,7 +52,8 @@ private void MainForm_Resize(object sender, EventArgs e)
// Lock the bitmap's bits.
BitmapData bmpData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, b.PixelFormat);

MandelbrotDLL.get_image_opencl(bmpData.Scan0, (uint)b.Width, (uint)b.Height, -2.0f, 1.0f, -1.0f, 1.0f, 40);
if (!MandelbrotDLL.get_image_opencl(bmpData.Scan0, (uint)b.Width, (uint)b.Height, min_x, max_x, min_y, max_y, max_iterations))
throw new Exception(Marshal.PtrToStringAnsi(MandelbrotDLL.get_status_message(), MandelbrotDLL.get_status_message_len()));

// Unlock the bits.
b.UnlockBits(bmpData);
Expand All @@ -57,5 +66,70 @@ private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
MandelbrotDLL.release_opencl();
}

private void MainForm_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Down:
case Keys.Up:
case Keys.Left:
case Keys.Right:
case Keys.Add:
case Keys.Subtract:
e.IsInputKey = true;
break;
default:
break;
}
}

private void MainForm_KeyDown(object sender, KeyEventArgs e)
{
float dist_x = max_x - min_x;
float dist_y = max_y - min_y;

switch (e.KeyCode)
{
case Keys.Down:
max_y += dist_y * 0.1f;
min_y += dist_y * 0.1f;
this.OnResize(null);
break;
case Keys.Up:
max_y -= dist_y * 0.1f;
min_y -= dist_y * 0.1f;
this.OnResize(null);
break;
case Keys.Left:
max_x -= dist_x * 0.1f;
min_x -= dist_x * 0.1f;
this.OnResize(null);
break;
case Keys.Right:
max_x += dist_x * 0.1f;
min_x += dist_x * 0.1f;
this.OnResize(null);
break;
case Keys.Add:
max_y -= dist_y * 0.1f;
min_y += dist_y * 0.1f;
max_x -= dist_x * 0.1f;
min_x += dist_x * 0.1f;
max_iterations += 10;
this.OnResize(null);
break;
case Keys.Subtract:
max_y += dist_y * 0.1f;
min_y -= dist_y * 0.1f;
max_x += dist_x * 0.1f;
min_x -= dist_x * 0.1f;
if (max_iterations > 40) max_iterations -= 10;
this.OnResize(null);
break;
default:
break;
}
}
}
}
3 changes: 1 addition & 2 deletions mandelbrot/mandelbrot-plotter/MandelbrotDLL.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Runtime.InteropServices;
using System.Text;

namespace mandelbrot_plotter
{
Expand All @@ -10,7 +9,7 @@ static class MandelbrotDLL
public static extern int get_status_message_len();

[DllImport("mandelbrot-dll.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern StringBuilder get_status_message();
public static extern IntPtr get_status_message();

[DllImport("mandelbrot-dll.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern bool init_opencl();
Expand Down
21 changes: 18 additions & 3 deletions mandelbrot/mandelbrot-plotter/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Windows.Forms;
using System.IO;

namespace mandelbrot_plotter
{
Expand All @@ -11,9 +12,23 @@ static class Program
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
try
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
catch(Exception ex)
{
using (StreamWriter writetext = new StreamWriter("error-log.txt"))
{
writetext.Write(ex.Message);
}

MessageBox.Show("Error occurred in OpenCL. Error log written to error-log.txt.", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);

return;
}
}
}
}
Binary file modified mandelbrot/mandelbrot-plotter/bin/x64/Release/mandelbrot-dll.dll
Binary file not shown.
Binary file not shown.
Binary file modified mandelbrot/mandelbrot.sdf
Binary file not shown.
Binary file modified mandelbrot/mandelbrot.v12.suo
Binary file not shown.

0 comments on commit f345446

Please sign in to comment.