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. 77
      engine/client/console.c

89
engine/client/cl_scrn.c

@ -577,77 +577,6 @@ void SCR_UpdateScreen( void )
V_PostRender(); 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 SCR_LoadCreditsFont
@ -657,6 +586,7 @@ INTERNAL RESOURCE
*/ */
void SCR_LoadCreditsFont( void ) void SCR_LoadCreditsFont( void )
{ {
cl_font_t *const font = &cls.creditsFont;
qboolean success = false; qboolean success = false;
dword crc = 0; dword crc = 0;
@ -669,17 +599,28 @@ void SCR_LoadCreditsFont( void )
"creditsfont_%s.fnt", Cvar_VariableString( "con_charset" )) > 0 ) "creditsfont_%s.fnt", Cvar_VariableString( "con_charset" )) > 0 )
{ {
if( FS_FileExists( charsetFnt, false )) 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" ); 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 );
void Con_Top( void ); void Con_Top( void );
void Con_PageDown( int lines ); void Con_PageDown( int lines );
void Con_PageUp( 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 // s_main.c

77
engine/client/console.c

@ -560,9 +560,10 @@ qboolean Con_FixedFont( void )
return false; 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 ) if( font->valid )
return true; // already loaded return true; // already loaded
@ -570,13 +571,12 @@ static qboolean Con_LoadFixedWidthFont( const char *fontname, cl_font_t *font )
if( !FS_FileExists( fontname, false )) if( !FS_FileExists( fontname, false ))
return false; return false;
// keep source to print directly into conback image font->hFontTexture = ref.dllFuncs.GL_LoadTexture( fontname, NULL, 0, texFlags );
font->hFontTexture = ref.dllFuncs.GL_LoadTexture( fontname, NULL, 0, TF_FONT|TF_KEEP_SOURCE );
R_GetTextureParms( &fontWidth, NULL, font->hFontTexture ); R_GetTextureParms( &fontWidth, NULL, font->hFontTexture );
if( font->hFontTexture && fontWidth != 0 ) if( font->hFontTexture && fontWidth != 0 )
{ {
font->charHeight = fontWidth / 16 * con_fontscale->value; font->charHeight = fontWidth / 16 * scale;
font->type = FONT_FIXED; font->type = FONT_FIXED;
// build fixed rectangles // build fixed rectangles
@ -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].right = font->fontRc[i].left + fontWidth / 16;
font->fontRc[i].top = (i / 16) * (fontWidth / 16); font->fontRc[i].top = (i / 16) * (fontWidth / 16);
font->fontRc[i].bottom = font->fontRc[i].top + 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; font->valid = true;
} }
@ -594,12 +594,13 @@ static qboolean Con_LoadFixedWidthFont( const char *fontname, cl_font_t *font )
return true; 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; fs_offset_t length;
qfont_t *src; qfont_t *src;
byte *buffer;
int fontWidth;
int i;
if( font->valid ) if( font->valid )
return true; // already loaded return true; // already loaded
@ -607,7 +608,7 @@ static qboolean Con_LoadVariableWidthFont( const char *fontname, cl_font_t *font
if( !FS_FileExists( fontname, false )) if( !FS_FileExists( fontname, false ))
return 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 ); R_GetTextureParms( &fontWidth, NULL, font->hFontTexture );
// setup consolefont // setup consolefont
@ -619,7 +620,7 @@ static qboolean Con_LoadVariableWidthFont( const char *fontname, cl_font_t *font
if( buffer && length >= sizeof( qfont_t )) if( buffer && length >= sizeof( qfont_t ))
{ {
src = (qfont_t *)buffer; src = (qfont_t *)buffer;
font->charHeight = src->rowheight * con_fontscale->value; font->charHeight = src->rowheight * scale;
font->type = FONT_VARIABLE; font->type = FONT_VARIABLE;
// build rectangles // build rectangles
@ -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].right = font->fontRc[i].left + src->fontinfo[i].charwidth;
font->fontRc[i].top = (word)src->fontinfo[i].startoffset / fontWidth; font->fontRc[i].top = (word)src->fontinfo[i].startoffset / fontWidth;
font->fontRc[i].bottom = font->fontRc[i].top + src->rowheight; 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; font->valid = true;
} }
@ -648,32 +649,45 @@ INTERNAL RESOURCE
*/ */
static void Con_LoadConsoleFont( int fontNumber, cl_font_t *font ) static void Con_LoadConsoleFont( int fontNumber, cl_font_t *font )
{ {
const char *path = NULL; qboolean success = false;
dword crc = 0;
if( font->valid ) return; // already loaded
// replace default fonts.wad textures by current charset's font if( font->valid )
if( !CRC32_File( &crc, "fonts.wad" ) || crc == 0x3c0a0029 ) return; // already loaded
{
const char *path2 = va("font%i_%s.fnt", fontNumber, Cvar_VariableString( "con_charset" ) );
if( FS_FileExists( path2, false ) )
path = path2;
}
// loading conchars // loading conchars
if( Sys_CheckParm( "-oldfont" )) if( Sys_CheckParm( "-oldfont" ))
Con_LoadVariableWidthFont( "gfx/conchars.fnt", font ); {
success = Con_LoadVariableWidthFont( "gfx/conchars.fnt", font, con_fontscale->value, TF_FONT|TF_NEAREST );
}
else else
{ {
if( !path ) string path;
path = va( "fonts/font%i", fontNumber ); dword crc = 0;
// replace default fonts.wad textures by current charset's font
if( !CRC32_File( &crc, "fonts.wad" ) || crc == 0x3c0a0029 )
{
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 );
}
}
Con_LoadVariableWidthFont( path, font ); if( !success )
{
Q_snprintf( path, sizeof( path ), "fonts/font%i", fontNumber );
success = Con_LoadVariableWidthFont( path, font, con_fontscale->value, TF_FONT|TF_NEAREST );
}
} }
// quake fixed font as fallback if( !success )
if( !font->valid ) Con_LoadFixedWidthFont( "gfx/conchars", font ); {
// quake fixed font as fallback
// 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" );
}
} }
/* /*
@ -2397,7 +2411,7 @@ void Con_RunConsole( void )
con.vislines = con.showlines; con.vislines = con.showlines;
} }
if( FBitSet( con_charset->flags, FCVAR_CHANGED ) || if( FBitSet( con_charset->flags, FCVAR_CHANGED ) ||
FBitSet( con_fontscale->flags, FCVAR_CHANGED ) || FBitSet( con_fontscale->flags, FCVAR_CHANGED ) ||
FBitSet( con_fontnum->flags, FCVAR_CHANGED ) || FBitSet( con_fontnum->flags, FCVAR_CHANGED ) ||
FBitSet( cl_charset->flags, FCVAR_CHANGED ) ) FBitSet( cl_charset->flags, FCVAR_CHANGED ) )
@ -2428,7 +2442,6 @@ void Con_RunConsole( void )
ClearBits( con_fontnum->flags, FCVAR_CHANGED ); ClearBits( con_fontnum->flags, FCVAR_CHANGED );
ClearBits( con_fontscale->flags, FCVAR_CHANGED ); ClearBits( con_fontscale->flags, FCVAR_CHANGED );
ClearBits( cl_charset->flags, FCVAR_CHANGED ); ClearBits( cl_charset->flags, FCVAR_CHANGED );
} }
} }

Loading…
Cancel
Save