refactor stub
This commit is contained in:
77
src/nu/Window.zig
Normal file
77
src/nu/Window.zig
Normal file
@@ -0,0 +1,77 @@
|
||||
//! GLFW Adaptor
|
||||
|
||||
const std = @import("std");
|
||||
|
||||
pub const c = @cImport({
|
||||
@cDefine("GLFW_INCLUDE_NONE", {});
|
||||
@cInclude("GLFW/glfw3.h");
|
||||
});
|
||||
|
||||
const Self = @This();
|
||||
|
||||
pub const Bus = @import("Bus.zig");
|
||||
|
||||
pub const Options = struct {
|
||||
title: [*:0]const u8,
|
||||
width: u32 = 1280,
|
||||
height: u32 = 720,
|
||||
x11_class_name: [*:0]const u8 = "floating_window",
|
||||
x11_instance_name: [*:0]const u8 = "floating_window",
|
||||
};
|
||||
|
||||
alloc: std.mem.Allocator,
|
||||
bus: *Bus,
|
||||
handle: *c.GLFWwindow,
|
||||
|
||||
pub fn init(alloc: std.mem.Allocator, options: Options) !Self {
|
||||
if (c.glfwInit() != c.GLFW_TRUE)
|
||||
return error.glfwInitFailed;
|
||||
errdefer c.glfwTerminate();
|
||||
|
||||
const bus: *Bus = try alloc.create(Bus);
|
||||
errdefer alloc.destroy(bus);
|
||||
bus.* = Bus.init(alloc);
|
||||
errdefer bus.deinit();
|
||||
|
||||
c.glfwWindowHint(c.GLFW_CLIENT_API, c.GLFW_NO_API);
|
||||
c.glfwWindowHintString(c.GLFW_X11_CLASS_NAME, options.x11_class_name);
|
||||
c.glfwWindowHintString(c.GLFW_X11_INSTANCE_NAME, options.x11_instance_name);
|
||||
|
||||
const handle: *c.GLFWwindow = c.glfwCreateWindow(
|
||||
@intCast(options.width),
|
||||
@intCast(options.height),
|
||||
options.title,
|
||||
null,
|
||||
null,
|
||||
) orelse
|
||||
return error.glfWCreateWindowFailed;
|
||||
errdefer c.glfwDestroyWindow(handle);
|
||||
|
||||
bus.connect(handle);
|
||||
errdefer bus.disconnect(handle);
|
||||
|
||||
return .{
|
||||
.alloc = alloc,
|
||||
.bus = bus,
|
||||
.handle = handle,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn deinit(self: *Self) void {
|
||||
self.bus.deinit();
|
||||
self.alloc.destroy(self.bus);
|
||||
c.glfwDestroyWindow(self.handle);
|
||||
c.glfwTerminate();
|
||||
}
|
||||
|
||||
pub fn next(self: *Self) ?[]Bus.Event {
|
||||
self.bus.clear();
|
||||
|
||||
if (c.glfwWindowShouldClose(self.handle) == c.GLFW_TRUE)
|
||||
return null;
|
||||
|
||||
// c.glfwPollEvents();
|
||||
c.glfwWaitEvents();
|
||||
|
||||
return self.bus.events.items;
|
||||
}
|
Reference in New Issue
Block a user