add dropped files example (#260)

* add dropped files example

* reformat build.zig
This commit is contained in:
leonardo-kr 2025-07-23 16:24:56 +02:00 committed by GitHub
parent 14f49a38dd
commit e8167c2e56
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 108 additions and 1 deletions

View File

@ -31,7 +31,7 @@ fn getRaylib(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.buil
.linux_display_backend = options.linux_display_backend,
.opengl_version = options.opengl_version,
.android_api_version = options.android_api_version,
.android_ndk = options.android_ndk
.android_ndk = options.android_ndk,
});
const raylib = raylib_dep.artifact("raylib");
@ -164,6 +164,11 @@ pub fn build(b: *std.Build) !void {
.path = "examples/core/3d_picking.zig",
.desc = "Shows picking in 3d mode",
},
.{
.name = "drop_files",
.path = "examples/core/drop_files.zig",
.desc = "Demonstrates how to implement a drop files functionality",
},
.{
.name = "window_flags",
.path = "examples/core/window_flags.zig",

View File

@ -0,0 +1,102 @@
// raylib-zig (c) Leonardo Kreienbuehl 2025
const rl = @import("raylib");
const std = @import("std");
const MAX_FILEPATH_RECORDED = 4096;
const MAX_FILEPATH_SIZE = 2048;
pub fn main() anyerror!void {
// Initialization
//--------------------------------------------------------------------------------------
const screenWidth = 800;
const screenHeight = 450;
rl.initWindow(screenWidth, screenHeight, "raylib-zig [core] example - drop files");
defer rl.closeWindow(); // Close window and OpenGL context
var filePathCounter: usize = 0;
var filePaths: [MAX_FILEPATH_RECORDED][MAX_FILEPATH_SIZE]u8 = std.mem.zeroes([MAX_FILEPATH_RECORDED][MAX_FILEPATH_SIZE]u8);
rl.setTargetFPS(60);
// Main game loop
while (!rl.windowShouldClose()) {
// Update
//----------------------------------------------------------------------------------
if (rl.isFileDropped()) {
const droppedFiles: rl.FilePathList = rl.loadDroppedFiles();
for (0..droppedFiles.count) |i| {
const offset: usize = @as(usize, @intCast(filePathCounter));
const droppedFilePathLength: usize = std.mem.len(droppedFiles.paths[i]);
if (filePathCounter < (MAX_FILEPATH_RECORDED - 1)) {
_ = rl.textCopy(
@ptrCast(@constCast(&filePaths[offset])),
droppedFiles.paths[i][0..droppedFilePathLength :0],
);
filePathCounter += 1;
}
}
rl.unloadDroppedFiles(droppedFiles);
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
rl.beginDrawing();
defer rl.endDrawing();
rl.clearBackground(rl.Color.ray_white);
if (filePathCounter == 0) {
rl.drawText(
"Drop your files to this window!",
100,
40,
20,
rl.Color.dark_gray,
);
} else {
rl.drawText(
"Dropped files:",
100,
40,
20,
rl.Color.dark_gray,
);
for (0..filePathCounter) |i| {
const castedI: i32 = @intCast(i);
if (@mod(i, 2) == 0) {
rl.drawRectangle(
0,
85 + 40 * castedI,
screenWidth,
40,
rl.fade(rl.Color.light_gray, 0.5),
);
} else {
rl.drawRectangle(
0,
85 + 40 * castedI,
screenWidth,
40,
rl.fade(rl.Color.light_gray, 0.3),
);
}
rl.drawText(
filePaths[i][0 .. MAX_FILEPATH_SIZE - 1 :0],
120,
100 + 40 * castedI,
10,
rl.Color.gray,
);
}
}
//----------------------------------------------------------------------------------
}
}