Skip to content

Commit

Permalink
Use nice zig bindings for raylib
Browse files Browse the repository at this point in the history
  • Loading branch information
adamws committed Oct 11, 2024
1 parent ef2459f commit e0cdbe7
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 5,848 deletions.
28 changes: 12 additions & 16 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,27 @@ pub fn build(b: *std.Build) void {

exe.linkLibC();

//const system_libs = [_][]const u8{ "SDL2", "SDL2_image", "X11", "Xi" };
const system_libs = [_][]const u8{ "raylib", "X11", "Xi" };
const system_libs = [_][]const u8{ "X11", "Xi" };
for (system_libs) |lib| {
exe.linkSystemLibrary(lib);
}

const clap = b.dependency("clap", .{});
exe.root_module.addImport("clap", clap.module("clap"));

//const raygui_dep = b.dependency("raygui", .{
// .target = target,
// .optimize = optimize,
//});
//exe.addIncludePath(raygui_dep.path(""));

exe.addCSourceFile(.{
.file = .{
.cwd_relative = "vendor/raygui/raygui_impl.c"
},
.flags = CFlags,
});
exe.addIncludePath(.{
.cwd_relative = "vendor/raygui/",
const raylib_dep = b.dependency("raylib-zig", .{
.target = target,
.optimize = optimize,
});

const raylib = raylib_dep.module("raylib");
const raygui = raylib_dep.module("raygui");
const raylib_artifact = raylib_dep.artifact("raylib");

exe.linkLibrary(raylib_artifact);
exe.root_module.addImport("raylib", raylib);
exe.root_module.addImport("raygui", raygui);

b.installArtifact(exe);

// This *creates* a Run step in the build graph, to be executed when another
Expand Down
5 changes: 3 additions & 2 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
.url = "https://github.com/Hejsil/zig-clap/archive/refs/tags/0.9.1.tar.gz",
.hash = "122062d301a203d003547b414237229b09a7980095061697349f8bef41be9c30266b",
},
.raygui = .{
.path = "vendor/raygui",
.@"raylib-zig" = .{
.url = "https://github.com/Not-Nik/raylib-zig/archive/devel.tar.gz",
.hash = "12206dbd1510ce7e5270c725f980ca3a5ff3db47adeeda8d51ebe8527d6b27dd60a8",
},
},

Expand Down
50 changes: 24 additions & 26 deletions src/main.zig
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
const clap = @import("clap");
const raylib = @import("raylib");
const rgui = @import("raygui");
const math = @import("math.zig");
const std = @import("std");
const kle = @import("kle.zig");
const raylib = @cImport({
@cInclude("raylib.h");
@cInclude("raygui.h");
});
const x11 = @cImport({
@cInclude("X11/Xlib.h");
@cInclude("X11/extensions/XInput2.h");
Expand Down Expand Up @@ -201,28 +199,28 @@ pub fn main() !void {
const thread = try std.Thread.spawn(.{}, x11Listener, .{});
_ = thread;

raylib.SetConfigFlags(raylib.FLAG_VSYNC_HINT | raylib.FLAG_MSAA_4X_HINT | raylib.FLAG_WINDOW_HIGHDPI);
raylib.InitWindow(width, height, "klawa");
defer raylib.CloseWindow();
raylib.setConfigFlags(.{ .msaa_4x_hint = true, .vsync_hint = true, .window_highdpi = true });
raylib.initWindow(width, height, "klawa");
defer raylib.closeWindow();

// TODO: make this optional/configurable:
raylib.SetWindowState(raylib.FLAG_WINDOW_UNDECORATED);
raylib.setWindowState(.{ .window_undecorated = true });

raylib.SetExitKey(raylib.KEY_NULL);
raylib.setExitKey(raylib.KeyboardKey.key_null);

const keycaps = @embedFile("keycaps.png");
const keycaps_image = raylib.LoadImageFromMemory(".png", keycaps, keycaps.len);
const keycap_texture = raylib.LoadTextureFromImage(keycaps_image);
defer raylib.UnloadTexture(keycap_texture);
const keycaps_image = raylib.loadImageFromMemory(".png", keycaps);
const keycap_texture = raylib.loadTextureFromImage(keycaps_image);
defer raylib.unloadTexture(keycap_texture);

raylib.SetTextureFilter(keycap_texture, raylib.TEXTURE_FILTER_BILINEAR);
raylib.setTextureFilter(keycap_texture, raylib.TextureFilter.texture_filter_bilinear);

// texture created, image no longer needed
raylib.UnloadImage(keycaps_image);
raylib.unloadImage(keycaps_image);

const font = raylib.LoadFont("/usr/share/fonts/TTF/DejaVuSans.ttf");
const font = raylib.loadFont("/usr/share/fonts/TTF/DejaVuSans.ttf");

raylib.SetTargetFPS(60);
raylib.setTargetFPS(60);

var exit_window = false;

Expand All @@ -232,38 +230,38 @@ pub fn main() !void {
var start_angle: f32 = 0.0;

while (!exit_window) {
if (raylib.WindowShouldClose()) {
if (raylib.windowShouldClose()) {
exit_window = true;
}

if (raylib.IsMouseButtonPressed(raylib.MOUSE_BUTTON_RIGHT)) {
if (raylib.isMouseButtonPressed(raylib.MouseButton.mouse_button_right)) {
std.debug.print("Toggle settings\n", .{});
show_gui = !show_gui;
}

raylib.BeginDrawing();
raylib.ClearBackground(raylib.RAYWHITE);
raylib.beginDrawing();
raylib.clearBackground(raylib.Color.white);

const rot = raylib.Vector2{ .x = 0, .y = 0 };

for (key_states) |k| {
raylib.DrawTexturePro(keycap_texture, k.src, k.dst, rot, k.angle, raylib.WHITE);
raylib.drawTexturePro(keycap_texture, k.src, k.dst, rot, k.angle, raylib.Color.white);

if (k.pressed) {
raylib.DrawRectanglePro(k.dst, rot, k.angle, raylib.Color{.r = 255, .g = 0, .b = 0, .a = 128});
raylib.drawRectanglePro(k.dst, rot, k.angle, raylib.Color{.r = 255, .g = 0, .b = 0, .a = 128});
}
}

if (show_gui) {
_ = raylib.GuiSliderBar(.{ .x = 600, .y = 40, .width = 120, .height = 20 }, "StartAngle", null, &start_angle, -450, 450);
raylib.DrawTextEx(font, "Congrats! You created your first window!", .{ .x =190, .y = 200 }, 32, 0,raylib.RED);
_ = rgui.guiSliderBar(.{ .x = 600, .y = 40, .width = 120, .height = 20 }, "StartAngle", "", &start_angle, -450, 450);
raylib.drawTextEx(font, "Congrats! You created your first window!", .{ .x =190, .y = 200 }, 32, 0,raylib.Color.red);

if (1 == raylib.GuiButton(.{.x = @floatFromInt(width - 64), .y = 32, .width = 32, .height = 32}, "#113#")) {
if (1 == rgui.guiButton(.{.x = @floatFromInt(width - 64), .y = 32, .width = 32, .height = 32}, "#113#")) {
exit_window = true;
}
}

raylib.EndDrawing();
raylib.endDrawing();
}

std.debug.print("Exit\n", .{});
Expand Down
Loading

0 comments on commit e0cdbe7

Please sign in to comment.