From 5468c81f47e6644d938e3cfedd21567b2936cd72 Mon Sep 17 00:00:00 2001 From: NaitLee Date: Sat, 30 Dec 2023 20:40:34 +0800 Subject: [PATCH] reload a dp_font only when its cmdline changes, to prevent unnecessary reloads; introduce cvar `r_font_always_reload` Signed-off-by: NaitLee --- draw.h | 3 +++ gl_draw.c | 20 ++++++++++++++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/draw.h b/draw.h index c703c80c..8496bb73 100644 --- a/draw.h +++ b/draw.h @@ -93,6 +93,7 @@ typedef struct ft2_settings_s #define MAX_FONT_SIZES 16 #define MAX_FONT_FALLBACKS 3 +#define MAX_FONT_CMDLINE MAX_QPATH * (MAX_FONT_FALLBACKS + 1) typedef struct dp_font_s { cachepic_t *pic; @@ -109,6 +110,8 @@ typedef struct dp_font_s struct ft2_font_s *ft2; ft2_settings_t settings; + + char cmdline[MAX_FONT_CMDLINE]; } dp_font_t; diff --git a/gl_draw.c b/gl_draw.c index eb8630a0..bc4d34f1 100644 --- a/gl_draw.c +++ b/gl_draw.c @@ -61,6 +61,7 @@ cvar_t r_font_postprocess_shadow_y = {CF_CLIENT | CF_ARCHIVE, "r_font_postproces cvar_t r_font_postprocess_shadow_z = {CF_CLIENT | CF_ARCHIVE, "r_font_postprocess_shadow_z", "0", "font shadow Z shift amount, applied during blurring"}; cvar_t r_font_hinting = {CF_CLIENT | CF_ARCHIVE, "r_font_hinting", "3", "0 = no hinting, 1 = light autohinting, 2 = full autohinting, 3 = full hinting"}; cvar_t r_font_antialias = {CF_CLIENT | CF_ARCHIVE, "r_font_antialias", "1", "0 = monochrome, 1 = grey" /* , 2 = rgb, 3 = bgr" */}; +cvar_t r_font_always_reload = {CF_CLIENT | CF_ARCHIVE, "r_font_always_reload", "0", "reload a font even given the same loadfont command. useful for trying out different versions of the same font file"}; cvar_t r_nearest_2d = {CF_CLIENT | CF_ARCHIVE, "r_nearest_2d", "0", "use nearest filtering on all 2d textures (including conchars)"}; cvar_t r_nearest_conchars = {CF_CLIENT | CF_ARCHIVE, "r_nearest_conchars", "0", "use nearest filtering on conchars texture"}; @@ -338,6 +339,9 @@ void LoadFont(qbool override, const char *name, dp_font_t *fnt, float scale, flo strlcpy(fnt->texpath, name, sizeof(fnt->texpath)); // load the cvars when the font is FIRST loader fnt->settings.scale = scale; + // fix bad scale + if (fnt->settings.scale <= 0) + fnt->settings.scale = 1; fnt->settings.voffset = voffset; fnt->settings.antialias = r_font_antialias.integer; fnt->settings.hinting = r_font_hinting.integer; @@ -347,16 +351,13 @@ void LoadFont(qbool override, const char *name, dp_font_t *fnt, float scale, flo fnt->settings.shadowy = r_font_postprocess_shadow_y.value; fnt->settings.shadowz = r_font_postprocess_shadow_z.value; } - // fix bad scale - if (fnt->settings.scale <= 0) - fnt->settings.scale = 1; if(drawtexturepool == NULL) return; // before gl_draw_start, so will be loaded later if(fnt->ft2) { - // clear previous freetype font to prevent leaking memory + // we are going to reload. clear old ft2 data Font_UnloadFont(fnt->ft2); Mem_Free(fnt->ft2); fnt->ft2 = NULL; @@ -572,6 +573,16 @@ static void LoadFont_f(cmd_state_t *cmd) Con_Printf("font function not found\n"); return; } + else + { + if (strcmp(cmd->cmdline, f->cmdline) != 0 || r_font_always_reload.integer) + strlcpy(f->cmdline, cmd->cmdline, MAX_FONT_CMDLINE); + else + { + Con_DPrintf("LoadFont: font %s is unchanged\n", Cmd_Argv(cmd, 1)); + return; + } + } if(Cmd_Argc(cmd) < 3) filelist = "gfx/conchars"; @@ -742,6 +753,7 @@ void GL_Draw_Init (void) Cvar_RegisterVariable(&r_font_postprocess_shadow_z); Cvar_RegisterVariable(&r_font_hinting); Cvar_RegisterVariable(&r_font_antialias); + Cvar_RegisterVariable(&r_font_always_reload); Cvar_RegisterVariable(&r_textshadow); Cvar_RegisterVariable(&r_textbrightness); Cvar_RegisterVariable(&r_textcontrast); -- 2.39.2