~mil/mepo

b9a74af0c69aa4591e16882bb19d3c6974533010 — Miles Alan 2 years ago 137a5a0 text-cache-experiment
Experiment with caching text textures rendered
1 files changed, 27 insertions(+), 12 deletions(-)

M src/Mepo.zig
M src/Mepo.zig => src/Mepo.zig +27 -12
@@ 39,6 39,7 @@ pin_group_active: u8 = 0,
pin_group_active_item: ?u32 = null,
pin_groups: [10]types.PinGroup = undefined,
renderer: *sdl.SDL_Renderer,
texture_text_cache: std.StringHashMap(*sdl.SDL_Texture),
table_clicks: std.array_hash_map.AutoArrayHashMap(types.ClickInput, []const u8),
table_keybindings: std.array_hash_map.AutoArrayHashMap(types.KeyInput, []const u8),
table_signals: std.array_hash_map.AutoArrayHashMap(u6, []const u8),


@@ 972,18 973,30 @@ pub fn blit_multiline_text(
        while (lines_it.next()) |line| {
            if (textures_array_size + 1 > textures_array.len - 1) break;

            const line_sentinel_terminated = try mepo.allocator.dupeZ(u8, line);
            defer mepo.allocator.free(line_sentinel_terminated);

            const text_surf = try utilsdl.errorcheck_ptr(
                sdl.SDL_Surface,
                sdl.TTF_RenderText_Blended(mepo.fonts_normal[font_size], @ptrCast([*c]const u8, line_sentinel_terminated), (types.Color{ .value = color }).sdl()),
            );
            defer sdl.SDL_FreeSurface(text_surf);
            const text = text: {
                if (mepo.texture_text_cache.get(line)) |stored| {
                    std.debug.warn("found", .{});
                    break :text stored;
                } else {
                    std.debug.warn("store", .{});
                    const line_sentinel_terminated = try mepo.allocator.dupeZ(u8, line);
                    defer mepo.allocator.free(line_sentinel_terminated);
                    const text_surf = try utilsdl.errorcheck_ptr(
                        sdl.SDL_Surface,
                        sdl.TTF_RenderText_Blended(mepo.fonts_normal[font_size], @ptrCast([*c]const u8, line_sentinel_terminated), (types.Color{ .value = color }).sdl()),
                    );
                    defer sdl.SDL_FreeSurface(text_surf);
                    const text = try utilsdl.errorcheck_ptr(sdl.SDL_Texture, sdl.SDL_CreateTextureFromSurface(mepo.renderer, text_surf));
                    try mepo.texture_text_cache.put(line, text);
                    break :text text;
                }
            };

            const text = try utilsdl.errorcheck_ptr(sdl.SDL_Texture, sdl.SDL_CreateTextureFromSurface(mepo.renderer, text_surf));
            textures_height += text_surf.h;
            textures_width = std.math.max(textures_width, text_surf.w);
            var textw : c_int = undefined;
            var texth : c_int = undefined;
            _ = sdl.SDL_QueryTexture(text, null, null,  &textw, &texth);
            textures_height += texth;
            textures_width = std.math.max(textures_width, textw);
            textures_array[textures_array_size] = text;
            textures_array_size += 1;
        }


@@ 1023,7 1036,7 @@ pub fn blit_multiline_text(

        // Reder text
        for (textures) |texture| {
            defer sdl.SDL_DestroyTexture(texture);
            //defer sdl.SDL_DestroyTexture(texture);
            var dest_rect = sdl.SDL_Rect{
                .x = x_off,
                .y = y_off,


@@ 1192,6 1205,7 @@ pub fn init(allocator: *std.mem.Allocator, tile_cache: *TileCache, use_sw_render
            -1,
            if (use_sw_renderer) sdl.SDL_RENDERER_SOFTWARE else sdl.SDL_RENDERER_ACCELERATED,
        )),
        .texture_text_cache = std.StringHashMap(*sdl.SDL_Texture).init(allocator),
        .table_keybindings = std.array_hash_map.AutoArrayHashMap(types.KeyInput, []const u8).init(allocator),
        .table_clicks = std.array_hash_map.AutoArrayHashMap(types.ClickInput, []const u8).init(allocator),
        .table_signals = std.array_hash_map.AutoArrayHashMap(u6, []const u8).init(allocator),


@@ 1199,6 1213,7 @@ pub fn init(allocator: *std.mem.Allocator, tile_cache: *TileCache, use_sw_render
        .uibuttons = std.ArrayList(types.UIButton).init(allocator),
        .window = window,
    });
    try instance.texture_text_cache.ensureCapacity(50000);

    // Set renderer for tile cache
    tile_cache.renderer = instance.renderer;