Skip to content

Commit

Permalink
Update saytext
Browse files Browse the repository at this point in the history
  • Loading branch information
Elinsrc committed Nov 19, 2024
1 parent 9d6a166 commit b78d611
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 64 deletions.
17 changes: 0 additions & 17 deletions cl_dll/cl_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,6 @@ inline struct cvar_s *CVAR_CREATE( const char *cv, const char *val, const int fl
#define ClientCmd ( *gEngfuncs.pfnClientCmd )
#define SetCrosshair ( *gEngfuncs.pfnSetCrosshair )
#define AngleVectors ( *gEngfuncs.pfnAngleVectors )
extern cvar_t *hud_textmode;
extern float g_hud_text_color[3];
inline void DrawSetTextColor( float r, float g, float b )
{
if( hud_textmode->value == 1 )
g_hud_text_color[0] = r, g_hud_text_color[1] = g, g_hud_text_color[2] = b;
else
gEngfuncs.pfnDrawSetTextColor( r, g, b );
}

// Gets the height & width of a sprite, at the specified frame
inline int SPR_Height( HSPRITE x, int f ) { return gEngfuncs.pfnSPR_Height(x, f); }
Expand All @@ -112,14 +103,6 @@ inline int TextMessageDrawChar( int x, int y, int number, int r, int g, int b )
return gEngfuncs.pfnDrawCharacter( x, y, number, r, g, b );
}

inline int DrawConsoleString( int x, int y, const char *string )
{
if( hud_textmode->value == 1 )
return gHUD.DrawHudString( x, y, 9999, (char*)string, (int)( (float)g_hud_text_color[0] * 255.0f ),
(int)( (float)g_hud_text_color[1] * 255.0f ), (int)( (float)g_hud_text_color[2] * 255.0f ) );
return gEngfuncs.pfnDrawConsoleString( x, y, (char*) string );
}

int DrawUtfString( int xpos, int ypos, int iMaxX, const char *szIt, int r, int g, int b );

inline int DrawConsoleString( int x, int y, char *string )
Expand Down
5 changes: 1 addition & 4 deletions cl_dll/ui/hud/ammohistory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,7 @@ int HistoryResource::DrawAmmoHistory( float flTime )
SPR_DrawAdditive( 0, xpos, ypos, &rcPic );
}

// do not draw black console string
if( !( ( hud_textmode->value == 2 ) && ( scale < 200 ) ) )
// Draw the number
gHUD.DrawHudNumberString( xpos - 10, ypos, xpos - 100, rgAmmoHistory[i].iCount, r, g, b );
gHUD.DrawHudNumberString( xpos - 10, ypos, xpos - 100, rgAmmoHistory[i].iCount, r, g, b );
}
else if( rgAmmoHistory[i].type == HISTSLOT_WEAP )
{
Expand Down
4 changes: 0 additions & 4 deletions cl_dll/ui/hud/hud.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,6 @@ class CHLVoiceStatusHelper : public IVoiceStatusHelper
static CHLVoiceStatusHelper g_VoiceStatusHelper;
#endif

cvar_t *hud_textmode;
float g_hud_text_color[3];

extern client_sprite_t *GetSpriteList( client_sprite_t *pList, const char *psz, int iRes, int iCount );

extern cvar_t *sensitivity;
Expand Down Expand Up @@ -484,7 +481,6 @@ void CHud::Init( void )

CVAR_CREATE( "hud_classautokill", "1", FCVAR_ARCHIVE | FCVAR_USERINFO ); // controls whether or not to suicide immediately on TF class switch
CVAR_CREATE( "hud_takesshots", "0", FCVAR_ARCHIVE ); // controls whether or not to automatically take screenshots at the end of a round
hud_textmode = CVAR_CREATE ( "hud_textmode", "0", FCVAR_ARCHIVE );

m_iLogo = 0;
m_iFOV = 0;
Expand Down
11 changes: 9 additions & 2 deletions cl_dll/ui/hud/hud_spectator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -452,8 +452,15 @@ int CHudSpectator::Draw( float flTime )

lx = strlen( string ) * 3; // 3 is avg. character length :)

DrawSetTextColor( color[0], color[1], color[2] );
DrawConsoleString( m_vPlayerPos[i][0] - lx,m_vPlayerPos[i][1], string );
gHUD.DrawConsoleStringWithColorTags(
m_vPlayerPos[i][0] - lx,
m_vPlayerPos[i][1],
string,
true,
color[0],
color[1],
color[2]
);
}

return 1;
Expand Down
116 changes: 79 additions & 37 deletions cl_dll/ui/hud/saytext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ int CHudSayText::Draw( float flTime )
static char buf[MAX_PLAYER_NAME_LENGTH + 32];

// draw the first x characters in the player color
strncpy( buf, g_szLineBuffer[i], Q_min(g_iNameLengths[i], MAX_PLAYER_NAME_LENGTH + 31 ) );
buf[Q_min( g_iNameLengths[i], MAX_PLAYER_NAME_LENGTH + 31 )] = 0;
int x = gHUD.DrawConsoleStringWithColorTags(
LINE_START,
y,
Expand All @@ -149,8 +151,6 @@ int CHudSayText::Draw( float flTime )
g_pflNameColors[i][1],
g_pflNameColors[i][2]
); // don't draw the control code at the start
strncpy( buf, g_szLineBuffer[i], Q_min(g_iNameLengths[i], MAX_PLAYER_NAME_LENGTH + 31 ) );
buf[Q_min( g_iNameLengths[i], MAX_PLAYER_NAME_LENGTH + 31 )] = 0;

// color is reset after each string draw
DrawConsoleString( x, y, g_szLineBuffer[i] + g_iNameLengths[i] );
Expand Down Expand Up @@ -187,27 +187,40 @@ static void convert_locations(char* dest, const char* src, size_t count, int pla
{
if (count == 0)
return;
if (count == 1) {

if (count == 1)
{
dest[0] = '\0';
return;
}

size_t i = 0;
for (; *src != '\0'; ++src) {
if (src[0] == '%' && (src[1] == 'l' || src[1] == 'L' || src[1] == 'd' || src[1] == 'D')) {

for (; *src != '\0'; ++src)
{
if (src[0] == '%' && (src[1] == 'l' || src[1] == 'L' || src[1] == 'd' || src[1] == 'D'))
{
auto loc = gHUD.m_Location.get_player_location(player_id).c_str();
auto loc_len = strlen(loc);
auto bytes_to_copy = Q_min(loc_len, count - i - 1);
strncpy(&dest[i], loc, bytes_to_copy);

memcpy(&dest[i], loc, bytes_to_copy);
i += bytes_to_copy;

if (i + 1 == count)
break;

++src;
} else {
}
else
{
dest[i++] = *src;

if (i + 1 == count)
break;
}
}

dest[i] = '\0';
}

Expand Down Expand Up @@ -266,7 +279,7 @@ void CHudSayText::SayTextPrint( const char *pszBuf, int iBufSize, int clientInde
}
}

convert_locations( g_szLineBuffer[i], pszBuf, Q_max(iBufSize , MAX_CHARS_PER_LINE), clientIndex );
convert_locations( g_szLineBuffer[i], pszBuf, MAX_CHARS_PER_LINE, clientIndex );

// make sure the text fits in one line
EnsureTextFitsInOneLineAndWrapIfHaveTo( i );
Expand All @@ -289,91 +302,120 @@ void CHudSayText::SayTextPrint( const char *pszBuf, int iBufSize, int clientInde
Y_START -= ( line_height * ( MAX_LINES + 1 ) );
}

void CHudSayText::EnsureTextFitsInOneLineAndWrapIfHaveTo( int line )
void CHudSayText :: EnsureTextFitsInOneLineAndWrapIfHaveTo( int line )
{
int line_width = 0;
GetConsoleStringSize( g_szLineBuffer[line], &line_width, &line_height );

if( ( line_width + LINE_START ) > MAX_LINE_WIDTH )
{
// string is too long to fit on line
if ( (line_width + LINE_START) > MAX_LINE_WIDTH )
{ // string is too long to fit on line
// scan the string until we find what word is too long, and wrap the end of the sentence after the word
int length = LINE_START;
int tmp_len = 0;
char *last_break = NULL;
for( char *x = g_szLineBuffer[line]; *x != 0; x++ )

int current_color = 0;
int color_before_last_break = 0;
for ( char *x = g_szLineBuffer[line]; *x != 0; x++ )
{
// check for a color change, if so skip past it
if( x[0] == '/' && x[1] == '(' )
if ( x[0] == '/' && x[1] == '(' )
{
x += 2;
// skip forward until past mode specifier
while ( *x != 0 && *x != ')' )
x++;

if( *x != 0 )
if ( *x != 0 )
x++;

if( *x == 0 )
if ( *x == 0 )
break;
}

// Skip past the color tags.
if (x[0] == '^' && x[1] >= '0' && x[1] <= '9')
{
if (g_szLineBuffer[line][0] != 2 || g_szLineBuffer[line] + g_iNameLengths[line] < x)
{
current_color = x[1] - '0';
if (current_color == 9)
current_color = 0;
}

x += 2;

if (*x == 0)
break;
}

// Skip past the control character at the start.
if (x[0] == 2)
{
++x;

if (*x == 0)
break;
}

char buf[2];
buf[1] = 0;

if( *x == ' ' && x != g_szLineBuffer[line] ) // store each line break, except for the very first character
if (*x == ' ' && x != g_szLineBuffer[line])
{
// store each line break, except for the very first character
last_break = x;
color_before_last_break = current_color;
}

buf[0] = *x; // get the length of the current character
GetConsoleStringSize( buf, &tmp_len, &line_height );
length += tmp_len;

if( length > MAX_LINE_WIDTH )
if ( length > MAX_LINE_WIDTH )
{
// needs to be broken up
if( !last_break )
if (!last_break) {
last_break = x - 1;
color_before_last_break = current_color;
}

// x = last_break;
x = last_break;

// find an empty string slot
int j;
do
do
{
for( j = 0; j < MAX_LINES; j++ )
for ( j = 0; j < MAX_LINES; j++ )
{
if( !( *g_szLineBuffer[j] ) )
if ( ! *g_szLineBuffer[j] )
break;
}
if( j == MAX_LINES )
if ( j == MAX_LINES )
{
// need to make more room to display text, scroll stuff up then fix the pointers
int linesmoved = ScrollTextUp();
line -= linesmoved;
last_break = last_break - ( sizeof(g_szLineBuffer[0]) * linesmoved );
last_break = last_break - (sizeof(g_szLineBuffer[0]) * linesmoved);
}
}
while( j == MAX_LINES );
while ( j == MAX_LINES );

// copy remaining string into next buffer, making sure it starts with a space character
if( (char)*last_break == (char)' ' )
{
int linelen = strlen( g_szLineBuffer[j] );
int remaininglen = strlen( last_break );
g_szLineBuffer[j][0] = ' ';

if( ( linelen - remaininglen ) <= MAX_CHARS_PER_LINE )
strcat( g_szLineBuffer[j], last_break );
if (color_before_last_break)
sprintf(g_szLineBuffer[j] + 1, "^%d", color_before_last_break);
else
g_szLineBuffer[j][1] = '\0';

if ( *last_break == ' ' )
{
strcat(g_szLineBuffer[j], last_break + 1);
}
else
{
if ( ( strlen( g_szLineBuffer[j] ) - strlen( last_break ) - 2 ) < MAX_CHARS_PER_LINE )
{
strcat( g_szLineBuffer[j], " " );
strcat( g_szLineBuffer[j], last_break );
}
strcat(g_szLineBuffer[j], last_break);
}

*last_break = 0; // cut off the last string
Expand Down

0 comments on commit b78d611

Please sign in to comment.