Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,34 @@ You can add SDL3 to your project like this by updating `build.zig.zon` from the
zig fetch --save <url-of-this-repo>
```

And then you can add it to your `build.zig` like this:
You'll then want to create a C header file, say `src/sdl3.h` like:
```c
#include <SDL3/SDL.h>
```

Next you can add the following to your `build.zig` file:
```zig
const sdl = b.dependency("sdl", .{
.optimize = optimize,
.target = target,
});
exe.root_module.linkLibrary(sdl.artifact("SDL3"));
const sdl3_lib = sdl.artifact("SDL3");

const sdl3_h = b.addTranslateC(.{
.root_source_file = b.path("src/sdl3.h"),
.target = target,
.optimize = optimize,
});
sdl3_h.addSystemIncludePath(sdl3_lib.getEmittedIncludeTree());

exe.root_module.linkLibrary(sdl3_lib);
exe.root_module.addImport("sdl3_h", sdl3_h.createModule());
```

Finally, you can use SDL's C API from Zig like this:
```zig
const std = @import("std");
const c = @cImport({
@cInclude("SDL3/SDL.h");
});
const c = @import("sdl3_h");
if (!c.SDL_Init(c.SDL_INIT_VIDEO)) {
std.debug.panic("SDL_Init failed: {s}\n", .{c.SDL_GetError()});
}
Expand Down
9 changes: 9 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,14 @@ pub fn build(b: *std.Build) !void {
// Add the Wayland scanner step
linux.addWaylandScannerStep(b);

// Translate the C header for the example
const sdl3_h = b.addTranslateC(.{
.root_source_file = b.path("src/sdl3.h"),
.target = target,
.optimize = optimize,
});
sdl3_h.addSystemIncludePath(lib.getEmittedIncludeTree());

// Add the example
const example = b.addExecutable(.{
.name = "example",
Expand All @@ -126,6 +134,7 @@ pub fn build(b: *std.Build) !void {
}),
});
example.root_module.linkLibrary(lib);
example.root_module.addImport("sdl3_h", sdl3_h.createModule());

const build_example_step = b.step("example", "Build the example app");
build_example_step.dependOn(&example.step);
Expand Down
4 changes: 1 addition & 3 deletions src/example.zig
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
const std = @import("std");
const c = @cImport({
@cInclude("SDL3/SDL.h");
});
const c = @import("sdl3_h");

const Io = std.Io;

Expand Down
1 change: 1 addition & 0 deletions src/sdl3.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include <SDL3/SDL.h>