|
| 1 | +/*** |
| 2 | +* |
| 3 | +* Copyright (c) 1999, Valve LLC. All rights reserved. |
| 4 | +* |
| 5 | +* This product contains software technology licensed from Id |
| 6 | +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. |
| 7 | +* All Rights Reserved. |
| 8 | +* |
| 9 | +* Use, distribution, and modification of this source code and/or resulting |
| 10 | +* object code is restricted to non-commercial enhancements to products from |
| 11 | +* Valve LLC. All other use, distribution, or modification is prohibited |
| 12 | +* without written permission from Valve LLC. |
| 13 | +* |
| 14 | +****/ |
| 15 | +// |
| 16 | +// MOTD.cpp |
| 17 | +// |
| 18 | +// for displaying a server-sent message of the day |
| 19 | +// |
| 20 | + |
| 21 | +#include "hud.h" |
| 22 | +#include "util.h" |
| 23 | +#include "parsemsg.h" |
| 24 | + |
| 25 | +#include <string.h> |
| 26 | +#include <stdio.h> |
| 27 | + |
| 28 | +DECLARE_MESSAGE( m_MOTD, MOTD ); |
| 29 | + |
| 30 | +int CHudMOTD::MOTD_DISPLAY_TIME; |
| 31 | + |
| 32 | +int CHudMOTD :: Init( void ) |
| 33 | +{ |
| 34 | + gHUD.AddHudElem( this ); |
| 35 | + |
| 36 | + HOOK_MESSAGE( MOTD ); |
| 37 | + |
| 38 | + CVAR_CREATE( "motd_display_time", "6", 0 ); |
| 39 | + |
| 40 | + m_iFlags &= ~HUD_ACTIVE; // start out inactive |
| 41 | + m_szMOTD[0] = 0; |
| 42 | + |
| 43 | + return 1; |
| 44 | +} |
| 45 | + |
| 46 | +int CHudMOTD :: VidInit( void ) |
| 47 | +{ |
| 48 | + // Load sprites here |
| 49 | + |
| 50 | + return 1; |
| 51 | +} |
| 52 | + |
| 53 | +void CHudMOTD :: Reset( void ) |
| 54 | +{ |
| 55 | + m_iFlags &= ~HUD_ACTIVE; // start out inactive |
| 56 | + m_szMOTD[0] = 0; |
| 57 | + m_iLines = 0; |
| 58 | + m_flActiveTill = 0; |
| 59 | +} |
| 60 | + |
| 61 | +#define LINE_HEIGHT 13 |
| 62 | + |
| 63 | +int CHudMOTD :: Draw( float fTime ) |
| 64 | +{ |
| 65 | + // Draw MOTD line-by-line |
| 66 | + |
| 67 | + if ( m_flActiveTill < gHUD.m_flTime ) |
| 68 | + { // finished with MOTD, disable it |
| 69 | + m_szMOTD[0] = 0; |
| 70 | + m_iLines = 0; |
| 71 | + m_iFlags &= ~HUD_ACTIVE; |
| 72 | + return 1; |
| 73 | + } |
| 74 | + |
| 75 | + // cap activetill time to the display time |
| 76 | + m_flActiveTill = min( gHUD.m_flTime + MOTD_DISPLAY_TIME, m_flActiveTill ); |
| 77 | + |
| 78 | + // find the top of where the MOTD should be drawn, so the whole thing is centered in the screen |
| 79 | + int ypos = max(((ScreenHeight - (m_iLines * LINE_HEIGHT)) / 2) - 40, 30 ); // shift it up slightly |
| 80 | + char *ch = m_szMOTD; |
| 81 | + while ( *ch ) |
| 82 | + { |
| 83 | + int line_length = 0; // count the length of the current line |
| 84 | + for ( char *next_line = ch; *next_line != '\n' && *next_line != 0; next_line++ ) |
| 85 | + line_length += gHUD.m_scrinfo.charWidths[ *next_line ]; |
| 86 | + char *top = next_line; |
| 87 | + if ( *top == '\n' ) |
| 88 | + *top = 0; |
| 89 | + else |
| 90 | + top = NULL; |
| 91 | + |
| 92 | + // find where to start drawing the line |
| 93 | + int xpos = (ScreenWidth - line_length) / 2; |
| 94 | + |
| 95 | + gHUD.DrawHudString( xpos, ypos, ScreenWidth, ch, 255, 180, 0 ); |
| 96 | + |
| 97 | + ypos += LINE_HEIGHT; |
| 98 | + |
| 99 | + if ( top ) // restore |
| 100 | + *top = '\n'; |
| 101 | + ch = next_line; |
| 102 | + if ( *ch == '\n' ) |
| 103 | + ch++; |
| 104 | + |
| 105 | + if ( ypos > (ScreenHeight - 20) ) |
| 106 | + break; // don't let it draw too low |
| 107 | + } |
| 108 | + |
| 109 | + return 1; |
| 110 | +} |
| 111 | + |
| 112 | +int CHudMOTD :: MsgFunc_MOTD( const char *pszName, int iSize, void *pbuf ) |
| 113 | +{ |
| 114 | + if ( m_iFlags & HUD_ACTIVE ) |
| 115 | + { |
| 116 | + Reset(); // clear the current MOTD in prep for this one |
| 117 | + } |
| 118 | + |
| 119 | + BEGIN_READ( pbuf, iSize ); |
| 120 | + |
| 121 | + int is_finished = READ_BYTE(); |
| 122 | + strcat( m_szMOTD, READ_STRING() ); |
| 123 | + |
| 124 | + if ( is_finished ) |
| 125 | + { |
| 126 | + m_iFlags |= HUD_ACTIVE; |
| 127 | + |
| 128 | + MOTD_DISPLAY_TIME = CVAR_GET_FLOAT( "motd_display_time" ); |
| 129 | + |
| 130 | + m_flActiveTill = gHUD.m_flTime + MOTD_DISPLAY_TIME; |
| 131 | + |
| 132 | + for ( char *sz = m_szMOTD; *sz != 0; sz++ ) // count the number of lines in the MOTD |
| 133 | + { |
| 134 | + if ( *sz == '\n' ) |
| 135 | + m_iLines++; |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + return 1; |
| 140 | +} |
| 141 | + |
0 commit comments