http://www.perlmonks.org?node_id=11114426

BenM has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I'm quite new to Raku and I was wondering if anyone could point me in the right direction with using the C FFI. I have been having some difficulty getting the C graphics library Raylib (https://github.com/raysan5/raylib) up and running.

I've been aiming to get a basic window open with text displayed (https://www.raylib.com/examples/web/core/loader.html?name=core_basic_window) but have been having issues with clearing the screen buffer with a specified color, as well as not being able to see the text from the "DrawText" function. The color that appears seems to be random. I was originally thinking it was possibly from the conversion of the "unsigned char" to a "uint8" within Raku, but they should be the same size in memory and I haven't managed to find a solution to the issue yet.

I have a raylib.dll from a github release within the same folder and I am currently on Windows 64 bit. The relevant C function signatures from raylib.h are:

void InitWindow(int width, int height, const char *title); void SetTargetFPS(int fps); bool WindowShouldClose(void); void BeginDrawing(void); void EndDrawing(void); void CloseWindow(void); void ClearBackground(Color color); void DrawText(const char *text, int posX, int posY, int fontSize, Colo +r color); typedef struct Color { unsigned char r; unsigned char g; unsigned char b; unsigned char a; } Color;

And the Raku code that I was testing:

use NativeCall; class Color is repr('CStruct') { has uint8 $.r; has uint8 $.g; has uint8 $.b; has uint8 $.a; } sub InitWindow(int32, int32, Str) is native('raylib.dll') { * } sub SetTargetFPS(int32) is native('raylib.dll') { * } sub WindowShouldClose() returns int32 is native('raylib.dll') { * } sub BeginDrawing() is native('raylib.dll') { * } sub EndDrawing() is native('raylib.dll') { * } sub CloseWindow() is native('raylib.dll') { * } sub ClearBackground(Color) is native('raylib.dll') { * } sub DrawText(Str, int32, int32, int32, Color) is native('raylib.dll') +{ * } InitWindow(800, 450, "Hello world!"); SetTargetFPS(60); my $color = Color.new(r => 255, g => 0, b => 0, a => 255); my $textColor = Color.new(r => 0, g => 0, b => 255, a => 255); while (!WindowShouldClose()) { BeginDrawing(); ClearBackground($color); DrawText("Congrats! You created your first window!", 190, 200, 20, + $textColor); EndDrawing(); } CloseWindow();

Any pointers to what may be causing the issue would be greatly appreciated!