diff --git a/README.md b/README.md index d16d31ed..c79a3975 100644 --- a/README.md +++ b/README.md @@ -25,19 +25,17 @@ const sdl = b.dependency("sdl", .{ .optimize = optimize, .target = target, }); -exe.root_module.linkLibrary(sdl.artifact("SDL3")); +exe.root_module.addImport("sdl3", sdl.module("sdl3")); ``` Finally, you can use SDL's C API from Zig like this: ```zig const std = @import("std"); -const c = @cImport({ - @cInclude("SDL3/SDL.h"); -}); -if (!c.SDL_Init(c.SDL_INIT_VIDEO)) { - std.debug.panic("SDL_Init failed: {s}\n", .{c.SDL_GetError()}); +const sdl = @import("sdl3"); +if (!sdl.SDL_Init(sdl.SDL_INIT_VIDEO)) { + std.debug.panic("SDL_Init failed: {s}\n", .{sdl.SDL_GetError()}); } -defer c.SDL_Quit(); +defer sdl.SDL_Quit(); ``` ## Example diff --git a/build.zig b/build.zig index 05437873..470232b4 100644 --- a/build.zig +++ b/build.zig @@ -34,7 +34,7 @@ pub fn build(b: *std.Build) !void { , ) orelse .static; - // Get the so version. This is the same as the SDL version, but the major version is elided + // Get the SO version. This is the same as the SDL version, but the major version is elided // since it's baked into the name. This mirrors the official build process. var sdl_so_version = comptime std.SemanticVersion.parse(build_zon.dependencies.sdl.version) catch unreachable; assert(sdl_so_version.major == 3); @@ -116,6 +116,16 @@ pub fn build(b: *std.Build) !void { // Add the Wayland scanner step linux.addWaylandScannerStep(b); + // Translate the SDL headers and export them as a Zig module + const translate_c = b.addTranslateC(.{ + .root_source_file = upstream.path("include/SDL3/SDL.h"), + .target = target, + .optimize = optimize, + }); + translate_c.addIncludePath(upstream.path("include")); + const module = translate_c.addModule("sdl3"); + module.linkLibrary(lib); + // Add the example const example = b.addExecutable(.{ .name = "example", @@ -125,7 +135,7 @@ pub fn build(b: *std.Build) !void { .optimize = optimize, }), }); - example.root_module.linkLibrary(lib); + example.root_module.addImport("sdl3", module); const build_example_step = b.step("example", "Build the example app"); build_example_step.dependOn(&example.step); diff --git a/src/example.zig b/src/example.zig index 80444525..68999e60 100644 --- a/src/example.zig +++ b/src/example.zig @@ -1,7 +1,5 @@ const std = @import("std"); -const c = @cImport({ - @cInclude("SDL3/SDL.h"); -}); +const sdl = @import("sdl3"); const Io = std.Io; @@ -10,54 +8,54 @@ pub fn main() void { const io = threaded_io.io(); // Initialize SDL - if (!c.SDL_Init(c.SDL_INIT_VIDEO)) { - std.debug.panic("{s}", .{c.SDL_GetError()}); + if (!sdl.SDL_Init(sdl.SDL_INIT_VIDEO)) { + std.debug.panic("{s}", .{sdl.SDL_GetError()}); } - defer c.SDL_Quit(); + defer sdl.SDL_Quit(); - std.debug.print("video driver: {s}\n", .{c.SDL_GetCurrentVideoDriver() orelse @as([*c]const u8, "null")}); + std.debug.print("video driver: {s}\n", .{sdl.SDL_GetCurrentVideoDriver() orelse @as([*c]const u8, "null")}); // Create a window and renderer - var window: ?*c.SDL_Window = null; - var renderer: ?*c.SDL_Renderer = null; - if (!c.SDL_CreateWindowAndRenderer( + var window: ?*sdl.SDL_Window = null; + var renderer: ?*sdl.SDL_Renderer = null; + if (!sdl.SDL_CreateWindowAndRenderer( "example", 960, 540, - c.SDL_WINDOW_RESIZABLE | c.SDL_WINDOW_HIGH_PIXEL_DENSITY, + sdl.SDL_WINDOW_RESIZABLE | sdl.SDL_WINDOW_HIGH_PIXEL_DENSITY, &window, &renderer, )) { - std.debug.panic("{s}", .{c.SDL_GetError()}); + std.debug.panic("{s}", .{sdl.SDL_GetError()}); } - defer c.SDL_DestroyWindow(window); - defer c.SDL_DestroyRenderer(renderer); + defer sdl.SDL_DestroyWindow(window); + defer sdl.SDL_DestroyRenderer(renderer); // Main loop while (true) { // Poll events - var event: c.SDL_Event = undefined; - while (c.SDL_PollEvent(&event)) { - if (event.type == c.SDL_EVENT_QUIT) { + var event: sdl.SDL_Event = undefined; + while (sdl.SDL_PollEvent(&event)) { + if (event.type == sdl.SDL_EVENT_QUIT) { std.process.cleanExit(io); return; } } // Update the background color - const now = @as(f64, @floatFromInt(c.SDL_GetTicks())) / 1000.0; + const now = @as(f64, @floatFromInt(sdl.SDL_GetTicks())) / 1000.0; const r: f32 = @floatCast(0.5 + 0.5 * @sin(now)); const g: f32 = @floatCast(0.5 + 0.5 * @sin(now + std.math.pi * 2 / 3.0)); const b: f32 = @floatCast(0.5 + 0.5 * @sin(now + std.math.pi * 4 / 3.0)); - if (!c.SDL_SetRenderDrawColorFloat(renderer, r, g, b, c.SDL_ALPHA_OPAQUE_FLOAT)) { - std.debug.panic("{s}", .{c.SDL_GetError()}); + if (!sdl.SDL_SetRenderDrawColorFloat(renderer, r, g, b, sdl.SDL_ALPHA_OPAQUE_FLOAT)) { + std.debug.panic("{s}", .{sdl.SDL_GetError()}); } - if (!c.SDL_RenderClear(renderer)) { - std.debug.panic("{s}", .{c.SDL_GetError()}); + if (!sdl.SDL_RenderClear(renderer)) { + std.debug.panic("{s}", .{sdl.SDL_GetError()}); } - if (!c.SDL_RenderPresent(renderer)) { - std.debug.panic("{s}", .{c.SDL_GetError()}); + if (!sdl.SDL_RenderPresent(renderer)) { + std.debug.panic("{s}", .{sdl.SDL_GetError()}); } } }