Chicken-Scheme FFI Examples
I'm currently working on refactoring the FFI implementation for the Rebel Game Engine. It was previously written using the Bind chicken egg but I wanted to have more control over the implementation by using the low level foreign functions.
To help me better understand I made some examples that has the basic FFI implementations that I'll be needing for my project.
foreign-lambda example
Let's say we have a structure Vec3
and a function Vec3Create
that we want to access from chicken-scheme.
typedef struct Vec3 {
float x;
float y;
float z;
} Vec3;
Vec3* Vec3Create(float x, float y, float z)
{
Vec3* v = (Vec3*)malloc(sizeof(Vec3));
v->x = x;
v->y = y;
v->z = z;
return v;
}
We could use foreign-lambda
to bind to the function:
(define vec3_create
(foreign-lambda
(c-pointer (struct "Vec3")) ; Return type, a pointer to a struct object of Vec3
"Vec3Create" ; Name fo the function
float float float)) ; The …