From ffd84715d7377f69c9bbb3f29d36352220562cf7 Mon Sep 17 00:00:00 2001 From: Dan Cojocaru Date: Sat, 17 Feb 2024 09:51:10 +0100 Subject: [PATCH] Refactor text drawing --- src/departure.zig | 78 ++++++++++++++--------------------------------- src/home.zig | 14 ++------- src/main.zig | 15 +++++---- src/state.zig | 2 +- 4 files changed, 34 insertions(+), 75 deletions(-) diff --git a/src/departure.zig b/src/departure.zig index 6bd9c70..bbce6de 100644 --- a/src/departure.zig +++ b/src/departure.zig @@ -94,7 +94,7 @@ pub fn render(state: *AppState) !void { ds.max_next_trains = @min(ds.max_next_trains + 1, if (ds.fetch_result) |fr| @as(c_int, @intCast(fr.value.object.get("departures").?.array.items.len)) else 5); }, rl.KEY_T => { - ds.include_tram = !ds.include_tram; + ds.include_tram = !ds.include_tram; }, else => {}, } @@ -141,12 +141,7 @@ pub fn render(state: *AppState) !void { const destination = try std.fmt.allocPrintZ(allocator, "{s}", .{first.get("direction").?.string}); defer allocator.free(destination); var next_y = y; - if (state.db_font) |db_font| { - next_y += @intFromFloat(raylib.DrawAndMeasureTextEx(db_font, line.ptr, 16, @floatFromInt(y), 32, 1, rl.WHITE).y); - } else { - rl.DrawText(line.ptr, 16, y, 32, rl.WHITE); - next_y += 32; - } + next_y += @intFromFloat(raylib.DrawAndMeasureTextEx(state.font, line.ptr, 16, @floatFromInt(y), 32, 1, rl.WHITE).y); next_y += 16; if (ds.platform.items.len == 0) blk: { if (first.get("platform")) |platform_raw| { @@ -154,31 +149,23 @@ pub fn render(state: *AppState) !void { .string => |p| { const platform = std.fmt.allocPrintZ(allocator, "{s}", .{p}) catch break :blk; defer allocator.free(platform); - if (state.db_font) |db_font| { - raylib.DrawRightAlignedTextEx(db_font, platform.ptr, @floatFromInt(rl.GetScreenWidth() - 16), @floatFromInt(y), 40, 1, rl.WHITE); - } else { - raylib.DrawRightAlignedText(platform.ptr, rl.GetScreenWidth() - 16, y, 40, rl.WHITE); - } + raylib.DrawRightAlignedTextEx(state.font, platform.ptr, @floatFromInt(rl.GetScreenWidth() - 16), @floatFromInt(y), 40, 1, rl.WHITE); }, else => {}, } } } y = next_y; - if (state.db_font) |db_font| { - y += @intFromFloat(raylib.DrawAndMeasureTextEx( - db_font, - destination.ptr, - 16, - @floatFromInt(y), - 56, - 1, - rl.WHITE, - ).y); - } else { - rl.DrawText(destination.ptr, 16, y, 56, rl.WHITE); - y += 56; - } + y += @intFromFloat(raylib.DrawAndMeasureTextEx( + state.font, + destination.ptr, + 16, + @floatFromInt(y), + 56, + 1, + rl.WHITE, + ).y); + y += 16; } if (not_cancelled.items.len > 1) { @@ -189,21 +176,15 @@ pub fn render(state: *AppState) !void { var y = rl.GetScreenHeight() - (font_size + 8) * max_trains - 4; rl.DrawRectangle(0, y, rl.GetScreenWidth(), rl.GetScreenHeight(), rl.WHITE); y += 8; - const label_measurement_width = if (state.db_font) |db_font| @as(c_int, @intFromFloat(raylib.DrawAndMeasureTextEx( - db_font, + const label_measurement_width = @as(c_int, @intFromFloat(raylib.DrawAndMeasureTextEx( + state.font, if (max_trains == 1) "Next train: " else "Next trains: ", @floatFromInt(x), @floatFromInt(y), @floatFromInt(font_size), 1, db_blue, - ).x)) else raylib.DrawAndMeasureText( - if (max_trains == 1) "Next train: " else "Next trains: ", - x, - y, - font_size, - db_blue, - ).width; + ).x)); x += label_measurement_width; // Compute line name width @@ -220,14 +201,10 @@ pub fn render(state: *AppState) !void { }, ); defer allocator.free(next_train_line); - if (state.db_font) |db_font| { - line_name_width = @max( - line_name_width, - @as(c_int, @intFromFloat(rl.MeasureTextEx(db_font, next_train_line.ptr, @floatFromInt(font_size), 1).x)), - ); - } else { - line_name_width = @max(line_name_width, rl.MeasureText(next_train_line.ptr, font_size)); - } + line_name_width = @max( + line_name_width, + @as(c_int, @intFromFloat(rl.MeasureTextEx(state.font, next_train_line.ptr, @floatFromInt(font_size), 1).x)), + ); } const destionation_x = x + line_name_width; @@ -251,24 +228,15 @@ pub fn render(state: *AppState) !void { }, ); defer allocator.free(next_train_direction); - if (state.db_font) |db_font| { - rl.DrawTextEx(db_font, next_train_line.ptr, .{ .x = @floatFromInt(x), .y = @floatFromInt(y) }, font_size, 1, db_blue); - rl.DrawTextEx(db_font, next_train_direction.ptr, .{ .x = @floatFromInt(destionation_x), .y = @floatFromInt(y) }, font_size, 1, db_blue); - } else { - rl.DrawText(next_train_line.ptr, x, y, font_size, db_blue); - rl.DrawText(next_train_direction.ptr, destionation_x, y, font_size, db_blue); - } + rl.DrawTextEx(state.font, next_train_line.ptr, .{ .x = @floatFromInt(x), .y = @floatFromInt(y) }, font_size, 1, db_blue); + rl.DrawTextEx(state.font, next_train_direction.ptr, .{ .x = @floatFromInt(destionation_x), .y = @floatFromInt(y) }, font_size, 1, db_blue); if (ds.platform.items.len == 0) blk: { if (second.get("platform")) |platform_raw| { switch (platform_raw) { .string => |p| { const platform = std.fmt.allocPrintZ(allocator, "{s}", .{p}) catch break :blk; defer allocator.free(platform); - if (state.db_font) |db_font| { - raylib.DrawRightAlignedTextEx(db_font, platform.ptr, @floatFromInt(rl.GetScreenWidth() - 16), @floatFromInt(y), @floatFromInt(font_size), 1, db_blue); - } else { - raylib.DrawRightAlignedText(platform.ptr, rl.GetScreenWidth() - 16, y, font_size, db_blue); - } + raylib.DrawRightAlignedTextEx(state.font, platform.ptr, @floatFromInt(rl.GetScreenWidth() - 16), @floatFromInt(y), @floatFromInt(font_size), 1, db_blue); }, else => {}, } diff --git a/src/home.zig b/src/home.zig index 9a5ddc6..5fa2658 100644 --- a/src/home.zig +++ b/src/home.zig @@ -136,13 +136,9 @@ pub fn render(state: *AppState) !void { const body_size: c_int = 28; rl.ClearBackground(rl.BLACK); - x += raylib.DrawAndMeasureText("Station: ", x, y, title_size, rl.WHITE).width + 8; + x += @intFromFloat(raylib.DrawAndMeasureTextEx(state.font, "Station: ", @floatFromInt(x), @floatFromInt(y), @floatFromInt(title_size), 1, rl.WHITE).x + 8); rl.DrawLine(x, y + title_size + 2, rl.GetScreenWidth() - 16, y + title_size + 2, rl.WHITE); - if (state.db_font) |db_font| { - rl.DrawTextEx(db_font, hs.station_name.items.ptr, rl.Vector2{ .x = @floatFromInt(x), .y = @floatFromInt(y) }, title_size, 0.9, rl.WHITE); - } else { - rl.DrawText(hs.station_name.items.ptr, x, y, title_size, rl.WHITE); - } + rl.DrawTextEx(state.font, hs.station_name.items.ptr, rl.Vector2{ .x = @floatFromInt(x), .y = @floatFromInt(y) }, title_size, 0.9, rl.WHITE); y += title_size + 2 + 16; @@ -175,11 +171,7 @@ pub fn render(state: *AppState) !void { } } - if (state.db_font) |db_font| { - rl.DrawTextEx(db_font, suggestion.name.ptr, rl.Vector2{ .x = @floatFromInt(x), .y = @floatFromInt(y) }, body_size, 0.9, color); - } else { - rl.DrawText(suggestion.name.ptr, x, y, body_size, color); - } + rl.DrawTextEx(state.font, suggestion.name.ptr, rl.Vector2{ .x = @floatFromInt(x), .y = @floatFromInt(y) }, body_size, 0.9, color); y += body_size + 2; } diff --git a/src/main.zig b/src/main.zig index 9f2a3ad..a1b5ccc 100644 --- a/src/main.zig +++ b/src/main.zig @@ -15,13 +15,12 @@ pub fn main() !void { defer rl.CloseWindow(); const font = blk: { - var cp_cnt: c_int = 0; - const cp = rl.LoadCodepoints("aäbcdeèéfghijklmnoöpqrsßtuüvwxyzAÄBCDEÈÉFGHIJKLMNOÖPQRSẞTUÜVWXYZ0123456789-_,()/\\:+", &cp_cnt,); - const maybeFont = rl.LoadFontEx("./db.ttf", 64, cp, cp_cnt); - if (std.meta.eql(maybeFont, rl.GetFontDefault())) { - break :blk null; - } - break :blk maybeFont; + var cp_cnt: c_int = 0; + const cp = rl.LoadCodepoints( + "aäbcdeèéfghijklmnoöpqrsßtuüvwxyzAÄBCDEÈÉFGHIJKLMNOÖPQRSẞTUÜVWXYZ0123456789-_,()/\\:+", + &cp_cnt, + ); + break :blk rl.LoadFontEx("./db.ttf", 64, cp, cp_cnt); }; var station_name_buffer: [100]u8 = .{0} ** 100; @@ -29,7 +28,7 @@ pub fn main() !void { var station_id_buffer: [10]u8 = .{0} ** 10; var appState = AppState{ .allocator = allocator, - .db_font = font, + .font = font, .home_screen_state = .{ .station_name = std.ArrayListUnmanaged(u8).initBuffer(&station_name_buffer), }, diff --git a/src/state.zig b/src/state.zig index eb33ca5..e0f9532 100644 --- a/src/state.zig +++ b/src/state.zig @@ -33,7 +33,7 @@ pub const DepartureScreenState = struct { allocator: std.mem.Allocator, close_app: bool = false, -db_font: ?rl.Font = null, +font: rl.Font, screen: Screen = .home, home_screen_state: HomeScreenState, departure_screen_state: DepartureScreenState,