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):

 

Next the BlitzMax code that uses the C code:

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++):

 

The BlitzMax code using the DLL is as follows:

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

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

 

 

Hex 2 Dec

In many programming languages there is built-in command or function to convert a decimal value to a hex value. But at least in BlitzMax there isn’t a command or function to convert a hex value to a dec value. In this post I present my little function to convert a hex value to dec value in BlitzMax and in C.

The BlitzMax function:

First, put somewhere in the beginning of your program line

The function itself:

The C version is a complete program that uses its functions:

Be careful with the C version: It doesn’t check if the input is correct. As you can see from the code the letters in the hex number should be given in capital letters. Feel free to use and improve these codes!

How to retrieve the %AppData% folder

AppData folder is used to store data like hiscores, config, etc. of a game (or app). The AppData folder is a special folder that’s location is related to Windows version one is using. This is why AppData folder must be retrieved with the aid of the system.

In Blitz3D retrieving the AppData folder is as simple as this:

AppDataFolder$ = GetEnv$(”AppData”)

In BlitzMax retrieving the AppData folder isn’t this straight forward. The easiest way to get the location of this special folder is to use external module, BAH.Volumes. Once this module is installed for BlitzMax and imported in the beginning of the code, retrieving the AppData folder is as easy as this:

AppDataFolder = GetUserAppDir()

Above is assumed that the variable AppDataFolder is defined as String.

In order to install external modules to BlitzMax, MinGW must be installed and configured for BlitzMax. The BlitzMax product page has a link to forum topic that explains how to set up MinGW for BlitzMax.

Link: BAH.Volumes (from Google Code)

Update!

With good old ANSI C the %AppData% folder can be retrieved as follows: