Skip to content

Commit

Permalink
Update saytext.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
Elinsrc authored Nov 19, 2024
1 parent 9d6a166 commit 333522b
Showing 1 changed file with 73 additions and 34 deletions.
107 changes: 73 additions & 34 deletions cl_dll/ui/hud/saytext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,27 +187,35 @@ static void convert_locations(char* dest, const char* src, size_t count, int pla
{
if (count == 0)
return;

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')) {
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);
auto bytes_to_copy = min(loc_len, count - i - 1);

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

if (i + 1 == count)
break;

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

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

dest[i] = '\0';
}

Expand Down Expand Up @@ -266,8 +274,16 @@ 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 );

char timestamp[16];
std::time_t curtime = std::time(nullptr);
auto written = std::strftime(timestamp, sizeof(timestamp), "[%H:%M:%S] ", std::localtime(&curtime));

if(written > 0)
ConsolePrint( timestamp );

ConsolePrint( g_szLineBuffer[i] );
// make sure the text fits in one line
EnsureTextFitsInOneLineAndWrapIfHaveTo( i );

Expand All @@ -289,91 +305,114 @@ 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;
}

if (x[0] == '^' && x[1] >= '0' && x[1] <= '9')
// 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 )
{
// needs to be broken up
if( !last_break )
if ( length > MAX_LINE_WIDTH )
{ // needs to be broken up
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
{
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 (color_before_last_break)
sprintf(g_szLineBuffer[j] + 1, "^%d", color_before_last_break);
else
g_szLineBuffer[j][1] = '\0';

if( ( linelen - remaininglen ) <= MAX_CHARS_PER_LINE )
strcat( g_szLineBuffer[j], last_break );
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 333522b

Please sign in to comment.