Backlog/engine/platform/src/platform.zig

116 lines
3.1 KiB
Zig

const std = @import("std");
const core = @import("core");
const test_vert = @import("test.vert");
pub const nfd = @import("nfd");
pub const file_dialogue = @import("file_dialogue.zig");
const watcher = @import("watcher");
// controls glfw and general windowing
// graphics depends on this one
pub extern fn setupMiniDump(takeFullDump: bool) callconv(.c) void;
pub extern fn generateAnException() callconv(.c) void;
pub extern fn takeDump() callconv(.c) void;
pub const windowing = @import("windowing.zig");
pub const Module: core.ModuleDescription = .{
.name = "platform",
.enabledByDefault = true,
};
var gPlatformInstance: *windowing.PlatformInstance = undefined;
pub fn context() *windowing.PlatformInstance {
return gPlatformInstance;
}
pub fn setupFromModule() void {
gPlatformInstance = core.getEngineObject(windowing.PlatformInstance).?;
}
pub fn getCursorPosition() core.Vector2f {
return gPlatformInstance.getCursorPosition();
}
pub fn setCursorVisible(visible: bool) void {
return gPlatformInstance.setCursorVisible(visible);
}
pub fn setMousePosition(pos: core.Vector2f) void {
gPlatformInstance.setMousePosition(pos);
}
pub fn setMouseRelativeMode(relativeMode: bool) void {
gPlatformInstance.setMouseRelativeMode(relativeMode);
}
pub fn setImguiVisible(visible: bool) void {
gPlatformInstance.imguiVisible = visible;
}
pub fn getMouseVisible() bool {
gPlatformInstance.isCursorEnabled();
}
pub fn watchPath(path: []const u8) void {
watchPathPacker(core.fs(), path) catch {
core.engine_log("unable to set up watch on path {s}", .{path});
};
}
fn watchPathPacker(self: *core.PackerFS, path: []const u8) !void {
// std.debug.print("self = {x}\n", .{@intFromPtr(self)});
std.fs.cwd().access(path, .{}) catch return;
// check the path exists before attempting to watch
const p = self.stringArena.allocator().dupeZ(u8, path) catch return;
const watch = watcher.createWatchPoint(
p,
core.PackerFS.watchCallback,
self,
) orelse {
return error.UnableToWatch;
};
_ = watch;
}
pub fn watchModules() void {
watchPath("zig-out/modules/");
}
pub fn start_module(spec: *core.SpecVariantMap, args: anytype, allocator: std.mem.Allocator) !void {
_ = args;
_ = spec;
_ = allocator;
if (core.isUtility()) {
return;
}
core.getModuleLoader().watchInitializeFn = watchModules;
const parameters = windowing.PlatformParams.init();
gPlatformInstance = try core.createObject(windowing.PlatformInstance, .{}); //try allocator.create(windowing.PlatformInstance);
try gPlatformInstance.setParams(parameters);
// gPlatformInstance.* = try windowing.PlatformInstance.init(allocator, parameters);
try gPlatformInstance.setupWindow();
try gPlatformInstance.registerFuncs();
}
pub fn getInstance() *windowing.PlatformInstance {
return gPlatformInstance;
}
pub fn shutdown_module(allocator: std.mem.Allocator) void {
if (core.isUtility()) {
return;
}
_ = allocator;
}