From 77be2ef6bb49369d6f7d1977fcabf6bcc5223d69 Mon Sep 17 00:00:00 2001 From: Daggolin Date: Sun, 7 Apr 2024 02:10:34 +0200 Subject: [PATCH 1/3] [SP] Add CG_MagicFontToReal function to map magic number fonts to their vanilla equivalents. This fixes #1226. --- code/cgame/cg_local.h | 2 ++ code/cgame/cg_main.cpp | 39 ++++++++++++++++++++++++++++++++++++++- code/cgame/cg_weapons.cpp | 2 +- 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/code/cgame/cg_local.h b/code/cgame/cg_local.h index 3a1f7e8bff..8c0960f8b4 100644 --- a/code/cgame/cg_local.h +++ b/code/cgame/cg_local.h @@ -1231,4 +1231,6 @@ void CG_ClearLightStyles( void ); void CG_RunLightStyles( void ); void CG_SetLightstyle( int i ); +int CG_MagicFontToReal( int menuFontIndex ); + #endif //__CG_LOCAL_H__ diff --git a/code/cgame/cg_main.cpp b/code/cgame/cg_main.cpp index 86bf1a0c78..db6f7dc4e0 100644 --- a/code/cgame/cg_main.cpp +++ b/code/cgame/cg_main.cpp @@ -4353,13 +4353,50 @@ void CG_DrawDataPadForceSelect( void ) const float textScale = 1.0f; CG_DisplayBoxedText(textboxXPos,textboxYPos,textboxWidth,textboxHeight,va("%s%s",text,text2), - 4, + CG_MagicFontToReal(4), textScale, colorTable[CT_WHITE] ); } } +int CG_MagicFontToReal( int menuFontIndex ) +{ + // As the engine supports multiple renderers now we can no longer assume the + // order of fontindex values to be the same as it was on vanilla jasp with + // vanilla assets. Sadly the code uses magic numbers in various places that + // no longer match. This function tries to map these magic numbers to the + // fonts the would refer to on vanilla jasp with vanilla assets. + + static int fonthandle_aurabesh; + static int fonthandle_ergoec; + static int fonthandle_anewhope; + static int fonthandle_arialnb; + + static qboolean fontsRegistered = qfalse; + + if ( !fontsRegistered ) + { // Only try registering the fonts once + fonthandle_aurabesh = cgi_R_RegisterFont( "aurabesh" ); + fonthandle_ergoec = cgi_R_RegisterFont( "ergoec" ); + fonthandle_anewhope = cgi_R_RegisterFont( "anewhope" ); + fonthandle_arialnb = cgi_R_RegisterFont( "arialnb" ); + + fontsRegistered = qtrue; + } + + // Default fonts from a clean installation + switch ( menuFontIndex ) { + case 1: return fonthandle_aurabesh; + case 2: return fonthandle_ergoec; + case 3: return fonthandle_anewhope; + case 4: return fonthandle_arialnb; + + default: + return cgs.media.qhFontMedium; + } +} + // actually, these are pretty pointless so far in CHC, since in TA codebase they were used only so init some HUD // function ptrs to allow cinematics in onscreen displays. So far, we don't use those, but here they are anyway... // diff --git a/code/cgame/cg_weapons.cpp b/code/cgame/cg_weapons.cpp index dc90ee497e..09a5b20ad4 100644 --- a/code/cgame/cg_weapons.cpp +++ b/code/cgame/cg_weapons.cpp @@ -1685,7 +1685,7 @@ void CG_DrawDataPadWeaponSelect( void ) textboxXPos, textboxYPos, textboxWidth, textboxHeight, text, - 4, + CG_MagicFontToReal(4), textScale, colorTable[CT_WHITE] ); From a7855cec31ae9ba7994e99fe303a82f4f6109788 Mon Sep 17 00:00:00 2001 From: Daggolin Date: Sun, 7 Apr 2024 02:14:03 +0200 Subject: [PATCH 2/3] [SP] Rename MenuFontToReal to UI_MenuFontToReal and replace hardcoded font mapping with a dynamic list of up to 64 fonts. This might help with possible corner cases where custom assets have the menu loads differents fonts in place of the vanilla ones. The maximum of 64 fonts was chosen, because I wanted to avoid dynamic allocation. As the vanilla game only loads 5 different fonts and as I am not aware of custom mods that add a lot more I chose the value 64 to hopefully leave enough room for mods. The same change can NOT be applied to CG_MagicFontToReal, because the cgame module does not parse the menu files and does not know the order of fonts in menu files. --- code/ui/ui_atoms.cpp | 19 +++++++++++++++++++ code/ui/ui_local.h | 4 ++++ code/ui/ui_shared.cpp | 44 ++++++++++++++----------------------------- 3 files changed, 37 insertions(+), 30 deletions(-) diff --git a/code/ui/ui_atoms.cpp b/code/ui/ui_atoms.cpp index 69ccfb6e3b..21488388bb 100644 --- a/code/ui/ui_atoms.cpp +++ b/code/ui/ui_atoms.cpp @@ -441,6 +441,9 @@ UI_RegisterFont ================= */ +int registeredFontsCount = 0; +int registeredFonts[MAX_FONTS]; + int UI_RegisterFont(const char *fontName) { int iFontIndex = ui.R_RegisterFont(fontName); @@ -449,6 +452,22 @@ int UI_RegisterFont(const char *fontName) iFontIndex = ui.R_RegisterFont("ergoec"); // fall back } + // Store font + if ( iFontIndex ) + { + int i; + for ( i = 0; i < registeredFontsCount; i++ ) + { + if ( registeredFonts[i] == iFontIndex ) break; + } + + if ( i == registeredFontsCount ) + { // It's not in the list: add it + if ( registeredFontsCount >= MAX_FONTS ) Com_Printf( "^3UI_RegisterFont: MAX_FONTS (%i) exceeded\n", MAX_FONTS ); + else registeredFonts[registeredFontsCount++] = iFontIndex; + } + } + return iFontIndex; } diff --git a/code/ui/ui_local.h b/code/ui/ui_local.h index e39c4f902d..6767efd5dd 100644 --- a/code/ui/ui_local.h +++ b/code/ui/ui_local.h @@ -246,4 +246,8 @@ void trap_S_StartLocalSound( sfxHandle_t sfx, int channelNum ); void _UI_Refresh( int realtime ); +#define MAX_FONTS 64 +extern int registeredFontsCount; +extern int registeredFonts[MAX_FONTS]; + #endif diff --git a/code/ui/ui_shared.cpp b/code/ui/ui_shared.cpp index f2d5a3cf3b..2f5cbdf877 100644 --- a/code/ui/ui_shared.cpp +++ b/code/ui/ui_shared.cpp @@ -352,7 +352,7 @@ qboolean MenuParse_font( itemDef_t *item) if (!DC->Assets.fontRegistered) { - DC->Assets.qhMediumFont = DC->registerFont(menu->font); + DC->Assets.qhMediumFont = UI_RegisterFont(menu->font); DC->Assets.fontRegistered = qtrue; } return qtrue; @@ -3072,35 +3072,19 @@ qboolean ItemParse_name( itemDef_t *item) return qtrue; } -int MenuFontToReal( int menuFontIndex ) +int UI_MenuFontToReal( int menuFontIndex ) { - static int fonthandle_aurabesh; - static int fonthandle_ergoec; - static int fonthandle_anewhope; - static int fonthandle_arialnb; + // The font array starts at 0, but the valid font indexes start at 1. As the + // original menu files have direct font indexes we need to subtract 1 from + // the given index to get the correct index for our mapping array. + menuFontIndex--; - static qboolean fontsRegistered = qfalse; + // Make sure we don't go out of bound, fallback to medium font + if ( menuFontIndex < 0 || menuFontIndex >= registeredFontsCount ) + return DC->Assets.qhMediumFont; - if ( !fontsRegistered ) - { // Only try registering the fonts once - fonthandle_aurabesh = UI_RegisterFont( "aurabesh" ); - fonthandle_ergoec = UI_RegisterFont( "ergoec" ); - fonthandle_anewhope = UI_RegisterFont( "anewhope" ); - fonthandle_arialnb = UI_RegisterFont( "arialnb" ); - - fontsRegistered = qtrue; - } - - // Default fonts from a clean installation - switch ( menuFontIndex ) { - case 1: return fonthandle_aurabesh; - case 2: return fonthandle_ergoec; - case 3: return fonthandle_anewhope; - case 4: return fonthandle_arialnb; - - default: - return DC->Assets.qhMediumFont; - } + // Use the mapped index + return registeredFonts[menuFontIndex]; } qboolean ItemParse_font( itemDef_t *item ) @@ -3111,7 +3095,7 @@ qboolean ItemParse_font( itemDef_t *item ) } // Translate to real font - item->font = MenuFontToReal( item->font ); + item->font = UI_MenuFontToReal( item->font ); return qtrue; } @@ -8294,7 +8278,7 @@ static qboolean Item_Paint(itemDef_t *item, qboolean bDraw) while (1) { // FIXME - add some type of parameter in the menu file like descfont to specify the font for the descriptions for this menu. - textWidth = DC->textWidth(textPtr, fDescScale, MenuFontToReal(4)); // item->font); + textWidth = DC->textWidth(textPtr, fDescScale, UI_MenuFontToReal(4)); // item->font); if (parent->descAlignment == ITEM_ALIGN_RIGHT) { @@ -8330,7 +8314,7 @@ static qboolean Item_Paint(itemDef_t *item, qboolean bDraw) } // FIXME - add some type of parameter in the menu file like descfont to specify the font for the descriptions for this menu. - DC->drawText(xPos, parent->descY + iYadj, fDescScale, parent->descColor, textPtr, 0, parent->descTextStyle, MenuFontToReal(4)); //item->font); + DC->drawText(xPos, parent->descY + iYadj, fDescScale, parent->descColor, textPtr, 0, parent->descTextStyle, UI_MenuFontToReal(4)); //item->font); break; } } From 39096763d717bb7ce431f3438e088dfcf373339f Mon Sep 17 00:00:00 2001 From: Daggolin Date: Sun, 7 Apr 2024 02:26:01 +0200 Subject: [PATCH 3/3] [Shared] Fix CFontInfo::m_isVariant not getting initialized for non-variant fonts. This fixes r_reloadfonts in SP. --- code/rd-common/tr_font.cpp | 1 + codemp/rd-common/tr_font.cpp | 1 + 2 files changed, 2 insertions(+) diff --git a/code/rd-common/tr_font.cpp b/code/rd-common/tr_font.cpp index f968fdb89d..253d58fd51 100644 --- a/code/rd-common/tr_font.cpp +++ b/code/rd-common/tr_font.cpp @@ -1019,6 +1019,7 @@ CFontInfo::CFontInfo(const char *_fontName) m_fAltSBCSFontScaleFactor = -1; #endif m_bIsFakeAlienLanguage = !strcmp(_fontName,"aurabesh"); // dont try and make SBCS or asian overrides for this + m_isVariant = qfalse; len = ri.FS_ReadFile(fontName, NULL); if (len == sizeof(dfontdat_t)) diff --git a/codemp/rd-common/tr_font.cpp b/codemp/rd-common/tr_font.cpp index 956e182f75..20f3ce8894 100644 --- a/codemp/rd-common/tr_font.cpp +++ b/codemp/rd-common/tr_font.cpp @@ -882,6 +882,7 @@ CFontInfo::CFontInfo(const char *_fontName) m_iOriginalFontWhenSBCSOverriden = -1; m_fAltSBCSFontScaleFactor = -1; m_bIsFakeAlienLanguage = !strcmp(_fontName,"aurabesh"); // dont try and make SBCS or asian overrides for this + m_isVariant = qfalse; len = ri.FS_ReadFile(fontName, NULL); if (len == sizeof(dfontdat_t))