Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Calling C function from Raku

by BenM (Initiate)
on Mar 18, 2020 at 11:37 UTC ( #11114426=perlquestion: print w/replies, xml ) Need Help??

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!

Replies are listed 'Best First'.
Re: Calling C function from Raku
by Nine (Novice) on Mar 18, 2020 at 13:42 UTC
    I bet on that Color argument. I don't think we support passing structs as value types. They will be passed as pointers instead which in turn will be interpreted as a color by that function (which causes the apparent randomness). Please try passing a manually packed uint32 instead: $r +< 24 +| $g +< 16 +| $b +< 8 +| $a (or maybe the other way round)
      Oh I should have thought of that. Structs being passed as a pointer makes complete sense. I gave it a go and packing a uint32 worked perfectly! Thank you very much for the help!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://11114426]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (11)
As of 2020-04-04 12:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The most amusing oxymoron is:
















    Results (32 votes). Check out past polls.

    Notices?