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
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,15 @@ money somehow. In those cases, instructions are provided on how to obtain the SD
- [x] Linux (includes Steam Deck)
- [x] Windows
- [x] macOS
- You can get the SDK with `1xcrun --sdk macosx --show-sdk-path` on a mac
- You can get the SDK with `xcrun --sdk macosx --show-sdk-path` on a mac
- Then pass the following options, either on the command line or into `b.dependency`:
`-Dinclude_path=$SDK/usr/bin`,
`-Dinclude_path=$SDK/usr/include`,
`-Dlibrary_path=$SDK/usr/lib`
- `-Dframework_path=$SDK/System/Library/Frameworks`.
- [x] iOS
- You can get the SDK with `1xcrun --sdk iphoneos --show-sdk-path` on a mac
- You can get the SDK with `xcrun --sdk iphoneos --show-sdk-path` on a mac
- Then pass the following options, either on the command line or into `b.dependency`:
`-Dinclude_path=$SDK/usr/bin`,
`-Dinclude_path=$SDK/usr/include`,
`-Dlibrary_path=$SDK/usr/lib`
- `-Dframework_path=$SDK/System/Library/Frameworks`.
- [x] Android
Expand Down
51 changes: 51 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ pub fn build(b: *std.Build) !void {
.library = b.option(std.Build.LazyPath, "library_path", "SDK library dir, e.g. $(xcrun --sdk iphoneos --show-sdk-path)/usr/lib"),
};

const main_callbacks = b.option(bool, "main_callbacks",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be reasonable to duplicate the example program and rename it to callbacks_example, and make use of this feature there.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do!

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, this is added! (Runs with zig build run-callbacks-example -Dmain_callbacks.) I decided to have it error out if no -Dmain_callbacks.

\\build SDL3_main to enter the app through SDL_AppInit/Iterate/Event/Quit rather
\\than a classic SDL_main, defaults to false
) orelse false;

// 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.upstream_version) catch unreachable;
Expand Down Expand Up @@ -100,6 +105,26 @@ pub fn build(b: *std.Build) !void {
.flags = flags,
});

var maybe_sdl_main_lib: ?*std.Build.Step.Compile = null;
if (main_callbacks) {
const sdl_main_lib = b.addLibrary(.{
.name = "SDL3_main",
.root_module = b.createModule(.{
.target = target,
.optimize = optimize,
.link_libc = true,
}),
.linkage = .static,
});
sdl_main_lib.root_module.addIncludePath(upstream.path("include"));
sdl_main_lib.root_module.addCMacro("SDL_MAIN_USE_CALLBACKS", "1");
sdl_main_lib.root_module.addCSourceFile(.{ .file = b.path("src/sdl_main.c") });
if (system_paths.include) |p| sdl_main_lib.root_module.addSystemIncludePath(p);
if (system_paths.framework) |p| sdl_main_lib.root_module.addSystemFrameworkPath(p);
b.installArtifact(sdl_main_lib);
maybe_sdl_main_lib = sdl_main_lib;
}

if (default_target_config) {
const build_config_h = b.addConfigHeader(.{
.style = .{ .cmake = upstream.path("include/build_config/SDL_build_config.h.cmake") },
Expand Down Expand Up @@ -149,6 +174,8 @@ pub fn build(b: *std.Build) !void {
translate_c.defineCMacro("USING_GENERATED_CONFIG_H", "1");
translate_c.addIncludePath(upstream.path("include"));
translate_c.addIncludePath(upstream.path("src/video/khronos"));
if (system_paths.include) |p| translate_c.addSystemIncludePath(p);
if (system_paths.framework) |p| translate_c.addSystemFrameworkPath(p);
const module = translate_c.addModule("sdl3");
if (system_paths.include) |p| module.addSystemIncludePath(p);
if (system_paths.framework) |p| module.addSystemFrameworkPath(p);
Expand All @@ -172,6 +199,30 @@ pub fn build(b: *std.Build) !void {
const run_example = b.addRunArtifact(example);
const run_step = b.step("run-example", "Run the example app");
run_step.dependOn(&run_example.step);

// Add a callbacks example. Note, it will fail if no -Dmain_callbacks.
const build_callbacks_example_step = b.step("callbacks-example", "Build the callbacks example app (requires -Dmain_callbacks)");
const run_callbacks_step = b.step("run-callbacks-example", "Run the callbacks example app (requires -Dmain_callbacks)");
if (maybe_sdl_main_lib) |sdl_main_lib| {
const callbacks_example = b.addExecutable(.{
.name = "callbacks_example",
.root_module = b.createModule(.{
.root_source_file = b.path("src/callbacks_example.zig"),
.target = target,
.optimize = optimize,
}),
});
callbacks_example.root_module.addImport("sdl3", module);
callbacks_example.root_module.linkLibrary(sdl_main_lib);
build_callbacks_example_step.dependOn(&callbacks_example.step);

const run_callbacks_example = b.addRunArtifact(callbacks_example);
run_callbacks_step.dependOn(&run_callbacks_example.step);
} else {
const fail = b.addFail("The callbacks example requires -Dmain_callbacks");
build_callbacks_example_step.dependOn(&fail.step);
run_callbacks_step.dependOn(&fail.step);
}
}

/// Fetches a lazy dependency, unless provided via `-fsys`.
Expand Down
71 changes: 71 additions & 0 deletions src/callbacks_example.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
const std = @import("std");
const sdl = @import("sdl3");

// main is provided by SDL_main.h, which calls the SDL_App* callbacks below
pub extern fn main(argc: c_int, argv: [*c][*c]u8) c_int;

var window: ?*sdl.SDL_Window = null;
var renderer: ?*sdl.SDL_Renderer = null;

export fn SDL_AppInit(appstate: ?*?*anyopaque, argc: c_int, argv: [*c][*c]u8) sdl.SDL_AppResult {
_ = appstate;
_ = argc;
_ = argv;

// Initialize SDL
if (!sdl.SDL_Init(sdl.SDL_INIT_VIDEO)) {
std.debug.panic("{s}", .{sdl.SDL_GetError()});
}

std.debug.print("video driver: {s}\n", .{sdl.SDL_GetCurrentVideoDriver() orelse @as([*c]const u8, "null")});

// Create a window and renderer
if (!sdl.SDL_CreateWindowAndRenderer(
"example",
960,
540,
sdl.SDL_WINDOW_RESIZABLE | sdl.SDL_WINDOW_HIGH_PIXEL_DENSITY,
&window,
&renderer,
)) {
std.debug.panic("{s}", .{sdl.SDL_GetError()});
}

return sdl.SDL_APP_CONTINUE;
}

export fn SDL_AppEvent(appstate: ?*anyopaque, event: ?*sdl.SDL_Event) sdl.SDL_AppResult {
_ = appstate;

if (event.?.type == sdl.SDL_EVENT_QUIT) {
return sdl.SDL_APP_SUCCESS;
}
return sdl.SDL_APP_CONTINUE;
}

export fn SDL_AppIterate(appstate: ?*anyopaque) sdl.SDL_AppResult {
_ = appstate;

// Update the background color
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 (!sdl.SDL_SetRenderDrawColorFloat(renderer, r, g, b, sdl.SDL_ALPHA_OPAQUE_FLOAT)) {
std.debug.panic("{s}", .{sdl.SDL_GetError()});
}
if (!sdl.SDL_RenderClear(renderer)) {
std.debug.panic("{s}", .{sdl.SDL_GetError()});
}
if (!sdl.SDL_RenderPresent(renderer)) {
std.debug.panic("{s}", .{sdl.SDL_GetError()});
}

return sdl.SDL_APP_CONTINUE;
}

export fn SDL_AppQuit(appstate: ?*anyopaque, result: sdl.SDL_AppResult) void {
_ = appstate;
_ = result;
}
5 changes: 4 additions & 1 deletion src/ios.zig
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ pub fn build(
lib.root_module.linkFramework("CoreHaptics", .{ .weak = true });

// Add the platform specific sources
const objc_flags = root.flags.* ++ [_][]const u8{"-fobjc-arc"};
const objc_flags = root.flags.* ++ [_][]const u8{
"-fobjc-arc",
"-fno-sanitize=builtin",
};
lib.root_module.addCSourceFiles(.{
.files = &(sources.uikit ++ sources.apple ++ sources.pthread),
.root = upstream.path("src"),
Expand Down
1 change: 1 addition & 0 deletions src/sdl_main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include <SDL3/SDL_main.h>