hlsdk-xash3d/cl_dll/MOTD.cpp

171 lines
3.8 KiB
C++
Raw Normal View History

2016-02-24 22:26:16 +01:00
/***
*
* Copyright (c) 1999, Valve LLC. All rights reserved.
2016-03-01 21:18:42 +01:00
*
* This product contains software technology licensed from Id
* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc.
2016-02-24 22:26:16 +01:00
* 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.
*
****/
//
// MOTD.cpp
//
// for displaying a server-sent message of the day
//
#include "hud.h"
#include "cl_util.h"
#include "parsemsg.h"
#include "kbutton.h"
#include "triangleapi.h"
#include <string.h>
#include <stdio.h>
#if !USE_VGUI || USE_NOVGUI_MOTD
2016-06-25 20:21:01 +02:00
DECLARE_MESSAGE( m_MOTD, MOTD )
#endif
2016-02-24 22:26:16 +01:00
2016-08-02 15:36:48 +02:00
int CHudMOTD::Init( void )
2016-02-24 22:26:16 +01:00
{
gHUD.AddHudElem( this );
#if !USE_VGUI || USE_NOVGUI_MOTD
2016-02-24 22:26:16 +01:00
HOOK_MESSAGE( MOTD );
#endif
2016-02-24 22:26:16 +01:00
m_bShow = false;
m_iFlags &= ~HUD_ACTIVE; // start out inactive
m_szMOTD[0] = 0;
return 1;
}
2016-08-02 15:36:48 +02:00
int CHudMOTD::VidInit( void )
2016-02-24 22:26:16 +01:00
{
// Load sprites here
return 1;
}
2016-08-02 15:36:48 +02:00
void CHudMOTD::Reset( void )
2016-02-24 22:26:16 +01:00
{
m_iFlags &= ~HUD_ACTIVE; // start out inactive
m_szMOTD[0] = 0;
m_iLines = 0;
m_bShow = 0;
}
#define LINE_HEIGHT (gHUD.m_scrinfo.iCharHeight)
2016-02-24 22:26:16 +01:00
#define ROW_GAP 13
#define ROW_RANGE_MIN 30
#define ROW_RANGE_MAX ( ScreenHeight - 100 )
2016-08-02 15:36:48 +02:00
int CHudMOTD::Draw( float fTime )
2016-02-24 22:26:16 +01:00
{
gHUD.m_iNoConsolePrint &= ~( 1 << 1 );
if( !m_bShow )
return 1;
gHUD.m_iNoConsolePrint |= 1 << 1;
//bool bScroll;
2016-02-24 22:26:16 +01:00
// find the top of where the MOTD should be drawn, so the whole thing is centered in the screen
2016-08-02 15:36:48 +02:00
int ypos = ( ScreenHeight - LINE_HEIGHT * m_iLines ) / 2; // shift it up slightly
2016-02-24 22:26:16 +01:00
char *ch = m_szMOTD;
2016-08-02 15:36:48 +02:00
int xpos = ( ScreenWidth - gHUD.m_scrinfo.charWidths['M'] * m_iMaxLength ) / 2;
if( xpos < 30 )
xpos = 30;
int xmax = xpos + gHUD.m_scrinfo.charWidths['M'] * m_iMaxLength;
2016-02-24 22:26:16 +01:00
int height = LINE_HEIGHT * m_iLines;
int ypos_r=ypos;
if( height > ROW_RANGE_MAX )
{
ypos = ROW_RANGE_MIN + 7 + scroll;
if( ypos > ROW_RANGE_MIN + 4 )
2019-10-13 14:12:41 +02:00
scroll-= ( ypos - ( ROW_RANGE_MIN + 4 ) ) / 3.0f;
2016-02-24 22:26:16 +01:00
if( ypos + height < ROW_RANGE_MAX )
2019-10-13 14:12:41 +02:00
scroll+= ( ROW_RANGE_MAX - ( ypos + height ) ) / 3.0f;
2016-02-24 22:26:16 +01:00
ypos_r = ROW_RANGE_MIN;
height = ROW_RANGE_MAX;
}
// int ymax = ypos + height;
2016-02-24 22:26:16 +01:00
if( xmax > ScreenWidth - 30 ) xmax = ScreenWidth - 30;
2016-08-02 15:36:48 +02:00
gHUD.DrawDarkRectangle( xpos - 5, ypos_r - 5, xmax - xpos + 10, height + 10 );
while( *ch )
2016-02-24 22:26:16 +01:00
{
2016-03-01 21:18:42 +01:00
char *next_line;
2016-02-24 22:26:16 +01:00
int line_length = 0; // count the length of the current line
2016-08-02 15:36:48 +02:00
for( next_line = ch; *next_line != '\n' && *next_line != 0; next_line++ )
line_length += gHUD.m_scrinfo.charWidths[*next_line];
2016-02-24 22:26:16 +01:00
char *top = next_line;
2016-08-02 15:36:48 +02:00
if( *top == '\n' )
2016-02-24 22:26:16 +01:00
*top = 0;
else
top = NULL;
// find where to start drawing the line
2016-08-02 15:36:48 +02:00
if( ( ypos > ROW_RANGE_MIN ) && ( ypos + LINE_HEIGHT <= ypos_r + height ) )
2017-03-29 21:59:50 +02:00
DrawUtfString( xpos, ypos, xmax, ch, 255, 180, 0 );
2016-02-24 22:26:16 +01:00
ypos += LINE_HEIGHT;
2016-08-02 15:36:48 +02:00
if( top ) // restore
2016-02-24 22:26:16 +01:00
*top = '\n';
ch = next_line;
2016-08-02 15:36:48 +02:00
if( *ch == '\n' )
2016-02-24 22:26:16 +01:00
ch++;
2016-08-02 15:36:48 +02:00
if( ypos > ( ScreenHeight - 20 ) )
2016-02-24 22:26:16 +01:00
break; // don't let it draw too low
}
2016-03-01 21:18:42 +01:00
2016-02-24 22:26:16 +01:00
return 1;
}
2016-08-02 15:36:48 +02:00
int CHudMOTD::MsgFunc_MOTD( const char *pszName, int iSize, void *pbuf )
2016-02-24 22:26:16 +01:00
{
2016-08-02 15:36:48 +02:00
if( m_iFlags & HUD_ACTIVE )
2016-02-24 22:26:16 +01:00
{
Reset(); // clear the current MOTD in prep for this one
}
BEGIN_READ( pbuf, iSize );
int is_finished = READ_BYTE();
2017-02-01 15:55:40 +01:00
strncat( m_szMOTD, READ_STRING(), sizeof(m_szMOTD) - 1 );
2016-02-24 22:26:16 +01:00
2016-08-02 15:36:48 +02:00
if( is_finished )
2016-02-24 22:26:16 +01:00
{
int length = 0;
2016-03-01 21:18:42 +01:00
2016-02-24 22:26:16 +01:00
m_iMaxLength = 0;
m_iFlags |= HUD_ACTIVE;
2016-08-02 15:36:48 +02:00
for( char *sz = m_szMOTD; *sz != 0; sz++ ) // count the number of lines in the MOTD
2016-02-24 22:26:16 +01:00
{
2016-08-02 15:36:48 +02:00
if( *sz == '\n' )
2016-02-24 22:26:16 +01:00
{
m_iLines++;
if( length > m_iMaxLength )
{
m_iMaxLength = length;
length = 0;
}
}
length++;
}
2016-03-01 21:18:42 +01:00
2016-02-24 22:26:16 +01:00
m_iLines++;
if( length > m_iMaxLength )
{
m_iMaxLength = length;
// length = 0;
2016-02-24 22:26:16 +01:00
}
m_bShow = true;
}
return 1;
}