trace_plugin/sdk_util.cpp
changeset 0 d15304fcb7a7
child 46 77537dee7ca6
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/trace_plugin/sdk_util.cpp	Tue Apr 06 07:36:14 2004 +0000
     1.3 @@ -0,0 +1,143 @@
     1.4 +// vi: set ts=4 sw=4 :
     1.5 +// vim: set tw=75 :
     1.6 +
     1.7 +// sdk_util.cpp - utility routines from HL SDK util.cpp
     1.8 +
     1.9 +// Selected portions of dlls/util.cpp from SDK 2.1.
    1.10 +// Functions copied from there as needed...
    1.11 +// And modified to avoid buffer overflows (argh).
    1.12 +
    1.13 +/***
    1.14 +*
    1.15 +*	Copyright (c) 1999, 2000 Valve LLC. All rights reserved.
    1.16 +*	
    1.17 +*	This product contains software technology licensed from Id 
    1.18 +*	Software, Inc. ("Id Technology").  Id Technology (c) 1996 Id Software, Inc. 
    1.19 +*	All Rights Reserved.
    1.20 +*
    1.21 +*   Use, distribution, and modification of this source code and/or resulting
    1.22 +*   object code is restricted to non-commercial enhancements to products from
    1.23 +*   Valve LLC.  All other use, distribution, or modification is prohibited
    1.24 +*   without written permission from Valve LLC.
    1.25 +*
    1.26 +****/
    1.27 +/*
    1.28 +
    1.29 +===== util.cpp ========================================================
    1.30 +
    1.31 +  Utility code.  Really not optional after all.
    1.32 +
    1.33 +*/
    1.34 +
    1.35 +#include <extdll.h>
    1.36 +#include "sdk_util.h"
    1.37 +#include <cbase.h>
    1.38 +
    1.39 +#include <string.h>			// for strncpy(), etc
    1.40 +
    1.41 +#include "osdep.h"			// win32 vsnprintf, etc
    1.42 +
    1.43 +char* UTIL_VarArgs( char *format, ... )
    1.44 +{
    1.45 +	va_list		argptr;
    1.46 +	static char		string[1024];
    1.47 +	
    1.48 +	va_start (argptr, format);
    1.49 +	vsnprintf (string, sizeof(string), format, argptr);
    1.50 +	va_end (argptr);
    1.51 +
    1.52 +	return string;
    1.53 +}
    1.54 +	
    1.55 +
    1.56 +//=========================================================
    1.57 +// UTIL_LogPrintf - Prints a logged message to console.
    1.58 +// Preceded by LOG: ( timestamp ) < message >
    1.59 +//=========================================================
    1.60 +void UTIL_LogPrintf( char *fmt, ... )
    1.61 +{
    1.62 +	va_list			argptr;
    1.63 +	static char		string[1024];
    1.64 +	
    1.65 +	va_start ( argptr, fmt );
    1.66 +	vsnprintf ( string, sizeof(string), fmt, argptr );
    1.67 +	va_end   ( argptr );
    1.68 +
    1.69 +	// Print to server console
    1.70 +	ALERT( at_logged, "%s", string );
    1.71 +}
    1.72 +
    1.73 +
    1.74 +void UTIL_HudMessage(CBaseEntity *pEntity, const hudtextparms_t &textparms, 
    1.75 +		const char *pMessage)
    1.76 +{
    1.77 +	if ( !pEntity )
    1.78 +		return;
    1.79 +
    1.80 +	MESSAGE_BEGIN( MSG_ONE, SVC_TEMPENTITY, NULL, ENT(pEntity->pev) );
    1.81 +		WRITE_BYTE( TE_TEXTMESSAGE );
    1.82 +		WRITE_BYTE( textparms.channel & 0xFF );
    1.83 +
    1.84 +		WRITE_SHORT( FixedSigned16( textparms.x, 1<<13 ) );
    1.85 +		WRITE_SHORT( FixedSigned16( textparms.y, 1<<13 ) );
    1.86 +		WRITE_BYTE( textparms.effect );
    1.87 +
    1.88 +		WRITE_BYTE( textparms.r1 );
    1.89 +		WRITE_BYTE( textparms.g1 );
    1.90 +		WRITE_BYTE( textparms.b1 );
    1.91 +		WRITE_BYTE( textparms.a1 );
    1.92 +
    1.93 +		WRITE_BYTE( textparms.r2 );
    1.94 +		WRITE_BYTE( textparms.g2 );
    1.95 +		WRITE_BYTE( textparms.b2 );
    1.96 +		WRITE_BYTE( textparms.a2 );
    1.97 +
    1.98 +		WRITE_SHORT( FixedUnsigned16( textparms.fadeinTime, 1<<8 ) );
    1.99 +		WRITE_SHORT( FixedUnsigned16( textparms.fadeoutTime, 1<<8 ) );
   1.100 +		WRITE_SHORT( FixedUnsigned16( textparms.holdTime, 1<<8 ) );
   1.101 +
   1.102 +		if ( textparms.effect == 2 )
   1.103 +			WRITE_SHORT( FixedUnsigned16( textparms.fxTime, 1<<8 ) );
   1.104 +		
   1.105 +		if ( strlen( pMessage ) < 512 )
   1.106 +		{
   1.107 +			WRITE_STRING( pMessage );
   1.108 +		}
   1.109 +		else
   1.110 +		{
   1.111 +			char tmp[512];
   1.112 +			strncpy( tmp, pMessage, 511 );
   1.113 +			tmp[511] = 0;
   1.114 +			WRITE_STRING( tmp );
   1.115 +		}
   1.116 +	MESSAGE_END();
   1.117 +}
   1.118 +
   1.119 +short FixedSigned16( float value, float scale )
   1.120 +{
   1.121 +	int output;
   1.122 +
   1.123 +	output = (int) (value * scale);
   1.124 +
   1.125 +	if ( output > 32767 )
   1.126 +		output = 32767;
   1.127 +
   1.128 +	if ( output < -32768 )
   1.129 +		output = -32768;
   1.130 +
   1.131 +	return (short)output;
   1.132 +}
   1.133 +
   1.134 +unsigned short FixedUnsigned16( float value, float scale )
   1.135 +{
   1.136 +	int output;
   1.137 +
   1.138 +	output = (int) (value * scale);
   1.139 +	if ( output < 0 )
   1.140 +		output = 0;
   1.141 +	if ( output > 0xFFFF )
   1.142 +		output = 0xFFFF;
   1.143 +
   1.144 +	return (unsigned short)output;
   1.145 +}
   1.146 +