trace_plugin/sdk_util.cpp
author David Anderson <dvander@alliedmods.net>
Tue Feb 12 20:28:50 2013 -0600 (2013-02-12)
changeset 263 b8ab4fdbb37e
parent 257 400a6f2712cf
child 265 9e61d0d70e9a
permissions -rw-r--r--
Fix build of sample plugins.
     1 // vi: set ts=4 sw=4 :
     2 // vim: set tw=75 :
     3 
     4 // sdk_util.cpp - utility routines from HL SDK util.cpp
     5 
     6 // Selected portions of dlls/util.cpp from SDK 2.1.
     7 // Functions copied from there as needed...
     8 // And modified to avoid buffer overflows (argh).
     9 // Also modified to remove dependency on CBaseEntity class.
    10 
    11 /***
    12 *
    13 *	Copyright (c) 1999, 2000 Valve LLC. All rights reserved.
    14 *	
    15 *	This product contains software technology licensed from Id 
    16 *	Software, Inc. ("Id Technology").  Id Technology (c) 1996 Id Software, Inc. 
    17 *	All Rights Reserved.
    18 *
    19 *   Use, distribution, and modification of this source code and/or resulting
    20 *   object code is restricted to non-commercial enhancements to products from
    21 *   Valve LLC.  All other use, distribution, or modification is prohibited
    22 *   without written permission from Valve LLC.
    23 *
    24 ****/
    25 /*
    26 
    27 ===== util.cpp ========================================================
    28 
    29   Utility code.  Really not optional after all.
    30 
    31 */
    32 
    33 #include <extdll.h>
    34 #include "sdk_util.h"
    35 
    36 #include <string.h>			// for strncpy(), etc
    37 
    38 #include "osdep.h"			// win32 vsnprintf, etc
    39 
    40 char* UTIL_VarArgs( const char *format, ... )
    41 {
    42 	va_list		argptr;
    43 	static char		string[1024];
    44 	
    45 	va_start (argptr, format);
    46 	vsnprintf (string, sizeof(string), format, argptr);
    47 	va_end (argptr);
    48 
    49 	return string;
    50 }
    51 	
    52 
    53 //=========================================================
    54 // UTIL_LogPrintf - Prints a logged message to console.
    55 // Preceded by LOG: ( timestamp ) < message >
    56 //=========================================================
    57 void UTIL_LogPrintf( char *fmt, ... )
    58 {
    59 	va_list			argptr;
    60 	static char		string[1024];
    61 	
    62 	va_start ( argptr, fmt );
    63 	vsnprintf ( string, sizeof(string), fmt, argptr );
    64 	va_end   ( argptr );
    65 
    66 	// Print to server console
    67 	ALERT( at_logged, "%s", string );
    68 }
    69 
    70 
    71 void META_UTIL_HudMessage(edict_t *pEntity, const hudtextparms_t &textparms, 
    72 		const char *pMessage)
    73 {
    74 	if ( FNullEnt(pEntity) || pEntity->free )
    75 		return;
    76 
    77 	MESSAGE_BEGIN( MSG_ONE, SVC_TEMPENTITY, NULL, pEntity );
    78 		WRITE_BYTE( TE_TEXTMESSAGE );
    79 		WRITE_BYTE( textparms.channel & 0xFF );
    80 
    81 		WRITE_SHORT( FixedSigned16( textparms.x, 1<<13 ) );
    82 		WRITE_SHORT( FixedSigned16( textparms.y, 1<<13 ) );
    83 		WRITE_BYTE( textparms.effect );
    84 
    85 		WRITE_BYTE( textparms.r1 );
    86 		WRITE_BYTE( textparms.g1 );
    87 		WRITE_BYTE( textparms.b1 );
    88 		WRITE_BYTE( textparms.a1 );
    89 
    90 		WRITE_BYTE( textparms.r2 );
    91 		WRITE_BYTE( textparms.g2 );
    92 		WRITE_BYTE( textparms.b2 );
    93 		WRITE_BYTE( textparms.a2 );
    94 
    95 		WRITE_SHORT( FixedUnsigned16( textparms.fadeinTime, 1<<8 ) );
    96 		WRITE_SHORT( FixedUnsigned16( textparms.fadeoutTime, 1<<8 ) );
    97 		WRITE_SHORT( FixedUnsigned16( textparms.holdTime, 1<<8 ) );
    98 
    99 		if ( textparms.effect == 2 )
   100 			WRITE_SHORT( FixedUnsigned16( textparms.fxTime, 1<<8 ) );
   101 		
   102 		if ( strlen( pMessage ) < 512 )
   103 		{
   104 			WRITE_STRING( pMessage );
   105 		}
   106 		else
   107 		{
   108 			char tmp[512];
   109 			strncpy( tmp, pMessage, 511 );
   110 			tmp[511] = 0;
   111 			WRITE_STRING( tmp );
   112 		}
   113 	MESSAGE_END();
   114 }
   115 
   116 short FixedSigned16( float value, float scale )
   117 {
   118 	int output;
   119 
   120 	output = (int) (value * scale);
   121 
   122 	if ( output > 32767 )
   123 		output = 32767;
   124 
   125 	if ( output < -32768 )
   126 		output = -32768;
   127 
   128 	return (short)output;
   129 }
   130 
   131 unsigned short FixedUnsigned16( float value, float scale )
   132 {
   133 	int output;
   134 
   135 	output = (int) (value * scale);
   136 	if ( output < 0 )
   137 		output = 0;
   138 	if ( output > 0xFFFF )
   139 		output = 0xFFFF;
   140 
   141 	return (unsigned short)output;
   142 }
   143