2
0
mirror of https://github.com/FWGS/hlsdk-xash3d synced 2024-12-01 14:31:16 +01:00
hlsdk-xash3d/cl_dll/saytext.cpp

312 lines
7.8 KiB
C++
Raw Normal View History

2016-06-04 15:24:23 +02:00
/***
*
* Copyright (c) 1996-2002, Valve LLC. All rights reserved.
*
* This product contains software technology licensed from Id
* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc.
* All Rights Reserved.
*
* Use, distribution, and modification of this source code and/or resulting
* object code is restricted to non-commercial enhancements to products from
* Valve LLC. All other use, distribution, or modification is prohibited
* without written permission from Valve LLC.
*
****/
//
// saytext.cpp
//
// implementation of CHudSayText class
//
#include "hud.h"
#include "cl_util.h"
#include "parsemsg.h"
#include <string.h>
#include <stdio.h>
extern float *GetClientColor( int clientIndex );
#define MAX_LINES 5
#define MAX_CHARS_PER_LINE 256 /* it can be less than this, depending on char size */
// allow 20 pixels on either side of the text
#define MAX_LINE_WIDTH ( ScreenWidth - 40 )
#define LINE_START 10
static float SCROLL_SPEED = 5;
2016-07-03 15:39:55 +02:00
static char g_szLineBuffer[MAX_LINES + 1][MAX_CHARS_PER_LINE];
static float *g_pflNameColors[MAX_LINES + 1];
static int g_iNameLengths[MAX_LINES + 1];
2016-06-04 15:24:23 +02:00
static float flScrollTime = 0; // the time at which the lines next scroll up
static int Y_START = 0;
static int line_height = 0;
2016-06-25 20:21:01 +02:00
DECLARE_MESSAGE( m_SayText, SayText )
2016-06-04 15:24:23 +02:00
2016-07-03 15:39:55 +02:00
int CHudSayText::Init( void )
2016-06-04 15:24:23 +02:00
{
gHUD.AddHudElem( this );
HOOK_MESSAGE( SayText );
InitHUDData();
2016-07-03 15:39:55 +02:00
m_HUD_saytext = gEngfuncs.pfnRegisterVariable( "hud_saytext", "1", 0 );
2016-06-04 15:24:23 +02:00
m_HUD_saytext_time = gEngfuncs.pfnRegisterVariable( "hud_saytext_time", "5", 0 );
m_iFlags |= HUD_INTERMISSION; // is always drawn during an intermission
return 1;
}
2016-07-03 15:39:55 +02:00
void CHudSayText::InitHUDData( void )
2016-06-04 15:24:23 +02:00
{
memset( g_szLineBuffer, 0, sizeof g_szLineBuffer );
memset( g_pflNameColors, 0, sizeof g_pflNameColors );
memset( g_iNameLengths, 0, sizeof g_iNameLengths );
}
2016-07-03 15:39:55 +02:00
int CHudSayText::VidInit( void )
2016-06-04 15:24:23 +02:00
{
return 1;
}
int ScrollTextUp( void )
{
ConsolePrint( g_szLineBuffer[0] ); // move the first line into the console buffer
g_szLineBuffer[MAX_LINES][0] = 0;
memmove( g_szLineBuffer[0], g_szLineBuffer[1], sizeof(g_szLineBuffer) - sizeof(g_szLineBuffer[0]) ); // overwrite the first line
memmove( &g_pflNameColors[0], &g_pflNameColors[1], sizeof(g_pflNameColors) - sizeof(g_pflNameColors[0]) );
memmove( &g_iNameLengths[0], &g_iNameLengths[1], sizeof(g_iNameLengths) - sizeof(g_iNameLengths[0]) );
g_szLineBuffer[MAX_LINES-1][0] = 0;
2016-07-03 15:39:55 +02:00
if( g_szLineBuffer[0][0] == ' ' ) // also scroll up following lines
2016-06-04 15:24:23 +02:00
{
g_szLineBuffer[0][0] = 2;
return 1 + ScrollTextUp();
}
return 1;
}
2016-07-03 15:39:55 +02:00
int CHudSayText::Draw( float flTime )
2016-06-04 15:24:23 +02:00
{
int y = Y_START;
// make sure the scrolltime is within reasonable bounds, to guard against the clock being reset
flScrollTime = min( flScrollTime, flTime + m_HUD_saytext_time->value );
// make sure the scrolltime is within reasonable bounds, to guard against the clock being reset
flScrollTime = min( flScrollTime, flTime + m_HUD_saytext_time->value );
2016-07-03 15:39:55 +02:00
if( flScrollTime <= flTime )
2016-06-04 15:24:23 +02:00
{
2016-07-03 15:39:55 +02:00
if( *g_szLineBuffer[0] )
2016-06-04 15:24:23 +02:00
{
flScrollTime = flTime + m_HUD_saytext_time->value;
// push the console up
ScrollTextUp();
}
else
2016-07-03 15:39:55 +02:00
{
// buffer is empty, just disable drawing of this section
2016-06-04 15:24:23 +02:00
m_iFlags &= ~HUD_ACTIVE;
}
}
2016-07-03 15:39:55 +02:00
for( int i = 0; i < MAX_LINES; i++ )
2016-06-04 15:24:23 +02:00
{
2016-07-03 15:39:55 +02:00
if( *g_szLineBuffer[i] )
2016-06-04 15:24:23 +02:00
{
2016-07-03 15:39:55 +02:00
if( *g_szLineBuffer[i] == 2 && g_pflNameColors[i] )
2016-06-04 15:24:23 +02:00
{
// it's a saytext string
2016-07-03 15:39:55 +02:00
static char buf[MAX_PLAYER_NAME_LENGTH + 32];
2016-06-04 15:24:23 +02:00
// draw the first x characters in the player color
2016-07-03 15:39:55 +02:00
strncpy( buf, g_szLineBuffer[i], min(g_iNameLengths[i], MAX_PLAYER_NAME_LENGTH + 32 ) );
buf[min( g_iNameLengths[i], MAX_PLAYER_NAME_LENGTH + 31 )] = 0;
2016-06-04 15:24:23 +02:00
DrawSetTextColor( g_pflNameColors[i][0], g_pflNameColors[i][1], g_pflNameColors[i][2] );
int x = DrawConsoleString( LINE_START, y, buf );
// color is reset after each string draw
DrawConsoleString( x, y, g_szLineBuffer[i] + g_iNameLengths[i] );
}
else
{
// normal draw
DrawConsoleString( LINE_START, y, g_szLineBuffer[i] );
}
}
y += line_height;
}
return 1;
}
2016-07-03 15:39:55 +02:00
int CHudSayText::MsgFunc_SayText( const char *pszName, int iSize, void *pbuf )
2016-06-04 15:24:23 +02:00
{
BEGIN_READ( pbuf, iSize );
int client_index = READ_BYTE(); // the client who spoke the message
SayTextPrint( READ_STRING(), iSize - 1, client_index );
2016-07-03 15:39:55 +02:00
2016-06-04 15:24:23 +02:00
return 1;
}
2016-07-03 15:39:55 +02:00
void CHudSayText::SayTextPrint( const char *pszBuf, int iBufSize, int clientIndex )
2016-06-04 15:24:23 +02:00
{
2016-04-17 22:30:05 +02:00
int i;
2016-06-04 15:24:23 +02:00
ConsolePrint( pszBuf );
// find an empty string slot
2016-07-03 15:39:55 +02:00
for( i = 0; i < MAX_LINES; i++ )
2016-06-04 15:24:23 +02:00
{
2016-07-03 15:39:55 +02:00
if( !( *g_szLineBuffer[i] ) )
2016-06-04 15:24:23 +02:00
break;
}
2016-07-03 15:39:55 +02:00
if( i == MAX_LINES )
2016-06-04 15:24:23 +02:00
{
// force scroll buffer up
ScrollTextUp();
i = MAX_LINES - 1;
}
g_iNameLengths[i] = 0;
g_pflNameColors[i] = NULL;
// if it's a say message, search for the players name in the string
2016-07-03 15:39:55 +02:00
if( *pszBuf == 2 && clientIndex > 0 )
2016-06-04 15:24:23 +02:00
{
GetPlayerInfo( clientIndex, &g_PlayerInfoList[clientIndex] );
const char *pName = g_PlayerInfoList[clientIndex].name;
2016-07-03 15:39:55 +02:00
if( pName )
2016-06-04 15:24:23 +02:00
{
const char *nameInString = strstr( pszBuf, pName );
2016-07-03 15:39:55 +02:00
if( nameInString )
2016-06-04 15:24:23 +02:00
{
2016-07-03 15:39:55 +02:00
g_iNameLengths[i] = strlen( pName ) + ( nameInString - pszBuf );
2016-06-04 15:24:23 +02:00
g_pflNameColors[i] = GetClientColor( clientIndex );
}
}
}
2016-07-03 15:39:55 +02:00
strncpy( g_szLineBuffer[i], pszBuf, max( iBufSize - 1, MAX_CHARS_PER_LINE - 1 ) );
2016-06-04 15:24:23 +02:00
// make sure the text fits in one line
EnsureTextFitsInOneLineAndWrapIfHaveTo( i );
// Set scroll time
2016-07-03 15:39:55 +02:00
if( i == 0 )
2016-06-04 15:24:23 +02:00
{
flScrollTime = gHUD.m_flTime + m_HUD_saytext_time->value;
}
m_iFlags |= HUD_ACTIVE;
PlaySound( "misc/talk.wav", 1 );
2016-07-03 15:39:55 +02:00
if( ScreenHeight >= 480 )
2016-06-04 15:24:23 +02:00
Y_START = ScreenHeight - 60;
else
Y_START = ScreenHeight - 45;
2016-07-03 15:39:55 +02:00
Y_START -= ( line_height * ( MAX_LINES + 1 ) );
2016-06-04 15:24:23 +02:00
}
2016-07-03 15:39:55 +02:00
void CHudSayText::EnsureTextFitsInOneLineAndWrapIfHaveTo( int line )
2016-06-04 15:24:23 +02:00
{
int line_width = 0;
GetConsoleStringSize( g_szLineBuffer[line], &line_width, &line_height );
2016-07-03 15:39:55 +02:00
if( ( line_width + LINE_START ) > MAX_LINE_WIDTH )
{
// string is too long to fit on line
2016-06-04 15:24:23 +02:00
// 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;
2016-07-03 15:39:55 +02:00
for( char *x = g_szLineBuffer[line]; *x != 0; x++ )
2016-06-04 15:24:23 +02:00
{
// check for a color change, if so skip past it
2016-07-03 15:39:55 +02:00
if( x[0] == '/' && x[1] == '(' )
2016-06-04 15:24:23 +02:00
{
x += 2;
// skip forward until past mode specifier
while ( *x != 0 && *x != ')' )
x++;
2016-07-03 15:39:55 +02:00
if( *x != 0 )
2016-06-04 15:24:23 +02:00
x++;
2016-07-03 15:39:55 +02:00
if( *x == 0 )
2016-06-04 15:24:23 +02:00
break;
}
char buf[2];
buf[1] = 0;
2016-07-03 15:39:55 +02:00
if( *x == ' ' && x != g_szLineBuffer[line] ) // store each line break, except for the very first character
2016-06-04 15:24:23 +02:00
last_break = x;
buf[0] = *x; // get the length of the current character
GetConsoleStringSize( buf, &tmp_len, &line_height );
length += tmp_len;
2016-07-03 15:39:55 +02:00
if( length > MAX_LINE_WIDTH )
{
// needs to be broken up
if( !last_break )
last_break = x - 1;
2016-06-04 15:24:23 +02:00
x = last_break;
// find an empty string slot
int j;
do
{
2016-07-03 15:39:55 +02:00
for( j = 0; j < MAX_LINES; j++ )
2016-06-04 15:24:23 +02:00
{
2016-07-03 15:39:55 +02:00
if( !( *g_szLineBuffer[j] ) )
2016-06-04 15:24:23 +02:00
break;
}
2016-07-03 15:39:55 +02:00
if( j == MAX_LINES )
2016-06-04 15:24:23 +02:00
{
// need to make more room to display text, scroll stuff up then fix the pointers
int linesmoved = ScrollTextUp();
line -= linesmoved;
2016-07-03 15:39:55 +02:00
last_break = last_break - ( sizeof(g_szLineBuffer[0]) * linesmoved );
2016-06-04 15:24:23 +02:00
}
}
2016-07-03 15:39:55 +02:00
while( j == MAX_LINES );
2016-06-04 15:24:23 +02:00
// copy remaining string into next buffer, making sure it starts with a space character
2016-07-03 15:39:55 +02:00
if( (char)*last_break == (char)' ' )
2016-06-04 15:24:23 +02:00
{
2016-07-03 15:39:55 +02:00
int linelen = strlen( g_szLineBuffer[j] );
int remaininglen = strlen( last_break );
2016-06-04 15:24:23 +02:00
2016-07-03 15:39:55 +02:00
if( ( linelen - remaininglen ) <= MAX_CHARS_PER_LINE )
2016-06-04 15:24:23 +02:00
strcat( g_szLineBuffer[j], last_break );
}
else
{
2016-07-03 15:39:55 +02:00
if ( ( strlen( g_szLineBuffer[j] ) - strlen( last_break ) - 2 ) < MAX_CHARS_PER_LINE )
2016-06-04 15:24:23 +02:00
{
strcat( g_szLineBuffer[j], " " );
strcat( g_szLineBuffer[j], last_break );
}
}
*last_break = 0; // cut off the last string
EnsureTextFitsInOneLineAndWrapIfHaveTo( j );
break;
}
}
}
}