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