commit 241edb630712713e8eec757b07da82c642f7e544
parent 63c3b62cc3757ced302c10113776441eb08cb824
Author: Christian Ermann <christianermann@gmail.com>
Date: Mon, 22 Apr 2024 20:02:02 -0400
Add mach-glfw
Diffstat:
3 files changed, 30 insertions(+), 48 deletions(-)
diff --git a/build.zig b/build.zig
@@ -36,6 +36,12 @@ pub fn build(b: *std.Build) void {
.optimize = optimize,
});
+ const glfw_dep = b.dependency("mach-glfw", .{
+ .target = target,
+ .optimize = optimize,
+ });
+ exe.root_module.addImport("mach-glfw", glfw_dep.module("mach-glfw"));
+
// This declares intent for the executable to be installed into the
// standard location when the user invokes the "install" step (the default
// step when running `zig build`).
diff --git a/build.zig.zon b/build.zig.zon
@@ -15,37 +15,11 @@
// Once all dependencies are fetched, `zig build` no longer requires
// internet connectivity.
.dependencies = .{
- // See `zig fetch --save <url>` for a command-line interface for adding dependencies.
- //.example = .{
- // // When updating this field to a new URL, be sure to delete the corresponding
- // // `hash`, otherwise you are communicating that you expect to find the old hash at
- // // the new URL.
- // .url = "https://example.com/foo.tar.gz",
- //
- // // This is computed from the file contents of the directory of files that is
- // // obtained after fetching `url` and applying the inclusion rules given by
- // // `paths`.
- // //
- // // This field is the source of truth; packages do not come from a `url`; they
- // // come from a `hash`. `url` is just one of many possible mirrors for how to
- // // obtain a package matching this `hash`.
- // //
- // // Uses the [multihash](https://multiformats.io/multihash/) format.
- // .hash = "...",
- //
- // // When this is provided, the package is found in a directory relative to the
- // // build root. In this case the package's hash is irrelevant and therefore not
- // // computed. This field and `url` are mutually exclusive.
- // .path = "foo",
- //},
+ .@"mach-glfw" = .{
+ .url = "https://pkg.machengine.org/mach-glfw/e57190c095097810980703aa26d4f0669a21dbab.tar.gz",
+ .hash = "12205a32c8e6ca23c68191b1e95405d2bd5f8e3055cba1c8ce0738d673ef49aef913",
+ },
},
-
- // Specifies the set of files and directories that are included in this package.
- // Only files and directories listed here are included in the `hash` that
- // is computed for this package.
- // Paths are relative to the build root. Use the empty string (`""`) to refer to
- // the build root itself.
- // A directory listed here means that all files within, recursively, are included.
.paths = .{
// This makes *all* files, recursively, included in this package. It is generally
// better to explicitly list the files and directories instead, to insure that
diff --git a/src/main.zig b/src/main.zig
@@ -1,24 +1,26 @@
const std = @import("std");
+const glfw = @import("mach-glfw");
-pub fn main() !void {
- // Prints to stderr (it's a shortcut based on `std.io.getStdErr()`)
- std.debug.print("All your {s} are belong to us.\n", .{"codebase"});
-
- // stdout is for the actual output of your application, for example if you
- // are implementing gzip, then only the compressed bytes should be sent to
- // stdout, not any debugging messages.
- const stdout_file = std.io.getStdOut().writer();
- var bw = std.io.bufferedWriter(stdout_file);
- const stdout = bw.writer();
+fn errorCallback(error_code: glfw.ErrorCode, description: [:0]const u8) void {
+ std.log.err("glfw: {}: {s}\n", .{ error_code, description });
+}
- try stdout.print("Run `zig build test` to run the tests.\n", .{});
+pub fn main() !void {
+ glfw.setErrorCallback(errorCallback);
+ if (!glfw.init(.{})) {
+ std.log.err("failed to initialize GLFW: {?s}", .{glfw.getErrorString()});
+ std.process.exit(1);
+ }
+ defer glfw.terminate();
- try bw.flush(); // don't forget to flush!
-}
+ const window = glfw.Window.create(640, 480, "Hello, World!", null, null, .{}) orelse {
+ std.log.err("failed to create GLFW window: {?s}", .{glfw.getErrorString()});
+ std.process.exit(1);
+ };
+ defer window.destroy();
-test "simple test" {
- var list = std.ArrayList(i32).init(std.testing.allocator);
- defer list.deinit(); // try commenting this out and see if zig detects the memory leak!
- try list.append(42);
- try std.testing.expectEqual(@as(i32, 42), list.pop());
+ while (!window.shouldClose()) {
+ window.swapBuffers();
+ glfw.pollEvents();
+ }
}