diff options
Diffstat (limited to 'src/host/gfx.zig')
| -rw-r--r-- | src/host/gfx.zig | 75 |
1 files changed, 24 insertions, 51 deletions
diff --git a/src/host/gfx.zig b/src/host/gfx.zig index 94e154f..f84568a 100644 --- a/src/host/gfx.zig +++ b/src/host/gfx.zig @@ -2,6 +2,7 @@ const std = @import("std"); const c = @import("c"); const abi = @import("abi"); +const Glyphs = @import("Glyphs.zig"); pub const Images = struct { gpa: std.mem.Allocator, @@ -172,6 +173,7 @@ pub const Painter = struct { backdrop_state: ?*c.SDL_GPURenderState = null, backdrop_shader: ?*c.SDL_GPUShader = null, backdrop_hash: u64 = 0, + glyphs: Glyphs, const Layer = struct { big: ?*c.SDL_Texture = null, @@ -199,6 +201,7 @@ pub const Painter = struct { } pub fn deinit(p: *Painter) void { + p.glyphs.deinit(); if (p.backdrop_texture) |t| c.SDL_DestroyTexture(t); if (p.backdrop_state) |state| c.SDL_DestroyGPURenderState(state); if (p.backdrop_shader) |shader| if (c.SDL_GetGPURendererDevice(p.renderer)) |device| c.SDL_ReleaseGPUShader(device, shader); @@ -220,6 +223,7 @@ pub const Painter = struct { /// Back to the canvas, blending alpha, unclipped: where overlays draw /// and screenshots read. pub fn end(p: *Painter) void { + p.glyphs.flush(p.renderer); p.flush(); if (p.target != .none) { p.target = .none; @@ -231,11 +235,11 @@ pub const Painter = struct { /// Draws one graphics command. Other commands are not graphics: ignored. pub fn draw(p: *Painter, command: abi.Command) void { + if (command != .text) p.glyphs.flush(p.renderer); if (command != .shape) p.flush(); const renderer = p.renderer; switch (command) { .clear => |color| { - if (@import("builtin").os.tag == .linux and p.target == .none) c.sk_text_begin_frame(renderer); // A target holds premultiplied colour, as a canvas cleared // to a translucent colour does. setColor(renderer, if (p.target == .none) color else premultiply(color)); @@ -249,10 +253,7 @@ pub const Painter = struct { .shape => |s| p.shape(s.color, s.points, s.ends, s.indices, s.colors), .image => |img| p.drawImage(img.image, img.dst, img.src, img.tint, img.corners), .backdrop => |b| p.backdrop(b.dst, b.fragment, b.uniforms), - .text => |text| { - _ = c.SDL_SetRenderDrawBlendMode(renderer, c.SDL_BLENDMODE_BLEND); - drawText(renderer, text.x, text.y, text.scale, text.bytes, text.color, text.face, if (text.raster > 0) text.raster else text.scale); - }, + .text => |text| p.glyphs.draw(renderer, text), .clip => |clip| { if (clip) |r| { const rect: c.SDL_Rect = .{ .x = @intFromFloat(r.x), .y = @intFromFloat(r.y), .w = @intFromFloat(r.w), .h = @intFromFloat(r.h) }; @@ -410,11 +411,19 @@ pub const Painter = struct { _ = c.SDL_SetGPURenderState(p.renderer, null); } + fn missing(p: *Painter, dst: abi.Rect) void { + _ = c.SDL_SetRenderDrawBlendMode(p.renderer, c.SDL_BLENDMODE_BLEND); + setColor(p.renderer, .{ .r = 110, .g = 30, .b = 70 }); + _ = c.SDL_RenderFillRect(p.renderer, &frect(dst)); + p.glyphs.draw(p.renderer, .{ .x = dst.x + 4, .y = dst.y + 4, .scale = 1, .bytes = "missing image", .color = .{ .r = 255, .g = 210, .b = 240 } }); + p.glyphs.flush(p.renderer); + } + fn drawImage(p: *Painter, handle: abi.Image, dst: abi.Rect, src_rect: abi.Rect, tint: abi.Color, corners: ?abi.Corners) void { const renderer = p.renderer; const entry = p.images.get(handle); const texture = if (entry) |e| e.texture else null; - const t = texture orelse return missing(renderer, dst); + const t = texture orelse return p.missing(dst); const e = entry.?; if (handle == p.target) return; // Drawing into itself: nothing. if (e.stale) p.resolve(e); @@ -604,45 +613,15 @@ fn fcolor(color: abi.Color) c.SDL_FColor { }; } -/// 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, face: abi.Face, raster: f32) void { - if (@import("builtin").os.tag == .linux and c.sk_text_draw_scaled(renderer, @backingInt(face), x, y, scale, raster, 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]); - buf[len] = 0; - setColor(renderer, color); - var sx: f32 = 1; - 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); -} - -/// What `drawText` gives `bytes` at `scale`: its width and the height of -/// its line, in canvas units (`Host.textSize`). -pub fn textSize(renderer: *c.SDL_Renderer, scale: f32, bytes: []const u8, face: abi.Face) [2]f32 { - var size: [2]f32 = .{ 0, 0 }; - if (@import("builtin").os.tag == .linux and c.sk_text_measure_face(renderer, @backingInt(face), scale, bytes.ptr, bytes.len, &size[0], &size[1])) return size; - // The debug font's 8 px cells. - const s = @max(scale, 1); - return .{ @as(f32, @floatFromInt(bytes.len)) * 8 * s, 8 * s }; +/// What `Command.text` covers at `scale`: the width of `bytes` and the +/// height of its line, in canvas units (`Host.textSize`, `textRun`). +pub fn textSize(scale: f32, bytes: []const u8, face: abi.Face) [2]f32 { + if (!(scale > 0) or !std.math.isFinite(scale)) return .{ 0, 0 }; + var advance: i32 = 0; + var ascent: i32 = 0; + var height: i32 = 0; + _ = c.sk_text_shape(@backingInt(face), Glyphs.pixels(scale), bytes.ptr, bytes.len, null, 0, &advance, &ascent, &height); + return .{ @floatFromInt(advance), @floatFromInt(height) }; } fn premultiply(color: abi.Color) abi.Color { @@ -650,12 +629,6 @@ fn premultiply(color: abi.Color) abi.Color { return .{ .r = @intCast((color.r * a + 127) / 255), .g = @intCast((color.g * a + 127) / 255), .b = @intCast((color.b * a + 127) / 255), .a = color.a }; } -fn missing(renderer: *c.SDL_Renderer, dst: abi.Rect) void { - _ = c.SDL_SetRenderDrawBlendMode(renderer, c.SDL_BLENDMODE_BLEND); - setColor(renderer, .{ .r = 110, .g = 30, .b = 70 }); - _ = c.SDL_RenderFillRect(renderer, &frect(dst)); - drawText(renderer, dst.x + 4, dst.y + 4, 1, "missing image", .{ .r = 255, .g = 210, .b = 240 }, .mono, 1); -} fn setColor(renderer: *c.SDL_Renderer, color: abi.Color) void { _ = c.SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a); |
