summaryrefslogtreecommitdiff
path: root/src/host/gfx.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/host/gfx.zig')
-rw-r--r--src/host/gfx.zig24
1 files changed, 20 insertions, 4 deletions
diff --git a/src/host/gfx.zig b/src/host/gfx.zig
index e00d325..16148a6 100644
--- a/src/host/gfx.zig
+++ b/src/host/gfx.zig
@@ -27,7 +27,7 @@ pub const Images = struct {
pub fn load(self: *Images, path: []const u8) abi.Image {
if (self.paths.get(path)) |image| return image;
var buf: [std.fs.max_path_bytes]u8 = undefined;
- const z = std.fmt.bufPrintZ(&buf, "{s}", .{path}) catch return .none;
+ const z = std.mem.printSentinel(&buf, "{s}", .{path}, 0) catch return .none;
const surface = c.SDL_LoadPNG(z.ptr);
if (surface == null) std.log.warn("image {s}: {s}", .{ path, c.SDL_GetError() });
const image = self.add(surface);
@@ -83,7 +83,7 @@ pub const Images = struct {
}
pub fn get(self: *Images, image: abi.Image) ?*Entry {
- const index = @intFromEnum(image);
+ const index = @backingInt(image);
if (index == 0 or index > self.entries.items.len) return null;
return &self.entries.items[index - 1];
}
@@ -99,7 +99,7 @@ pub const Images = struct {
fn append(self: *Images, entry: Entry) abi.Image {
self.entries.append(self.gpa, entry) catch return .none;
- return @enumFromInt(self.entries.items.len);
+ return @fromBackingInt(@intCast(self.entries.items.len));
}
fn setup(texture: *c.SDL_Texture) void {
@@ -112,6 +112,7 @@ pub const Images = struct {
pub fn draw(renderer: *c.SDL_Renderer, images: *Images, command: abi.Command) void {
switch (command) {
.clear => |color| {
+ if (@import("builtin").os.tag == .linux) c.sk_text_begin_frame(renderer);
setColor(renderer, color);
_ = c.SDL_RenderClear(renderer);
},
@@ -146,8 +147,10 @@ pub fn draw(renderer: *c.SDL_Renderer, images: *Images, command: abi.Command) vo
}
}
-/// SDL's built-in 8x8 font, `scale` times larger. Stops at the first NUL.
+/// Native shaped text on Linux, using a 13px monospace face at scale 1.
+/// Other targets retain SDL's built-in 8x8 fallback. Coordinates are line-box tops.
pub fn drawText(renderer: *c.SDL_Renderer, x: f32, y: f32, scale: f32, bytes: []const u8, color: abi.Color) void {
+ if (@import("builtin").os.tag == .linux and c.sk_text_draw(renderer, x, y, scale, bytes.ptr, bytes.len, color.r, color.g, color.b, color.a)) return;
var buf: [1024]u8 = undefined;
const len = @min(std.mem.indexOfScalar(u8, bytes, 0) orelse bytes.len, buf.len - 1);
@memcpy(buf[0..len], bytes[0..len]);
@@ -157,9 +160,22 @@ pub fn drawText(renderer: *c.SDL_Renderer, x: f32, y: f32, scale: f32, bytes: []
var sy: f32 = 1;
_ = c.SDL_GetRenderScale(renderer, &sx, &sy);
const s = @max(scale, 1);
+ // The clip rect scales with the renderer: shrink it to match, to the
+ // whole pixels around it (so it may let up to `s - 1` pixels more through).
+ var clip: c.SDL_Rect = undefined;
+ const clipped = c.SDL_RenderClipEnabled(renderer) and c.SDL_GetRenderClipRect(renderer, &clip);
_ = c.SDL_SetRenderScale(renderer, sx * s, sy * s);
+ if (clipped and s != 1) {
+ const x0 = @floor(@as(f32, @floatFromInt(clip.x)) / s);
+ const y0 = @floor(@as(f32, @floatFromInt(clip.y)) / s);
+ const x1 = @ceil(@as(f32, @floatFromInt(clip.x + clip.w)) / s);
+ const y1 = @ceil(@as(f32, @floatFromInt(clip.y + clip.h)) / s);
+ const scaled: c.SDL_Rect = .{ .x = @intFromFloat(x0), .y = @intFromFloat(y0), .w = @intFromFloat(x1 - x0), .h = @intFromFloat(y1 - y0) };
+ _ = c.SDL_SetRenderClipRect(renderer, &scaled);
+ }
_ = c.SDL_RenderDebugText(renderer, x / s, y / s, &buf);
_ = c.SDL_SetRenderScale(renderer, sx, sy);
+ if (clipped and s != 1) _ = c.SDL_SetRenderClipRect(renderer, &clip);
}
fn missing(renderer: *c.SDL_Renderer, dst: abi.Rect) void {