Using C and DLLs in BlitzMax

BlitzMax apps can be made faster by using C. Also using DLLs in BlitzMax is possible, but that isn’t documented in BlitzMax’s documentation at all.

First, an example of using C code in BlitzMax. As an example we generate the Mandlebrot set using C code to determine does the point belong to the set (see my article on my personal blog on generating the Mandelbrot set, whole example code is in BlitzMax).

To compile the example below, you need to have MinGW configured to BlitzMax.

We start with the C code (save it as mandelbrot.c):

int mandelbrot(double dr, double di, double minR, double minI, int x, int y, int maxIter);

int mandelbrot(double dr, double di, double minR, double minI, int x, int y, int maxIter) {

    int iterations = 0;

	double cr,ci,zr,zi,zr_temp,zi_temp;

    cr = minR + x * dr;
    ci = minI + y * di;
    zr = 0.0;   
    zi = 0.0;
    zr_temp = 0.0;
    zi_temp = 0.0;

	while (iterations < maxIter) {

        zr_temp = zr * zr - zi * zi + cr;
       
        zi_temp = 2.0 * zr * zi + ci;
        
        zr = zr_temp;
        zi = zi_temp;       

        iterations++;

        if (zr * zr + zi * zi > 4.0) return iterations;
    
	}

    return 0;
}

 

Next the BlitzMax code that uses the C code:

Import "mandelbrot.c"

' tell the compiler that there is an external function
Extern
	Function mandelbrot:Int(dr:Double,di:Double,minR:Double,minI:Double,x:Int,y:Int,maxIter:Int)
End Extern

Const maxIter:Int = 40

Graphics 640,480

Local minR:Double = -2.25, maxR:Double = 0.75
Local minI:Double = -1.5, maxI:Double = 1.5

Local dr:Double, di:Double

Local w:Double, h:Double, sW:Double, diff:Double
Local shade:Int

w = maxR - minR
h = maxI - minI
sW = h * (640.0) / (480.0)
diff = sW - w

minR = minR - diff / 2
maxR = maxR + diff / 2

dr = (maxR - minR) / 640
di = (maxI - minI) / 480

For Local y:Int = 0 To 480 - 1
	For Local x:Int = 0 To 640 - 1
		shade = mandelbrot(dr,di,minR,minI,x,y,MAXITER)
		shade = 255 - Double(MAXITER - shade) / MAXITER * 255.0
		SetColor 0,0,shade
		Plot x,y
	Next
Next

Flip
WaitKey
End

Compile and run the code and you’ll see a blue shaded Mandelbrot set that should look like following:

mandelbrot

In the next part of this post we’ll create a DLL in C and call it from BlitzMax.

I’m still mostly using Visual C++ 2008.

The steps to create a DLL:

  • Start Visual C++ and start a new project
  • Select WIN32 console application, name it (in our example Mandelbrot) and press OK
  • Then click ”Next” and select DLL

You’ll get a pre-generated code for a DLL.

The C code for our DLL is following (Mandelbrot.cpp in Visual C++):

#include "stdafx.h"	// this for Visual C++

extern "C"{ 
_declspec(dllexport) int mandelbrot(double dr, double di, double minR, double minI, int x, int y, int maxIter); 
} 
int mandelbrot(double dr, double di, double minR, double minI, int x, int y, int maxIter) {

    int iterations = 0;

	double cr,ci,zr,zi,zr_temp,zi_temp;

    cr = minR + x * dr;
    ci = minI + y * di;
    zr = 0.0;   
    zi = 0.0;
    zr_temp = 0.0;
    zi_temp = 0.0;

	while (iterations < maxIter) {

        zr_temp = zr * zr - zi * zi + cr;
       
        zi_temp = 2.0 * zr * zi + ci;
        
        zr = zr_temp;
        zi = zi_temp;       

        iterations++;

        if (zr * zr + zi * zi > 4.0) return iterations;
    
	}

    return 0;
}

 

The BlitzMax code using the DLL is as follows:

Extern "win32"
	Function FreeLibrary(hLibrary:Int)
End Extern

Global mandelbrot:Int(dr:Double,di:Double,minR:Double,minI:Double,x:Int,y:Int,MAXITER:Int)"Win32"

Global lib = LoadLibraryA("Mandelbrot.dll")

If lib Then
	mandelbrot = GetProcAddress(lib, "mandelbrot")
Else
	Notify "DLL-file not found!"
	End
EndIf

Global x:Int, y:Int
Const MAXITER:Int = 40

Graphics 640,480

Global minR:Double = -2.25, maxR:Double = 0.75
Global minI:Double = -1.5, maxI:Double = 1.5

Global dr:Double, di:Double

Global w:Double, h:Double, sW:Double, diff:Double
Global shade:Int

w = maxR - minR
h = maxI - minI
sW = h * (640.0) / (480.0)
diff = sW - w

minR = minR - diff / 2
maxR = maxR + diff / 2

dr = (maxR - minR) / 640
di = (maxI - minI) / 480

For y = 0 To 480 - 1
	For x = 0 To 640 - 1
		shade = getShade()
		shade = 255 - Double(MAXITER - shade) / MAXITER * 255.0
		SetColor 0,0,shade
		Plot x,y
	Next
Next

Flip
WaitKey
FreeLibrary(lib)	' free the resources
End

Function getShade:Int()
	Return mandelbrot(dr,di,minR,minI,x,y,MAXITER)
EndFunction

The DLL function is called from a BlitzMax function, because otherwise one would get a EXCEPTION_STACK_OVERFLOW.

When the DLL is called from a function the memory allocated for the DLL-call is local; after returning from the function all the memory allocated for the function call is freed.

As always, this information is free. You may use the codes above in your projects as you wish.