From 626be93750560b424b3fbca2373c94d897c251fa Mon Sep 17 00:00:00 2001 From: 54ac Date: Sat, 2 May 2026 00:05:33 +0200 Subject: [PATCH] Use mainui's truetype font rendering for on screen text --- BaseMenu.h | 1 + HudFont.cpp | 49 ++++++++++++++++++++++++++++++++++ Utils.h | 3 +++ font/FontManager.cpp | 9 +++++++ sdk_includes/engine/menu_int.h | 3 +++ udll_int.cpp | 5 +++- 6 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 HudFont.cpp diff --git a/BaseMenu.h b/BaseMenu.h index 4677495..b462330 100755 --- a/BaseMenu.h +++ b/BaseMenu.h @@ -95,6 +95,7 @@ typedef struct HFont hBoldFont; HFont hLightBlur; HFont hHeavyBlur; + HFont hHudFont; bool m_fDemosPlayed; bool m_fNoOldBackground; diff --git a/HudFont.cpp b/HudFont.cpp new file mode 100644 index 0000000..12be025 --- /dev/null +++ b/HudFont.cpp @@ -0,0 +1,49 @@ +/* +HudFont.cpp - engine callbacks for drawing HUD text +Uses mainui's font rendering stack instead of the engine bitmap font +*/ +#include "extdll_menu.h" +#include "BaseMenu.h" +#include "font/FontManager.h" +#include "font/BaseFontBackend.h" + +// all coordinates and return values are in real screen pixels + +int UI_DrawHudCharacter( int x, int y, int ch, int r, int g, int b ) +{ + if( !g_FontMgr || !uiStatic.hHudFont ) + return 0; + + CBaseFont *font = g_FontMgr->GetIFontFromHandle( uiStatic.hHudFont ); + if( !font ) + return 0; + + unsigned int color = PackRGBA( r, g, b, 255 ); + return font->DrawCharacter( ch, Point( x, y ), font->GetTall( ), color, true ); +} + +int UI_GetHudFontHeight( void ) +{ + if( !g_FontMgr || !uiStatic.hHudFont ) + return 0; + + CBaseFont *font = g_FontMgr->GetIFontFromHandle( uiStatic.hHudFont ); + if( !font ) + return 0; + + return font->GetHeight( ); +} + +int UI_GetHudCharWidth( int ch ) +{ + if( !g_FontMgr || !uiStatic.hHudFont ) + return 0; + + CBaseFont *font = g_FontMgr->GetIFontFromHandle( uiStatic.hHudFont ); + if( !font ) + return 0; + + int a, b, c; + font->GetCharABCWidths( ch, a, b, c ); + return a + b + c; +} diff --git a/Utils.h b/Utils.h index 3387649..13d8a84 100644 --- a/Utils.h +++ b/Utils.h @@ -166,6 +166,9 @@ int KEY_GetKey( const char *binding ); // ripped out from engine char *StringCopy( const char *input ); // copy string into new memory void Com_EscapeCommand( char *newCommand, const char *oldCommand, int len ); void UI_EnableTextInput( bool enable ); +int UI_DrawHudCharacter( int x, int y, int ch, int r, int g, int b ); +int UI_GetHudFontHeight( void ); +int UI_GetHudCharWidth( int ch ); void UI_LoadCustomStrings( void ); const char *L( const char *szStr ); // L means Localize! diff --git a/font/FontManager.cpp b/font/FontManager.cpp index e319369..e4c33c3 100644 --- a/font/FontManager.cpp +++ b/font/FontManager.cpp @@ -104,6 +104,15 @@ void CFontManager::VidInit( void ) .Create(); prevScale = scale; } + + // rebuild hud font every VidInit since hud_truetype_size may change + if( uiStatic.hHudFont ) + g_FontMgr->DeleteFont( uiStatic.hHudFont ); + float hudSize = EngFuncs::GetCvarFloat( "hud_truetype_size" ); + if( hudSize <= 0.0f ) + hudSize = 16.0f; + uiStatic.hHudFont = CFontBuilder( DEFAULT_MENUFONT, hudSize * scale, DEFAULT_WEIGHT ) + .Create(); } void CFontManager::DeleteAllFonts() diff --git a/sdk_includes/engine/menu_int.h b/sdk_includes/engine/menu_int.h index f86c8b1..e84de66 100644 --- a/sdk_includes/engine/menu_int.h +++ b/sdk_includes/engine/menu_int.h @@ -246,6 +246,9 @@ typedef struct void (*pfnConnectionProgress_Connect)( const char *server ); // NULL for local server void (*pfnConnectionProgress_ChangeLevel)( void ); void (*pfnConnectionProgress_ParseServerInfo)( const char *server ); + int (*pfnDrawHudCharacter)( int x, int y, int ch, int r, int g, int b ); + int (*pfnGetHudFontHeight)( void ); + int (*pfnGetHudCharWidth)( int ch ); } UI_EXTENDED_FUNCTIONS; typedef int (*MENUAPI)( UI_FUNCTIONS *pFunctionTable, ui_enginefuncs_t* engfuncs, ui_globalvars_t *pGlobals ); diff --git a/udll_int.cpp b/udll_int.cpp index 1d63e55..36708ca 100644 --- a/udll_int.cpp +++ b/udll_int.cpp @@ -76,7 +76,10 @@ static UI_EXTENDED_FUNCTIONS gExtendedTable = UI_ConnectionProgress_Precache, UI_ConnectionProgress_Connect, UI_ConnectionProgress_ChangeLevel, - UI_ConnectionProgress_ParseServerInfo + UI_ConnectionProgress_ParseServerInfo, + UI_DrawHudCharacter, + UI_GetHudFontHeight, + UI_GetHudCharWidth, }; extern "C" EXPORT int GetExtAPI( int version, UI_EXTENDED_FUNCTIONS *pFunctionTable, ui_extendedfuncs_t *pEngfuncsFromEngine )