Files
raylib-speedruns/zig/main.zig
2026-03-11 20:46:40 +01:00

54 lines
1.7 KiB
Zig

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();
}