Skip to content
/ SDL Public
forked from pwbh/SDL

SDL on Zig build system and in-sync with latest SDL versions

License

Notifications You must be signed in to change notification settings

jsonxr/SDL

This branch is up to date with pwbh/SDL:main.

Repository files navigation

SDL on Zig

This is a fork of SDL, packaged for Zig (Initially by Andrew Kelley) and in-sync with the latest SDL releases in the main repository. Unnecessary files have been deleted, and the build system has been replaced with build.zig

Usage

We can easily fetch this library using zig fetch, for example:

zig fetch --save https://github.com/pwbh/SDL/archive/refs/tags/release-2.30.3.tar.gz

If another SDL version needed, please see available tags released.

In your build.zig:

const exe = b.addExecutable(.{
      .name = "my-project",
      .root_source_file = b.path("src/main.zig"),
      .target = target,
      .optimize = optimize,
  });

if (target.result.os.tag == .linux) {
    // The SDL package doesn't work for Linux yet, so we rely on system
    // packages for now.
    exe.linkSystemLibrary("SDL2");
    exe.linkLibC();
} else {
    const sdl_dep = b.dependency("SDL", .{
        .optimize = .ReleaseFast,
        .target = target,
    });
    exe.linkLibrary(sdl_dep.artifact("SDL2"));
}

b.installArtifact(exe);

Test it out

Now lets test it out and see if it creates a window.

Following codesnippet should create a window that can be exited.

const c = @cImport({
    @cInclude("SDL2/SDL.h");
});

pub fn main() !void {
    if (c.SDL_Init(c.SDL_INIT_VIDEO) != 0) {
        c.SDL_Log("Unable to initialize SDL: %s", c.SDL_GetError());
        return error.SDLInitializationFailed;
    }
    defer c.SDL_Quit();

    const screen = c.SDL_CreateWindow("My Game Window", c.SDL_WINDOWPOS_UNDEFINED, c.SDL_WINDOWPOS_UNDEFINED, 400, 140, c.SDL_WINDOW_OPENGL) orelse
        {
        c.SDL_Log("Unable to create window: %s", c.SDL_GetError());
        return error.SDLInitializationFailed;
    };
    defer c.SDL_DestroyWindow(screen);

    const renderer = c.SDL_CreateRenderer(screen, -1, 0) orelse {
        c.SDL_Log("Unable to create renderer: %s", c.SDL_GetError());
        return error.SDLInitializationFailed;
    };
    defer c.SDL_DestroyRenderer(renderer);

    var quit = false;

    while (!quit) {
        var event: c.SDL_Event = undefined;
        while (c.SDL_PollEvent(&event) != 0) {
            switch (event.type) {
                c.SDL_QUIT => {
                    quit = true;
                },
                else => {},
            }
        }

        _ = c.SDL_RenderClear(renderer);
        c.SDL_RenderPresent(renderer);

        c.SDL_Delay(10);
    }
}

About

SDL on Zig build system and in-sync with latest SDL versions

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Zig 100.0%