Browse Source

engine: client: consolidate variable and quake fixed width font loading functions

pull/2/head
Alibek Omarov 2 years ago
parent
commit
c481e52558
  1. 89
      engine/client/cl_scrn.c
  2. 2
      engine/client/client.h
  3. 69
      engine/client/console.c

89
engine/client/cl_scrn.c

@ -577,77 +577,6 @@ void SCR_UpdateScreen( void ) @@ -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 @@ -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 ) @@ -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];
}
}
/*

2
engine/client/client.h

@ -1044,6 +1044,8 @@ void Con_Bottom( void ); @@ -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

69
engine/client/console.c

@ -560,9 +560,10 @@ qboolean Con_FixedFont( void ) @@ -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 ) @@ -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 ) @@ -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 ) @@ -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;
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 @@ -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 @@ -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 @@ -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 @@ -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
if( font->valid )
return; // already loaded
// loading conchars
if( Sys_CheckParm( "-oldfont" ))
{
success = Con_LoadVariableWidthFont( "gfx/conchars.fnt", font, con_fontscale->value, TF_FONT|TF_NEAREST );
}
else
{
string path;
dword crc = 0;
// 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( 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 );
}
}
// loading conchars
if( Sys_CheckParm( "-oldfont" ))
Con_LoadVariableWidthFont( "gfx/conchars.fnt", font );
else
if( !success )
{
if( !path )
path = va( "fonts/font%i", fontNumber );
Con_LoadVariableWidthFont( path, font );
Q_snprintf( path, sizeof( path ), "fonts/font%i", fontNumber );
success = Con_LoadVariableWidthFont( path, font, con_fontscale->value, TF_FONT|TF_NEAREST );
}
}
if( !success )
{
// quake fixed font as fallback
if( !font->valid ) Con_LoadFixedWidthFont( "gfx/conchars", font );
// 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" );
}
}
/*
@ -2428,7 +2442,6 @@ void Con_RunConsole( void ) @@ -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 );
}
}

Loading…
Cancel
Save