Skip to content

antlilja/zig-window

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

56 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

zig-window

A work in progress zig window library

Currently only supports linux X11 and Windows.

Example:

const std = @import("std");
const zw = @import("zig-window");

fn handleEvent(_: ?*anyopaque, event: zw.Event) void {
    switch (event) {
        .Destroy => std.log.info("Window destroyed", .{}),
        .FocusIn => std.log.info("Focus in", .{}),
        .FocusOut => std.log.info("Focus out", .{}),
        .Resize => |size| {
            const width, const height = size;
            std.log.info("Resize: {}, {}", .{ width, height });
        },
        .KeyPress => |value| std.log.info("Key pressed: {}", .{value}),
        .KeyRelease => |value| std.log.info("Key released: {}", .{value}),
        .MouseScrollV => |value| std.log.info("Mouse scroll vertical: {}", .{value}),
        .MousePress => |value| std.log.info("{} pressed", .{value}),
        .MouseRelease => |value| std.log.info("{} released", .{value}),
        else => {},
    }
}

pub fn main() anyerror!void {
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    defer _ = gpa.deinit();

    const context = try zw.init(gpa.allocator());
    defer context.deinit();

    const window = try context.createWindow(.{
        .name = "Example window",
        .width = 1920,
        .height = 1080,
        .event_handler = .{
            .handle = null,
            .handle_event_fn = @ptrCast(&handleEvent),
        },
    });
    defer window.destroy();

    while (window.isOpen()) {
        context.pollEvents();
    }
}

Example with non resizable window querying monitors:

const std = @import("std");
const zw = @import("zig-window");

fn handleEvent(_: ?*anyopaque, event: zw.Event) void {
    switch (event) {
        .Destroy => std.log.info("Window destroyed", .{}),
        .FocusIn => std.log.info("Focus in", .{}),
        .FocusOut => std.log.info("Focus out", .{}),
        .Resize => |size| {
            const width, const height = size;
            std.log.info("Resize: {}, {}", .{ width, height });
        },
        .KeyPress => |value| std.log.info("Key pressed: {}", .{value}),
        .KeyRelease => |value| std.log.info("Key released: {}", .{value}),
        .MouseScrollV => |value| std.log.info("Mouse scroll vertical: {}", .{value}),
        .MousePress => |value| std.log.info("{} pressed", .{value}),
        .MouseRelease => |value| std.log.info("{} released", .{value}),
        else => {},
    }
}

pub fn main() !void {
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    defer _ = gpa.deinit();

    const context = try zw.init(gpa.allocator());
    defer context.deinit();

    const width, const height = blk: {
        const monitors = try context.getMonitors(gpa.allocator());
        defer gpa.allocator().free(monitors);
        for (monitors) |monitor| {
            if (monitor.is_primary) break :blk .{ monitor.width, monitor.height };
        }
        break :blk if (monitors.len != 0) .{ monitors[0].width, monitors[0].height } else .{ 1920, 1080 };
    };

    const window = try context.createWindow(.{
        .name = "Example window",
        .width = width,
        .height = height,
        .resizable = false,
        .event_handler = .{
            .handle = null,
            .handle_event_fn = @ptrCast(&handleEvent),
        },
    });
    defer window.destroy();

    while (window.isOpen()) {
        context.pollEvents();
    }
}

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages