From 308075678be3e06b4bf326e5313c58a9105cfdfa Mon Sep 17 00:00:00 2001 From: Kasper Sauramo Date: Mon, 27 Oct 2025 23:14:25 +0200 Subject: [PATCH] add sdl3 Needed to change to zig 0.15.2 as well, didn't work with 0.16 --- build.zig | 67 ++++++++++++++++++++++++++++++++++++++---------- build.zig.zon | 41 +++-------------------------- src/main.zig | 43 +++++++++++++++++++++++++++++-- src/omt.zig | 4 --- utils/sender.zig | 13 +++++++++- 5 files changed, 111 insertions(+), 57 deletions(-) diff --git a/build.zig b/build.zig index b7a7e6d..0b5c3cb 100644 --- a/build.zig +++ b/build.zig @@ -93,22 +93,63 @@ pub fn build(b: *std.Build) void { omt_mod.addLibraryPath(lib_output_path); omt_mod.addIncludePath(include_output_path); - // Main executable - const omtoy_exe = b.addExecutable(.{ - .name = "omtoy", - .root_module = b.createModule(.{ - .root_source_file = b.path("src/main.zig"), - .target = target, - .optimize = optimize, - }), + const sdl3 = b.dependency("sdl3", .{ + .target = target, + .optimize = optimize, + + // Lib options. + // .callbacks = false, + // .ext_image = false, + // .ext_net = false, + // .ext_ttf = false, + // .log_message_stack_size = 1024, + // .main = false, + // .renderer_debug_text_stack_size = 1024, + + // Options passed directly to https://github.com/castholm/SDL (SDL3 C Bindings): + // .c_sdl_preferred_linkage = .static, + // .c_sdl_strip = false, + // .c_sdl_sanitize_c = .off, + // .c_sdl_lto = .none, + // .c_sdl_emscripten_pthreads = false, + // .c_sdl_install_build_config_h = false, + + // Options if `ext_image` is enabled: + // .image_enable_bmp = true, + // .image_enable_gif = true, + // .image_enable_jpg = true, + // .image_enable_lbm = true, + // .image_enable_pcx = true, + // .image_enable_png = true, + // .image_enable_pnm = true, + // .image_enable_qoi = true, + // .image_enable_svg = true, + // .image_enable_tga = true, + // .image_enable_xcf = true, + // .image_enable_xpm = true, + // .image_enable_xv = true, }); - b.installArtifact(omtoy_exe); + { + // Main executable + const omtoy_exe = b.addExecutable(.{ + .name = "omtoy", + .root_module = b.createModule(.{ + .root_source_file = b.path("src/main.zig"), + .target = target, + .optimize = optimize, + }), + }); - const omtoy_run_step = b.step("run", "Run omtoy"); - const omtoy_run = b.addRunArtifact(omtoy_exe); - omtoy_run_step.dependOn(&omtoy_run); - omtoy_run.step.dependOn(b.getInstallStep()); + omtoy_exe.root_module.addImport("sdl3", sdl3.module("sdl3")); + b.installArtifact(omtoy_exe); + + // Run step for main executable + const run_step = b.step("run", "Run omtoy"); + const run_cmd = b.addRunArtifact(omtoy_exe); + run_step.dependOn(&run_cmd.step); + run_cmd.step.dependOn(b.getInstallStep()); + } // Util executables { diff --git a/build.zig.zon b/build.zig.zon index d49b6f4..170931e 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -32,44 +32,11 @@ // Once all dependencies are fetched, `zig build` no longer requires // internet connectivity. .dependencies = .{ - // See `zig fetch --save ` for a command-line interface for adding dependencies. - //.example = .{ - // // When updating this field to a new URL, be sure to delete the corresponding - // // `hash`, otherwise you are communicating that you expect to find the old hash at - // // the new URL. If the contents of a URL change this will result in a hash mismatch - // // which will prevent zig from using it. - // .url = "https://example.com/foo.tar.gz", - // - // // This is computed from the file contents of the directory of files that is - // // obtained after fetching `url` and applying the inclusion rules given by - // // `paths`. - // // - // // This field is the source of truth; packages do not come from a `url`; they - // // come from a `hash`. `url` is just one of many possible mirrors for how to - // // obtain a package matching this `hash`. - // // - // // Uses the [multihash](https://multiformats.io/multihash/) format. - // .hash = "...", - // - // // When this is provided, the package is found in a directory relative to the - // // build root. In this case the package's hash is irrelevant and therefore not - // // computed. This field and `url` are mutually exclusive. - // .path = "foo", - // - // // When this is set to `true`, a package is declared to be lazily - // // fetched. This makes the dependency only get fetched if it is - // // actually used. - // .lazy = false, - //}, + .sdl3 = .{ + .url = "git+https://github.com/Gota7/zig-sdl3?ref=v0.1.5#014b7bcb2899f3ed9c945c4abfcfe1b25d75bfeb", + .hash = "sdl3-0.1.5-NmT1QxARJgAH1Wp0cMBJDAc9vD7weufTkIwVa5rehA2q", + }, }, - // Specifies the set of files and directories that are included in this package. - // Only files and directories listed here are included in the `hash` that - // is computed for this package. Only files listed here will remain on disk - // when using the zig package manager. As a rule of thumb, one should list - // files required for compilation plus any license(s). - // Paths are relative to the build root. Use the empty string (`""`) to refer to - // the build root itself. - // A directory listed here means that all files within, recursively, are included. .paths = .{ "build.zig", "build.zig.zon", diff --git a/src/main.zig b/src/main.zig index 3b5d940..7ff24e9 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,6 +1,45 @@ +const sdl3 = @import("sdl3"); const std = @import("std"); -const omt = @import("omt.zig"); -pub fn main() void { +const fps = 60; +const screen_width = 640; +const screen_height = 480; + +pub fn main() !void { std.log.info("Starting omtoy...", .{}); + + defer sdl3.shutdown(); + + // Initialize SDL with subsystems you need here. + const init_flags = sdl3.InitFlags{ .video = true }; + try sdl3.init(init_flags); + defer sdl3.quit(init_flags); + + // Initial window setup. + const window = try sdl3.video.Window.init("Hello SDL3", screen_width, screen_height, .{}); + defer window.deinit(); + + // Useful for limiting the FPS and getting the delta time. + var fps_capper = sdl3.extras.FramerateCapper(f32){ .mode = .{ .limited = fps } }; + + var quit = false; + while (!quit) { + + // Delay to limit the FPS, returned delta time not needed. + const dt = fps_capper.delay(); + _ = dt; + + // Update logic. + const surface = try window.getSurface(); + try surface.fillRect(null, surface.mapRgb(128, 30, 255)); + try window.updateSurface(); + + // Event logic. + while (sdl3.events.poll()) |event| + switch (event) { + .quit => quit = true, + .terminating => quit = true, + else => {}, + }; + } } diff --git a/src/omt.zig b/src/omt.zig index 3ad58d4..61cb352 100644 --- a/src/omt.zig +++ b/src/omt.zig @@ -25,7 +25,3 @@ pub fn colorSpaceToString(cs: c.OMTColorSpace) []const u8 { else => "Invalid", }; } - -pub fn whaaat() void { - std.log.info("Whaaaat", .{}); -} diff --git a/utils/sender.zig b/utils/sender.zig index 0951ba8..add8a21 100644 --- a/utils/sender.zig +++ b/utils/sender.zig @@ -55,7 +55,18 @@ pub fn main() !void { .FrameMetadataLength = 0, }; - const content = try std.fs.cwd().readFileAlloc("california-1080-uyvy.yuv", allocator, .unlimited); + // 0.15 + const content = try std.fs.cwd().readFileAlloc( + allocator, + "california-1080-uyvy.yuv", + std.math.maxInt(usize), + ); + // 0.16 + // const content = try std.fs.cwd().readFileAlloc( + // "california-1080-uyvy.yuv", + // allocator, + // .unlimited, + // ); defer allocator.free(content); const audio_samples = 800;