Skip to content

Commit

Permalink
SDL3 linux support
Browse files Browse the repository at this point in the history
  • Loading branch information
Deins committed Dec 22, 2024
1 parent 4ba879f commit 6bedaf8
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ zig-cache
.zig-cache
zig-out
*.swp
sdl_files
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ How to run the built-in examples:
- ```zig build web-test```
- then load `zig-out/bin/index.html`
- [online demo](https://david-vanderson.github.io)
- SDL3:
- Windows (SDL3 built from source):
- ```zig build sdl-standalone -Dsdl3```
- ```zig build sdl-ontop -Dsdl3```
- Linux (SDL3 dev must be installed on system):
- ```zig build sdl-standalone -fsys=sdl3```
- ```zig build sdl-ontop -fsys=sdl3```
- if you encounter error `No Wayland` also add flag `-Dlinux_display_backend=X11`


[Online Docs](https://david-vanderson.github.io/docs) This document is a broad overview. See [implementation details](readme-implementation.md) for how to write and modify widgets.

Expand Down
5 changes: 5 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,11 @@ fn addDvuiModule(
backend_mod.linkLibrary(sdl3.artifact("sdl3"));
backend_mod.addImport("sdl3_c", sdl3.module("sdl"));
}
} else if (b.systemIntegrationOption("sdl3", .{})) {
// SDL3 from system
sdl_options.addOption(std.SemanticVersion, "version", .{ .major = 3, .minor = 0, .patch = 0 });
sdl_options.addOption(bool, "from_system", true);
backend_mod.linkSystemLibrary("SDL3", .{});
} else {
sdl_options.addOption(std.SemanticVersion, "version", .{ .major = 2, .minor = 0, .patch = 0 });
if (target.result.os.tag == .linux) {
Expand Down
1 change: 1 addition & 0 deletions examples/sdl-standalone.zig
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub fn main() !void {
// on windows graphical apps have no console, so output goes to nowhere - attach it manually. related: https://github.com/ziglang/zig/issues/4196
_ = winapi.AttachConsole(0xFFFFFFFF);
}
std.log.info("SDL version: {}", .{Backend.getSDLVersion()});

dvui.Examples.show_demo_window = show_demo;

Expand Down
34 changes: 31 additions & 3 deletions src/backends/sdl_backend.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,18 @@ const dvui = @import("dvui");

const sdl_options = @import("sdl_options");
pub const sdl3 = sdl_options.version.major == 3;
pub const c = if (sdl3) @import("sdl3_c") else @cImport({
@cInclude("SDL2/SDL.h");
});
pub const c = blk: {
if (sdl3) {
if (@hasDecl(sdl_options, "from_system") and sdl_options.from_system) {
break :blk @cImport({
@cInclude("SDL3/SDL.h");
});
} else break :blk @import("sdl3_c");
}
break :blk @cImport({
@cInclude("SDL2/SDL.h");
});
};

const SDLBackend = @This();
pub const Context = *SDLBackend;
Expand Down Expand Up @@ -932,3 +941,22 @@ pub fn SDL_keysym_to_dvui(keysym: i32) dvui.enums.Key {
},
};
}

pub fn getSDLVersion() std.SemanticVersion {
if (sdl3) {
const v: u32 = @bitCast(c.SDL_GetVersion());
return .{
.major = @divTrunc(v, 1000000),
.minor = @mod(@divTrunc(v, 1000), 1000),
.patch = @mod(v, 1000),
};
} else {
var v: c.SDL_version = .{};
c.SDL_GetVersion(&v);
return .{
.major = @intCast(v.major),
.minor = @intCast(v.minor),
.patch = @intCast(v.patch),
};
}
}

0 comments on commit 6bedaf8

Please sign in to comment.