hlsdk-xash3d/cl_dll/message.cpp

523 lines
12 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.
*
****/
//
// Message.cpp
//
// implementation of CHudMessage class
//
#include "hud.h"
#include "cl_util.h"
#include <string.h>
#include <stdio.h>
#include "parsemsg.h"
DECLARE_MESSAGE( m_Message, HudText )
DECLARE_MESSAGE( m_Message, GameTitle )
// 1 Global client_textmessage_t for custom messages that aren't in the titles.txt
client_textmessage_t g_pCustomMessage;
2017-06-29 15:56:03 +02:00
const char *g_pCustomName = "Custom";
2016-06-04 15:24:23 +02:00
char g_pCustomText[1024];
2016-07-03 15:39:55 +02:00
int CHudMessage::Init( void )
2016-06-04 15:24:23 +02:00
{
HOOK_MESSAGE( HudText );
HOOK_MESSAGE( GameTitle );
2016-07-03 15:39:55 +02:00
gHUD.AddHudElem( this );
2016-06-04 15:24:23 +02:00
Reset();
return 1;
}
2016-06-04 15:24:23 +02:00
int CHudMessage::VidInit( void )
{
m_HUD_title_half = gHUD.GetSpriteIndex( "title_half" );
m_HUD_title_life = gHUD.GetSpriteIndex( "title_life" );
return 1;
}
2016-06-04 15:24:23 +02:00
void CHudMessage::Reset( void )
{
2016-07-03 15:39:55 +02:00
memset( m_pMessages, 0, sizeof(m_pMessages[0]) * maxHUDMessages );
memset( m_startTime, 0, sizeof(m_startTime[0]) * maxHUDMessages );
2016-06-04 15:24:23 +02:00
m_gameTitleTime = 0;
m_pGameTitle = NULL;
}
float CHudMessage::FadeBlend( float fadein, float fadeout, float hold, float localTime )
{
float fadeTime = fadein + hold;
float fadeBlend;
2016-07-03 15:39:55 +02:00
if( localTime < 0 )
2016-06-04 15:24:23 +02:00
return 0;
2016-07-03 15:39:55 +02:00
if( localTime < fadein )
2016-06-04 15:24:23 +02:00
{
2016-07-03 15:39:55 +02:00
fadeBlend = 1 - ( ( fadein - localTime ) / fadein );
2016-06-04 15:24:23 +02:00
}
2016-07-03 15:39:55 +02:00
else if( localTime > fadeTime )
2016-06-04 15:24:23 +02:00
{
2016-07-03 15:39:55 +02:00
if( fadeout > 0 )
fadeBlend = 1 - ( ( localTime - fadeTime ) / fadeout );
2016-06-04 15:24:23 +02:00
else
fadeBlend = 0;
}
else
fadeBlend = 1;
return fadeBlend;
}
2016-07-03 15:39:55 +02:00
int CHudMessage::XPosition( float x, int width, int totalWidth )
2016-06-04 15:24:23 +02:00
{
int xPos;
2016-07-03 15:39:55 +02:00
if( x == -1 )
2016-06-04 15:24:23 +02:00
{
2016-07-03 15:39:55 +02:00
xPos = ( ScreenWidth - width ) / 2;
2016-06-04 15:24:23 +02:00
}
else
{
2016-07-03 15:39:55 +02:00
if( x < 0 )
xPos = ( 1.0 + x ) * ScreenWidth - totalWidth; // Alight right
2016-06-04 15:24:23 +02:00
else
xPos = x * ScreenWidth;
}
2016-07-03 15:39:55 +02:00
if( xPos + width > ScreenWidth )
2016-06-04 15:24:23 +02:00
xPos = ScreenWidth - width;
2016-07-03 15:39:55 +02:00
else if( xPos < 0 )
2016-06-04 15:24:23 +02:00
xPos = 0;
return xPos;
}
int CHudMessage::YPosition( float y, int height )
{
int yPos;
2016-07-03 15:39:55 +02:00
if( y == -1 ) // Centered?
yPos = ( ScreenHeight - height ) * 0.5;
2016-06-04 15:24:23 +02:00
else
{
// Alight bottom?
if ( y < 0 )
2016-07-03 15:39:55 +02:00
yPos = ( 1.0 + y ) * ScreenHeight - height; // Alight bottom
2016-06-04 15:24:23 +02:00
else // align top
yPos = y * ScreenHeight;
}
if ( yPos + height > ScreenHeight )
yPos = ScreenHeight - height;
else if ( yPos < 0 )
yPos = 0;
return yPos;
}
void CHudMessage::MessageScanNextChar( void )
{
int srcRed, srcGreen, srcBlue, destRed = 0, destGreen = 0, destBlue = 0;
int blend;
srcRed = m_parms.pMessage->r1;
srcGreen = m_parms.pMessage->g1;
srcBlue = m_parms.pMessage->b1;
blend = 0; // Pure source
switch( m_parms.pMessage->effect )
{
// Fade-in / Fade-out
case 0:
case 1:
destRed = destGreen = destBlue = 0;
blend = m_parms.fadeBlend;
break;
case 2:
m_parms.charTime += m_parms.pMessage->fadein;
2016-07-03 15:39:55 +02:00
if( m_parms.charTime > m_parms.time )
2016-06-04 15:24:23 +02:00
{
srcRed = srcGreen = srcBlue = 0;
blend = 0; // pure source
}
else
{
float deltaTime = m_parms.time - m_parms.charTime;
destRed = destGreen = destBlue = 0;
if ( m_parms.time > m_parms.fadeTime )
{
blend = m_parms.fadeBlend;
}
else if ( deltaTime > m_parms.pMessage->fxtime )
blend = 0; // pure dest
else
{
destRed = m_parms.pMessage->r2;
destGreen = m_parms.pMessage->g2;
destBlue = m_parms.pMessage->b2;
blend = 255 - (deltaTime * (1.0/m_parms.pMessage->fxtime) * 255.0 + 0.5);
}
}
break;
}
2016-07-03 15:39:55 +02:00
if( blend > 255 )
2016-06-04 15:24:23 +02:00
blend = 255;
2016-07-03 15:39:55 +02:00
else if( blend < 0 )
2016-06-04 15:24:23 +02:00
blend = 0;
2016-07-03 15:39:55 +02:00
m_parms.r = ( ( srcRed * ( 255 - blend ) ) + ( destRed * blend ) ) >> 8;
m_parms.g = ( ( srcGreen * (255 - blend ) ) + ( destGreen * blend ) ) >> 8;
m_parms.b = ( ( srcBlue * ( 255 - blend ) ) + ( destBlue * blend ) ) >> 8;
2016-06-04 15:24:23 +02:00
2016-07-03 15:39:55 +02:00
if( m_parms.pMessage->effect == 1 && m_parms.charTime != 0 )
2016-06-04 15:24:23 +02:00
{
2016-07-03 15:39:55 +02:00
if( m_parms.x >= 0 && m_parms.y >= 0 && ( m_parms.x + gHUD.m_scrinfo.charWidths[m_parms.text] ) <= ScreenWidth )
2016-06-04 15:24:23 +02:00
TextMessageDrawChar( m_parms.x, m_parms.y, m_parms.text, m_parms.pMessage->r2, m_parms.pMessage->g2, m_parms.pMessage->b2 );
}
}
void CHudMessage::MessageScanStart( void )
{
switch( m_parms.pMessage->effect )
{
// Fade-in / out with flicker
case 1:
case 0:
m_parms.fadeTime = m_parms.pMessage->fadein + m_parms.pMessage->holdtime;
2016-07-03 15:39:55 +02:00
if( m_parms.time < m_parms.pMessage->fadein )
2016-06-04 15:24:23 +02:00
{
2016-07-03 15:39:55 +02:00
m_parms.fadeBlend = ( ( m_parms.pMessage->fadein - m_parms.time ) * ( 1.0 / m_parms.pMessage->fadein ) * 255 );
2016-06-04 15:24:23 +02:00
}
2016-07-03 15:39:55 +02:00
else if( m_parms.time > m_parms.fadeTime )
2016-06-04 15:24:23 +02:00
{
2016-07-03 15:39:55 +02:00
if( m_parms.pMessage->fadeout > 0 )
m_parms.fadeBlend = ( ( ( m_parms.time - m_parms.fadeTime ) / m_parms.pMessage->fadeout) * 255);
2016-06-04 15:24:23 +02:00
else
m_parms.fadeBlend = 255; // Pure dest (off)
}
else
m_parms.fadeBlend = 0; // Pure source (on)
m_parms.charTime = 0;
2016-07-03 15:39:55 +02:00
if( m_parms.pMessage->effect == 1 && ( rand() % 100 ) < 10 )
2016-06-04 15:24:23 +02:00
m_parms.charTime = 1;
break;
case 2:
m_parms.fadeTime = (m_parms.pMessage->fadein * m_parms.length) + m_parms.pMessage->holdtime;
if ( m_parms.time > m_parms.fadeTime && m_parms.pMessage->fadeout > 0 )
2016-07-03 15:39:55 +02:00
m_parms.fadeBlend = ( ( ( m_parms.time - m_parms.fadeTime ) / m_parms.pMessage->fadeout ) * 255 );
2016-06-04 15:24:23 +02:00
else
m_parms.fadeBlend = 0;
break;
}
}
void CHudMessage::MessageDrawScan( client_textmessage_t *pMessage, float time )
{
int i, j, length, width;
const char *pText;
2019-04-11 16:21:47 +02:00
const char *pLineStart;
2016-06-04 15:24:23 +02:00
pText = pMessage->pMessage;
// Count lines
m_parms.lines = 1;
m_parms.time = time;
m_parms.pMessage = pMessage;
length = 0;
width = 0;
m_parms.totalWidth = 0;
2016-07-03 15:39:55 +02:00
while( *pText )
2016-06-04 15:24:23 +02:00
{
2016-07-03 15:39:55 +02:00
if( *pText == '\n' )
2016-06-04 15:24:23 +02:00
{
m_parms.lines++;
2016-07-03 15:39:55 +02:00
if( width > m_parms.totalWidth )
2016-06-04 15:24:23 +02:00
m_parms.totalWidth = width;
width = 0;
}
else
2017-06-29 15:56:03 +02:00
width += gHUD.m_scrinfo.charWidths[(unsigned char)*pText];
2016-06-04 15:24:23 +02:00
pText++;
length++;
}
m_parms.length = length;
2016-07-03 15:39:55 +02:00
m_parms.totalHeight = ( m_parms.lines * gHUD.m_scrinfo.iCharHeight );
2016-06-04 15:24:23 +02:00
m_parms.y = YPosition( pMessage->y, m_parms.totalHeight );
pText = pMessage->pMessage;
m_parms.charTime = 0;
MessageScanStart();
2016-07-03 15:39:55 +02:00
for( i = 0; i < m_parms.lines; i++ )
2016-06-04 15:24:23 +02:00
{
m_parms.lineLength = 0;
m_parms.width = 0;
2019-04-11 16:21:47 +02:00
pLineStart = pText;
2016-07-03 15:39:55 +02:00
while( *pText && *pText != '\n' )
2016-06-04 15:24:23 +02:00
{
unsigned char c = *pText;
2019-04-11 16:21:47 +02:00
m_parms.width += gHUD.m_scrinfo.charWidths[c];
m_parms.lineLength++;
2016-06-04 15:24:23 +02:00
pText++;
}
pText++; // Skip LF
m_parms.x = XPosition( pMessage->x, m_parms.width, m_parms.totalWidth );
2016-07-03 15:39:55 +02:00
for( j = 0; j < m_parms.lineLength; j++ )
2016-06-04 15:24:23 +02:00
{
2019-04-11 16:21:47 +02:00
m_parms.text = pLineStart[j];
2016-07-03 15:39:55 +02:00
int next = m_parms.x + gHUD.m_scrinfo.charWidths[m_parms.text];
2016-06-04 15:24:23 +02:00
MessageScanNextChar();
2016-07-03 15:39:55 +02:00
if( m_parms.x >= 0 && m_parms.y >= 0 && next <= ScreenWidth )
2016-06-04 15:24:23 +02:00
TextMessageDrawChar( m_parms.x, m_parms.y, m_parms.text, m_parms.r, m_parms.g, m_parms.b );
m_parms.x = next;
}
m_parms.y += gHUD.m_scrinfo.iCharHeight;
}
}
int CHudMessage::Draw( float fTime )
{
int i, drawn;
client_textmessage_t *pMessage;
2017-06-29 15:56:03 +02:00
float endTime = 0.0f;
2016-06-04 15:24:23 +02:00
drawn = 0;
2016-07-03 15:39:55 +02:00
if( m_gameTitleTime > 0 )
2016-06-04 15:24:23 +02:00
{
float localTime = gHUD.m_flTime - m_gameTitleTime;
float brightness;
// Maybe timer isn't set yet
2016-07-03 15:39:55 +02:00
if( m_gameTitleTime > gHUD.m_flTime )
2016-06-04 15:24:23 +02:00
m_gameTitleTime = gHUD.m_flTime;
2016-07-03 15:39:55 +02:00
if( localTime > ( m_pGameTitle->fadein + m_pGameTitle->holdtime + m_pGameTitle->fadeout ) )
2016-06-04 15:24:23 +02:00
m_gameTitleTime = 0;
else
{
brightness = FadeBlend( m_pGameTitle->fadein, m_pGameTitle->fadeout, m_pGameTitle->holdtime, localTime );
2016-07-03 15:39:55 +02:00
int halfWidth = gHUD.GetSpriteRect( m_HUD_title_half ).right - gHUD.GetSpriteRect( m_HUD_title_half ).left;
int fullWidth = halfWidth + gHUD.GetSpriteRect( m_HUD_title_life ).right - gHUD.GetSpriteRect( m_HUD_title_life ).left;
int fullHeight = gHUD.GetSpriteRect( m_HUD_title_half ).bottom - gHUD.GetSpriteRect( m_HUD_title_half ).top;
2016-06-04 15:24:23 +02:00
int x = XPosition( m_pGameTitle->x, fullWidth, fullWidth );
int y = YPosition( m_pGameTitle->y, fullHeight );
2016-07-03 15:39:55 +02:00
SPR_Set( gHUD.GetSprite( m_HUD_title_half ), brightness * m_pGameTitle->r1, brightness * m_pGameTitle->g1, brightness * m_pGameTitle->b1 );
SPR_DrawAdditive( 0, x, y, &gHUD.GetSpriteRect( m_HUD_title_half ) );
2016-06-04 15:24:23 +02:00
2016-07-03 15:39:55 +02:00
SPR_Set( gHUD.GetSprite( m_HUD_title_life ), brightness * m_pGameTitle->r1, brightness * m_pGameTitle->g1, brightness * m_pGameTitle->b1 );
SPR_DrawAdditive( 0, x + halfWidth, y, &gHUD.GetSpriteRect( m_HUD_title_life ) );
2016-06-04 15:24:23 +02:00
drawn = 1;
}
}
// Fixup level transitions
2016-07-03 15:39:55 +02:00
for( i = 0; i < maxHUDMessages; i++ )
2016-06-04 15:24:23 +02:00
{
// Assume m_parms.time contains last time
2016-07-03 15:39:55 +02:00
if( m_pMessages[i] )
2016-06-04 15:24:23 +02:00
{
pMessage = m_pMessages[i];
2016-07-03 15:39:55 +02:00
if( m_startTime[i] > gHUD.m_flTime )
2016-06-04 15:24:23 +02:00
m_startTime[i] = gHUD.m_flTime + m_parms.time - m_startTime[i] + 0.2; // Server takes 0.2 seconds to spawn, adjust for this
}
}
2016-07-03 15:39:55 +02:00
for( i = 0; i < maxHUDMessages; i++ )
2016-06-04 15:24:23 +02:00
{
2016-07-03 15:39:55 +02:00
if( m_pMessages[i] )
2016-06-04 15:24:23 +02:00
{
pMessage = m_pMessages[i];
// This is when the message is over
switch( pMessage->effect )
{
case 0:
case 1:
endTime = m_startTime[i] + pMessage->fadein + pMessage->fadeout + pMessage->holdtime;
break;
2016-07-03 15:39:55 +02:00
2016-06-04 15:24:23 +02:00
// Fade in is per character in scanning messages
case 2:
2016-07-03 15:39:55 +02:00
endTime = m_startTime[i] + ( pMessage->fadein * strlen( pMessage->pMessage ) ) + pMessage->fadeout + pMessage->holdtime;
2016-06-04 15:24:23 +02:00
break;
}
2016-07-03 15:39:55 +02:00
if( fTime <= endTime )
2016-06-04 15:24:23 +02:00
{
float messageTime = fTime - m_startTime[i];
// Draw the message
// effect 0 is fade in/fade out
// effect 1 is flickery credits
// effect 2 is write out (training room)
MessageDrawScan( pMessage, messageTime );
drawn++;
}
else
{
// The message is over
m_pMessages[i] = NULL;
}
}
}
// Remember the time -- to fix up level transitions
m_parms.time = gHUD.m_flTime;
// Don't call until we get another message
2016-07-03 15:39:55 +02:00
if( !drawn )
2016-06-04 15:24:23 +02:00
m_iFlags &= ~HUD_ACTIVE;
return 1;
}
void CHudMessage::MessageAdd( const char *pName, float time )
{
2016-07-03 15:39:55 +02:00
int i, j;
2016-06-04 15:24:23 +02:00
client_textmessage_t *tempMessage;
2016-07-03 15:39:55 +02:00
for( i = 0; i < maxHUDMessages; i++ )
2016-06-04 15:24:23 +02:00
{
2016-07-03 15:39:55 +02:00
if( !m_pMessages[i] )
2016-06-04 15:24:23 +02:00
{
// Trim off a leading # if it's there
2016-07-03 15:39:55 +02:00
if( pName[0] == '#' )
tempMessage = TextMessageGet( pName + 1 );
2016-06-04 15:24:23 +02:00
else
tempMessage = TextMessageGet( pName );
// If we couldnt find it in the titles.txt, just create it
2016-07-03 15:39:55 +02:00
if( !tempMessage )
2016-06-04 15:24:23 +02:00
{
g_pCustomMessage.effect = 2;
g_pCustomMessage.r1 = g_pCustomMessage.g1 = g_pCustomMessage.b1 = g_pCustomMessage.a1 = 100;
g_pCustomMessage.r2 = 240;
g_pCustomMessage.g2 = 110;
g_pCustomMessage.b2 = 0;
g_pCustomMessage.a2 = 0;
g_pCustomMessage.x = -1; // Centered
g_pCustomMessage.y = 0.7;
g_pCustomMessage.fadein = 0.01;
g_pCustomMessage.fadeout = 1.5;
g_pCustomMessage.fxtime = 0.25;
g_pCustomMessage.holdtime = 5;
g_pCustomMessage.pName = g_pCustomName;
strcpy( g_pCustomText, pName );
g_pCustomMessage.pMessage = g_pCustomText;
tempMessage = &g_pCustomMessage;
}
2016-07-03 15:39:55 +02:00
for( j = 0; j < maxHUDMessages; j++ )
2016-06-04 15:24:23 +02:00
{
2016-07-03 15:39:55 +02:00
if( m_pMessages[j] )
2016-06-04 15:24:23 +02:00
{
// is this message already in the list
2016-07-03 15:39:55 +02:00
if( !strcmp( tempMessage->pMessage, m_pMessages[j]->pMessage ) )
2016-06-04 15:24:23 +02:00
{
return;
}
// get rid of any other messages in same location (only one displays at a time)
2016-07-03 15:39:55 +02:00
if( fabs( tempMessage->y - m_pMessages[j]->y ) < 0.0001 )
2016-06-04 15:24:23 +02:00
{
if ( fabs( tempMessage->x - m_pMessages[j]->x ) < 0.0001 )
{
m_pMessages[j] = NULL;
}
}
}
}
m_pMessages[i] = tempMessage;
m_startTime[i] = time;
return;
}
}
}
int CHudMessage::MsgFunc_HudText( const char *pszName, int iSize, void *pbuf )
{
BEGIN_READ( pbuf, iSize );
char *pString = READ_STRING();
MessageAdd( pString, gHUD.m_flTime );
2016-07-03 15:39:55 +02:00
2016-06-04 15:24:23 +02:00
// Remember the time -- to fix up level transitions
m_parms.time = gHUD.m_flTime;
// Turn on drawing
2016-07-03 15:39:55 +02:00
if( !( m_iFlags & HUD_ACTIVE ) )
2016-06-04 15:24:23 +02:00
m_iFlags |= HUD_ACTIVE;
return 1;
}
int CHudMessage::MsgFunc_GameTitle( const char *pszName, int iSize, void *pbuf )
{
m_pGameTitle = TextMessageGet( "GAMETITLE" );
2016-07-03 15:39:55 +02:00
if( m_pGameTitle != NULL )
2016-06-04 15:24:23 +02:00
{
m_gameTitleTime = gHUD.m_flTime;
// Turn on drawing
2016-07-03 15:39:55 +02:00
if( !( m_iFlags & HUD_ACTIVE ) )
2016-06-04 15:24:23 +02:00
m_iFlags |= HUD_ACTIVE;
}
return 1;
}
2016-07-03 15:39:55 +02:00
void CHudMessage::MessageAdd( client_textmessage_t * newMessage )
2016-06-04 15:24:23 +02:00
{
m_parms.time = gHUD.m_flTime;
// Turn on drawing
2016-07-03 15:39:55 +02:00
if( !( m_iFlags & HUD_ACTIVE ) )
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 < maxHUDMessages; i++ )
2016-06-04 15:24:23 +02:00
{
2016-07-03 15:39:55 +02:00
if( !m_pMessages[i] )
2016-06-04 15:24:23 +02:00
{
m_pMessages[i] = newMessage;
m_startTime[i] = gHUD.m_flTime;
return;
}
}
}