From e5013196ac0b2d20926a2388c8ad41234c64068a Mon Sep 17 00:00:00 2001 From: Alibek Omarov Date: Wed, 11 Mar 2026 10:13:23 +0500 Subject: [PATCH] engine: client: reduce amount of glColor4ub calls when rendering text --- engine/client/cl_font.c | 27 ++++++++++++++++++--------- engine/client/cl_game.c | 3 ++- engine/client/client.h | 2 ++ engine/client/console.c | 3 ++- 4 files changed, 24 insertions(+), 11 deletions(-) diff --git a/engine/client/cl_font.c b/engine/client/cl_font.c index b346721d..e03f97f4 100644 --- a/engine/client/cl_font.c +++ b/engine/client/cl_font.c @@ -68,6 +68,15 @@ void CL_SetFontRendermode( cl_font_t *font ) ref.dllFuncs.GL_SetRenderMode( CL_FontRenderMode( font->rendermode )); } +void CL_SetFontColor( cl_font_t *font, const rgba_t color ) +{ + // don't apply color to fixed fonts it's already colored + if( font->type != FONT_FIXED || REF_GET_PARM( PARM_TEX_GLFORMAT, font->hFontTexture ) == 0x8045 ) // GL_LUMINANCE8_ALPHA8 + ref.dllFuncs.Color4ub( color[0], color[1], color[2], color[3] ); + else + ref.dllFuncs.Color4ub( 255, 255, 255, color[3] ); +} + qboolean Con_LoadFixedWidthFont( const char *fontname, cl_font_t *font, float scale, convar_t *rendermode, uint texFlags ) { int font_width, i; @@ -222,10 +231,9 @@ int CL_DrawCharacter( float x, float y, int number, const rgba_t color, cl_font_ if( !FBitSet( flags, FONT_DRAW_NORENDERMODE )) CL_SetFontRendermode( font ); - // don't apply color to fixed fonts it's already colored - if( font->type != FONT_FIXED || REF_GET_PARM( PARM_TEX_GLFORMAT, font->hFontTexture ) == 0x8045 ) // GL_LUMINANCE8_ALPHA8 - ref.dllFuncs.Color4ub( color[0], color[1], color[2], color[3] ); - else ref.dllFuncs.Color4ub( 255, 255, 255, color[3] ); + if( !FBitSet( flags, FONT_DRAW_NOCOLOR )) + CL_SetFontColor( font, color ); + ref.dllFuncs.R_DrawStretchPic( x, y, w, h, s1, t1, s2, t2, font->hFontTexture ); return font->charWidths[number]; @@ -233,7 +241,6 @@ int CL_DrawCharacter( float x, float y, int number, const rgba_t color, cl_font_ int CL_DrawString( float x, float y, const char *s, const rgba_t color, cl_font_t *font, int flags ) { - rgba_t current_color; int draw_len = 0; if( !font || !font->valid ) @@ -245,7 +252,9 @@ int CL_DrawString( float x, float y, const char *s, const rgba_t color, cl_font_ if( !FBitSet( flags, FONT_DRAW_NORENDERMODE )) CL_SetFontRendermode( font ); - Vector4Copy( color, current_color ); + CL_SetFontColor( font, color ); + + SetBits( flags, FONT_DRAW_NOCOLOR | FONT_DRAW_NORENDERMODE ); while( *s ) { @@ -264,7 +273,7 @@ int CL_DrawString( float x, float y, const char *s, const rgba_t color, cl_font_ } if( FBitSet( flags, FONT_DRAW_RESETCOLORONLF )) - Vector4Copy( color, current_color ); + CL_SetFontColor( font, color ); continue; } @@ -272,14 +281,14 @@ int CL_DrawString( float x, float y, const char *s, const rgba_t color, cl_font_ { // don't copy alpha if( !FBitSet( flags, FONT_DRAW_FORCECOL )) - VectorCopy( g_color_table[ColorIndex(*( s + 1 ))], current_color ); + CL_SetFontColor( font, g_color_table[ColorIndex(*( s + 1 ))] ); s += 2; continue; } // skip setting rendermode, it was changed for this string already - draw_len += CL_DrawCharacter( x + draw_len, y, (byte)*s, current_color, font, flags | FONT_DRAW_NORENDERMODE ); + draw_len += CL_DrawCharacter( x + draw_len, y, (byte)*s, NULL, font, flags ); s++; } diff --git a/engine/client/cl_game.c b/engine/client/cl_game.c index 69bfc1b3..a3873a9e 100644 --- a/engine/client/cl_game.c +++ b/engine/client/cl_game.c @@ -433,6 +433,7 @@ void CL_DrawCenterPrint( void ) CL_DrawCharacterLen( font, 0, NULL, &charHeight ); CL_SetFontRendermode( font ); + CL_SetFontColor( font, colorDefault ); for( i = 0; i < clgame.centerPrint.lines; i++ ) { lineLength = 0; @@ -462,7 +463,7 @@ void CL_DrawCenterPrint( void ) for( j = 0; j < lineLength; j++ ) { if( x >= 0 && y >= 0 && x <= refState.width ) - x += CL_DrawCharacter( x, y, line[j], colorDefault, font, FONT_DRAW_HUD | FONT_DRAW_NORENDERMODE ); + x += CL_DrawCharacter( x, y, line[j], NULL, font, FONT_DRAW_HUD | FONT_DRAW_NORENDERMODE | FONT_DRAW_NOCOLOR ); } y += charHeight; } diff --git a/engine/client/client.h b/engine/client/client.h index e4682363..0e3e8578 100644 --- a/engine/client/client.h +++ b/engine/client/client.h @@ -336,6 +336,7 @@ typedef struct #define FONT_DRAW_NORENDERMODE BIT( 3 ) // ignore font's default rendermode #define FONT_DRAW_NOLF BIT( 4 ) // ignore \n #define FONT_DRAW_RESETCOLORONLF BIT( 5 ) // yet another flag to simulate consecutive Con_DrawString calls... +#define FONT_DRAW_NOCOLOR BIT( 6 ) // do not set color to draw this character typedef struct { @@ -831,6 +832,7 @@ qboolean Con_LoadFixedWidthFont( const char *fontname, cl_font_t *font, float sc qboolean Con_LoadVariableWidthFont( const char *fontname, cl_font_t *font, float scale, convar_t *rendermode, uint texFlags ); void CL_FreeFont( cl_font_t *font ); void CL_SetFontRendermode( cl_font_t *font ); +void CL_SetFontColor( cl_font_t *font, const rgba_t color ); int CL_DrawCharacter( float x, float y, int number, const rgba_t color, cl_font_t *font, int flags ); int CL_DrawString( float x, float y, const char *s, const rgba_t color, cl_font_t *font, int flags ); void CL_DrawCharacterLen( cl_font_t *font, int number, int *width, int *height ); diff --git a/engine/client/console.c b/engine/client/console.c index 59a73c91..2f561193 100644 --- a/engine/client/console.c +++ b/engine/client/console.c @@ -1981,8 +1981,9 @@ static void Con_DrawSolidConsole( int lines ) start = con.curFont->charWidths[' ']; // offset one space at left screen side // draw red arrows to show the buffer is backscrolled + CL_SetFontColor( con.curFont, g_color_table[1] ); for( x = 0; x < con.linewidth; x += 4 ) - CL_DrawCharacter( ( x + 1 ) * start, y, '^', g_color_table[1], con.curFont, 0 ); + CL_DrawCharacter( ( x + 1 ) * start, y, '^', NULL, con.curFont, FONT_DRAW_NOCOLOR ); y -= con.curFont->charHeight; } x = lastline;