88 lines
2.3 KiB
Markdown
88 lines
2.3 KiB
Markdown
# [zglfw](https://github.com/zig-gamedev/zglfw)
|
|
|
|
Zig build package and bindings for [GLFW 3.4](https://github.com/glfw/glfw/releases/tag/3.4)
|
|
|
|
## Getting started
|
|
|
|
Example `build.zig`:
|
|
```zig
|
|
const std = @import("std");
|
|
|
|
pub fn build(b: *std.Build) void {
|
|
const target = b.standardTargetOptions(.{});
|
|
const optimize = b.standardOptimizeOption(.{});
|
|
|
|
const exe = b.addExecutable(.{ ... });
|
|
|
|
const zglfw = b.dependency("zglfw", .{
|
|
.target = target,
|
|
.optimize = optimize,
|
|
});
|
|
exe.root_module.addImport("zglfw", zglfw.module("root"));
|
|
|
|
if (target.result.os.tag != .emscripten) {
|
|
exe.root_module.linkLibrary(zglfw.artifact("glfw"));
|
|
}
|
|
|
|
b.installArtifact(exe);
|
|
}
|
|
```
|
|
|
|
Now in your code you may import and use `zglfw`:
|
|
```zig
|
|
const glfw = @import("zglfw");
|
|
|
|
pub fn main() !void {
|
|
try glfw.init();
|
|
defer glfw.terminate();
|
|
|
|
const window = try glfw.createWindow(600, 600, "zig-gamedev: minimal_glfw_gl", null, null);
|
|
defer glfw.destroyWindow(window);
|
|
|
|
// or, using the equivalent, encapsulated, "objecty" API:
|
|
const window = try glfw.Window.create(600, 600, "zig-gamedev: minimal_glfw_gl", null, null);
|
|
defer window.destroy();
|
|
|
|
// setup your graphics context here
|
|
|
|
while (!window.shouldClose()) {
|
|
glfw.pollEvents();
|
|
|
|
// render your things here
|
|
|
|
window.swapBuffers();
|
|
}
|
|
}
|
|
```
|
|
|
|
See [zig-gamedev samples](https://github.com/zig-gamedev/zig-gamedev/tree/main/samples) for more complete usage examples.
|
|
|
|
|
|
## Usage with Vulkan
|
|
|
|
To match types from `zglfw` functions and Vulkan library `import_vulkan` option may be used. When using this option `vulkan` import must be provided to the root module.
|
|
|
|
Example `build.zig` with [`vulkan-zig`](https://github.com/Snektron/vulkan-zig):
|
|
|
|
```zig
|
|
const vulkan_headers = b.dependency("vulkan_headers", .{});
|
|
const vulkan = b.dependency("vulkan", .{
|
|
.registry = vulkan_headers.path("registry/vk.xml"),
|
|
}).module("vulkan-zig");
|
|
exe.root_module.addImport("vulkan", vulkan);
|
|
|
|
const zglfw = b.dependency("zglfw", .{
|
|
.target = target,
|
|
.optimize = optimize,
|
|
.import_vulkan = true,
|
|
});
|
|
|
|
const zglfw_mod = zglfw.module("root");
|
|
zglfw_mod.addImport("vulkan", vulkan);
|
|
exe.root_module.addImport("zglfw", zglfw_mod);
|
|
|
|
if (target.result.os.tag != .emscripten) {
|
|
exe.root_module.linkLibrary(zglfw.artifact("glfw"));
|
|
}
|
|
```
|