From 50833263583a20ed3e99728aa7c7336d1fd74be2 Mon Sep 17 00:00:00 2001 From: falsycat Date: Tue, 14 May 2024 00:48:50 +0900 Subject: [PATCH] create new project --- .gitignore | 2 + .gitmodules | 3 ++ build.zig | 37 ++++++++++++++++ build.zig.zon | 38 ++++++++++++++++ main.zig | 99 ++++++++++++++++++++++++++++++++++++++++++ thirdparty/zig-gamedev | 1 + 6 files changed, 180 insertions(+) create mode 100644 .gitignore create mode 100644 .gitmodules create mode 100644 build.zig create mode 100644 build.zig.zon create mode 100644 main.zig create mode 160000 thirdparty/zig-gamedev diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d864d9e --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/zig-cache/ +/zig-out/ diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..bc20ed6 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "thirdparty/zig-gamedev"] + path = thirdparty/zig-gamedev + url = https://github.com/zig-gamedev/zig-gamedev/ diff --git a/build.zig b/build.zig new file mode 100644 index 0000000..0d4e4bd --- /dev/null +++ b/build.zig @@ -0,0 +1,37 @@ +const std = @import("std"); + +pub fn build(b: *std.Build) void { + const target = b.standardTargetOptions(.{}); + const optimize = b.standardOptimizeOption(.{}); + + const hypatia = b.addExecutable(.{ + .name = "hypatia", + + .root_source_file = .{ + .path = "main.zig", + }, + .target = target, + .optimize = optimize, + }); + + const zglfw = b.dependency("zglfw", .{}); + hypatia.root_module.addImport("zglfw", zglfw.module("root")); + hypatia.linkLibrary(zglfw.artifact("glfw")); + + const zgpu = b.dependency("zgpu", .{}); + hypatia.root_module.addImport("zgpu", zgpu.module("root")); + hypatia.linkLibrary(zgpu.artifact("zdawn")); + + const zgui = b.dependency("zgui", .{ + .shared = false, + .with_implot = false, + .backend = .glfw_wgpu, + }); + hypatia.root_module.addImport("zgui", zgui.module("root")); + hypatia.linkLibrary(zgui.artifact("imgui")); + + @import("system_sdk").addLibraryPathsTo(hypatia); + @import("zgpu").addLibraryPathsTo(hypatia); + + b.installArtifact(hypatia); +} diff --git a/build.zig.zon b/build.zig.zon new file mode 100644 index 0000000..8c562ac --- /dev/null +++ b/build.zig.zon @@ -0,0 +1,38 @@ +.{ + .name = "hypatia", + .version = "0.0.1", + .paths = .{ + "build.zig", + "build.zig.zon", + "main.zig", + "thirdparty", + }, + + .dependencies = .{ + .system_sdk = .{ .path = "thirdparty/zig-gamedev/libs/system-sdk", }, + .zglfw = .{ .path = "thirdparty/zig-gamedev/libs/zglfw", }, + .zgpu = .{ .path = "thirdparty/zig-gamedev/libs/zgpu", }, + .zgui = .{ .path = "thirdparty/zig-gamedev/libs/zgui", }, + + .dawn_x86_64_windows_gnu = .{ + .url = "https://github.com/michal-z/webgpu_dawn-x86_64-windows-gnu/archive/d3a68014e6b6b53fd330a0ccba99e4dcfffddae5.tar.gz", + .hash = "1220f9448cde02ef3cd51bde2e0850d4489daa0541571d748154e89c6eb46c76a267", + }, + .dawn_x86_64_linux_gnu = .{ + .url = "https://github.com/michal-z/webgpu_dawn-x86_64-linux-gnu/archive/7d70db023bf254546024629cbec5ee6113e12a42.tar.gz", + .hash = "12204a3519efd49ea2d7cf63b544492a3a771d37eda320f86380813376801e4cfa73", + }, + .dawn_aarch64_linux_gnu = .{ + .url = "https://github.com/michal-z/webgpu_dawn-aarch64-linux-gnu/archive/c1f55e740a62f6942ff046e709ecd509a005dbeb.tar.gz", + .hash = "12205cd13f6849f94ef7688ee88c6b74c7918a5dfb514f8a403fcc2929a0aa342627", + }, + .dawn_aarch64_macos = .{ + .url = "https://github.com/michal-z/webgpu_dawn-aarch64-macos/archive/d2360cdfff0cf4a780cb77aa47c57aca03cc6dfe.tar.gz", + .hash = "12201fe677e9c7cfb8984a36446b329d5af23d03dc1e4f79a853399529e523a007fa", + }, + .dawn_x86_64_macos = .{ + .url = "https://github.com/michal-z/webgpu_dawn-x86_64-macos/archive/901716b10b31ce3e0d3fe479326b41e91d59c661.tar.gz", + .hash = "1220b1f02f2f7edd98a078c64e3100907d90311d94880a3cc5927e1ac009d002667a", + }, + }, +} diff --git a/main.zig b/main.zig new file mode 100644 index 0000000..23f400c --- /dev/null +++ b/main.zig @@ -0,0 +1,99 @@ +const std = @import("std"); + +const zglfw = @import("zglfw"); +const zgpu = @import("zgpu"); +const wgpu = zgpu.wgpu; +const zgui = @import("zgui"); + +pub fn main() !void { + try zglfw.init(); + defer zglfw.terminate(); + + zglfw.windowHintTyped(.client_api, .no_api); + + const window = try zglfw.Window.create(800, 500, "HELLOWORLD", null); + defer window.destroy(); + window.setSizeLimits(400, 400, -1, -1); + + var gpa_state = std.heap.GeneralPurposeAllocator(.{}){}; + defer _ = gpa_state.deinit(); + const gpa = gpa_state.allocator(); + + const gctx = try zgpu.GraphicsContext.create( + gpa, + .{ + .window = window, + .fn_getTime = @ptrCast(&zglfw.getTime), + .fn_getFramebufferSize = @ptrCast(&zglfw.Window.getFramebufferSize), + .fn_getWin32Window = @ptrCast(&zglfw.getWin32Window), + .fn_getX11Display = @ptrCast(&zglfw.getX11Display), + .fn_getX11Window = @ptrCast(&zglfw.getX11Window), + .fn_getWaylandDisplay = @ptrCast(&zglfw.getWaylandDisplay), + .fn_getWaylandSurface = @ptrCast(&zglfw.getWaylandWindow), + .fn_getCocoaWindow = @ptrCast(&zglfw.getCocoaWindow), + }, + .{}, + ); + defer gctx.destroy(gpa); + + zgui.init(gpa); + defer zgui.deinit(); + + zgui.backend.init( + window, + gctx.device, + @intFromEnum(zgpu.GraphicsContext.swapchain_format), + @intFromEnum(wgpu.TextureFormat.undef), + ); + defer zgui.backend.deinit(); + + var prev_scale: f32 = 1.0; + while (!window.shouldClose() and window.getKey(.escape) != .press) { + zglfw.pollEvents(); + + const scale_factor = scale_factor: { + const scale = window.getContentScale(); + break :scale_factor @max(scale[0], scale[1]); + }; + std.debug.print("{}", .{scale_factor / prev_scale}); + zgui.getStyle().scaleAllSizes(scale_factor / prev_scale); + prev_scale = scale_factor; + + zgui.backend.newFrame( + gctx.swapchain_descriptor.width, + gctx.swapchain_descriptor.height, + ); + + // Set the starting window position and size to custom values + zgui.setNextWindowPos(.{ .x = 20.0, .y = 20.0, .cond = .first_use_ever }); + zgui.setNextWindowSize(.{ .w = -1.0, .h = -1.0, .cond = .first_use_ever }); + + if (zgui.begin("My window", .{})) { + if (zgui.button("Press me!", .{ .w = 200.0 })) { + std.debug.print("Button pressed\n", .{}); + } + } + zgui.end(); + + const swapchain_texv = gctx.swapchain.getCurrentTextureView(); + defer swapchain_texv.release(); + + const commands = commands: { + const encoder = gctx.device.createCommandEncoder(null); + defer encoder.release(); + + // GUI pass + { + const pass = zgpu.beginRenderPassSimple(encoder, .load, swapchain_texv, null, null, null); + defer zgpu.endReleasePass(pass); + zgui.backend.draw(pass); + } + + break :commands encoder.finish(null); + }; + defer commands.release(); + + gctx.submit(&.{commands}); + _ = gctx.present(); + } +} diff --git a/thirdparty/zig-gamedev b/thirdparty/zig-gamedev new file mode 160000 index 0000000..09cb2d2 --- /dev/null +++ b/thirdparty/zig-gamedev @@ -0,0 +1 @@ +Subproject commit 09cb2d2bd814f6856f437ef2c9cb051abb068a54