Skip to content

Commit 2436d6d

Browse files
committed
NUKE font handle table
We only load one font, one time - the console font. No need for a font table caching system to detect duplicate font load requests.
1 parent 7ecdc7a commit 2436d6d

File tree

3 files changed

+25
-149
lines changed

3 files changed

+25
-149
lines changed

src/engine/client/cl_main.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1440,8 +1440,7 @@ void CL_Vid_Restart_f()
14401440
Audio::StopAllSounds();
14411441
// shutdown the CGame
14421442
CL_ShutdownCGame();
1443-
// clear the font cache
1444-
re.UnregisterFont( nullptr );
1443+
re.UnregisterFont( cls.consoleFont );
14451444
cls.consoleFont = nullptr;
14461445
// shutdown the renderer and clear the renderer interface
14471446
CL_ShutdownRef();
@@ -2461,7 +2460,7 @@ void CL_Shutdown()
24612460

24622461
if ( re.UnregisterFont )
24632462
{
2464-
re.UnregisterFont( nullptr );
2463+
re.UnregisterFont( cls.consoleFont );
24652464
cls.consoleFont = nullptr;
24662465
}
24672466

src/engine/qcommon/q_shared.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2171,9 +2171,6 @@ struct glyphInfo_t
21712171
char shaderName[ 32 ];
21722172
};
21732173

2174-
// Unlike with many other handle types, 0 is valid, not an error or default return value.
2175-
using fontHandle_t = int;
2176-
21772174
using glyphBlock_t = glyphInfo_t[256];
21782175

21792176
struct fontInfo_t

src/engine/renderer/tr_font.cpp

Lines changed: 23 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,6 @@ FT_Library ftLibrary = nullptr;
5454

5555
static const int FONT_SIZE = 512;
5656

57-
static const int MAX_FONTS = 16;
58-
static const int MAX_FILES = ( MAX_FONTS );
59-
static fontInfo_t registeredFont[ MAX_FONTS ];
60-
static unsigned int fontUsage[ MAX_FONTS ];
61-
62-
static struct {
63-
void *data;
64-
int length;
65-
int count;
66-
char name[ MAX_QPATH ];
67-
} fontData[ MAX_FILES ];
68-
6957
void RE_RenderChunk( fontInfo_t *font, const int chunk );
7058

7159

@@ -427,79 +415,33 @@ void RE_RenderChunk( fontInfo_t *font, const int chunk )
427415

428416
static int RE_LoadFontFile( const char *name, void **buffer )
429417
{
430-
int i;
418+
void *tmp;
419+
int length = ri.FS_ReadFile( name, &tmp );
431420

432-
// if we already have this file, return it
433-
for ( i = 0; i < MAX_FILES; ++i )
421+
if ( length <= 0 )
434422
{
435-
if ( !fontData[ i ].count || Q_stricmp( name, fontData[ i ].name ) )
436-
{
437-
continue;
438-
}
439-
440-
++fontData[ i ].count;
441-
442-
*buffer = fontData[ i ].data;
443-
return fontData[ i ].length;
423+
return 0;
444424
}
445425

446-
// otherwise, find a free entry and load the file
447-
for ( i = 0; i < MAX_FILES; ++i )
448-
{
449-
if ( !fontData [ i ].count )
450-
{
451-
void *tmp;
452-
int length = ri.FS_ReadFile( name, &tmp );
426+
void *data = Z_AllocUninit( length );
427+
*buffer = data;
453428

454-
if ( length <= 0 )
455-
{
456-
return 0;
457-
}
429+
memcpy( data, tmp, length );
430+
ri.FS_FreeFile( tmp );
458431

459-
fontData[ i ].data = Z_AllocUninit( length );
460-
fontData[ i ].length = length;
461-
fontData[ i ].count = 1;
462-
*buffer = fontData[ i ].data;
463-
464-
memcpy( fontData[ i ].data, tmp, length );
465-
ri.FS_FreeFile( tmp );
466-
467-
Q_strncpyz( fontData[ i ].name, name, sizeof( fontData[ i ].name ) );
468-
469-
return length;
470-
}
471-
}
472-
473-
return 0;
432+
return length;
474433
}
475434

476435
static void RE_FreeFontFile( void *data )
477436
{
478-
int i;
479-
480-
if ( !data )
481-
{
482-
return;
483-
}
484-
485-
for ( i = 0; i < MAX_FILES; ++i )
486-
{
487-
if ( fontData[ i ].data == data )
488-
{
489-
if ( !--fontData[ i ].count )
490-
{
491-
Z_Free( fontData[ i ].data );
492-
}
493-
break;
494-
}
495-
}
437+
Z_Free( data );
496438
}
497439

498440
fontInfo_t* RE_RegisterFont( const char *fontName, int pointSize )
499441
{
500442
FT_Face face;
501443
void *faceData = nullptr;
502-
int i, len, fontNo;
444+
int len;
503445
char strippedName[ MAX_QPATH ];
504446

505447
if ( pointSize <= 0 )
@@ -512,41 +454,12 @@ fontInfo_t* RE_RegisterFont( const char *fontName, int pointSize )
512454

513455
COM_StripExtension2( fontName, strippedName, sizeof( strippedName ) );
514456

515-
fontNo = -1;
516-
517-
for ( i = 0; i < MAX_FONTS; i++ )
518-
{
519-
if ( !fontUsage[ i ] )
520-
{
521-
if ( fontNo < 0 )
522-
{
523-
fontNo = i;
524-
}
525-
}
526-
else if ( pointSize == registeredFont[ i ].pointSize && Q_stricmp( strippedName, registeredFont[ i ].name ) == 0 )
527-
{
528-
++fontUsage[ i ];
529-
return &registeredFont[ i ];
530-
}
531-
}
532-
533-
if ( fontNo < 0 )
534-
{
535-
Log::Warn("RE_RegisterFont: Too many fonts registered already." );
536-
return nullptr;
537-
}
538-
539-
fontInfo_t* font = &registeredFont[ fontNo ];
540-
ResetStruct( *font );
541-
542457
if ( ftLibrary == nullptr )
543458
{
544459
Log::Warn("RE_RegisterFont: FreeType not initialized." );
545460
return nullptr;
546461
}
547462

548-
Q_strncpyz( font->name, strippedName, sizeof( font->name ) );
549-
550463
len = RE_LoadFontFile( fontName, &faceData );
551464

552465
if ( len <= 0 )
@@ -572,13 +485,14 @@ fontInfo_t* RE_RegisterFont( const char *fontName, int pointSize )
572485
return nullptr;
573486
}
574487

488+
auto *font = new fontInfo_t{};
489+
Q_strncpyz( font->name, strippedName, sizeof( font->name ) );
575490
font->face = face;
576491
font->faceData = faceData;
577492
font->pointSize = pointSize;
578493

579494
RE_RenderChunk( font, 0 );
580495

581-
++fontUsage[ fontNo ];
582496
return font;
583497
}
584498

@@ -590,69 +504,35 @@ void R_InitFreeType()
590504
}
591505
}
592506

593-
void RE_UnregisterFont_Internal( fontHandle_t handle )
507+
void RE_UnregisterFont( fontInfo_t *font )
594508
{
595-
int i;
596-
597-
if ( !fontUsage[ handle ] )
598-
{
599-
return;
600-
}
601-
602-
if ( --fontUsage[ handle ] )
509+
if ( !font )
603510
{
604511
return;
605512
}
606513

607-
608-
if ( registeredFont[ handle ].face )
514+
if ( font->face )
609515
{
610-
FT_Done_Face( (FT_Face) registeredFont[ handle ].face );
611-
RE_FreeFontFile( registeredFont[ handle ].faceData );
516+
FT_Done_Face( (FT_Face) font->face );
517+
RE_FreeFontFile( font->faceData );
612518
}
613519

614-
for ( i = 0; i < 0x1100; ++i )
520+
for ( int i = 0; i < 0x1100; ++i )
615521
{
616-
if ( registeredFont[ handle ].glyphBlock[ i ] && registeredFont[ handle ].glyphBlock[ i ] != nullGlyphs )
522+
if ( font->glyphBlock[ i ] && font->glyphBlock[ i ] != nullGlyphs )
617523
{
618-
Z_Free( registeredFont[ handle ].glyphBlock[ i ] );
619-
registeredFont[ handle ].glyphBlock[ i ] = nullptr;
524+
Z_Free( font->glyphBlock[ i ] );
525+
font->glyphBlock[ i ] = nullptr;
620526
}
621527
}
622528

623-
ResetStruct( registeredFont[ handle ] );
624-
}
625-
626-
void RE_UnregisterFont( fontInfo_t *font )
627-
{
628-
int i;
629-
630-
for ( i = 0; i < MAX_FONTS; ++i )
631-
{
632-
if ( !fontUsage[ i ] )
633-
{
634-
continue;
635-
}
636-
637-
if ( font && font != &registeredFont[ i ] )
638-
{
639-
continue; // name & size don't match
640-
}
641-
642-
RE_UnregisterFont_Internal( i );
643-
644-
if ( font )
645-
{
646-
break;
647-
}
648-
}
529+
delete font;
649530
}
650531

651532
void R_DoneFreeType()
652533
{
653534
if ( ftLibrary )
654535
{
655-
RE_UnregisterFont( nullptr );
656536
FT_Done_FreeType( ftLibrary );
657537
ftLibrary = nullptr;
658538
}

0 commit comments

Comments
 (0)