use C++ function from BlitzMax command?

BlitzMax Forums/BlitzMax Programming/use C++ function from BlitzMax command?

Captain Wicker (crazy hillbilly)(Posted 2013) [#1]
was just wondering if its possible to use a C++ function through a BlitzMax command/function? I am trying to make accessible to use C++ within BlitzMax code. I was going to make a wrapper for BlitzMax.


Russell(Posted 2013) [#2]
Sure! Look at the source code for one of the modules (like mod/pub/freetype.bmx for example) for some examples. Basically, you'll 'import' the C/C++ source code (and any *.h files it may need) and then, in an 'Extern/End Extern' block, those external functions will be declared. Then you can use them just like any other Bmax command!
For example, in the freetype.bmx module, you'll see a long list of 'Import xxx', followed by the Extern/End Extern block:
Import "include/*.h"

Import "src/base/ftbase.c"

Import "src/base/ftapi.c"
Import "src/base/ftbbox.c"
Import "src/base/ftbdf.c"
Import "src/base/ftbitmap.c"
Import "src/base/ftdebug.c"
Import "src/base/ftgasp.c"
Import "src/base/ftglyph.c"
Import "src/base/ftgxval.c"
Import "src/base/ftinit.c"
Import "src/base/ftlcdfil.c"
Import "src/base/ftmm.c"
Import "src/base/ftotval.c"
Import "src/base/ftpfr.c"
Import "src/base/ftstroke.c"
Import "src/base/ftsynth.c"
Import "src/base/ftsystem.c"
Import "src/base/fttype1.c"
Import "src/base/ftwinfnt.c"
Import "src/base/ftxf86.c"
Import "src/base/ftpatent.c"

Import "src/autofit/autofit.c"
Import "src/bdf/bdf.c"
Import "src/cache/ftcache.c"
Import "src/cff/cff.c"
Import "src/cid/type1cid.c"
Import "src/gzip/ftgzip.c"
Import "src/lzw/ftlzw.c"
Import "src/otvalid/otvalid.c"
Import "src/pcf/pcf.c"
Import "src/pfr/pfr.c"
Import "src/psaux/psaux.c"
Import "src/pshinter/pshinter.c"
Import "src/psnames/psnames.c"
Import "src/raster/raster.c"
Import "src/sfnt/sfnt.c"
Import "src/smooth/smooth.c"
Import "src/truetype/truetype.c"
Import "src/type1/type1.c"
Import "src/type42/type42.c"
Import "src/winfonts/winfnt.c"

?

Extern

Function FT_Init_FreeType( ft_lib:Byte Ptr Ptr )

Function FT_Done_FreeType( ft_lib:Byte Ptr )
Function FT_Done_Face( ft_face:Byte Ptr )
Function FT_Done_Glyph( ft_glyph:Byte Ptr )

Function FT_New_Face( ft_lib:Byte Ptr,arg$z,faceIndex,ft_face:Byte Ptr Ptr )
Function FT_New_Memory_Face( ft_lib:Byte Ptr,buf:Byte Ptr,size,faceIndex,ft_face:Byte Ptr Ptr )

Function FT_Set_Pixel_Sizes( ft_face:Byte Ptr,width,height )
Function FT_Get_Char_Index( ft_face:Byte Ptr,index )
Function FT_Set_Charmap( ft_face:Byte Ptr,charmap )

Function FT_Load_Char( ft_face:Byte Ptr,index,flags )
Function FT_Load_Glyph( ft_face:Byte Ptr,index,flags )
Function FT_Render_Glyph( ft_glyph:Byte Ptr,mode )

End Extern

Then, any of those functions can be used as though they were BMax functions. Very cool.

This is an oversimplified example (they can get a bit more complicated in some cases), but you get the idea.

Russell

p.s. My 7 year old son is in the high-functioning autism spectrum, so I can relate to your signature. :) He's doing great in the ABA school we have him in, and he loves it. He should be able to get streamlined into the public school system in the next year or so.


Captain Wicker (crazy hillbilly)(Posted 2013) [#3]
Thanks Russell
:)