Skip to content

Commit

Permalink
[SP] Rename MenuFontToReal to UI_MenuFontToReal and replace hardcoded…
Browse files Browse the repository at this point in the history
… 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.
  • Loading branch information
Daggolin committed Apr 7, 2024
1 parent 77be2ef commit a7855ce
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 30 deletions.
19 changes: 19 additions & 0 deletions code/ui/ui_atoms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}

4 changes: 4 additions & 0 deletions code/ui/ui_local.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
44 changes: 14 additions & 30 deletions code/ui/ui_shared.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 )
Expand All @@ -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;
}
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
}
}
Expand Down

0 comments on commit a7855ce

Please sign in to comment.