Skip to content

Instantly share code, notes, and snippets.

@peterhellberg
Created May 25, 2024 13:28
Show Gist options
  • Save peterhellberg/96175034f9f01defe7b989cced2236b1 to your computer and use it in GitHub Desktop.
Save peterhellberg/96175034f9f01defe7b989cced2236b1 to your computer and use it in GitHub Desktop.
Munching squares example for Fenster
#include "fenster.h"
#define W 1920
#define H 1080
static void fenster_rect(struct fenster *f, int x, int y, int w, int h, uint32_t c) {
for (int row = 0; row < h; row++) {
for (int col = 0; col < w; col++) {
fenster_pixel(f, x + col, y + row) = c;
}
}
}
static int run() {
uint32_t buf[W * H];
struct fenster f = {
.title = "Munching squares",
.width = W,
.height = H,
.buf = buf,
};
fenster_open(&f);
uint32_t t = 0;
int64_t now = fenster_time();
while (fenster_loop(&f) == 0) {
t++;
if (f.keys[27]) {
break;
}
for (int x = 0; x < W; x++) {
for (int y = 0; y < H; y++) {
fenster_pixel(&f, x, y) = x ^ y ^ t + 0x220000;
}
}
if (f.x > 5 && f.y > 5 && f.x < f.width - 5 && f.y < f.height - 5) {
fenster_rect(&f, f.x - 4, f.y - 4, 8, 8, f.mouse ? 0xffffff : 0x00ff00);
}
int64_t time = fenster_time();
if (time - now < 1000 / 60) {
fenster_sleep(time - now);
}
now = time;
}
fenster_close(&f);
return 0;
}
int main() {
return run();
}
@peterhellberg
Copy link
Author

Updated build.zig for use with fenster.h under Linux and Zig 0.13.0-dev

const std = @import("std");

pub fn build(b: *std.Build) void {
    const target = b.standardTargetOptions(.{});
    const optimize = b.standardOptimizeOption(.{});

    const exe = b.addExecutable(.{
        .name = "minimal-zig",
        .root_source_file = b.path("src/main.zig"),
        .target = target,
        .optimize = optimize,
    });

    exe.addIncludePath(b.path("src"));

    exe.addCSourceFiles(.{
        .root = b.path("src"),
        .files = &.{"fenster.c"},
    });

    exe.linkLibC();

    exe.linkSystemLibrary("X11");

    b.installArtifact(exe);

    const run_cmd = b.addRunArtifact(exe);

    run_cmd.step.dependOn(b.getInstallStep());

    if (b.args) |args| {
        run_cmd.addArgs(args);
    }

    const run_step = b.step("run", "Run the app");
    run_step.dependOn(&run_cmd.step);
}

src/fenster.c

#include "../include/fenster.h"

src/fenster.h

#define FENSTER_HEADER
#include "../include/fenster.h"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment