~mil/mepo

863d0e8c9d894b0b022deed6bdd77b3bceffdd20 — Miles Alan 2 years ago b9a74af text-cache-experiment-2
Text cache directly / w/o blit_multiline abstraction junk
1 files changed, 57 insertions(+), 11 deletions(-)

M src/Mepo.zig
M src/Mepo.zig => src/Mepo.zig +57 -11
@@ 306,17 306,63 @@ fn blit_pin(mepo: *@This(), pin: types.Pin, prev_pin: ?types.Pin, pin_group: u8,
    // Draw Pin label for current pin group
    if (pin.category == .Instructive and mepo.pin_group_active == pin_group) {
        const pin_label_border = .{ .value = 0xe8e8e8, .opacity = 255 };
        try mepo.blit_multiline_text(
            0x000000,
            pin_label_border,
            pin_label_bg,
            .{},
            x,
            y,
            config.ZoomLevelToPinFontSize[mepo.zoom],
            "{s}",
            .{pin.name},
        );

        const text = text: {
            if (mepo.texture_text_cache.get(pin.name)) |p| {
                break :text p;
            } else {
                const text_surf = try utilsdl.errorcheck_ptr(
                    sdl.SDL_Surface,
                    sdl.TTF_RenderText_Blended(mepo.fonts_normal[config.ZoomLevelToPinFontSize[mepo.zoom]], @ptrCast([*c]const u8, pin.name), (types.Color{ .value = 0x000000 }).sdl()),
                );
                defer sdl.SDL_FreeSurface(text_surf);
                const text = try utilsdl.errorcheck_ptr(sdl.SDL_Texture, sdl.SDL_CreateTextureFromSurface(mepo.renderer, text_surf));


                const textt = try utilsdl.errorcheck_ptr(
                    sdl.SDL_Texture, 
                    sdl.SDL_CreateTexture(
                        mepo.renderer, 
                        sdl.SDL_GetWindowPixelFormat(mepo.window),
                        sdl.SDL_TEXTUREACCESS_TARGET,
                        text_surf.w,
                        text_surf.h
                    )
                );

                const dest_rect = sdl.SDL_Rect{ .x = 0, .y = 0, .w = text_surf.w, .h = text_surf.h};

                _ = sdl.SDL_SetRenderTarget(mepo.renderer, textt);
                try utilsdl.sdl_renderer_rect(mepo.renderer, pin_label_border, dest_rect, .Fill);
                try utilsdl.sdl_renderer_rect(mepo.renderer, pin_label_bg, dest_rect, .Draw);
                try utilsdl.errorcheck(sdl.SDL_RenderCopy(mepo.renderer, text, null, &dest_rect));


                try mepo.texture_text_cache.put(pin.name, textt);
                break :text text;
            }
        };
        _ = sdl.SDL_SetRenderTarget(mepo.renderer, null);


        var textw : c_int = undefined;
        var texth : c_int = undefined;
        _ = sdl.SDL_QueryTexture(text, null, null,  &textw, &texth);
        const dest_rect = .{ .x = x, .y = y, .w = textw, .h = texth };
        try utilsdl.errorcheck(sdl.SDL_RenderCopy(mepo.renderer, text, null, &dest_rect));


        //try mepo.blit_multiline_text(
        //    0x000001,
        //    pin_label_border,
        //    pin_label_bg,
        //    .{},
        //    x,
        //    y,
        //    config.ZoomLevelToPinFontSize[mepo.zoom],
        //    "{s}",
        //    .{pin.name},
        //);
    }

    // Draw connecting line for ordered pin group