feat(zig): implement
This commit is contained in:
2
zig/Makefile
Normal file
2
zig/Makefile
Normal file
@@ -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
|
||||||
8
zig/README.md
Normal file
8
zig/README.md
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
# Raylib in Zig
|
||||||
|
|
||||||
|
## Quick start
|
||||||
|
|
||||||
|
```sh
|
||||||
|
$ make
|
||||||
|
$ ./raylib_zig
|
||||||
|
```
|
||||||
53
zig/main.zig
Normal file
53
zig/main.zig
Normal file
@@ -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();
|
||||||
|
}
|
||||||
BIN
zig/zig_logo.png
Normal file
BIN
zig/zig_logo.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 4.4 KiB |
Reference in New Issue
Block a user