#Raylib file drop

1 messages · Page 1 of 1 (latest)

tender grotto
#

I'm trying to get a simple window working where I drop a file onto it and the filepath gets dispayed. Here's my code so far:

const rl = @cImport({
    @cInclude("raylib.h");
});

pub fn main() !void {
    rl.InitWindow(800, 800, "File Drop");
    defer rl.CloseWindow();

    var filepath: [2048]u8 = undefined;
    var has_file: bool = false;

    while (!rl.WindowShouldClose()) {
        // Detect file
        if (rl.IsFileDropped()) {
            var dropped: rl.FilePathList = rl.LoadDroppedFiles();
            defer rl.UnloadDroppedFiles(dropped);
            @memcpy(&filepath, @as([*]u8, @ptrCast(&(dropped.paths.*[0]))));
            has_file = true;
        }

        // Draw
        rl.BeginDrawing();
        rl.ClearBackground(rl.DARKGRAY);

        if (!has_file) {
            rl.DrawText("Drop file here", 200, 380, 20, rl.LIGHTGRAY);
            continue;
        }

        rl.DrawText(@as([*c]const u8, &filepath), 200, 380, 20, rl.LIGHTGRAY);
        rl.EndDrawing();
    }
}

It compiles but the window is not responsive. This is my first time using C libraries from Zig so I'm unsure what to do.

weak furnace
#

You are trying to draw text from filepath buffer that will initially be undefined

#

I think you meant to put it in else statement for if above

#

Ahh, i see the continue now in the if(!has_file) body. That's a problem too. Because of it, you never call EndDrawing, so the window appears unresponsive

#

I imagine that every BeginDrawing call must be paired with EndDrawing once