| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
SDL ported to the Zig build system.
Supports cross compilation and custom platform configuration.
See build.zig.zon for the current Zig version, see releases for support for previous Zig versions.
The versions specified in build.zig.zon are split between the package version (before the plus) and the SDL version (after the plus). For example, version 1.0.0+3.4.4 is package version 1.0.0 with support for SDL 3.4.4.
For SDL2, see the SDL2 branch.
You can add SDL to your project like this by updating build.zig.zon from the command line:
zig fetch --save <url-of-this-repo>And then you can add it to your build.zig like this:
const sdl = b.dependency("sdl", .{
.optimize = optimize,
.target = target,
});
exe.root_module.addImport("sdl3", sdl.module("sdl3"));Finally, you can use SDL's C API from Zig like this:
const std = @import("std");
const sdl = @import("sdl3");
if (!sdl.SDL_Init(sdl.SDL_INIT_VIDEO)) {
std.debug.panic("SDL_Init failed: {s}\n", .{sdl.SDL_GetError()});
}
defer sdl.SDL_Quit();You can run src/example.zig from the command line:
zig build run-exampleThis should produce a window with a pulsing gradient.
SDL_Init failed: No available video deviceBy default, SDL loads most of its dependencies at runtime on Linux. This lets it decide at runtime which audio drivers to use, whether to use Wayland or X11, etc.
For this to work, the libraries SDL is looking for need to be on your LD_LIBRARY_PATH. This may not be the case by default on distributions like NixOS.
Here's a shell.nix for a Vulkan app as an example of running an SDL application on NixOS with either X11 or Wayland.
{ pkgs ? import <nixpkgs> {}}:
pkgs.mkShell {
packages = with pkgs; [
vulkan-validation-layers
];
LD_LIBRARY_PATH = pkgs.lib.makeLibraryPath (with pkgs; [
alsa-lib
libdecor
libusb1
libxkbcommon
vulkan-loader
wayland
xorg.libX11
xorg.libXext
xorg.libXi
udev
]);
}
Not all of these dependencies in this example are required. Since both X11 and Wayland dependencies are listed, SDL will use its judgement to decide which to prefer unless SDL_VIDEODRIVER is set.
This library provides a default configuration for common targets:
You can override the default target configuration by setting default_target_config to false, and then providing your own configuration. This is typically only necessary when your platform doesn't yet have a default configuration:
const sdl = b.dependency("sdl", .{
.optimize = optimize,
.target = target,
.default_target_config = false,
});
const sdl_lib = sdl.artifact("SDL3");
sdl_lib.addIncludePath(...); // Path to your `SDL_build_config.h`, see `windows.zig` for an example of generating thisAny other necessary tweaks such as turning of linking with libc, linking with dependencies, or adding other headers can be done here as well.
If you're interested in adding default configuration for additional targets, listed or not, contributions are welcome! See src/linux.zig or src/windows.zig for examples of how this works.
When making a PR that adds support for a new target:
This should rarely be necessary. When it is, you can update their version in build.zig.zon if present, and any relevant files in /deps if present.
| Back | FazBrowse Home | New Git URL |