Skip to content
This repository was archived by the owner on Dec 11, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions inc/common/msg.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,9 @@ static inline int MSG_PackSolid16(const vec3_t mins, const vec3_t maxs)
int zd = -mins[2] / 8;
int zu = (maxs[2] + 32) / 8;

clamp(x, 1, 31);
clamp(zd, 1, 31);
clamp(zu, 1, 63);
x = Q_clip(x, 1, 31);
zd = Q_clip(zd, 1, 31);
zu = Q_clip(zu, 1, 63);

return (zu << 10) | (zd << 5) | x;
}
Expand All @@ -202,9 +202,9 @@ static inline uint32_t MSG_PackSolid32_Ver1(const vec3_t mins, const vec3_t maxs
int zd = -mins[2];
int zu = maxs[2] + 32768;

clamp(x, 1, 255);
clamp(zd, 0, 255);
clamp(zu, 0, 65535);
x = Q_clip(x, 1, 255);
zd = Q_clip_uint8(zd);
zu = Q_clip_uint16(zu);

return ((uint32_t)zu << 16) | (zd << 8) | x;
}
Expand All @@ -216,10 +216,10 @@ static inline uint32_t MSG_PackSolid32_Ver2(const vec3_t mins, const vec3_t maxs
int zd = -mins[2];
int zu = maxs[2] + 32;

clamp(x, 1, 255);
clamp(y, 1, 255);
clamp(zd, 0, 255);
clamp(zu, 0, 255);
x = Q_clip(x, 1, 255);
y = Q_clip(y, 1, 255);
zd = Q_clip_uint8(zd);
zu = Q_clip_uint8(zu);

return MakeLittleLong(x, y, zd, zu);
}
Expand Down
69 changes: 65 additions & 4 deletions inc/shared/shared.h
Original file line number Diff line number Diff line change
Expand Up @@ -354,12 +354,65 @@ void Q_srand(uint32_t seed);
uint32_t Q_rand(void);
uint32_t Q_rand_uniform(uint32_t n);

#define clamp(a,b,c) ((a)<(b)?(a)=(b):(a)>(c)?(a)=(c):(a))
#define cclamp(a,b,c) ((b)>(c)?clamp(a,c,b):clamp(a,b,c))

static inline int Q_clip(int a, int b, int c)
{
return clamp(a, b, c);
if (a < b)
return b;
if (a > c)
return c;
return a;
}

static inline float Q_clipf(float a, float b, float c)
{
#if defined(__GNUC__) && defined(__SSE__)
__asm__("maxss %1, %0 \n\t"
"minss %2, %0 \n\t"
: "+&x"(a) : "xm"(b), "xm"(c));
return a;
#else
if (a < b)
return b;
if (a > c)
return c;
return a;
#endif
}

static inline float Q_circ_clipf(float a, float b, float c)
{
return b > c ? Q_clipf(a, c, b) : Q_clipf(a, b, c);
}

static inline int8_t Q_clip_int8(int a)
{
return ((a + 0x80U) & ~0xFF) ? (a >> 31) ^ 0x7F : a;
}

static inline int16_t Q_clip_int16(int a)
{
return ((a + 0x8000U) & ~0xFFFF) ? (a >> 31) ^ 0x7FFF : a;
}

static inline int32_t Q_clip_int32(int64_t a)
{
return ((a + 0x80000000ULL) & ~0xFFFFFFFFULL) ? (a >> 63) ^ 0x7FFFFFFF : a;
}

#ifdef _LP64
#define Q_clipl_int32(a) Q_clip_int32(a)
#else
#define Q_clipl_int32(a) (a)
#endif

static inline uint8_t Q_clip_uint8(int a)
{
return (a & ~0xFF) ? ~a >> 31 : a;
}

static inline uint16_t Q_clip_uint16(int a)
{
return (a & ~0xFFFF) ? ~a >> 31 : a;
}

#ifndef max
Expand Down Expand Up @@ -484,6 +537,14 @@ char *Q_strchrnul(const char *s, int c);
void *Q_memccpy(void *dst, const void *src, int c, size_t size);
size_t Q_strnlen(const char *s, size_t maxlen);

#ifdef _WIN32
#define Q_atoi(s) atoi(s)
#else
int Q_atoi(const char *s);
#endif

#define Q_atof(s) strtof(s, NULL)

char *COM_SkipPath(const char *pathname);
size_t COM_StripExtension(char *out, const char *in, size_t size);
void COM_FilePath(const char *in, char *out, size_t size);
Expand Down
42 changes: 21 additions & 21 deletions src/client/ascii.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,39 +111,39 @@ static void TH_DrawLayoutString(char *dst, const char *s)
if (token[0] == 'x') {
if (token[1] == 'l') {
token = COM_Parse(&s);
x = atoi(token) / 8;
x = Q_atoi(token) / 8;
continue;
}

if (token[1] == 'r') {
token = COM_Parse(&s);
x = TH_WIDTH + atoi(token) / 8;
x = TH_WIDTH + Q_atoi(token) / 8;
continue;
}

if (token[1] == 'v') {
token = COM_Parse(&s);
x = TH_WIDTH / 2 - 20 + atoi(token) / 8;
x = TH_WIDTH / 2 - 20 + Q_atoi(token) / 8;
continue;
}
}

if (token[0] == 'y') {
if (token[1] == 't') {
token = COM_Parse(&s);
y = atoi(token) / 8;
y = Q_atoi(token) / 8;
continue;
}

if (token[1] == 'b') {
token = COM_Parse(&s);
y = TH_HEIGHT + atoi(token) / 8;
y = TH_HEIGHT + Q_atoi(token) / 8;
continue;
}

if (token[1] == 'v') {
token = COM_Parse(&s);
y = TH_HEIGHT / 2 - 15 + atoi(token) / 8;
y = TH_HEIGHT / 2 - 15 + Q_atoi(token) / 8;
continue;
}
}
Expand All @@ -160,25 +160,25 @@ static void TH_DrawLayoutString(char *dst, const char *s)
int score, ping, time;

token = COM_Parse(&s);
x = TH_WIDTH / 2 - 20 + atoi(token) / 8;
x = TH_WIDTH / 2 - 20 + Q_atoi(token) / 8;
token = COM_Parse(&s);
y = TH_HEIGHT / 2 - 15 + atoi(token) / 8;
y = TH_HEIGHT / 2 - 15 + Q_atoi(token) / 8;

token = COM_Parse(&s);
value = atoi(token);
value = Q_atoi(token);
if (value < 0 || value >= MAX_CLIENTS) {
Com_Error(ERR_DROP, "%s: invalid client index", __func__);
}
ci = &cl.clientinfo[value];

token = COM_Parse(&s);
score = atoi(token);
score = Q_atoi(token);

token = COM_Parse(&s);
ping = atoi(token);
ping = Q_atoi(token);

token = COM_Parse(&s);
time = atoi(token);
time = Q_atoi(token);

len = strlen(ci->name);
TH_DrawString(dst, x + 4, y, ci->name, len);
Expand All @@ -196,22 +196,22 @@ static void TH_DrawLayoutString(char *dst, const char *s)
int score, ping;

token = COM_Parse(&s);
x = TH_WIDTH / 2 - 20 + atoi(token) / 8;
x = TH_WIDTH / 2 - 20 + Q_atoi(token) / 8;
token = COM_Parse(&s);
y = TH_HEIGHT / 2 - 15 + atoi(token) / 8;
y = TH_HEIGHT / 2 - 15 + Q_atoi(token) / 8;

token = COM_Parse(&s);
value = atoi(token);
value = Q_atoi(token);
if (value < 0 || value >= MAX_CLIENTS) {
Com_Error(ERR_DROP, "%s: invalid client index", __func__);
}
ci = &cl.clientinfo[value];

token = COM_Parse(&s);
score = atoi(token);
score = Q_atoi(token);

token = COM_Parse(&s);
ping = atoi(token);
ping = Q_atoi(token);
if (ping > 999)
ping = 999;

Expand All @@ -230,9 +230,9 @@ static void TH_DrawLayoutString(char *dst, const char *s)
if (!strcmp(token, "num")) {
// draw a number
token = COM_Parse(&s);
width = atoi(token);
width = Q_atoi(token);
token = COM_Parse(&s);
value = atoi(token);
value = Q_atoi(token);
if (value < 0 || value >= MAX_STATS) {
Com_Error(ERR_DROP, "%s: invalid stat index", __func__);
}
Expand All @@ -243,7 +243,7 @@ static void TH_DrawLayoutString(char *dst, const char *s)

if (!strcmp(token, "stat_string")) {
token = COM_Parse(&s);
index = atoi(token);
index = Q_atoi(token);
if (index < 0 || index >= MAX_STATS) {
Com_Error(ERR_DROP, "%s: invalid string index", __func__);
}
Expand Down Expand Up @@ -272,7 +272,7 @@ static void TH_DrawLayoutString(char *dst, const char *s)

if (!strcmp(token, "if")) {
token = COM_Parse(&s);
value = atoi(token);
value = Q_atoi(token);
if (value < 0 || value >= MAX_STATS) {
Com_Error(ERR_DROP, "%s: invalid stat index", __func__);
}
Expand Down
6 changes: 1 addition & 5 deletions src/client/console.c
Original file line number Diff line number Diff line change
Expand Up @@ -366,16 +366,12 @@ If the line width has changed, reformat the buffer.
*/
void Con_CheckResize(void)
{
int width;

con.scale = R_ClampScale(con_scale);

con.vidWidth = Q_rint(r_config.width * con.scale);
con.vidHeight = Q_rint(r_config.height * con.scale);

width = con.vidWidth / CHAR_WIDTH - 2;

con.linewidth = clamp(width, 0, CON_LINEWIDTH);
con.linewidth = Q_clip(con.vidWidth / CHAR_WIDTH - 2, 0, CON_LINEWIDTH);
con.prompt.inputLine.visibleChars = con.linewidth;
con.prompt.widthInChars = con.linewidth;
con.chatPrompt.inputLine.visibleChars = con.linewidth;
Expand Down
2 changes: 1 addition & 1 deletion src/client/demo.c
Original file line number Diff line number Diff line change
Expand Up @@ -981,7 +981,7 @@ static void CL_Seek_f(void)
return;
}

clamp(percent, 0, 100);
percent = Q_clipf(percent, 0, 100);
dest = cls.demo.file_offset + cls.demo.file_size * percent / 100;

byte_seek = true;
Expand Down
3 changes: 1 addition & 2 deletions src/client/entities.c
Original file line number Diff line number Diff line change
Expand Up @@ -1379,8 +1379,7 @@ void CL_AddTestModel(void)
VectorCopy(cl_testmodel_position, entity.origin);
VectorCopy(cl_testmodel_position, entity.oldorigin);

entity.alpha = cl_testalpha->value;
clamp(entity.alpha, 0.f, 1.f);
entity.alpha = Q_clipf(cl_testalpha->value, 0.f, 1.f);
if (entity.alpha < 1.f)
entity.flags |= RF_TRANSLUCENT;

Expand Down
25 changes: 11 additions & 14 deletions src/client/input.c
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ static void KeyDown(kbutton_t *b)

c = Cmd_Argv(1);
if (c[0])
k = atoi(c);
k = Q_atoi(c);
else
k = -1; // typed manually at the console for continuous down

Expand All @@ -270,7 +270,7 @@ static void KeyDown(kbutton_t *b)

// save timestamp
c = Cmd_Argv(2);
b->downtime = atoi(c);
b->downtime = Q_atoi(c);
if (!b->downtime) {
b->downtime = com_eventTime - 100;
}
Expand All @@ -286,7 +286,7 @@ static void KeyUp(kbutton_t *b)

c = Cmd_Argv(1);
if (c[0])
k = atoi(c);
k = Q_atoi(c);
else {
// typed manually at the console, assume for unsticking, so clear all
b->down[0] = b->down[1] = 0;
Expand All @@ -308,7 +308,7 @@ static void KeyUp(kbutton_t *b)

// save timestamp
c = Cmd_Argv(2);
uptime = atoi(c);
uptime = Q_atoi(c);
if (!uptime) {
b->msec += 10;
} else if (uptime > b->downtime) {
Expand Down Expand Up @@ -384,7 +384,7 @@ static void IN_UseUp(void)

static void IN_Impulse(void)
{
in_impulse = atoi(Cmd_Argv(1));
in_impulse = Q_atoi(Cmd_Argv(1));
}

static void IN_CenterView(void)
Expand Down Expand Up @@ -415,7 +415,6 @@ Returns the fraction of the frame that the key was down
static float CL_KeyState(kbutton_t *key)
{
unsigned msec = key->msec;
float val;

if (key->state & 1) {
// still down
Expand All @@ -429,9 +428,7 @@ static float CL_KeyState(kbutton_t *key)
return (float)(key->state & 1);
}

val = (float)msec / cl.cmd.msec;

return clamp(val, 0, 1);
return Q_clipf((float)msec / cl.cmd.msec, 0, 1);
}

//==========================================================================
Expand Down Expand Up @@ -565,11 +562,11 @@ static void CL_BaseMove(vec3_t move)

static void CL_ClampSpeed(vec3_t move)
{
float speed = 400; // default (maximum) running speed
const float speed = 400; // default (maximum) running speed

clamp(move[0], -speed, speed);
clamp(move[1], -speed, speed);
clamp(move[2], -speed, speed);
move[0] = Q_clipf(move[0], -speed, speed);
move[1] = Q_clipf(move[1], -speed, speed);
move[2] = Q_clipf(move[2], -speed, speed);
}

static void CL_ClampPitch(void)
Expand All @@ -584,7 +581,7 @@ static void CL_ClampPitch(void)
if (angle > 180)
angle -= 360; // wrapped

clamp(angle, -89, 89);
angle = Q_clipf(angle, -89, 89);
cl.viewangles[PITCH] = angle - pitch;
}

Expand Down
Loading