74 lines
1.8 KiB
Zig
74 lines
1.8 KiB
Zig
//! GLFW Adaptor
|
|
|
|
// todo restructure to handle multiple windows
|
|
|
|
const std = @import("std");
|
|
|
|
const nu = @import("../nu.zig");
|
|
|
|
pub const c = @cImport({
|
|
@cDefine("GLFW_INCLUDE_NONE", {});
|
|
@cInclude("GLFW/glfw3.h");
|
|
});
|
|
|
|
pub const Bus = @import("Bus.zig");
|
|
|
|
pub const Options = struct {
|
|
title: [*:0]const u8 = "Hello World",
|
|
width: u32 = 1280,
|
|
height: u32 = 720,
|
|
x11_class_name: [*:0]const u8 = "floating_window",
|
|
x11_instance_name: [*:0]const u8 = "floating_window",
|
|
};
|
|
|
|
var bus: Bus = undefined;
|
|
pub var handle: *c.GLFWwindow = undefined;
|
|
var unfocused_rate: f32 = 1.0 / 20.0;
|
|
|
|
pub fn init(alloc: std.mem.Allocator) !void {
|
|
if (c.glfwInit() != c.GLFW_TRUE)
|
|
return error.glfwInitFailed;
|
|
errdefer c.glfwTerminate();
|
|
|
|
bus = Bus.init(alloc);
|
|
errdefer bus.deinit();
|
|
|
|
c.glfwWindowHint(c.GLFW_CLIENT_API, c.GLFW_NO_API);
|
|
c.glfwWindowHintString(c.GLFW_X11_CLASS_NAME, nu.options.window.x11_class_name);
|
|
c.glfwWindowHintString(c.GLFW_X11_INSTANCE_NAME, nu.options.window.x11_instance_name);
|
|
|
|
handle = c.glfwCreateWindow(
|
|
@intCast(nu.options.window.width),
|
|
@intCast(nu.options.window.height),
|
|
nu.options.window.title,
|
|
null,
|
|
null,
|
|
) orelse
|
|
return error.glfWCreateWindowFailed;
|
|
errdefer c.glfwDestroyWindow(handle);
|
|
|
|
bus.connect(handle);
|
|
errdefer bus.disconnect(handle);
|
|
}
|
|
|
|
pub fn deinit() void {
|
|
bus.deinit();
|
|
c.glfwDestroyWindow(handle);
|
|
c.glfwTerminate();
|
|
}
|
|
|
|
pub fn next() ?[]Bus.Event {
|
|
bus.clear();
|
|
|
|
if (c.glfwWindowShouldClose(handle) == c.GLFW_TRUE)
|
|
return null;
|
|
|
|
if (c.glfwGetWindowAttrib(handle, c.GLFW_FOCUSED) == c.GLFW_TRUE) {
|
|
c.glfwPollEvents();
|
|
} else {
|
|
c.glfwWaitEventsTimeout(unfocused_rate);
|
|
}
|
|
|
|
return bus.events.items;
|
|
}
|