Skip to content

Commit 3662636

Browse files
committed
Constify some color and string functions
1 parent b0d405a commit 3662636

27 files changed

Lines changed: 132 additions & 436 deletions

code/api/et/cg_syscalls.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ int trap_CM_MarkFragments( int numPoints, const vec3_t *points,
225225
}
226226

227227
// ydnar
228-
void trap_R_ProjectDecal( qhandle_t hShader, int numPoints, vec3_t *points, vec4_t projection, vec4_t color, int lifeTime, int fadeTime )
228+
void trap_R_ProjectDecal( qhandle_t hShader, int numPoints, vec3_t *points, vec4_t projection, const vec4_t color, int lifeTime, int fadeTime )
229229
{
230230
SystemCall( CG_R_PROJECTDECAL, hShader, numPoints, points, projection, color, lifeTime, fadeTime );
231231
}

code/api/shared/q_color.c

Lines changed: 26 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -39,30 +39,30 @@ If you have questions concerning this license or the applicable additional terms
3939

4040
#include <string.h>
4141

42-
vec4_t colorBlack = {0, 0, 0, 1};
43-
vec4_t colorRed = {1, 0, 0, 1};
44-
vec4_t colorGreen = {0, 1, 0, 1};
45-
vec4_t colorBlue = {0, 0, 1, 1};
46-
vec4_t colorYellow = {1, 1, 0, 1};
47-
vec4_t colorOrange = {1, 0.5, 0, 1};
48-
vec4_t colorMagenta = {1, 0, 1, 1};
49-
vec4_t colorCyan = {0, 1, 1, 1};
50-
vec4_t colorWhite = {1, 1, 1, 1};
51-
vec4_t colorLtGrey = {0.75, 0.75, 0.75, 1};
52-
vec4_t colorMdGrey = {0.5, 0.5, 0.5, 1};
53-
vec4_t colorDkGrey = {0.25, 0.25, 0.25, 1};
54-
vec4_t colorMdRed = {0.5, 0, 0, 1};
55-
vec4_t colorMdGreen = {0, 0.5, 0, 1};
56-
vec4_t colorDkGreen = {0, 0.20f, 0, 1};
57-
vec4_t colorMdCyan = {0, 0.5, 0.5, 1};
58-
vec4_t colorMdYellow = {0.5, 0.5, 0, 1};
59-
vec4_t colorMdOrange = {0.5, 0.25, 0, 1};
60-
vec4_t colorMdBlue = {0, 0, 0.5, 1};
61-
vec4_t colorLtBlue = {0.367f, 0.261f, 0.722f, 1};
62-
vec4_t colorDkBlue = {0.199f, 0.0f, 0.398f, 1};
63-
vec4_t colorPurple = {0.5, 0, 0.5, 1};
64-
vec4_t colorPipeTrail = { 0.75, 0.75, 0.5, 1 };
65-
vec4_t colorDrawFriend = { 32.f / 255.f, 1, 32.f / 255.f, 1 };
42+
const vec4_t colorBlack = {0, 0, 0, 1};
43+
const vec4_t colorRed = {1, 0, 0, 1};
44+
const vec4_t colorGreen = {0, 1, 0, 1};
45+
const vec4_t colorBlue = {0, 0, 1, 1};
46+
const vec4_t colorYellow = {1, 1, 0, 1};
47+
const vec4_t colorOrange = {1, 0.5, 0, 1};
48+
const vec4_t colorMagenta = {1, 0, 1, 1};
49+
const vec4_t colorCyan = {0, 1, 1, 1};
50+
const vec4_t colorWhite = {1, 1, 1, 1};
51+
const vec4_t colorLtGrey = {0.75, 0.75, 0.75, 1};
52+
const vec4_t colorMdGrey = {0.5, 0.5, 0.5, 1};
53+
const vec4_t colorDkGrey = {0.25, 0.25, 0.25, 1};
54+
const vec4_t colorMdRed = {0.5, 0, 0, 1};
55+
const vec4_t colorMdGreen = {0, 0.5, 0, 1};
56+
const vec4_t colorDkGreen = {0, 0.20f, 0, 1};
57+
const vec4_t colorMdCyan = {0, 0.5, 0.5, 1};
58+
const vec4_t colorMdYellow = {0.5, 0.5, 0, 1};
59+
const vec4_t colorMdOrange = {0.5, 0.25, 0, 1};
60+
const vec4_t colorMdBlue = {0, 0, 0.5, 1};
61+
const vec4_t colorLtBlue = {0.367f, 0.261f, 0.722f, 1};
62+
const vec4_t colorDkBlue = {0.199f, 0.0f, 0.398f, 1};
63+
const vec4_t colorPurple = {0.5, 0, 0.5, 1};
64+
const vec4_t colorPipeTrail = { 0.75, 0.75, 0.5, 1 };
65+
const vec4_t colorDrawFriend = { 32.f / 255.f, 1, 32.f / 255.f, 1 };
6666

6767
const vec4_t g_color_table[Q_COLOR_BITS+1] = {
6868
{ 0.0, 0.0, 0.0, 1.0 }, // 0 - black 0
@@ -99,81 +99,12 @@ const vec4_t g_color_table[Q_COLOR_BITS+1] = {
9999
{ 1.0, 1.0, 0.5, 1.0 }, // O 31
100100
};
101101

102-
#if 0
103-
qboolean GetColourFromHex( const char *string, vec4_t colour ) {
104-
char hexvalue[2];
105-
const char *ptr, *digits = "0123456789ABCDEF\0";
106-
int i;
107-
char *a, *b;
108-
109-
VectorSet4( colour, 1, 1, 1, 1 );
110-
111-
if( strlen(string) != 8 )
112-
return( qfalse ); // bad string
113-
else if( *string == '0' && *(string+1) == 'x' ) {
114-
for( i = 0, ptr = (string + 2); *ptr && *(ptr + 1) && i <= 3; ptr+=2, i++ ) {
115-
hexvalue[0] = *ptr;
116-
hexvalue[1] = *(ptr + 1);
117-
118-
a = strchr(digits,toupper(hexvalue[0]));
119-
b = strchr(digits,toupper(hexvalue[1]));
120-
121-
if( a && b ) {
122-
colour[i] = ((a - digits) * 16) + (b - digits);
123-
}
124-
}
125-
} else {
126-
return( qfalse );
127-
}
128-
return( qtrue );
129-
}
130-
131-
qboolean GetColourFromString( const char *string, vec4_t colour ) {
132-
if( !Q_stricmp( string, "white" ) ) {
133-
VectorCopy4( colorWhite, colour );
134-
} else if( !Q_stricmp( string, "black" ) ) {
135-
VectorCopy4( colorBlack, colour );
136-
} else if( !Q_stricmp( string, "red" ) ) {
137-
VectorCopy4( colorRed, colour );
138-
} else if( !Q_stricmp( string, "green" ) ) {
139-
VectorCopy4( colorGreen, colour );
140-
} else if( !Q_stricmp( string, "blue" ) ) {
141-
VectorCopy4( colorBlue, colour );
142-
} else if( !Q_stricmp( string, "yellow" ) ) {
143-
VectorCopy4( colorYellow, colour );
144-
} else if( !Q_stricmp( string, "magenta" ) ) {
145-
VectorCopy4( colorMagenta, colour );
146-
} else if( !Q_stricmp( string, "cyan" ) ) {
147-
VectorCopy4( colorCyan, colour );
148-
} else if( !Q_stricmp( string, "gray" ) ) {
149-
VectorCopy4( colorMdGrey, colour );
150-
} else {
151-
VectorSet4( colour, 1, 1, 1, 1 );
152-
return( qfalse );
153-
}
154-
return( qtrue );
155-
}
156-
#endif
157-
158102
unsigned ColorBytes3 (float r, float g, float b) {
159-
unsigned i;
160-
161-
( (byte *)&i )[0] = (byte)(r * 255);
162-
( (byte *)&i )[1] = (byte)(g * 255);
163-
( (byte *)&i )[2] = (byte)(b * 255);
164-
165-
return i;
103+
return ((uint32_t)(r * 255) << 16) | ((uint32_t)(g * 255) << 8) | (uint32_t)(b * 255);
166104
}
167105

168106
unsigned ColorBytes4 (float r, float g, float b, float a) {
169-
unsigned i;
170-
171-
( (byte *)&i )[0] = (byte)(r * 255);
172-
( (byte *)&i )[1] = (byte)(g * 255);
173-
( (byte *)&i )[2] = (byte)(b * 255);
174-
( (byte *)&i )[3] = (byte)(a * 255);
175-
176-
return i;
107+
return ((uint32_t)(r * 255) << 24) | ((uint32_t)(g * 255) << 16) | ((uint32_t)(b * 255) << 8) | (uint32_t)(a * 255);
177108
}
178109

179110
float NormalizeColor( const vec3_t in, vec3_t out ) {

code/api/shared/q_color.h

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -106,37 +106,41 @@ extern "C" {
106106
#define S_COLOR_NULL "^*"
107107

108108

109-
extern vec4_t colorBlack;
110-
extern vec4_t colorRed;
111-
extern vec4_t colorGreen;
112-
extern vec4_t colorBlue;
113-
extern vec4_t colorYellow;
114-
extern vec4_t colorOrange;
115-
extern vec4_t colorMagenta;
116-
extern vec4_t colorCyan;
117-
extern vec4_t colorWhite;
118-
extern vec4_t colorLtGrey;
119-
extern vec4_t colorMdGrey;
120-
extern vec4_t colorDkGrey;
121-
extern vec4_t colorMdRed;
122-
extern vec4_t colorMdGreen;
123-
extern vec4_t colorDkGreen;
124-
extern vec4_t colorMdCyan;
125-
extern vec4_t colorMdYellow;
126-
extern vec4_t colorMdOrange;
127-
extern vec4_t colorMdBlue;
128-
extern vec4_t colorPurple;
129-
extern vec4_t colorPipeTrail;
130-
extern vec4_t colorDrawFriend;
109+
extern const vec4_t colorBlack;
110+
extern const vec4_t colorRed;
111+
extern const vec4_t colorGreen;
112+
extern const vec4_t colorBlue;
113+
extern const vec4_t colorYellow;
114+
extern const vec4_t colorOrange;
115+
extern const vec4_t colorMagenta;
116+
extern const vec4_t colorCyan;
117+
extern const vec4_t colorWhite;
118+
extern const vec4_t colorLtGrey;
119+
extern const vec4_t colorMdGrey;
120+
extern const vec4_t colorDkGrey;
121+
extern const vec4_t colorMdRed;
122+
extern const vec4_t colorMdGreen;
123+
extern const vec4_t colorDkGreen;
124+
extern const vec4_t colorMdCyan;
125+
extern const vec4_t colorMdYellow;
126+
extern const vec4_t colorMdOrange;
127+
extern const vec4_t colorMdBlue;
128+
extern const vec4_t colorPurple;
129+
extern const vec4_t colorPipeTrail;
130+
extern const vec4_t colorDrawFriend;
131131

132132
extern const vec4_t g_color_table[Q_COLOR_BITS+1];
133133

134-
//qboolean GetColourFromHex( const char *string, vec4_t colour );
135-
//qboolean GetColourFromString( const char *string, vec4_t colour );
136134
unsigned ColorBytes3 (float r, float g, float b);
137135
unsigned ColorBytes4 (float r, float g, float b, float a);
138136
float NormalizeColor( const vec3_t in, vec3_t out );
139137

138+
#define RGBCopyvf(src, dest) VectorCopy(src, dest)
139+
#define RGBACopyvf(src, dest) VectorCopy4(src, dest)
140+
141+
#define RGBCopy(src, dest) (dest[0]=src[0],dest[1]=src[1],dest[2]=src[2])
142+
#define RGBACopy(src, dest) (dest[0]=src[0],dest[1]=src[1],dest[2]=src[2],dest[3]=src[3])
143+
140144
#if defined(__cplusplus)
141145
} // extern "C"
142146
#endif

code/api/shared/q_string.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -546,12 +546,15 @@ char *Q_CleanStr( char *string ) {
546546
}
547547

548548
// strips whitespaces and bad characters
549-
qboolean Q_isBadDirChar( char c ) {
550-
char badchars[] = { ';', '&', '(', ')', '|', '<', '>', '*', '?', '[', ']', '~', '+', '@', '!', '\\', '/', ' ', '\'', '\"', '\0' };
549+
qboolean Q_isBadDirChar( const char c ) {
550+
const char badchars[] = { '<', '>', ':', '\"', '/', '\\', '|', '?', '*', ' ', '\0' };
551551
int i;
552552

553553
for( i = 0; badchars[i] != '\0'; i++ ) {
554-
if( c == badchars[i] ) {
554+
if ( c > '\0' && c < ' ' ) {
555+
return qtrue;
556+
}
557+
else if( c == badchars[i] ) {
555558
return qtrue;
556559
}
557560
}
@@ -696,7 +699,7 @@ const char *Q_strchrs( const char *string, const char *search )
696699
return NULL;
697700
}
698701

699-
#if defined(_MSC_VER)
702+
#if defined(_MSC_VER) && (_MSC_VER < 1900)
700703
/*
701704
=============
702705
Q_vsnprintf
@@ -710,9 +713,10 @@ int Q_vsnprintf(char *str, size_t size, const char *format, va_list ap)
710713
{
711714
int retval;
712715

716+
str[size - 1] = '\0';
713717
retval = _vsnprintf(str, size, format, ap);
714718

715-
if(retval < 0 || retval == size)
719+
if((unsigned int)retval >= size)
716720
{
717721
// Microsoft doesn't adhere to the C99 standard of vsnprintf,
718722
// which states that the return value must be the number of
@@ -722,7 +726,7 @@ int Q_vsnprintf(char *str, size_t size, const char *format, va_list ap)
722726
// implementation, so we have no choice but to return size.
723727

724728
str[size - 1] = '\0';
725-
return size;
729+
return (int)size;
726730
}
727731

728732
return retval;

code/api/shared/q_string.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,16 @@ void Q_StripColor(char *text);
9090
const char *Q_strchrs( const char *string, const char *search );
9191

9292
// strips whitespaces and bad characters
93-
qboolean Q_isBadDirChar( char c );
93+
qboolean Q_isBadDirChar( const char c );
9494
char *Q_CleanDirName( char *dirname );
9595

9696
//void Q_strstrip( char *string, const char *strip, const char *repl );
9797

98-
#if defined (_MSC_VER)
98+
#if defined (_MSC_VER) && _MSC_VER < 1900
9999
// vsnprintf is ISO/IEC 9899:1999
100100
// abstracting this to make it portable
101101
int Q_vsnprintf( char *str, size_t size, const char *format, va_list args );
102-
#else // not using MSVC
102+
#else // not using MSVC old msvc
103103
#define Q_vsnprintf vsnprintf
104104
#endif
105105

code/cgame/cg_draw.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ void CG_Text_PaintChar(float x, float y, float width, float height, float scale,
250250
trap_R_DrawStretchPic( x, y, w, h, s, t, s2, t2, hShader );
251251
}
252252

253-
void CG_Text_Paint_MaxWidth(float x, float y, float scale, vec4_t color, const char *text, float adjust, int limit, int style, fontStruct_t *parentfont, int textalignment, int maxwidth) {
253+
void CG_Text_Paint_MaxWidth(float x, float y, float scale, const vec4_t color, const char *text, float adjust, int limit, int style, fontStruct_t *parentfont, int textalignment, int maxwidth) {
254254
static char buffer[1024];
255255

256256
if(!maxwidth) {
@@ -263,7 +263,7 @@ void CG_Text_Paint_MaxWidth(float x, float y, float scale, vec4_t color, const c
263263
CG_Text_Paint(x, y, scale, color, buffer, adjust, limit, style, parentfont, textalignment);
264264
}
265265

266-
void CG_Text_Paint(float x, float y, float scale, vec4_t color, const char *text, float adjust, int limit, int style, fontStruct_t *parentfont, int textalignment) {
266+
void CG_Text_Paint(float x, float y, float scale, const vec4_t color, const char *text, float adjust, int limit, int style, fontStruct_t *parentfont, int textalignment) {
267267
int len, count;
268268
vec4_t newColor;
269269
glyphInfo_t *glyph;
@@ -341,9 +341,8 @@ void CG_Text_Paint(float x, float y, float scale, vec4_t color, const char *text
341341

342342
if (style == ITEM_TEXTSTYLE_SHADOWED || style == ITEM_TEXTSTYLE_SHADOWEDMORE) {
343343
int ofs = style == ITEM_TEXTSTYLE_SHADOWED ? 1 : 2;
344-
345-
colorBlack[3] = newColor[3];
346-
trap_R_SetColor( colorBlack );
344+
const vec4_t shadowColor = { 0, 0, 0, newColor[3] };
345+
trap_R_SetColor( shadowColor );
347346
CG_Text_PaintChar(x + ofs + alignmentoffset, y - yadj + ofs,
348347
glyph->imageWidth,
349348
glyph->imageHeight,
@@ -353,7 +352,6 @@ void CG_Text_Paint(float x, float y, float scale, vec4_t color, const char *text
353352
glyph->s2,
354353
glyph->t2,
355354
glyph->glyph);
356-
colorBlack[3] = 1.0;
357355
trap_R_SetColor( newColor );
358356
}
359357
CG_Text_PaintChar(x + alignmentoffset, y - yadj,

code/cgame/cg_effects.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ localEntity_t *CG_Q3F_MakeBeam( const vec3_t origin,
327327
int numSubdivisions,
328328
float scale,
329329
int leFlags,
330-
vec4_t colour,
330+
const vec4_t colour,
331331
int duration,
332332
float speedscale,
333333
qhandle_t hShader ) {

code/cgame/cg_local.h

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1479,8 +1479,8 @@ extern itemInfo_t cg_items[MAX_ITEMS];
14791479
//unlagged - client options
14801480
//unlagged - cg_unlagged.c
14811481
void CG_PredictWeaponEffects( centity_t *cent );
1482-
void CG_AddBoundingBox( centity_t *cent, vec3_t color );
1483-
void CG_DrawBoundingBox( vec3_t origin, vec3_t mins,vec3_t maxs, vec3_t color );
1482+
void CG_AddBoundingBox( centity_t *cent, const vec3_t color );
1483+
void CG_DrawBoundingBox( vec3_t origin, vec3_t mins,vec3_t maxs, const vec3_t color );
14841484

14851485
//
14861486
// cg_main.c
@@ -1594,8 +1594,8 @@ void CG_DrawActive( stereoFrame_t stereoView );
15941594
void CG_ETF_DrawSkyPortal( refdef_t *parentrefdef, vec4_t *parentflareblind, stereoFrame_t stereoView, vec3_t sky_origin );
15951595
void CG_DrawTeamBackground( int x, int y, int w, int h, float alpha, int team );
15961596
void CG_OwnerDraw( itemDef_t *item, float x, float y, float w, float h, float text_x, float text_y, int ownerDraw, int ownerDrawFlags, int align, float special, float scale, vec4_t color, qhandle_t shader, int textStyle, int textalignment );
1597-
void CG_Text_Paint_MaxWidth(float x, float y, float scale, vec4_t color, const char *text, float adjust, int limit, int style, fontStruct_t *parentfont, int textalignment, int maxwidth);
1598-
void CG_Text_Paint(float x, float y, float scale, vec4_t color, const char *text, float adjust, int limit, int style, fontStruct_t *parentfont, int textalignment);
1597+
void CG_Text_Paint_MaxWidth(float x, float y, float scale, const vec4_t color, const char *text, float adjust, int limit, int style, fontStruct_t *parentfont, int textalignment, int maxwidth);
1598+
void CG_Text_Paint(float x, float y, float scale, const vec4_t color, const char *text, float adjust, int limit, int style, fontStruct_t *parentfont, int textalignment);
15991599
int CG_Text_Width(const char *text, float scale, int limit, fontStruct_t *parentfont);
16001600
int CG_Text_Height(const char *text, float scale, int limit, fontStruct_t *parentfont);
16011601
float CG_GetValue(int ownerDraw);
@@ -1742,16 +1742,11 @@ void CG_AddWeaponWithPowerups( refEntity_t *gun, entityState_t *state, int team
17421742

17431743
void CG_InitMarkPolys( void );
17441744
void CG_AddMarks( void );
1745-
void CG_OldMark(qhandle_t markShader, const vec3_t origin, const vec3_t dir,
1746-
float orientation,float radius,vec4_t color,
1747-
int lifetime,leMarkFadeType_t fadetype);
1748-
void CG_DecalMark(qhandle_t markShader, const vec3_t origin, vec4_t projection,
1749-
float orientation,float radius,vec4_t color,
1750-
int lifetime,int fadetime
1751-
);
1752-
void CG_ExplosionMark(vec3_t origin,float radius,vec4_t color );
1753-
void CG_BulletMark(qhandle_t shader,vec3_t origin,vec3_t dir,float radius,vec4_t color);
1754-
qboolean CG_ShadowMark(vec3_t origin, float radius, float height, float *shadowPlane );
1745+
void CG_OldMark( qhandle_t markShader, const vec3_t origin, const vec3_t dir, float orientation, float radius, const vec4_t color, int lifetime, leMarkFadeType_t fadetype);
1746+
void CG_DecalMark( qhandle_t markShader, const vec3_t origin, vec4_t projection, float orientation, float radius, const vec4_t color, int lifetime, int fadetime );
1747+
void CG_ExplosionMark(const vec3_t origin,float radius, const vec4_t color );
1748+
void CG_BulletMark(qhandle_t shader, const vec3_t origin, const vec3_t dir, float radius, const vec4_t color);
1749+
qboolean CG_ShadowMark(const vec3_t origin, float radius, float height, float *shadowPlane );
17551750

17561751
//
17571752
// cg_trails.c
@@ -1830,7 +1825,7 @@ localEntity_t *CG_Q3F_MakeBeam( const vec3_t origin,
18301825
int numSubdivisions,
18311826
float radius,
18321827
int leFlags,
1833-
vec4_t colour,
1828+
const vec4_t colour,
18341829
int duration,
18351830
float speedscale,
18361831
qhandle_t hShader );
@@ -2102,7 +2097,7 @@ int trap_CM_MarkFragments( int numPoints, const vec3_t *points,
21022097
int maxPoints, vec3_t pointBuffer,
21032098
int maxFragments, markFragment_t *fragmentBuffer );
21042099
// ydnar: projects a decal onto brush model surfaces
2105-
void trap_R_ProjectDecal( qhandle_t hShader, int numPoints, vec3_t *points, vec4_t projection, vec4_t color, int lifeTime, int fadeTime );
2100+
void trap_R_ProjectDecal( qhandle_t hShader, int numPoints, vec3_t *points, vec4_t projection, const vec4_t color, int lifeTime, int fadeTime );
21062101
void trap_R_ClearDecals( void );
21072102

21082103
// normal sounds will have their volume dynamically changed as their entity

0 commit comments

Comments
 (0)