diff --git a/zig/Makefile b/zig/Makefile new file mode 100644 index 0000000..4fba023 --- /dev/null +++ b/zig/Makefile @@ -0,0 +1,2 @@ +raylib_zig: main.zig + zig build-exe main.zig -I../_raylib-5.5_linux_amd64/include ../_raylib-5.5_linux_amd64/lib/libraylib.a -lm -femit-bin=raylib_zig diff --git a/zig/README.md b/zig/README.md new file mode 100644 index 0000000..32279f3 --- /dev/null +++ b/zig/README.md @@ -0,0 +1,8 @@ +# Raylib in Zig + +## Quick start + +```sh +$ make +$ ./raylib_zig +``` diff --git a/zig/main.zig b/zig/main.zig new file mode 100644 index 0000000..a699a3c --- /dev/null +++ b/zig/main.zig @@ -0,0 +1,53 @@ +const std = @import("std"); +const rl = @cImport({ + @cInclude("raylib.h"); +}); + +const WINDOW_WIDTH = 800; +const WINDOW_HEIGHT = 600; +const LOGO_HEIGHT = 64; +const LOGO_SPEED: f32 = 300; + +const BLACK = rl.Color{ .r = 0, .g = 0, .b = 0, .a = 255 }; +const WHITE = rl.Color{ .r = 255, .g = 255, .b = 255, .a = 255 }; + +pub fn main() void { + rl.InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Raylib in Zig"); + rl.SetTargetFPS(60); + rl.SetRandomSeed(@intCast(std.time.timestamp())); + + var logo_image = rl.LoadImage("./zig_logo.png"); + rl.ImageResize(&logo_image, @divTrunc(logo_image.width * LOGO_HEIGHT, logo_image.height), LOGO_HEIGHT); + const logo_texture = rl.LoadTextureFromImage(logo_image); + rl.UnloadImage(logo_image); + + var x: f32 = @floatFromInt(rl.GetRandomValue(0, WINDOW_WIDTH - logo_texture.width)); + var y: f32 = @floatFromInt(rl.GetRandomValue(0, WINDOW_HEIGHT - logo_texture.height)); + + var dx: f32 = if (rl.GetRandomValue(0, 1) == 1) -LOGO_SPEED else LOGO_SPEED; + var dy: f32 = if (rl.GetRandomValue(0, 1) == 1) -LOGO_SPEED else LOGO_SPEED; + + while (!rl.WindowShouldClose()) { + const delta_time = rl.GetFrameTime(); + + x += dx * delta_time; + y += dy * delta_time; + + if (x < 0 or x + @as(f32, @floatFromInt(logo_texture.width)) >= WINDOW_WIDTH - 1) { + dx *= -1; + x += dx * delta_time; + } + if (y < 0 or y + @as(f32, @floatFromInt(logo_texture.height)) >= WINDOW_HEIGHT - 1) { + dy *= -1; + y += dy * delta_time; + } + + rl.BeginDrawing(); + rl.ClearBackground(BLACK); + rl.DrawTextureV(logo_texture, rl.Vector2{ .x = x, .y = y }, WHITE); + rl.EndDrawing(); + } + + rl.UnloadTexture(logo_texture); + rl.CloseWindow(); +} diff --git a/zig/zig_logo.png b/zig/zig_logo.png new file mode 100644 index 0000000..c5eb152 Binary files /dev/null and b/zig/zig_logo.png differ