Proper C Pointer Binding

Mike Parker aldacron at gmail.com
Wed Mar 26 04:13:52 PDT 2014


On 3/26/2014 6:51 PM, "Róbert László Páli" wrote:
> I am programming a new GUI widget library based on OpenGL for D.
> For that I manually bind the used OpenGL functions to D and create
> an abstraction layer to draw things, boxes, texts, shapes, etc.
>
> http://palaes.rudanium.org/HueApp/intro.php
>
> The thing compiles nicely with SDL, FreeType, FTGL. But
> for the text drawing I use some pretty lame binding currently.
> It is a fresh part of the code and want to do it properly:
>
> C Code:
>
> unsigned long loadFont(char * path) {
>    FTGLfont * font = FTGLloadFont(path);
>    return (unsigned long) font;
> }
>
> void drawText(unsigned long font, unsigned size, char * text) {
>    // do the text drawing here
> }
>
> void destroyFont(unsigned long font) {
>    FTGLdestroyFont((FTGLfont * ) font);
> }
>
> D Code:
>
> extern (C) ulong loadFont(char * path);
>
> extern (C) void destroyFont(ulong font);
>
> void main() {
>
>    // init screen and OpenGL setup
>
>    auto font = loadFont(cast (char * ) "Arial.TTF");
>
>    scope (exit) destroyFont(font);
>
>    // draw some text
>
>    // close OpenGL and SDL with some second delay
> }
>
> This works properly, and long is surely large enough to hold
> a pointer in it, I could use sizet, I know that would be better.

A big gaping hole here is that longs in C can be 32-bit or 64-bit 
depending on the compiler and platform. Any time you want to bind to 
anything using longs in C, you should import core.stdc.config and use 
the c_long and c_ulong types.





More information about the Digitalmars-d mailing list