From c481e52558d23e120bba4f22dc7c9ffe0e573a9b Mon Sep 17 00:00:00 2001 From: Alibek Omarov Date: Wed, 25 Jan 2023 02:37:30 +0300 Subject: [PATCH] engine: client: consolidate variable and quake fixed width font loading functions --- engine/client/cl_scrn.c | 89 +++++++---------------------------------- engine/client/client.h | 2 + engine/client/console.c | 77 ++++++++++++++++++++--------------- 3 files changed, 62 insertions(+), 106 deletions(-) diff --git a/engine/client/cl_scrn.c b/engine/client/cl_scrn.c index 3949d4ae..602c13a9 100644 --- a/engine/client/cl_scrn.c +++ b/engine/client/cl_scrn.c @@ -577,77 +577,6 @@ void SCR_UpdateScreen( void ) V_PostRender(); } -static qboolean SCR_LoadFixedWidthFont( const char *fontname ) -{ - int i, fontWidth; - - if( cls.creditsFont.valid ) - return true; // already loaded - - if( !FS_FileExists( fontname, false )) - return false; - - cls.creditsFont.hFontTexture = ref.dllFuncs.GL_LoadTexture( fontname, NULL, 0, TF_IMAGE|TF_KEEP_SOURCE|TF_NEAREST ); - R_GetTextureParms( &fontWidth, NULL, cls.creditsFont.hFontTexture ); - cls.creditsFont.charHeight = clgame.scrInfo.iCharHeight = fontWidth / 16; - cls.creditsFont.type = FONT_FIXED; - cls.creditsFont.valid = true; - - // build fixed rectangles - for( i = 0; i < 256; i++ ) - { - cls.creditsFont.fontRc[i].left = (i * (fontWidth / 16)) % fontWidth; - cls.creditsFont.fontRc[i].right = cls.creditsFont.fontRc[i].left + fontWidth / 16; - cls.creditsFont.fontRc[i].top = (i / 16) * (fontWidth / 16); - cls.creditsFont.fontRc[i].bottom = cls.creditsFont.fontRc[i].top + fontWidth / 16; - cls.creditsFont.charWidths[i] = clgame.scrInfo.charWidths[i] = fontWidth / 16; - } - - return true; -} - -static qboolean SCR_LoadVariableWidthFont( const char *fontname ) -{ - int i, fontWidth; - byte *buffer; - fs_offset_t length; - qfont_t *src; - - if( cls.creditsFont.valid ) - return true; // already loaded - - if( !FS_FileExists( fontname, false )) - return false; - - cls.creditsFont.hFontTexture = ref.dllFuncs.GL_LoadTexture( fontname, NULL, 0, TF_IMAGE|TF_NEAREST ); - R_GetTextureParms( &fontWidth, NULL, cls.creditsFont.hFontTexture ); - - // half-life font with variable chars witdh - buffer = FS_LoadFile( fontname, &length, false ); - - // setup creditsfont - if( buffer && length >= sizeof( qfont_t )) - { - src = (qfont_t *)buffer; - cls.creditsFont.charHeight = clgame.scrInfo.iCharHeight = src->rowheight; - cls.creditsFont.type = FONT_VARIABLE; - - // build rectangles - for( i = 0; i < 256; i++ ) - { - cls.creditsFont.fontRc[i].left = (word)src->fontinfo[i].startoffset % fontWidth; - cls.creditsFont.fontRc[i].right = cls.creditsFont.fontRc[i].left + src->fontinfo[i].charwidth; - cls.creditsFont.fontRc[i].top = (word)src->fontinfo[i].startoffset / fontWidth; - cls.creditsFont.fontRc[i].bottom = cls.creditsFont.fontRc[i].top + src->rowheight; - cls.creditsFont.charWidths[i] = clgame.scrInfo.charWidths[i] = src->fontinfo[i].charwidth; - } - cls.creditsFont.valid = true; - } - if( buffer ) Mem_Free( buffer ); - - return true; -} - /* ================ SCR_LoadCreditsFont @@ -657,6 +586,7 @@ INTERNAL RESOURCE */ void SCR_LoadCreditsFont( void ) { + cl_font_t *const font = &cls.creditsFont; qboolean success = false; dword crc = 0; @@ -669,17 +599,28 @@ void SCR_LoadCreditsFont( void ) "creditsfont_%s.fnt", Cvar_VariableString( "con_charset" )) > 0 ) { if( FS_FileExists( charsetFnt, false )) - success = SCR_LoadVariableWidthFont( charsetFnt ); + success = Con_LoadVariableWidthFont( charsetFnt, font, 1.0f, TF_FONT ); } } - if( !success && !SCR_LoadVariableWidthFont( "gfx/creditsfont.fnt" )) + if( !success && !Con_LoadVariableWidthFont( "gfx/creditsfont.fnt", font, 1.0f, TF_FONT )) { - if( !SCR_LoadFixedWidthFont( "gfx/conchars" )) + if( !Con_LoadFixedWidthFont( "gfx/conchars", font, 1.0f, TF_FONT )) { Con_DPrintf( S_ERROR "failed to load HUD font\n" ); } } + + // copy font size for client.dll + if( success ) + { + int i; + + clgame.scrInfo.iCharHeight = cls.creditsFont.charHeight; + + for( i = 0; i < ARRAYSIZE( cls.creditsFont.charWidths ); i++ ) + clgame.scrInfo.charWidths[i] = cls.creditsFont.charWidths[i]; + } } /* diff --git a/engine/client/client.h b/engine/client/client.h index f473d426..fc093312 100644 --- a/engine/client/client.h +++ b/engine/client/client.h @@ -1044,6 +1044,8 @@ void Con_Bottom( void ); void Con_Top( void ); void Con_PageDown( int lines ); void Con_PageUp( int lines ); +qboolean Con_LoadVariableWidthFont( const char *fontname, cl_font_t *font, float scale, uint texFlags ); +qboolean Con_LoadFixedWidthFont( const char *fontname, cl_font_t *font, float scale, uint texFlags ); // // s_main.c diff --git a/engine/client/console.c b/engine/client/console.c index cf6e1e28..03823b52 100644 --- a/engine/client/console.c +++ b/engine/client/console.c @@ -560,9 +560,10 @@ qboolean Con_FixedFont( void ) return false; } -static qboolean Con_LoadFixedWidthFont( const char *fontname, cl_font_t *font ) +qboolean Con_LoadFixedWidthFont( const char *fontname, cl_font_t *font, float scale, uint texFlags ) { - int i, fontWidth; + int fontWidth; + int i; if( font->valid ) return true; // already loaded @@ -570,13 +571,12 @@ static qboolean Con_LoadFixedWidthFont( const char *fontname, cl_font_t *font ) if( !FS_FileExists( fontname, false )) return false; - // keep source to print directly into conback image - font->hFontTexture = ref.dllFuncs.GL_LoadTexture( fontname, NULL, 0, TF_FONT|TF_KEEP_SOURCE ); + font->hFontTexture = ref.dllFuncs.GL_LoadTexture( fontname, NULL, 0, texFlags ); R_GetTextureParms( &fontWidth, NULL, font->hFontTexture ); if( font->hFontTexture && fontWidth != 0 ) { - font->charHeight = fontWidth / 16 * con_fontscale->value; + font->charHeight = fontWidth / 16 * scale; font->type = FONT_FIXED; // build fixed rectangles @@ -586,7 +586,7 @@ static qboolean Con_LoadFixedWidthFont( const char *fontname, cl_font_t *font ) font->fontRc[i].right = font->fontRc[i].left + fontWidth / 16; font->fontRc[i].top = (i / 16) * (fontWidth / 16); font->fontRc[i].bottom = font->fontRc[i].top + fontWidth / 16; - font->charWidths[i] = fontWidth / 16 * con_fontscale->value; + font->charWidths[i] = fontWidth / 16 * scale; } font->valid = true; } @@ -594,12 +594,13 @@ static qboolean Con_LoadFixedWidthFont( const char *fontname, cl_font_t *font ) return true; } -static qboolean Con_LoadVariableWidthFont( const char *fontname, cl_font_t *font ) +qboolean Con_LoadVariableWidthFont( const char *fontname, cl_font_t *font, float scale, uint texFlags ) { - int i, fontWidth; - byte *buffer; fs_offset_t length; - qfont_t *src; + qfont_t *src; + byte *buffer; + int fontWidth; + int i; if( font->valid ) return true; // already loaded @@ -607,7 +608,7 @@ static qboolean Con_LoadVariableWidthFont( const char *fontname, cl_font_t *font if( !FS_FileExists( fontname, false )) return false; - font->hFontTexture = ref.dllFuncs.GL_LoadTexture( fontname, NULL, 0, TF_FONT|TF_NEAREST ); + font->hFontTexture = ref.dllFuncs.GL_LoadTexture( fontname, NULL, 0, texFlags ); R_GetTextureParms( &fontWidth, NULL, font->hFontTexture ); // setup consolefont @@ -619,7 +620,7 @@ static qboolean Con_LoadVariableWidthFont( const char *fontname, cl_font_t *font if( buffer && length >= sizeof( qfont_t )) { src = (qfont_t *)buffer; - font->charHeight = src->rowheight * con_fontscale->value; + font->charHeight = src->rowheight * scale; font->type = FONT_VARIABLE; // build rectangles @@ -629,7 +630,7 @@ static qboolean Con_LoadVariableWidthFont( const char *fontname, cl_font_t *font font->fontRc[i].right = font->fontRc[i].left + src->fontinfo[i].charwidth; font->fontRc[i].top = (word)src->fontinfo[i].startoffset / fontWidth; font->fontRc[i].bottom = font->fontRc[i].top + src->rowheight; - font->charWidths[i] = src->fontinfo[i].charwidth * con_fontscale->value; + font->charWidths[i] = src->fontinfo[i].charwidth * scale; } font->valid = true; } @@ -648,32 +649,45 @@ INTERNAL RESOURCE */ static void Con_LoadConsoleFont( int fontNumber, cl_font_t *font ) { - const char *path = NULL; - dword crc = 0; + qboolean success = false; - if( font->valid ) return; // already loaded - - // replace default fonts.wad textures by current charset's font - if( !CRC32_File( &crc, "fonts.wad" ) || crc == 0x3c0a0029 ) - { - const char *path2 = va("font%i_%s.fnt", fontNumber, Cvar_VariableString( "con_charset" ) ); - if( FS_FileExists( path2, false ) ) - path = path2; - } + if( font->valid ) + return; // already loaded // loading conchars if( Sys_CheckParm( "-oldfont" )) - Con_LoadVariableWidthFont( "gfx/conchars.fnt", font ); + { + success = Con_LoadVariableWidthFont( "gfx/conchars.fnt", font, con_fontscale->value, TF_FONT|TF_NEAREST ); + } else { - if( !path ) - path = va( "fonts/font%i", fontNumber ); + string path; + dword crc = 0; - Con_LoadVariableWidthFont( path, font ); + // replace default fonts.wad textures by current charset's font + if( !CRC32_File( &crc, "fonts.wad" ) || crc == 0x3c0a0029 ) + { + if( Q_snprintf( path, sizeof( path ), + "font%i_%s.fnt", fontNumber, Cvar_VariableString( "con_charset" )) > 0 ) + { + success = Con_LoadVariableWidthFont( path, font, con_fontscale->value, TF_FONT|TF_NEAREST ); + } + } + + if( !success ) + { + Q_snprintf( path, sizeof( path ), "fonts/font%i", fontNumber ); + success = Con_LoadVariableWidthFont( path, font, con_fontscale->value, TF_FONT|TF_NEAREST ); + } } - // quake fixed font as fallback - if( !font->valid ) Con_LoadFixedWidthFont( "gfx/conchars", font ); + if( !success ) + { + // quake fixed font as fallback + // keep source to print directly into conback image + if( !Con_LoadFixedWidthFont( "gfx/conchars", font, con_fontscale->value, TF_FONT|TF_KEEP_SOURCE )) + Con_DPrintf( S_ERROR "failed to load console font\n" ); + } } /* @@ -2397,7 +2411,7 @@ void Con_RunConsole( void ) con.vislines = con.showlines; } - if( FBitSet( con_charset->flags, FCVAR_CHANGED ) || + if( FBitSet( con_charset->flags, FCVAR_CHANGED ) || FBitSet( con_fontscale->flags, FCVAR_CHANGED ) || FBitSet( con_fontnum->flags, FCVAR_CHANGED ) || FBitSet( cl_charset->flags, FCVAR_CHANGED ) ) @@ -2428,7 +2442,6 @@ void Con_RunConsole( void ) ClearBits( con_fontnum->flags, FCVAR_CHANGED ); ClearBits( con_fontscale->flags, FCVAR_CHANGED ); ClearBits( cl_charset->flags, FCVAR_CHANGED ); - } }