Proper C Pointer Binding
ralex
krckoorascic at gmail.com
Wed Mar 26 04:25:44 PDT 2014
On Wednesday, 26 March 2014 at 09:51:04 UTC, 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.
>
> My problem is that auto font here is an ulong. Could that be
> wrapped into a type Font so that it only accepts assignment
> from other Font type but no insecure numeric caluclations
> and make the loadFont return that Font type, and other
> methods use Font as arguments?
You probably want something like this:
struct Font {
static Font opCall(string path) {
Font f;
f.fptr = loadFont(path.toStringz());
return s;
}
c_ulong fptr;
}
use it like this:
Font arial = "Arial.TTF";
when pasing font to extern functionst that accept ulong use
arial.fptr, in your (D) functions use Font.
More information about the Digitalmars-d
mailing list