From 8b46f46cf8d7f61877330963203290199913f18a Mon Sep 17 00:00:00 2001 From: iddev5 Date: Thu, 12 May 2022 12:54:01 +0530 Subject: [PATCH] mach: Create binding methods for all glfw methods in use and update examples --- examples/advanced-gen-texture-light/main.zig | 10 +++++----- examples/fractal-cube/main.zig | 4 ++-- examples/gkurve/main.zig | 10 ++++------ examples/instanced-cube/main.zig | 4 ++-- examples/rotating-cube/main.zig | 4 ++-- examples/textured-cube/main.zig | 10 +++++----- examples/two-cubes/main.zig | 4 ++-- src/Engine.zig | 13 +++++++++++++ src/main.zig | 1 + src/native.zig | 17 +++++++++++++++++ src/structs.zig | 9 +++++++++ 11 files changed, 62 insertions(+), 24 deletions(-) create mode 100644 src/structs.zig diff --git a/examples/advanced-gen-texture-light/main.zig b/examples/advanced-gen-texture-light/main.zig index dc5cd91e..b4baa786 100755 --- a/examples/advanced-gen-texture-light/main.zig +++ b/examples/advanced-gen-texture-light/main.zig @@ -23,7 +23,7 @@ cube: Cube, camera: Camera, light: Light, depth: Texture, -depth_size: glfw.Window.Size, +depth_size: mach.Size, keys: u8 = 0, const Dir = struct { @@ -48,12 +48,12 @@ pub fn init(app: *App, engine: *mach.Engine) !void { // } // } // }.callback); - try engine.core.internal.window.setSizeLimits(.{ .width = 20, .height = 20 }, .{ .width = null, .height = null }); + try engine.core.setSizeLimits(.{ .width = 20, .height = 20 }, .{ .width = null, .height = null }); const eye = vec3(5.0, 7.0, 5.0); const target = vec3(0.0, 0.0, 0.0); - const size = try engine.core.internal.window.getFramebufferSize(); + const size = try engine.core.getFramebufferSize(); const aspect_ratio = @intToFloat(f32, size.width) / @intToFloat(f32, size.height); app.queue = engine.gpu_driver.device.getQueue(); @@ -68,7 +68,7 @@ pub fn deinit(_: *App, _: *mach.Engine) void {} pub fn update(app: *App, engine: *mach.Engine) !bool { // If window is resized, recreate depth buffer otherwise we cannot use it. - const size = engine.core.internal.window.getFramebufferSize() catch unreachable; // TODO: return type inference can't handle this + const size = engine.core.getFramebufferSize() catch unreachable; // TODO: return type inference can't handle this if (size.width != app.depth_size.width or size.height != app.depth_size.height) { app.depth = Texture.depth(engine.gpu_driver.device, size.width, size.height); app.depth_size = size; @@ -874,7 +874,7 @@ const Instance = struct { fn keyCallback(app: *App, engine: *mach.Engine, key: mach.Key, action: mach.Action) void { if (action == .press) { switch (key) { - .q, .escape, .space => engine.core.internal.window.setShouldClose(true), + .q, .escape, .space => engine.core.setShouldClose(true), .w, .up => { app.keys |= Dir.up; }, diff --git a/examples/fractal-cube/main.zig b/examples/fractal-cube/main.zig index 277c5b4a..05ef551f 100755 --- a/examples/fractal-cube/main.zig +++ b/examples/fractal-cube/main.zig @@ -44,13 +44,13 @@ pub fn init(app: *App, engine: *mach.Engine) !void { fn callback(_: *App, eng: *mach.Engine, key: mach.Key, action: mach.Action) void { if (action == .press) { switch (key) { - .space => eng.core.internal.window.setShouldClose(true), + .space => eng.core.setShouldClose(true), else => {}, } } } }.callback); - try engine.core.internal.window.setSizeLimits(.{ .width = 20, .height = 20 }, .{ .width = null, .height = null }); + try engine.core.setSizeLimits(.{ .width = 20, .height = 20 }, .{ .width = null, .height = null }); const vs_module = engine.gpu_driver.device.createShaderModule(&.{ .label = "my vertex shader", diff --git a/examples/gkurve/main.zig b/examples/gkurve/main.zig index 074aace9..3d5fa875 100644 --- a/examples/gkurve/main.zig +++ b/examples/gkurve/main.zig @@ -50,19 +50,17 @@ frag_uniform_buffer: gpu.Buffer, bind_group: gpu.BindGroup, pub fn init(app: *App, engine: *mach.Engine) !void { - engine.core.internal.window.setKeyCallback(struct { - fn callback(window: glfw.Window, key: glfw.Key, scancode: i32, action: glfw.Action, mods: glfw.Mods) void { - _ = scancode; - _ = mods; + engine.core.setKeyCallback(struct { + fn callback(_: *App, eng: *mach.Engine, key: mach.Key, action: mach.Action) void { if (action == .press) { switch (key) { - .space => window.setShouldClose(true), + .space => eng.core.setShouldClose(true), else => {}, } } } }.callback); - try engine.core.internal.window.setSizeLimits(.{ .width = 20, .height = 20 }, .{ .width = null, .height = null }); + try engine.core.setSizeLimits(.{ .width = 20, .height = 20 }, .{ .width = null, .height = null }); const vs_module = engine.gpu_driver.device.createShaderModule(&.{ .label = "my vertex shader", diff --git a/examples/instanced-cube/main.zig b/examples/instanced-cube/main.zig index afdc7964..47aac605 100755 --- a/examples/instanced-cube/main.zig +++ b/examples/instanced-cube/main.zig @@ -27,13 +27,13 @@ pub fn init(app: *App, engine: *mach.Engine) !void { fn callback(_: *App, eng: *mach.Engine, key: mach.Key, action: mach.Action) void { if (action == .press) { switch (key) { - .space => eng.core.internal.window.setShouldClose(true), + .space => eng.core.setShouldClose(true), else => {}, } } } }.callback); - try engine.core.internal.window.setSizeLimits(.{ .width = 20, .height = 20 }, .{ .width = null, .height = null }); + try engine.core.setSizeLimits(.{ .width = 20, .height = 20 }, .{ .width = null, .height = null }); const vs_module = engine.gpu_driver.device.createShaderModule(&.{ .label = "my vertex shader", diff --git a/examples/rotating-cube/main.zig b/examples/rotating-cube/main.zig index c007b56a..82abb27a 100755 --- a/examples/rotating-cube/main.zig +++ b/examples/rotating-cube/main.zig @@ -28,13 +28,13 @@ pub fn init(app: *App, engine: *mach.Engine) !void { fn callback(_: *App, eng: *mach.Engine, key: mach.Key, action: mach.Action) void { if (action == .press) { switch (key) { - .space => eng.core.internal.window.setShouldClose(true), + .space => eng.core.setShouldClose(true), else => {}, } } } }.callback); - try engine.core.internal.window.setSizeLimits(.{ .width = 20, .height = 20 }, .{ .width = null, .height = null }); + try engine.core.setSizeLimits(.{ .width = 20, .height = 20 }, .{ .width = null, .height = null }); const vs_module = engine.gpu_driver.device.createShaderModule(&.{ .label = "my vertex shader", diff --git a/examples/textured-cube/main.zig b/examples/textured-cube/main.zig index afb32264..d280e6db 100644 --- a/examples/textured-cube/main.zig +++ b/examples/textured-cube/main.zig @@ -19,7 +19,7 @@ vertex_buffer: gpu.Buffer, uniform_buffer: gpu.Buffer, bind_group: gpu.BindGroup, depth_texture: gpu.Texture, -depth_size: glfw.Window.Size, +depth_size: mach.Size, const App = @This(); @@ -30,13 +30,13 @@ pub fn init(app: *App, engine: *mach.Engine) !void { fn callback(_: *App, eng: *mach.Engine, key: mach.Key, action: mach.Action) void { if (action == .press) { switch (key) { - .space => eng.core.internal.window.setShouldClose(true), + .space => eng.core.setShouldClose(true), else => {}, } } } }.callback); - try engine.core.internal.window.setSizeLimits(.{ .width = 20, .height = 20 }, .{ .width = null, .height = null }); + try engine.core.setSizeLimits(.{ .width = 20, .height = 20 }, .{ .width = null, .height = null }); const vs_module = engine.gpu_driver.device.createShaderModule(&.{ .label = "my vertex shader", @@ -165,7 +165,7 @@ pub fn init(app: *App, engine: *mach.Engine) !void { }, ); - const size = try engine.core.internal.window.getFramebufferSize(); + const size = try engine.core.getFramebufferSize(); const depth_texture = engine.gpu_driver.device.createTexture(&gpu.Texture.Descriptor{ .size = gpu.Extent3D{ .width = size.width, @@ -198,7 +198,7 @@ pub fn deinit(app: *App, _: *mach.Engine) void { pub fn update(app: *App, engine: *mach.Engine) !bool { // If window is resized, recreate depth buffer otherwise we cannot use it. - const size = engine.core.internal.window.getFramebufferSize() catch unreachable; // TODO: return type inference can't handle this + const size = engine.core.getFramebufferSize() catch unreachable; // TODO: return type inference can't handle this if (size.width != app.depth_size.width or size.height != app.depth_size.height) { app.depth_texture = engine.gpu_driver.device.createTexture(&gpu.Texture.Descriptor{ .size = gpu.Extent3D{ diff --git a/examples/two-cubes/main.zig b/examples/two-cubes/main.zig index 71c2065c..a4b9307f 100755 --- a/examples/two-cubes/main.zig +++ b/examples/two-cubes/main.zig @@ -28,13 +28,13 @@ pub fn init(app: *App, engine: *mach.Engine) !void { fn callback(_: *App, eng: *mach.Engine, key: mach.Key, action: mach.Action) void { if (action == .press) { switch (key) { - .space => eng.core.internal.window.setShouldClose(true), + .space => eng.core.setShouldClose(true), else => {}, } } } }.callback); - try engine.core.internal.window.setSizeLimits(.{ .width = 20, .height = 20 }, .{ .width = null, .height = null }); + try engine.core.setSizeLimits(.{ .width = 20, .height = 20 }, .{ .width = null, .height = null }); const vs_module = engine.gpu_driver.device.createShaderModule(&.{ .label = "my vertex shader", diff --git a/src/Engine.zig b/src/Engine.zig index 1613bcb2..2e75304b 100644 --- a/src/Engine.zig +++ b/src/Engine.zig @@ -3,6 +3,7 @@ const Allocator = std.mem.Allocator; const glfw = @import("glfw"); const gpu = @import("gpu"); const App = @import("app"); +const structs = @import("structs.zig"); const enums = @import("enums.zig"); pub const VSyncMode = enum { @@ -71,6 +72,18 @@ timer: std.time.Timer, pub const Core = struct { internal: GetCoreInternalType(), + pub fn setShouldClose(core: *Core, value: bool) void { + core.internal.setShouldClose(value); + } + + pub fn getFramebufferSize(core: *Core) !structs.Size { + return core.internal.getFramebufferSize(); + } + + pub fn setSizeLimits(core: *Core, min: structs.SizeOptional, max: structs.SizeOptional) !void { + return core.internal.setSizeLimits(min, max); + } + pub fn setKeyCallback(core: *Core, comptime cb: fn (app: *App, engine: *Engine, key: enums.Key, action: enums.Action) void) void { core.internal.setKeyCallback(cb); } diff --git a/src/main.zig b/src/main.zig index b546f7ef..9afeded3 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,2 +1,3 @@ +pub usingnamespace @import("structs.zig"); pub usingnamespace @import("enums.zig"); pub const Engine = @import("Engine.zig"); diff --git a/src/native.zig b/src/native.zig index d972da71..b2a66124 100644 --- a/src/native.zig +++ b/src/native.zig @@ -3,6 +3,7 @@ const glfw = @import("glfw"); const gpu = @import("gpu"); const App = @import("app"); const Engine = @import("Engine.zig"); +const structs = @import("structs.zig"); const enums = @import("enums.zig"); const util = @import("util.zig"); const c = @import("c.zig").c; @@ -50,6 +51,22 @@ pub const CoreGlfw = struct { self.window.setUserPointer(&self.user_ptr); } + pub fn setShouldClose(self: *CoreGlfw, value: bool) void { + self.window.setShouldClose(value); + } + + pub fn getFramebufferSize(self: *CoreGlfw) !structs.Size { + const size = try self.window.getFramebufferSize(); + return @bitCast(structs.Size, size); + } + + pub fn setSizeLimits(self: *CoreGlfw, min: structs.SizeOptional, max: structs.SizeOptional) !void { + try self.window.setSizeLimits( + @bitCast(glfw.Window.SizeOptional, min), + @bitCast(glfw.Window.SizeOptional, max), + ); + } + pub fn setKeyCallback(self: *CoreGlfw, comptime cb: fn (app: *App, engine: *Engine, key: enums.Key, action: enums.Action) void) void { const callback = struct { fn callback(window: glfw.Window, key: glfw.Key, scancode: i32, action: glfw.Action, mods: glfw.Mods) void { diff --git a/src/structs.zig b/src/structs.zig new file mode 100644 index 00000000..e6eaa534 --- /dev/null +++ b/src/structs.zig @@ -0,0 +1,9 @@ +pub const Size = struct { + width: u32, + height: u32, +}; + +pub const SizeOptional = struct { + width: ?u32, + height: ?u32, +};