diff --git a/README.md b/README.md index d8854099c..8f848c6b0 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/build.zig b/build.zig index 9fa4fe8db..4a2aea70e 100644 --- a/build.zig +++ b/build.zig @@ -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", + \\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; @@ -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") }, @@ -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); @@ -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`. diff --git a/src/callbacks_example.zig b/src/callbacks_example.zig new file mode 100644 index 000000000..fda69d2de --- /dev/null +++ b/src/callbacks_example.zig @@ -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; +} diff --git a/src/ios.zig b/src/ios.zig index 4144047d5..cdc8176ff 100644 --- a/src/ios.zig +++ b/src/ios.zig @@ -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"), diff --git a/src/sdl_main.c b/src/sdl_main.c new file mode 100644 index 000000000..6e1dd53c6 --- /dev/null +++ b/src/sdl_main.c @@ -0,0 +1 @@ +#include