Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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!


In reply to Calling C function from Raku by BenM

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2022-01-23 05:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (63 votes). Check out past polls.

    Notices?