replace a bunch of clamp/min/max with std

master
Daniel Kolesa 2020-07-30 05:26:57 +02:00
parent a2081e6672
commit 6ea2cdb306
28 changed files with 324 additions and 281 deletions

View File

@ -1,5 +1,7 @@
#include "aa.hh"
#include <algorithm>
#include <shared/glemu.hh>
#include <shared/command.hh>
@ -84,7 +86,7 @@ VAR(debugtqaa, 0, 0, 2);
static void viewtqaa()
{
int w = min(hudw, hudh)*1.0f, h = (w*hudh)/hudw, tw = gw, th = gh;
int w = std::min(hudw, hudh)*1.0f, h = (w*hudh)/hudw, tw = gw, th = gh;
SETSHADER(hudrect);
gle::colorf(1, 1, 1);
switch(debugtqaa)
@ -330,7 +332,7 @@ static inline vec2 areaortho(float p1x, float p1y, float p2x, float p2y, float l
static inline void smootharea(float d, vec2 &a1, vec2 &a2)
{
vec2 b1(sqrtf(a1.x*2)*0.5f, sqrtf(a1.y*2)*0.5f), b2(sqrtf(a2.x*2)*0.5f, sqrtf(a2.y*2)*0.5f);
float p = clamp(d / 32.0f, 0.0f, 1.0f);
float p = std::clamp(d / 32.0f, 0.0f, 1.0f);
a1.lerp(b1, a1, p);
a2.lerp(b2, a2, p);
}
@ -387,9 +389,9 @@ static float areaunderdiag(const vec2 &p1, const vec2 &p2, const vec2 &p)
if(!d.x)
{
if(!d.y) return 1;
return clamp(d.y > 0 ? 1 - dp/d.y : dp/d.y, 0.0f, 1.0f);
return std::clamp(d.y > 0 ? 1 - dp/d.y : dp/d.y, 0.0f, 1.0f);
}
if(!d.y) return clamp(d.x > 0 ? 1 - dp/d.x : dp/d.x, 0.0f, 1.0f);
if(!d.y) return std::clamp(d.x > 0 ? 1 - dp/d.x : dp/d.x, 0.0f, 1.0f);
float l = dp/d.y, r = (dp-d.x)/d.y, b = dp/d.x, t = (dp-d.y)/d.x;
if(0 <= dp)
{
@ -400,25 +402,25 @@ static float areaunderdiag(const vec2 &p1, const vec2 &p2, const vec2 &p)
if(d.y+d.x <= dp) return 0;
return 0.5f*(1-r)*(1-t);
}
if(d.y+d.x > dp) return min(1-b, 1-t) + 0.5f*fabs(b-t);
if(d.y+d.x > dp) return std::min(1-b, 1-t) + 0.5f*fabs(b-t);
return 0.5f*(1-b)*r;
}
if(d.x <= dp)
{
if(d.y+d.x <= dp) return 0.5f*(1-l)*t;
return min(1-l, 1-r) + 0.5f*fabs(r-l);
return std::min(1-l, 1-r) + 0.5f*fabs(r-l);
}
return 1 - 0.5f*l*b;
}
if(d.y <= dp)
{
if(d.x <= dp) return 0.5f*l*b;
if(d.y+d.x <= dp) return min(l, r) + 0.5f*fabs(r-l);
if(d.y+d.x <= dp) return std::min(l, r) + 0.5f*fabs(r-l);
return 1 - 0.5f*(1-l)*t;
}
if(d.x <= dp)
{
if(d.y+d.x <= dp) return min(b, t) + 0.5f*fabs(b-t);
if(d.y+d.x <= dp) return std::min(b, t) + 0.5f*fabs(b-t);
return 1 - 0.5f*(1-b)*r;
}
if(d.y+d.x <= dp) return 1 - 0.5f*(1-t)*(1-r);
@ -570,7 +572,7 @@ static void setupsmaa(int w, int h)
static void viewsmaa()
{
int w = min(hudw, hudh)*1.0f, h = (w*hudh)/hudw, tw = gw, th = gh;
int w = std::min(hudw, hudh)*1.0f, h = (w*hudh)/hudw, tw = gw, th = gh;
SETSHADER(hudrect);
gle::colorf(1, 1, 1);
switch(debugsmaa)

View File

@ -42,8 +42,8 @@ struct animmodel : model
}
else
{
fr1 = min(fr1, info.range-1)+info.frame;
fr2 = min(fr1+1, info.frame+info.range-1);
fr1 = std::min(fr1, info.range-1)+info.frame;
fr2 = std::min(fr1+1, info.frame+info.range-1);
}
if(info.anim&ANIM_REVERSE)
{
@ -547,7 +547,7 @@ struct animmodel : model
virtual int totalframes() const { return 1; }
bool hasframe(int i) const { return i>=0 && i<totalframes(); }
bool hasframes(int i, int n) const { return i>=0 && i+n<=totalframes(); }
int clipframes(int i, int n) const { return min(n, totalframes() - i); }
int clipframes(int i, int n) const { return std::min(n, totalframes() - i); }
virtual void cleanup() {}
virtual void preload(part *p) {}
@ -837,7 +837,7 @@ struct animmodel : model
if(d && interp>=0)
{
animinterpinfo &ai = d->animinterp[interp];
if((info.anim&(ANIM_LOOP|ANIM_CLAMP))==ANIM_CLAMP) aitime = min(aitime, int(info.range*info.speed*0.5e-3f));
if((info.anim&(ANIM_LOOP|ANIM_CLAMP))==ANIM_CLAMP) aitime = std::min(aitime, int(info.range*info.speed*0.5e-3f));
void *ak = meshes->animkey();
if(d->ragdoll && d->ragdoll->millis != lastmillis)
{
@ -894,7 +894,7 @@ struct animmodel : model
vec oaxis, oforward, oo, oray;
matrixstack[matrixpos].transposedtransformnormal(axis, oaxis);
float pitchamount = pitchscale*pitch + pitchoffset;
if((pitchmin || pitchmax) && pitchmin <= pitchmax) pitchamount = clamp(pitchamount, pitchmin, pitchmax);
if((pitchmin || pitchmax) && pitchmin <= pitchmax) pitchamount = std::clamp(pitchamount, pitchmin, pitchmax);
if(as->cur.anim&ANIM_NOPITCH || (as->interp < 1 && as->prev.anim&ANIM_NOPITCH))
pitchamount *= (as->cur.anim&ANIM_NOPITCH ? 0 : as->interp) + (as->interp < 1 && as->prev.anim&ANIM_NOPITCH ? 0 : 1-as->interp);
if(pitchamount)
@ -980,7 +980,7 @@ struct animmodel : model
vec oaxis, oforward;
matrixstack[matrixpos].transposedtransformnormal(axis, oaxis);
float pitchamount = pitchscale*pitch + pitchoffset;
if(pitchmin || pitchmax) pitchamount = clamp(pitchamount, pitchmin, pitchmax);
if(pitchmin || pitchmax) pitchamount = std::clamp(pitchamount, pitchmin, pitchmax);
if(as->cur.anim&ANIM_NOPITCH || (as->interp < 1 && as->prev.anim&ANIM_NOPITCH))
pitchamount *= (as->cur.anim&ANIM_NOPITCH ? 0 : as->interp) + (as->interp < 1 && as->prev.anim&ANIM_NOPITCH ? 0 : 1-as->interp);
if(pitchamount)
@ -1786,7 +1786,7 @@ template<class MDL, class MESH> struct modelcommands
static void setgloss(char *meshname, int *gloss)
{
loopskins(meshname, s, s.gloss = clamp(*gloss, 0, 2));
loopskins(meshname, s, s.gloss = std::clamp(*gloss, 0, 2));
}
static void setglow(char *meshname, float *percent, float *delta, float *pulse)
@ -1798,7 +1798,7 @@ template<class MDL, class MESH> struct modelcommands
static void setalphatest(char *meshname, float *cutoff)
{
loopskins(meshname, s, s.alphatest = max(0.0f, min(1.0f, *cutoff)));
loopskins(meshname, s, s.alphatest = std::max(0.0f, std::min(1.0f, *cutoff)));
}
static void setcullface(char *meshname, int *cullface)

View File

@ -1,3 +1,5 @@
#include <algorithm>
#include "ents.hh"
#include "physics.hh"
#include "rendermodel.hh" // loadmapmodel
@ -36,8 +38,8 @@ bool BIH::triintersect(const mesh &m, int tidx, const vec &mo, const vec &mray,
{
vec2 at = m.gettc(t.vert[0]), bt = m.gettc(t.vert[1]).sub(at).mul(v*invdet), ct = m.gettc(t.vert[2]).sub(at).mul(w*invdet);
at.add(bt).add(ct);
int si = clamp(int(m.tex->xs * at.x), 0, m.tex->xs-1),
ti = clamp(int(m.tex->ys * at.y), 0, m.tex->ys-1);
int si = std::clamp(int(m.tex->xs * at.x), 0, m.tex->xs-1),
ti = std::clamp(int(m.tex->ys * at.y), 0, m.tex->ys-1);
if(!(m.tex->alphamask[ti*((m.tex->xs+7)/8) + si/8] & (1<<(si%8)))) return false;
}
if(!(mode&RAY_SHADOW)) hitsurface = m.xformnorm.transform(n).normalize();
@ -71,7 +73,7 @@ inline bool BIH::traverse(const mesh &m, const vec &o, const vec &ray, const vec
if(!curnode->isleaf(faridx))
{
curnode += curnode->childindex(faridx);
tmin = max(tmin, farsplit);
tmin = std::max(tmin, farsplit);
continue;
}
else if(triintersect(m, curnode->childindex(faridx), mo, mray, maxdist, dist, mode)) return true;
@ -85,7 +87,7 @@ inline bool BIH::traverse(const mesh &m, const vec &o, const vec &ray, const vec
if(!curnode->isleaf(faridx))
{
curnode += curnode->childindex(faridx);
tmin = max(tmin, farsplit);
tmin = std::max(tmin, farsplit);
continue;
}
else if(triintersect(m, curnode->childindex(faridx), mo, mray, maxdist, dist, mode)) return true;
@ -101,21 +103,21 @@ inline bool BIH::traverse(const mesh &m, const vec &o, const vec &ray, const vec
{
traversestate &save = stack[stacksize++];
save.node = curnode + curnode->childindex(faridx);
save.tmin = max(tmin, farsplit);
save.tmin = std::max(tmin, farsplit);
save.tmax = tmax;
}
else
{
if(traverse(m, o, ray, invray, maxdist, dist, mode, curnode + curnode->childindex(nearidx), tmin, min(tmax, nearsplit))) return true;
if(traverse(m, o, ray, invray, maxdist, dist, mode, curnode + curnode->childindex(nearidx), tmin, std::min(tmax, nearsplit))) return true;
curnode += curnode->childindex(faridx);
tmin = max(tmin, farsplit);
tmin = std::max(tmin, farsplit);
continue;
}
}
else if(triintersect(m, curnode->childindex(faridx), mo, mray, maxdist, dist, mode)) return true;
}
curnode += curnode->childindex(nearidx);
tmax = min(tmax, nearsplit);
tmax = std::min(tmax, nearsplit);
continue;
}
if(stacksize <= 0) return false;
@ -139,11 +141,11 @@ inline bool BIH::traverse(const vec &o, const vec &ray, float maxdist, float &di
if(invray.x > 0) { tmin = t1; tmax = t2; } else { tmin = t2; tmax = t1; }
t1 = (m.bbmin.y - o.y)*invray.y;
t2 = (m.bbmax.y - o.y)*invray.y;
if(invray.y > 0) { tmin = max(tmin, t1); tmax = min(tmax, t2); } else { tmin = max(tmin, t2); tmax = min(tmax, t1); }
if(invray.y > 0) { tmin = std::max(tmin, t1); tmax = std::min(tmax, t2); } else { tmin = std::max(tmin, t2); tmax = std::min(tmax, t1); }
t1 = (m.bbmin.z - o.z)*invray.z;
t2 = (m.bbmax.z - o.z)*invray.z;
if(invray.z > 0) { tmin = max(tmin, t1); tmax = min(tmax, t2); } else { tmin = max(tmin, t2); tmax = min(tmax, t1); }
tmax = min(tmax, maxdist);
if(invray.z > 0) { tmin = std::max(tmin, t1); tmax = std::min(tmax, t2); } else { tmin = std::max(tmin, t2); tmax = std::min(tmax, t1); }
tmax = std::min(tmax, maxdist);
if(tmin < tmax && traverse(m, o, ray, invray, maxdist, dist, mode, m.nodes, tmin, tmax)) return true;
}
return false;
@ -168,10 +170,10 @@ void BIH::build(mesh &m, ushort *indices, int numindices, const ivec &vmin, cons
ivec trimin = ivec(tri.center).sub(ivec(tri.radius)),
trimax = ivec(tri.center).add(ivec(tri.radius));
int amin = trimin[axis], amax = trimax[axis];
if(max(split - amin, 0) > max(amax - split, 0))
if(std::max(split - amin, 0) > std::max(amax - split, 0))
{
++left;
splitleft = max(splitleft, amax);
splitleft = std::max(splitleft, amax);
leftmin.min(trimin);
leftmax.max(trimax);
}
@ -179,7 +181,7 @@ void BIH::build(mesh &m, ushort *indices, int numindices, const ivec &vmin, cons
{
--right;
swap(indices[left], indices[right]);
splitright = min(splitright, amin);
splitright = std::min(splitright, amin);
rightmin.min(trimin);
rightmax.max(trimax);
}
@ -202,13 +204,13 @@ void BIH::build(mesh &m, ushort *indices, int numindices, const ivec &vmin, cons
trimax = ivec(tri.center).add(ivec(tri.radius));
if(i < left)
{
splitleft = max(splitleft, trimax[axis]);
splitleft = std::max(splitleft, trimax[axis]);
leftmin.min(trimin);
leftmax.max(trimax);
}
else
{
splitright = min(splitright, trimin[axis]);
splitright = std::min(splitright, trimin[axis]);
rightmin.min(trimin);
rightmax.max(trimax);
}
@ -366,7 +368,7 @@ static inline float segmentdistance(const vec &d1, const vec &d2, const vec &r)
{
if(e <= 1e-4f) return r.squaredlen();
s = 0;
t = clamp(-f / e, 0.0f, 1.0f);
t = std::clamp(-f / e, 0.0f, 1.0f);
}
else
{
@ -374,22 +376,22 @@ static inline float segmentdistance(const vec &d1, const vec &d2, const vec &r)
if(e <= 1e-4f)
{
t = 0;
s = clamp(c / a, 0.0f, 1.0f);
s = std::clamp(c / a, 0.0f, 1.0f);
}
else
{
float b = d1.dot(d2), denom = a*e - b*b;
s = denom ? clamp((c*e - b*f) / denom, 0.0f, 1.0f) : 0.0f;
s = denom ? std::clamp((c*e - b*f) / denom, 0.0f, 1.0f) : 0.0f;
t = b*s - f;
if(t < 0)
{
t = 0;
s = clamp(c / a, 0.0f, 1.0f);
s = std::clamp(c / a, 0.0f, 1.0f);
}
else if(t > e)
{
t = 1;
s = clamp((b + c) / a, 0.0f, 1.0f);
s = std::clamp((b + c) / a, 0.0f, 1.0f);
}
else t /= e;
}
@ -413,34 +415,34 @@ static inline float trisegmentdistance(const vec &a, const vec &b, const vec &c,
if(ap.dot(nab) < 0) // P outside AB
{
dist = segmentdistance(ab, pq, ap);
if(bq.dot(nbc) < 0) dist = min(dist, segmentdistance(bc, pq, bp)); // Q outside BC
else if(aq.dot(nca) < 0) dist = min(dist, segmentdistance(pq, ca, cp)); // Q outside CA
else if(aq.dot(nab) >= 0) dist = min(dist, dq*dq/n.squaredlen()); // Q inside AB
if(bq.dot(nbc) < 0) dist = std::min(dist, segmentdistance(bc, pq, bp)); // Q outside BC
else if(aq.dot(nca) < 0) dist = std::min(dist, segmentdistance(pq, ca, cp)); // Q outside CA
else if(aq.dot(nab) >= 0) dist = std::min(dist, dq*dq/n.squaredlen()); // Q inside AB
else return dist;
}
else if(bp.dot(nbc) < 0) // P outside BC
{
dist = segmentdistance(bc, pq, bp);
if(aq.dot(nca) < 0) dist = min(dist, segmentdistance(ca, pq, cp)); // Q outside CA
else if(aq.dot(nab) < 0) dist = min(dist, segmentdistance(ab, pq, ap)); // Q outside AB
else if(bq.dot(nbc) >= 0) dist = min(dist, dq*dq/n.squaredlen()); // Q inside BC
if(aq.dot(nca) < 0) dist = std::min(dist, segmentdistance(ca, pq, cp)); // Q outside CA
else if(aq.dot(nab) < 0) dist = std::min(dist, segmentdistance(ab, pq, ap)); // Q outside AB
else if(bq.dot(nbc) >= 0) dist = std::min(dist, dq*dq/n.squaredlen()); // Q inside BC
else return dist;
}
else if(cp.dot(nca) < 0) // P outside CA
{
dist = segmentdistance(ca, pq, cp);
if(aq.dot(nab) < 0) dist = min(dist, segmentdistance(ab, pq, ap)); // Q outside AB
else if(bq.dot(nbc) < 0) dist = min(dist, segmentdistance(bc, pq, bp)); // Q outside BC
else if(aq.dot(nca) >= 0) dist = min(dist, dq*dq/n.squaredlen()); // Q inside CA
if(aq.dot(nab) < 0) dist = std::min(dist, segmentdistance(ab, pq, ap)); // Q outside AB
else if(bq.dot(nbc) < 0) dist = std::min(dist, segmentdistance(bc, pq, bp)); // Q outside BC
else if(aq.dot(nca) >= 0) dist = std::min(dist, dq*dq/n.squaredlen()); // Q inside CA
else return dist;
}
else if(aq.dot(nab) < 0) dist = min(segmentdistance(ab, pq, ap), dp); // Q outside AB
else if(bq.dot(nbc) < 0) dist = min(segmentdistance(bc, pq, bp), dp); // Q outside BC
else if(aq.dot(nca) < 0) dist = min(segmentdistance(ca, pq, cp), dp); // Q outside CA
else if(aq.dot(nab) < 0) dist = std::min(segmentdistance(ab, pq, ap), dp); // Q outside AB
else if(bq.dot(nbc) < 0) dist = std::min(segmentdistance(bc, pq, bp), dp); // Q outside BC
else if(aq.dot(nca) < 0) dist = std::min(segmentdistance(ca, pq, cp), dp); // Q outside CA
else // both P and Q inside
{
if(dp > 0 ? dq <= 0 : dq >= 0) return 0; // P and Q on different sides of triangle
dist = min(dp*dp, dq*dq)/n.squaredlen();
dist = std::min(dp*dp, dq*dq)/n.squaredlen();
return dist;
}
if(dp > 0 ? dq >= 0 : dq <= 0) return dist; // both P and Q on same side of triangle
@ -480,10 +482,10 @@ static inline bool triboxoverlap(const vec &radius, const vec &a, const vec &b,
if(a.w < b.w) \
{ \
if(b.w < c.w) { if(c.w < -radius.w || a.w > radius.w) return false; } \
else if(b.w < -radius.w || min(a.w, c.w) > radius.w) return false; \
else if(b.w < -radius.w || std::min(a.w, c.w) > radius.w) return false; \
} \
else if(a.w < c.w) { if(c.w < -radius.w || b.w > radius.w) return false; } \
else if(a.w < -radius.w || min(b.w, c.w) > radius.w) return false; \
else if(a.w < -radius.w || std::min(b.w, c.w) > radius.w) return false; \
}
TESTFACE(x);
@ -551,7 +553,7 @@ inline void BIH::tricollide<COLLIDE_OBB>(const mesh &m, int tidx, physent *d, co
if(d->type==ENT_PLAYER &&
pdist < (dir.z*n.z < 0 ?
2*radius.z*(d->zmargin/(d->aboveeye+d->eyeheight)-(dir.z < 0 ? 1/3.0f : 1/4.0f)) :
(dir.x*n.x < 0 || dir.y*n.y < 0 ? -max(radius.x, radius.y) : 0)))
(dir.x*n.x < 0 || dir.y*n.y < 0 ? -std::max(radius.x, radius.y) : 0)))
return;
}

View File

@ -1,5 +1,7 @@
#include "blend.hh"
#include <algorithm>
#include <shared/command.hh>
#include <shared/igame.hh>
@ -150,9 +152,9 @@ bool setblendmaporigin(BlendMapCache *cache, const ivec &o, int size)
BlendMapBranch *bm = blendmap.branch;
int bmscale = worldscale-BM_SCALE, bmsize = 1<<bmscale,
x = o.x>>BM_SCALE, y = o.y>>BM_SCALE,
x1 = max(x-1, 0), y1 = max(y-1, 0),
x2 = min(((o.x + size + (1<<BM_SCALE)-1)>>BM_SCALE) + 1, bmsize),
y2 = min(((o.y + size + (1<<BM_SCALE)-1)>>BM_SCALE) + 1, bmsize),
x1 = std::max(x-1, 0), y1 = std::max(y-1, 0),
x2 = std::min(((o.x + size + (1<<BM_SCALE)-1)>>BM_SCALE) + 1, bmsize),
y2 = std::min(((o.y + size + (1<<BM_SCALE)-1)>>BM_SCALE) + 1, bmsize),
diff = (x1^x2)|(y1^y2);
if(diff < bmsize) while(!(diff&(1<<(bmscale-1))))
{
@ -205,7 +207,7 @@ uchar lookupblendmap(BlendMapCache *cache, const vec &pos)
rx = ix-cache->origin.x, ry = iy-cache->origin.y;
loop(vy, 2) loop(vx, 2)
{
int cx = clamp(rx+vx, 0, (1<<cache->scale)-1), cy = clamp(ry+vy, 0, (1<<cache->scale)-1);
int cx = std::clamp(rx+vx, 0, (1<<cache->scale)-1), cy = std::clamp(ry+vy, 0, (1<<cache->scale)-1);
if(cache->node.type==BM_IMAGE)
*val++ = cache->node.image->data[cy*BM_IMAGE_SIZE + cx];
else *val++ = lookupblendmap(cx, cy, cache->node.branch, cache->scale);
@ -217,7 +219,7 @@ uchar lookupblendmap(BlendMapCache *cache, const vec &pos)
static void fillblendmap(uchar &type, BlendMapNode &node, int size, uchar val, int x1, int y1, int x2, int y2)
{
if(max(x1, y1) <= 0 && min(x2, y2) >= size)
if(std::max(x1, y1) <= 0 && std::min(x2, y2) >= size)
{
node.cleanup(type);
type = BM_SOLID;
@ -231,16 +233,16 @@ static void fillblendmap(uchar &type, BlendMapNode &node, int size, uchar val, i
if(y1 < size)
{
if(x1 < size) fillblendmap(node.branch->type[0], node.branch->children[0], size, val,
x1, y1, min(x2, size), min(y2, size));
x1, y1, std::min(x2, size), std::min(y2, size));
if(x2 > size) fillblendmap(node.branch->type[1], node.branch->children[1], size, val,
max(x1-size, 0), y1, x2-size, min(y2, size));
std::max(x1-size, 0), y1, x2-size, std::min(y2, size));
}
if(y2 > size)
{
if(x1 < size) fillblendmap(node.branch->type[2], node.branch->children[2], size, val,
x1, max(y1-size, 0), min(x2, size), y2-size);
x1, std::max(y1-size, 0), std::min(x2, size), y2-size);
if(x2 > size) fillblendmap(node.branch->type[3], node.branch->children[3], size, val,
max(x1-size, 0), max(y1-size, 0), x2-size, y2-size);
std::max(x1-size, 0), std::max(y1-size, 0), x2-size, y2-size);
}
loopi(4) if(node.branch->type[i]!=BM_SOLID || node.branch->children[i].solid->val!=val) return;
node.cleanup(type);
@ -276,11 +278,11 @@ static void fillblendmap(uchar &type, BlendMapNode &node, int size, uchar val, i
static void fillblendmap(int x, int y, int w, int h, uchar val)
{
int bmsize = worldsize>>BM_SCALE,
x1 = clamp(x, 0, bmsize),
y1 = clamp(y, 0, bmsize),
x2 = clamp(x+w, 0, bmsize),
y2 = clamp(y+h, 0, bmsize);
if(max(x1, y1) >= bmsize || min(x2, y2) <= 0 || x1>=x2 || y1>=y2) return;
x1 = std::clamp(x, 0, bmsize),
y1 = std::clamp(y, 0, bmsize),
x2 = std::clamp(x+w, 0, bmsize),
y2 = std::clamp(y+h, 0, bmsize);
if(std::max(x1, y1) >= bmsize || std::min(x2, y2) <= 0 || x1>=x2 || y1>=y2) return;
fillblendmap(blendmap.type, blendmap, bmsize, val, x1, y1, x2, y2);
}
@ -292,16 +294,16 @@ static void invertblendmap(uchar &type, BlendMapNode &node, int size, int x1, in
if(y1 < size)
{
if(x1 < size) invertblendmap(node.branch->type[0], node.branch->children[0], size,
x1, y1, min(x2, size), min(y2, size));
x1, y1, std::min(x2, size), std::min(y2, size));
if(x2 > size) invertblendmap(node.branch->type[1], node.branch->children[1], size,
max(x1-size, 0), y1, x2-size, min(y2, size));
std::max(x1-size, 0), y1, x2-size, std::min(y2, size));
}
if(y2 > size)
{
if(x1 < size) invertblendmap(node.branch->type[2], node.branch->children[2], size,
x1, max(y1-size, 0), min(x2, size), y2-size);
x1, std::max(y1-size, 0), std::min(x2, size), y2-size);
if(x2 > size) invertblendmap(node.branch->type[3], node.branch->children[3], size,
max(x1-size, 0), max(y1-size, 0), x2-size, y2-size);
std::max(x1-size, 0), std::max(y1-size, 0), x2-size, y2-size);
}
return;
}
@ -323,11 +325,11 @@ static void invertblendmap(uchar &type, BlendMapNode &node, int size, int x1, in
static void invertblendmap(int x, int y, int w, int h)
{
int bmsize = worldsize>>BM_SCALE,
x1 = clamp(x, 0, bmsize),
y1 = clamp(y, 0, bmsize),
x2 = clamp(x+w, 0, bmsize),
y2 = clamp(y+h, 0, bmsize);
if(max(x1, y1) >= bmsize || min(x2, y2) <= 0 || x1>=x2 || y1>=y2) return;
x1 = std::clamp(x, 0, bmsize),
y1 = std::clamp(y, 0, bmsize),
x2 = std::clamp(x+w, 0, bmsize),
y2 = std::clamp(y+h, 0, bmsize);
if(std::max(x1, y1) >= bmsize || std::min(x2, y2) <= 0 || x1>=x2 || y1>=y2) return;
invertblendmap(blendmap.type, blendmap, bmsize, x1, y1, x2, y2);
}
@ -405,10 +407,10 @@ static void blitblendmap(uchar &type, BlendMapNode &node, int bmx, int bmy, int
memset(node.image->data, val, sizeof(node.image->data));
}
int x1 = clamp(sx - bmx, 0, bmsize), y1 = clamp(sy - bmy, 0, bmsize),
x2 = clamp(sx+sw - bmx, 0, bmsize), y2 = clamp(sy+sh - bmy, 0, bmsize);
int x1 = std::clamp(sx - bmx, 0, bmsize), y1 = std::clamp(sy - bmy, 0, bmsize),
x2 = std::clamp(sx+sw - bmx, 0, bmsize), y2 = std::clamp(sy+sh - bmy, 0, bmsize);
uchar *dst = &node.image->data[y1*BM_IMAGE_SIZE + x1];
src += max(bmy - sy, 0)*sw + max(bmx - sx, 0);
src += std::max(bmy - sy, 0)*sw + std::max(bmx - sx, 0);
loopi(y2-y1)
{
switch(smode)
@ -418,19 +420,19 @@ static void blitblendmap(uchar &type, BlendMapNode &node, int bmx, int bmy, int
break;
case 2:
loopi(x2 - x1) dst[i] = min(dst[i], src[i]);
loopi(x2 - x1) dst[i] = std::min(dst[i], src[i]);
break;
case 3:
loopi(x2 - x1) dst[i] = max(dst[i], src[i]);
loopi(x2 - x1) dst[i] = std::max(dst[i], src[i]);
break;
case 4:
loopi(x2 - x1) dst[i] = min(dst[i], uchar(0xFF - src[i]));
loopi(x2 - x1) dst[i] = std::min(dst[i], uchar(0xFF - src[i]));
break;
case 5:
loopi(x2 - x1) dst[i] = max(dst[i], uchar(0xFF - src[i]));
loopi(x2 - x1) dst[i] = std::max(dst[i], uchar(0xFF - src[i]));
break;
}
dst += BM_IMAGE_SIZE;
@ -441,7 +443,7 @@ static void blitblendmap(uchar &type, BlendMapNode &node, int bmx, int bmy, int
static void blitblendmap(uchar *src, int sx, int sy, int sw, int sh, int smode)
{
int bmsize = worldsize>>BM_SCALE;
if(max(sx, sy) >= bmsize || min(sx+sw, sy+sh) <= 0 || min(sw, sh) <= 0) return;
if(std::max(sx, sy) >= bmsize || std::min(sx+sw, sy+sh) <= 0 || std::min(sw, sh) <= 0) return;
blitblendmap(blendmap.type, blendmap, 0, 0, bmsize, src, sx, sy, sw, sh, smode);
}
@ -511,8 +513,8 @@ static int calcblendlayer(uchar &type, BlendMapNode &node, int bmx, int bmy, int
if(type == BM_SOLID) val = node.solid->val;
else
{
int x1 = clamp(cx - bmx, 0, bmsize), y1 = clamp(cy - bmy, 0, bmsize),
x2 = clamp(cx+cw - bmx, 0, bmsize), y2 = clamp(cy+ch - bmy, 0, bmsize);
int x1 = std::clamp(cx - bmx, 0, bmsize), y1 = std::clamp(cy - bmy, 0, bmsize),
x2 = std::clamp(cx+cw - bmx, 0, bmsize), y2 = std::clamp(cy+ch - bmy, 0, bmsize);
uchar *src = &node.image->data[y1*BM_IMAGE_SIZE + x1];
val = src[0];
loopi(y2-y1)
@ -532,10 +534,10 @@ static int calcblendlayer(uchar &type, BlendMapNode &node, int bmx, int bmy, int
int calcblendlayer(int x1, int y1, int x2, int y2)
{
int bmsize = worldsize>>BM_SCALE,
ux1 = max(x1, 0) >> BM_SCALE,
ux2 = (min(x2, worldsize) + (1<<BM_SCALE)-1) >> BM_SCALE,
uy1 = max(y1, 0) >> BM_SCALE,
uy2 = (min(y2, worldsize) + (1<<BM_SCALE)-1) >> BM_SCALE;
ux1 = std::max(x1, 0) >> BM_SCALE,
ux2 = (std::min(x2, worldsize) + (1<<BM_SCALE)-1) >> BM_SCALE,
uy1 = std::max(y1, 0) >> BM_SCALE,
uy2 = (std::min(y2, worldsize) + (1<<BM_SCALE)-1) >> BM_SCALE;
if(ux1 >= ux2 || uy1 >= uy2) return LAYER_TOP;
return calcblendlayer(blendmap.type, blendmap, 0, 0, bmsize, ux1, uy1, ux2-ux1, uy2-uy1);
}
@ -659,7 +661,7 @@ struct BlendTexture
bool setup(int sz)
{
if(!tex) glGenTextures(1, &tex);
sz = min(sz, maxtexsize ? min(maxtexsize, hwtexsize) : hwtexsize);
sz = std::min(sz, maxtexsize ? std::min(maxtexsize, hwtexsize) : hwtexsize);
while(sz&(sz-1)) sz &= sz-1;
if(sz == size) return false;
size = sz;
@ -722,11 +724,11 @@ static void renderblendtexture(uchar &type, BlendMapNode &node, int bmx, int bmy
return;
}
int x1 = clamp(dx - bmx, 0, bmsize), y1 = clamp(dy - bmy, 0, bmsize),
x2 = clamp(dx+dw - bmx, 0, bmsize), y2 = clamp(dy+dh - bmy, 0, bmsize),
tsize = 1<<(min(worldscale, 12)-BM_SCALE),
int x1 = std::clamp(dx - bmx, 0, bmsize), y1 = std::clamp(dy - bmy, 0, bmsize),
x2 = std::clamp(dx+dw - bmx, 0, bmsize), y2 = std::clamp(dy+dh - bmy, 0, bmsize),
tsize = 1<<(std::min(worldscale, 12)-BM_SCALE),
step = tsize/dsize, stepw = (x2 - x1)/step, steph = (y2 - y1)/step;
dst += max(bmy - dy, 0)/step*dsize + max(bmx - dx, 0)/step;
dst += std::max(bmy - dy, 0)/step*dsize + std::max(bmx - dx, 0)/step;
if(type == BM_SOLID) loopi(steph)
{
memset(dst, node.solid->val, stepw);
@ -748,7 +750,7 @@ static void renderblendtexture(uchar &type, BlendMapNode &node, int bmx, int bmy
void renderblendtexture(uchar *dst, int dsize, int dx, int dy, int dw, int dh)
{
int bmsize = worldsize>>BM_SCALE;
if(max(dx, dy) >= bmsize || min(dx+dw, dy+dh) <= 0 || min(dw, dh) <= 0) return;
if(std::max(dx, dy) >= bmsize || std::min(dx+dw, dy+dh) <= 0 || std::min(dw, dh) <= 0) return;
renderblendtexture(blendmap.type, blendmap, 0, 0, bmsize, dst, dsize, dx, dy, dw, dh);
}
@ -779,10 +781,10 @@ static bool usesblendmap(uchar &type, BlendMapNode &node, int bmx, int bmy, int
bool usesblendmap(int x1, int y1, int x2, int y2)
{
int bmsize = worldsize>>BM_SCALE,
ux1 = max(x1, 0) >> BM_SCALE,
ux2 = (min(x2, worldsize) + (1<<BM_SCALE)-1) >> BM_SCALE,
uy1 = max(y1, 0) >> BM_SCALE,
uy2 = (min(y2, worldsize) + (1<<BM_SCALE)-1) >> BM_SCALE;
ux1 = std::max(x1, 0) >> BM_SCALE,
ux2 = (std::min(x2, worldsize) + (1<<BM_SCALE)-1) >> BM_SCALE,
uy1 = std::max(y1, 0) >> BM_SCALE,
uy2 = (std::min(y2, worldsize) + (1<<BM_SCALE)-1) >> BM_SCALE;
if(ux1 >= ux2 || uy1 >= uy2) return false;
return usesblendmap(blendmap.type, blendmap, 0, 0, bmsize, ux1, uy1, ux2-ux1, uy2-uy1);
}
@ -792,7 +794,7 @@ void bindblendtexture(const ivec &p)
loopv(blendtexs) if(blendtexs[i].contains(p))
{
BlendTexture &bt = blendtexs[i];
int tsize = 1<<min(worldscale, 12);
int tsize = 1<<std::min(worldscale, 12);
GLOBALPARAMF(blendmapparams, bt.x, bt.y, 1.0f/tsize, 1.0f/tsize);
glBindTexture(GL_TEXTURE_2D, bt.tex);
break;
@ -824,10 +826,10 @@ static void updateblendtextures(uchar &type, BlendMapNode &node, int bmx, int bm
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
int tx1 = max(bmx, ux)&~((0x1000>>BM_SCALE)-1),
tx2 = (min(bmx+bmsize, ux+uw) + (0x1000>>BM_SCALE)-1)&~((0x1000>>BM_SCALE)-1),
ty1 = max(bmy, uy)&~((0x1000>>BM_SCALE)-1),
ty2 = (min(bmy+bmsize, uy+uh) + (0x1000>>BM_SCALE)-1)&~((0x1000>>BM_SCALE)-1);
int tx1 = std::max(bmx, ux)&~((0x1000>>BM_SCALE)-1),
tx2 = (std::min(bmx+bmsize, ux+uw) + (0x1000>>BM_SCALE)-1)&~((0x1000>>BM_SCALE)-1),
ty1 = std::max(bmy, uy)&~((0x1000>>BM_SCALE)-1),
ty2 = (std::min(bmy+bmsize, uy+uh) + (0x1000>>BM_SCALE)-1)&~((0x1000>>BM_SCALE)-1);
for(int ty = ty1; ty < ty2; ty += 0x1000>>BM_SCALE) for(int tx = tx1; tx < tx2; tx += 0x1000>>BM_SCALE)
{
BlendTexture *bt = nullptr;
@ -838,16 +840,16 @@ static void updateblendtextures(uchar &type, BlendMapNode &node, int bmx, int bm
bt->x = tx<<BM_SCALE;
bt->y = ty<<BM_SCALE;
}
bt->setup(1<<min(worldscale-BM_SCALE, blendtexsize));
int tsize = 1<<(min(worldscale, 12)-BM_SCALE),
bt->setup(1<<std::min(worldscale-BM_SCALE, blendtexsize));
int tsize = 1<<(std::min(worldscale, 12)-BM_SCALE),
ux1 = tx, ux2 = tx + tsize, uy1 = ty, uy2 = ty + tsize,
step = tsize/bt->size;
if(!bt->valid)
{
ux1 = max(ux1, ux&~(step-1));
ux2 = min(ux2, (ux+uw+step-1)&~(step-1));
uy1 = max(uy1, uy&~(step-1));
uy2 = min(uy2, (uy+uh+step-1)&~(step-1));
ux1 = std::max(ux1, ux&~(step-1));
ux2 = std::min(ux2, (ux+uw+step-1)&~(step-1));
uy1 = std::max(uy1, uy&~(step-1));
uy2 = std::min(uy2, (uy+uh+step-1)&~(step-1));
bt->valid = true;
}
uchar *data = bt->data + (uy1-ty)/step*bt->size + (ux1-tx)/step;
@ -863,10 +865,10 @@ static void updateblendtextures(uchar &type, BlendMapNode &node, int bmx, int bm
void updateblendtextures(int x1, int y1, int x2, int y2)
{
int bmsize = worldsize>>BM_SCALE,
ux1 = max(x1, 0) >> BM_SCALE,
ux2 = (min(x2, worldsize) + (1<<BM_SCALE)-1) >> BM_SCALE,
uy1 = max(y1, 0) >> BM_SCALE,
uy2 = (min(y2, worldsize) + (1<<BM_SCALE)-1) >> BM_SCALE;
ux1 = std::max(x1, 0) >> BM_SCALE,
ux2 = (std::min(x2, worldsize) + (1<<BM_SCALE)-1) >> BM_SCALE,
uy1 = std::max(y1, 0) >> BM_SCALE,
uy2 = (std::min(y2, worldsize) + (1<<BM_SCALE)-1) >> BM_SCALE;
if(ux1 >= ux2 || uy1 >= uy2) return;
updateblendtextures(blendmap.type, blendmap, 0, 0, bmsize, ux1, uy1, ux2-ux1, uy2-uy1);
}
@ -896,7 +898,7 @@ static void delblendbrush(const char *name)
delete brushes[i];
brushes.remove(i--);
}
curbrush = brushes.empty() ? -1 : clamp(curbrush, 0, brushes.length()-1);
curbrush = brushes.empty() ? -1 : std::clamp(curbrush, 0, brushes.length()-1);
}
COMMAND(delblendbrush, "s");
@ -907,7 +909,7 @@ static void addblendbrush(const char *name, const char *imgname)
ImageData s;
if(!loadimage(imgname, s)) { conoutf(CON_ERROR, "could not load blend brush image %s", imgname); return; }
if(max(s.w, s.h) > (1<<12))
if(std::max(s.w, s.h) > (1<<12))
{
conoutf(CON_ERROR, "blend brush image size exceeded %dx%d pixels: %s", 1<<12, 1<<12, imgname);
return;
@ -973,7 +975,7 @@ ICOMMAND(rotateblendbrush, "i", (int *val),
if(!canpaintblendmap()) return;
BlendBrush *brush = brushes[curbrush];
const texrotation &r = texrotations[*val < 0 ? 3 : clamp(*val, 1, 7)];
const texrotation &r = texrotations[*val < 0 ? 3 : std::clamp(*val, 1, 7)];
brush->reorient(r.flipx, r.flipy, r.swapxy);
});
@ -982,8 +984,8 @@ static void paintblendmap(bool msg)
if(!canpaintblendmap(true, false, msg)) return;
BlendBrush *brush = brushes[curbrush];
int x = (int)floor(clamp(worldpos.x, 0.0f, float(worldsize))/(1<<BM_SCALE) - 0.5f*brush->w),
y = (int)floor(clamp(worldpos.y, 0.0f, float(worldsize))/(1<<BM_SCALE) - 0.5f*brush->h);
int x = (int)floor(std::clamp(worldpos.x, 0.0f, float(worldsize))/(1<<BM_SCALE) - 0.5f*brush->w),
y = (int)floor(std::clamp(worldpos.y, 0.0f, float(worldsize))/(1<<BM_SCALE) - 0.5f*brush->h);
blitblendmap(brush->data, x, y, brush->w, brush->h, blendpaintmode);
previewblends(ivec((x-1)<<BM_SCALE, (y-1)<<BM_SCALE, 0),
ivec((x+brush->w+1)<<BM_SCALE, (y+brush->h+1)<<BM_SCALE, worldsize));
@ -1091,12 +1093,12 @@ void renderblendbrush()
if(!blendpaintmode || !brushes.inrange(curbrush)) return;
BlendBrush *brush = brushes[curbrush];
int x1 = (int)floor(clamp(worldpos.x, 0.0f, float(worldsize))/(1<<BM_SCALE) - 0.5f*brush->w) << BM_SCALE,
y1 = (int)floor(clamp(worldpos.y, 0.0f, float(worldsize))/(1<<BM_SCALE) - 0.5f*brush->h) << BM_SCALE,
int x1 = (int)floor(std::clamp(worldpos.x, 0.0f, float(worldsize))/(1<<BM_SCALE) - 0.5f*brush->w) << BM_SCALE,
y1 = (int)floor(std::clamp(worldpos.y, 0.0f, float(worldsize))/(1<<BM_SCALE) - 0.5f*brush->h) << BM_SCALE,
x2 = x1 + (brush->w << BM_SCALE),
y2 = y1 + (brush->h << BM_SCALE);
if(max(x1, y1) >= worldsize || min(x2, y2) <= 0 || x1>=x2 || y1>=y2) return;
if(std::max(x1, y1) >= worldsize || std::min(x2, y2) <= 0 || x1>=x2 || y1>=y2) return;
if(!brush->tex) brush->gentex();
renderblendbrush(brush->tex, x1, y1, x2 - x1, y2 - y1);

View File

@ -6,6 +6,8 @@
#include <cctype>
#include <climits>
#include <algorithm>
#include <sauerlib/encoding.hh>
#include <shared/igame.hh>
@ -653,7 +655,7 @@ void setvar(const char *name, int i, bool dofunc, bool doclamp)
{
GETVAR(id, name, );
OVERRIDEVAR(return, id->overrideval.i = *id->storage.i, , )
if(doclamp) *id->storage.i = clamp(i, id->minval, id->maxval);
if(doclamp) *id->storage.i = std::clamp(i, id->minval, id->maxval);
else *id->storage.i = i;
if(dofunc) id->changed();
}
@ -661,7 +663,7 @@ void setfvar(const char *name, float f, bool dofunc, bool doclamp)
{
_GETVAR(id, ID_FVAR, name, );
OVERRIDEVAR(return, id->overrideval.f = *id->storage.f, , );
if(doclamp) *id->storage.f = clamp(f, id->minvalf, id->maxvalf);
if(doclamp) *id->storage.f = std::clamp(f, id->minvalf, id->maxvalf);
else *id->storage.f = f;
if(dofunc) id->changed();
}
@ -924,7 +926,7 @@ static char *conc(tagval *v, int n, bool space, const char *prefix, int prefixle
default: vlen[i] = 0; break;
}
overflow:
if(space) len += max(prefix ? i : i-1, 0);
if(space) len += std::max(prefix ? i : i-1, 0);
char *buf = newstring(len + numlen);
int offset = 0, numoffset = 0;
if(prefix)
@ -2210,8 +2212,8 @@ static inline void callcommand(ident *id, tagval *args, int numargs, bool lookup
#ifndef STANDALONE
case 'D': if(++i < numargs) freearg(args[i]); addreleaseaction(id, args, i); fakeargs++; break;
#endif
case 'C': { i = max(i+1, numargs); vector<char> buf; ((comfun1)id->fun)(conc(buf, args, i, true)); goto cleanup; }
case 'V': i = max(i+1, numargs); ((comfunv)id->fun)(args, i); goto cleanup;
case 'C': { i = std::max(i+1, numargs); vector<char> buf; ((comfun1)id->fun)(conc(buf, args, i, true)); goto cleanup; }
case 'V': i = std::max(i+1, numargs); ((comfunv)id->fun)(args, i); goto cleanup;
case '1': case '2': case '3': case '4': if(i+1 < numargs) { fmt -= *fmt-'0'+1; rep = true; } break;
}
++i;
@ -3520,14 +3522,14 @@ COMMAND(at, "si1V");
void substr(char *s, int *start, int *count, int *numargs)
{
int len = strlen(s), offset = clamp(*start, 0, len);
commandret->setstr(newstring(&s[offset], *numargs >= 3 ? clamp(*count, 0, len - offset) : len - offset));
int len = strlen(s), offset = std::clamp(*start, 0, len);
commandret->setstr(newstring(&s[offset], *numargs >= 3 ? std::clamp(*count, 0, len - offset) : len - offset));
}
COMMAND(substr, "siiN");
void sublist(const char *s, int *skip, int *count, int *numargs)
{
int offset = max(*skip, 0), len = *numargs >= 3 ? max(*count, 0) : -1;
int offset = std::max(*skip, 0), len = *numargs >= 3 ? std::max(*count, 0) : -1;
loopi(offset) if(!parselist(s)) break;
if(len < 0) { if(offset > 0) skiplist(s); commandret->setstr(newstring(s)); return; }
const char *list = s, *start, *end, *qstart, *qend = s;
@ -3798,7 +3800,7 @@ LISTMERGECMD(listunion, p.put(list, strlen(list)), elems, list, <);
void listsplice(const char *s, const char *vals, int *skip, int *count)
{
int offset = max(*skip, 0), len = max(*count, 0);
int offset = std::max(*skip, 0), len = std::max(*count, 0);
const char *list = s, *start, *end, *qstart, *qend = s;
loopi(offset) if(!parselist(s, start, end, qstart, qend)) break;
vector<char> p;
@ -3942,7 +3944,7 @@ void sortlist(char *list, ident *x, ident *y, uint *body, uint *unique)
poparg(*y);
char *sorted = cstr;
int sortedlen = totalunique + max(numunique - 1, 0);
int sortedlen = totalunique + std::max(numunique - 1, 0);
if(clen < sortedlen)
{
delete[] cstr;
@ -4018,8 +4020,8 @@ MATHICMD(|, 0, );
MATHICMD(^~, 0, );
MATHICMD(&~, 0, );
MATHICMD(|~, 0, );
MATHCMD("<<", i, int, val = val2 < 32 ? val << max(val2, 0) : 0, 0, );
MATHCMD(">>", i, int, val >>= clamp(val2, 0, 31), 0, );
MATHCMD("<<", i, int, val = val2 < 32 ? val << std::max(val2, 0) : 0, 0, );
MATHCMD(">>", i, int, val >>= std::clamp(val2, 0, 31), 0, );
MATHFCMD(+, 0, );
MATHFCMD(*, 1, );
@ -4152,12 +4154,12 @@ CASECOMMAND(cases, "s", const char *, args[0].getstr(), args[i].type == VAL_NULL
ICOMMAND(rnd, "ii", (int *a, int *b), intret(*a - *b > 0 ? rnd(*a - *b) + *b : *b));
ICOMMAND(rndstr, "i", (int *len),
{
int n = clamp(*len, 0, 10000);
int n = std::clamp(*len, 0, 10000);
char *s = newstring(n);
for(int i = 0; i < n;)
{
uint r = randomMT();
for(int j = min(i + 4, n); i < j; i++)
for(int j = std::min(i + 4, n); i < j; i++)
{
s[i] = (r%255) + 1;
r /= 255;
@ -4171,7 +4173,7 @@ ICOMMAND(tohex, "ii", (int *n, int *p),
{
const int len = 20;
char *buf = newstring(len);
nformatstring(buf, len, "0x%.*X", max(*p, 1), *n);
nformatstring(buf, len, "0x%.*X", std::max(*p, 1), *n);
stringret(buf);
});
@ -4247,8 +4249,8 @@ ICOMMAND(strreplace, "ssss", (char *s, char *o, char *n, char *n2), commandret->
void strsplice(const char *s, const char *vals, int *skip, int *count)
{
int slen = strlen(s), vlen = strlen(vals),
offset = clamp(*skip, 0, slen),
len = clamp(*count, 0, slen - offset);
offset = std::clamp(*skip, 0, slen),
len = std::clamp(*count, 0, slen - offset);
char *p = newstring(slen - len + vlen);
if(offset) memcpy(p, s, offset);
if(vlen) memcpy(&p[offset], vals, vlen);
@ -4271,7 +4273,7 @@ vector<sleepcmd> sleepcmds;
void addsleep(int *msec, char *cmd)
{
sleepcmd &s = sleepcmds.add();
s.delay = max(*msec, 1);
s.delay = std::max(*msec, 1);
s.millis = lastmillis;
s.command = newstring(cmd);
s.flags = identflags;

View File

@ -93,13 +93,13 @@ static int conskip = 0, miniconskip = 0;
static void setconskip(int &skip, int filter, int n)
{
int offset = abs(n), dir = n < 0 ? -1 : 1;
skip = clamp(skip, 0, conlines.length()-1);
skip = std::clamp(skip, 0, conlines.length()-1);
while(offset)
{
skip += dir;
if(!conlines.inrange(skip))
{
skip = clamp(skip, 0, conlines.length()-1);
skip = std::clamp(skip, 0, conlines.length()-1);
return;
}
if(conlines[skip].type&filter) --offset;
@ -113,7 +113,7 @@ ICOMMAND(clearconsole, "", (), { while(conlines.length()) delete[] conlines.pop(
static float drawconlines(int conskip, int confade, float conwidth, float conheight, float conoff, int filter, float y = 0, int dir = 1)
{
int numl = conlines.length(), offset = min(conskip, numl);
int numl = conlines.length(), offset = std::min(conskip, numl);
if(confade)
{
@ -164,11 +164,11 @@ float renderfullconsole(float w, float h)
float renderconsole(float w, float h, float abovehud)
{
float conpad = FONTH/2,
conheight = min(float(FONTH*consize), h - 2*conpad),
conheight = std::min(float(FONTH*consize), h - 2*conpad),
conwidth = w - 2*conpad - game::clipconsole(w, h);
float y = drawconlines(conskip, confade, conwidth, conheight, conpad, confilter);
if(miniconsize && miniconwidth)
drawconlines(miniconskip, miniconfade, (miniconwidth*(w - 2*conpad))/100, min(float(FONTH*miniconsize), abovehud - y), conpad, miniconfilter, abovehud, -1);
drawconlines(miniconskip, miniconfade, (miniconwidth*(w - 2*conpad))/100, std::min(float(FONTH*miniconsize), abovehud - y), conpad, miniconfilter, abovehud, -1);
return y;
}
@ -474,7 +474,7 @@ static bool consoleinput(const char *str, int len)
resetcomplete();
int cmdlen = (int)strlen(commandbuf), cmdspace = int(sizeof(commandbuf)) - (cmdlen+1);
len = min(len, cmdspace);
len = std::min(len, cmdspace);
if(commandpos<0)
{
memcpy(&commandbuf[cmdlen], str, len);
@ -798,7 +798,7 @@ static void complete(char *s, int maxlen, const char *cmdprefix)
DELETEA(lastcomplete);
if(nextcomplete)
{
cmdlen = min(cmdlen, maxlen-1);
cmdlen = std::min(cmdlen, maxlen-1);
if(cmdlen) memmove(s, cmdprefix, cmdlen);
copystring(&s[cmdlen], nextcomplete, maxlen-cmdlen);
lastcomplete = newstring(nextcomplete);

View File

@ -67,8 +67,8 @@ struct skelbih
Texture *tex = s[t.mesh].tex; \
if(tex->type&Texture::ALPHA && (tex->alphamask || loadalphamask(tex))) \
{ \
int si = clamp(int(tex->xs * (va.tc.x + u*(vb.tc.x - va.tc.x) + v*(vc.tc.x - va.tc.x))), 0, tex->xs-1), \
ti = clamp(int(tex->ys * (va.tc.y + u*(vb.tc.y - va.tc.y) + v*(vc.tc.y - va.tc.y))), 0, tex->ys-1); \
int si = std::clamp(int(tex->xs * (va.tc.x + u*(vb.tc.x - va.tc.x) + v*(vc.tc.x - va.tc.x))), 0, tex->xs-1), \
ti = std::clamp(int(tex->ys * (va.tc.y + u*(vb.tc.y - va.tc.y) + v*(vc.tc.y - va.tc.y))), 0, tex->ys-1); \
if(!(tex->alphamask[ti*((tex->xs+7)/8) + si/8] & (1<<(si%8)))) return false; \
} \
} \

View File

@ -1,5 +1,7 @@
#include "light.hh"
#include <algorithm>
#include <shared/command.hh>
#include <shared/igame.hh>
@ -680,7 +682,7 @@ static void lightreaching(const vec &target, vec &color, vec &dir, bool fast = f
if(e.attached && e.attached->type==ET_SPOTLIGHT)
{
vec spot = vec(e.attached->o).sub(e.o).normalize();
float spotatten = 1 - (1 - ray.dot(spot)) / (1 - cos360(clamp(int(e.attached->attr1), 1, 89)));
float spotatten = 1 - (1 - ray.dot(spot)) / (1 - cos360(std::clamp(int(e.attached->attr1), 1, 89)));
if(spotatten <= 0) continue;
intensity *= spotatten;
}

View File

@ -34,7 +34,7 @@ static void renderlightning(Texture *tex, const vec &o, const vec &d, float sz)
vec step(d);
step.sub(o);
float len = step.magnitude();
int numsteps = clamp(int(ceil(len/LIGHTNINGSTEP)), 2, MAXLIGHTNINGSTEPS);
int numsteps = std::clamp(int(ceil(len/LIGHTNINGSTEP)), 2, MAXLIGHTNINGSTEPS);
step.div(numsteps+1);
int jitteroffset = detrnd(int(d.x+d.y+d.z), MAXLIGHTNINGSTEPS);
vec cur(o), up, right;
@ -44,7 +44,7 @@ static void renderlightning(Texture *tex, const vec &o, const vec &d, float sz)
right.normalize();
float scroll = -float(lastmillis%lnscrollmillis)/lnscrollmillis,
scrollscale = lnscrollscale*(LIGHTNINGSTEP*tex->ys)/(sz*tex->xs),
blend = pow(clamp(float(lastmillis - lastlnjitter)/lnjittermillis, 0.0f, 1.0f), lnblendpower),
blend = pow(std::clamp(float(lastmillis - lastlnjitter)/lnjittermillis, 0.0f, 1.0f), lnblendpower),
jitter0 = (1-blend)*lnjitterscale*sz/lnjitterradius, jitter1 = blend*lnjitterscale*sz/lnjitterradius;
gle::begin(GL_TRIANGLE_STRIP);
loopj(numsteps)

View File

@ -4,6 +4,8 @@
#include <cassert>
#include <algorithm>
#include <sauerlib/encoding.hh>
#include <shared/command.hh>
@ -518,8 +520,8 @@ VARF(fullscreen, 0, 1, 1, setfullscreen(fullscreen!=0));
static void screenres(int w, int h)
{
scr_w = clamp(w, SCR_MINW, SCR_MAXW);
scr_h = clamp(h, SCR_MINH, SCR_MAXH);
scr_w = std::clamp(w, SCR_MINW, SCR_MAXW);
scr_h = std::clamp(h, SCR_MINH, SCR_MAXH);
if(screen)
{
scr_w = min(scr_w, desktopw);
@ -850,8 +852,8 @@ static void checkinput()
SDL_GetWindowSize(screen, &screenw, &screenh);
if(!(SDL_GetWindowFlags(screen) & SDL_WINDOW_FULLSCREEN))
{
scr_w = clamp(screenw, SCR_MINW, SCR_MAXW);
scr_h = clamp(screenh, SCR_MINH, SCR_MAXH);
scr_w = std::clamp(screenw, SCR_MINW, SCR_MAXW);
scr_h = std::clamp(screenh, SCR_MINH, SCR_MAXH);
}
gl_resize();
break;
@ -1102,15 +1104,15 @@ int main(int argc, char **argv)
break;
}
case 'g': break;
case 'w': scr_w = clamp(atoi(&argv[i][2]), SCR_MINW, SCR_MAXW); if(!findarg(argc, argv, "-h")) scr_h = -1; break;
case 'h': scr_h = clamp(atoi(&argv[i][2]), SCR_MINH, SCR_MAXH); if(!findarg(argc, argv, "-w")) scr_w = -1; break;
case 'w': scr_w = std::clamp(atoi(&argv[i][2]), SCR_MINW, SCR_MAXW); if(!findarg(argc, argv, "-h")) scr_h = -1; break;
case 'h': scr_h = std::clamp(atoi(&argv[i][2]), SCR_MINH, SCR_MAXH); if(!findarg(argc, argv, "-w")) scr_w = -1; break;
case 'f': fullscreen = atoi(&argv[i][2]); break;
case 'x': initscript = &argv[i][2]; break;
default: break;
}
}
numcpus = clamp(SDL_GetCPUCount(), 1, 16);
numcpus = std::clamp(SDL_GetCPUCount(), 1, 16);
printf("init: sdl\n");

View File

@ -5,6 +5,8 @@
#include <cassert>
#include <climits>
#include <algorithm>
#include <shared/command.hh>
#include <shared/igame.hh>
@ -280,9 +282,9 @@ ivec lu;
int lusize;
cube &lookupcube(const ivec &to, int tsize, ivec &ro, int &rsize)
{
int tx = clamp(to.x, 0, worldsize-1),
ty = clamp(to.y, 0, worldsize-1),
tz = clamp(to.z, 0, worldsize-1);
int tx = std::clamp(to.x, 0, worldsize-1),
ty = std::clamp(to.y, 0, worldsize-1),
tz = std::clamp(to.z, 0, worldsize-1);
int scale = worldscale-1, csize = abs(tsize);
cube *c = &worldroot[octastep(tx, ty, tz, scale)];
if(!(csize>>scale)) do
@ -483,10 +485,10 @@ bool subdividecube(cube &c, bool fullcheck, bool brighten)
{
ch[i].texture[j] = c.texture[j];
int rd = (i>>R[d])&1, cd = (i>>C[d])&1, dd = (i>>D[d])&1;
edgeset(cubeedge(ch[i], d, 0, 0), z, clamp(e[rd][cd] - dd*8, 0, 8));
edgeset(cubeedge(ch[i], d, 1, 0), z, clamp(e[1+rd][cd] - dd*8, 0, 8));
edgeset(cubeedge(ch[i], d, 0, 1), z, clamp(e[rd][1+cd] - dd*8, 0, 8));
edgeset(cubeedge(ch[i], d, 1, 1), z, clamp(e[1+rd][1+cd] - dd*8, 0, 8));
edgeset(cubeedge(ch[i], d, 0, 0), z, std::clamp(e[rd][cd] - dd*8, 0, 8));
edgeset(cubeedge(ch[i], d, 1, 0), z, std::clamp(e[1+rd][cd] - dd*8, 0, 8));
edgeset(cubeedge(ch[i], d, 0, 1), z, std::clamp(e[rd][1+cd] - dd*8, 0, 8));
edgeset(cubeedge(ch[i], d, 1, 1), z, std::clamp(e[1+rd][1+cd] - dd*8, 0, 8));
}
}

View File

@ -1,6 +1,8 @@
#ifndef ENGINE_OCTA_HH
#define ENGINE_OCTA_HH
#include <algorithm>
#include <shared/gl.hh>
#include <shared/geom.hh>
@ -26,9 +28,9 @@ struct selinfo
if(o.x < 0) { s.x -= (grid - 1 - o.x)/grid; o.x = 0; }
if(o.y < 0) { s.y -= (grid - 1 - o.y)/grid; o.y = 0; }
if(o.z < 0) { s.z -= (grid - 1 - o.z)/grid; o.z = 0; }
s.x = clamp(s.x, 0, (worldsize - o.x)/grid);
s.y = clamp(s.y, 0, (worldsize - o.y)/grid);
s.z = clamp(s.z, 0, (worldsize - o.z)/grid);
s.x = std::clamp(s.x, 0, (worldsize - o.x)/grid);
s.y = std::clamp(s.y, 0, (worldsize - o.y)/grid);
s.z = std::clamp(s.z, 0, (worldsize - o.z)/grid);
return s.x > 0 && s.y > 0 && s.z > 0;
}
};

View File

@ -1,6 +1,7 @@
#include "octaedit.hh"
#include <new>
#include <algorithm>
#include <zlib.h>
@ -445,7 +446,7 @@ void rendereditcursor()
if(!insideworld(w))
{
wdist = 0;
loopi(3) w[i] = clamp(player->o[i], 0.0f, float(worldsize));
loopi(3) w[i] = std::clamp(player->o[i], 0.0f, float(worldsize));
}
}
cube *c = &lookupcube(ivec(w));
@ -1656,7 +1657,7 @@ namespace hmap
*x += MAXBRUSH2 - brushx + 1; // +1 for automatic padding
*y += MAXBRUSH2 - brushy + 1;
if(*x<0 || *y<0 || *x>=MAXBRUSH || *y>=MAXBRUSH) return;
brush[*x][*y] = clamp(*v, 0, 8);
brush[*x][*y] = std::clamp(*v, 0, 8);
paintbrush = paintbrush || (brush[*x][*y] > 0);
brushmaxx = min(MAXBRUSH-1, max(brushmaxx, *x+1));
brushmaxy = min(MAXBRUSH-1, max(brushmaxy, *y+1));
@ -1943,8 +1944,8 @@ namespace hmap
memset(flags, 0, sizeof flags);
selecting = true;
select(clamp(MAXBRUSH2-cx, bmx, bnx),
clamp(MAXBRUSH2-cy, bmy, bny),
select(std::clamp(MAXBRUSH2-cx, bmx, bnx),
std::clamp(MAXBRUSH2-cy, bmy, bny),
dc ? gz : hws - gz);
selecting = false;
if(paintme)
@ -2261,7 +2262,7 @@ static void vrotate(int *n)
if(noedit()) return;
VSlot ds;
ds.changed = 1<<VSLOT_ROTATION;
ds.rotation = usevdelta ? *n : clamp(*n, 0, 7);
ds.rotation = usevdelta ? *n : std::clamp(*n, 0, 7);
mpeditvslot(usevdelta, ds, allfaces, sel, true);
}
COMMAND(vrotate, "i");
@ -2304,7 +2305,7 @@ static void vscale(float *scale)
if(noedit()) return;
VSlot ds;
ds.changed = 1<<VSLOT_SCALE;
ds.scale = *scale <= 0 ? 1 : (usevdelta ? *scale : clamp(*scale, 1/8.0f, 8.0f));
ds.scale = *scale <= 0 ? 1 : (usevdelta ? *scale : std::clamp(*scale, 1/8.0f, 8.0f));
mpeditvslot(usevdelta, ds, allfaces, sel, true);
}
COMMAND(vscale, "f");
@ -2347,8 +2348,8 @@ static void valpha(float *front, float *back)
if(noedit()) return;
VSlot ds;
ds.changed = 1<<VSLOT_ALPHA;
ds.alphafront = clamp(*front, 0.0f, 1.0f);
ds.alphaback = clamp(*back, 0.0f, 1.0f);
ds.alphafront = std::clamp(*front, 0.0f, 1.0f);
ds.alphaback = std::clamp(*back, 0.0f, 1.0f);
mpeditvslot(usevdelta, ds, allfaces, sel, true);
}
COMMAND(valpha, "ff");
@ -2364,7 +2365,7 @@ static void vcolor(float *r, float *g, float *b)
if(noedit()) return;
VSlot ds;
ds.changed = 1<<VSLOT_COLOR;
ds.colorscale = vec(clamp(*r, 0.0f, 2.0f), clamp(*g, 0.0f, 2.0f), clamp(*b, 0.0f, 2.0f));
ds.colorscale = vec(std::clamp(*r, 0.0f, 2.0f), std::clamp(*g, 0.0f, 2.0f), std::clamp(*b, 0.0f, 2.0f));
mpeditvslot(usevdelta, ds, allfaces, sel, true);
}
COMMAND(vcolor, "fff");
@ -2380,9 +2381,9 @@ static void vrefract(float *k, float *r, float *g, float *b)
if(noedit()) return;
VSlot ds;
ds.changed = 1<<VSLOT_REFRACT;
ds.refractscale = clamp(*k, 0.0f, 1.0f);
ds.refractscale = std::clamp(*k, 0.0f, 1.0f);
if(ds.refractscale > 0 && (*r > 0 || *g > 0 || *b > 0))
ds.refractcolor = vec(clamp(*r, 0.0f, 1.0f), clamp(*g, 0.0f, 1.0f), clamp(*b, 0.0f, 1.0f));
ds.refractcolor = vec(std::clamp(*r, 0.0f, 1.0f), std::clamp(*g, 0.0f, 1.0f), std::clamp(*b, 0.0f, 1.0f));
else
ds.refractcolor = vec(1, 1, 1);
mpeditvslot(usevdelta, ds, allfaces, sel, true);
@ -2546,7 +2547,7 @@ static void edittex_(int *dir)
if(texmru.empty()) return;
texpaneltimer = 5000;
if(!(lastsel==sel)) tofronttex();
curtexindex = clamp(curtexindex<0 ? 0 : curtexindex+*dir, 0, texmru.length()-1);
curtexindex = std::clamp(curtexindex<0 ? 0 : curtexindex+*dir, 0, texmru.length()-1);
edittex(texmru[curtexindex], false);
}

View File

@ -4,6 +4,8 @@
#include <cassert>
#include <algorithm>
#include <shared/command.hh>
#include <shared/glemu.hh>
#include <shared/igame.hh>
@ -840,7 +842,7 @@ ushort encodenormal(const vec &n)
{
if(n.iszero()) return 0;
int yaw = int(-atan2(n.x, n.y)/RAD), pitch = int(asin(n.z)/RAD);
return ushort(clamp(pitch + 90, 0, 180)*360 + (yaw < 0 ? yaw%360 + 360 : yaw%360) + 1);
return ushort(std::clamp(pitch + 90, 0, 180)*360 + (yaw < 0 ? yaw%360 + 360 : yaw%360) + 1);
}
static vec decodenormal(ushort norm)
@ -1591,7 +1593,7 @@ static int updateva(cube *c, const ivec &co, int size, int csi)
int tcount = count + (csi <= MAXMERGELEVEL ? vamerges[csi].length() : 0);
if(tcount > vafacemax || (tcount >= vafacemin && size >= vacubesize) || size == min(0x1000, worldsize/2))
{
loadprogress = clamp(recalcprogress/float(allocnodes), 0.0f, 1.0f);
loadprogress = std::clamp(recalcprogress/float(allocnodes), 0.0f, 1.0f);
setva(c[i], o, size, csi);
if(c[i].ext && c[i].ext->va)
{

View File

@ -7,6 +7,8 @@
#include <cassert>
#include <algorithm>
#include <shared/command.hh>
#include <shared/igame.hh>
@ -455,7 +457,7 @@ static bool ellipseboxcollide(physent *d, const vec &dir, const vec &o, const ve
yo.rotate_around_z(-yaw*RAD);
yo.sub(center);
float dx = clamp(yo.x, -xr, xr) - yo.x, dy = clamp(yo.y, -yr, yr) - yo.y,
float dx = std::clamp(yo.x, -xr, xr) - yo.x, dy = std::clamp(yo.y, -yr, yr) - yo.y,
dist = sqrtf(dx*dx + dy*dy) - d->radius;
if(dist < 0)
{
@ -1828,12 +1830,12 @@ static void modifygravity(physent *pl, bool water, int curtime)
if(water || pl->physstate >= PHYS_SLOPE)
{
float fric = water ? 2.0f : 6.0f,
c = water ? 1.0f : clamp((pl->floor.z - SLOPEZ)/(FLOORZ-SLOPEZ), 0.0f, 1.0f);
c = water ? 1.0f : std::clamp((pl->floor.z - SLOPEZ)/(FLOORZ-SLOPEZ), 0.0f, 1.0f);
pl->falling.mul(pow(1 - c/fric, curtime/20.0f));
// old fps friction
// float friction = water ? 2.0f : 6.0f,
// fpsfric = friction/curtime*20.0f,
// c = water ? 1.0f : clamp((pl->floor.z - SLOPEZ)/(FLOORZ-SLOPEZ), 0.0f, 1.0f);
// c = water ? 1.0f : std::clamp((pl->floor.z - SLOPEZ)/(FLOORZ-SLOPEZ), 0.0f, 1.0f);
// pl->falling.mul(1 - c/fpsfric);
}
}
@ -1889,7 +1891,7 @@ bool moveplayer(physent *pl, int moveres, bool local, int curtime)
// automatically apply smooth roll when strafing
if(pl->strafe && maxroll) pl->roll = clamp(pl->roll - pow(clamp(1.0f + pl->strafe*pl->roll/maxroll, 0.0f, 1.0f), 0.33f)*pl->strafe*curtime*straferoll, -maxroll, maxroll);
if(pl->strafe && maxroll) pl->roll = std::clamp(pl->roll - pow(std::clamp(1.0f + pl->strafe*pl->roll/maxroll, 0.0f, 1.0f), 0.33f)*pl->strafe*curtime*straferoll, float(-maxroll), float(maxroll));
else pl->roll *= curtime == PHYSFRAMETIME ? faderoll : pow(faderoll, curtime/float(PHYSFRAMETIME));
// play sounds on water transitions
@ -1916,7 +1918,7 @@ void physicsframe() // optimally schedule physics frames inside the gra
if(diff <= 0) physsteps = 0;
else
{
physframetime = clamp(game::scaletime(PHYSFRAMETIME)/100, 1, PHYSFRAMETIME);
physframetime = std::clamp(game::scaletime(PHYSFRAMETIME)/100, 1, PHYSFRAMETIME);
physsteps = (diff + physframetime - 1)/physframetime;
lastphysframe += physsteps * physframetime;
}

View File

@ -4,6 +4,8 @@
#include <ctime>
#include <algorithm>
#include <shared/command.hh>
#include <shared/glemu.hh>
#include <shared/igame.hh>
@ -1474,14 +1476,14 @@ void recomputecamera()
if(game::collidecamera())
{
movecamera(camera1, dir, thirdpersondistance, 1);
movecamera(camera1, dir, clamp(thirdpersondistance - camera1->o.dist(player->o), 0.0f, 1.0f), 0.1f);
movecamera(camera1, dir, std::clamp(thirdpersondistance - camera1->o.dist(player->o), 0.0f, 1.0f), 0.1f);
if(thirdpersonup)
{
vec pos = camera1->o;
float dist = fabs(thirdpersonup);
if(thirdpersonup < 0) up.neg();
movecamera(camera1, up, dist, 1);
movecamera(camera1, up, clamp(dist - camera1->o.dist(pos), 0.0f, 1.0f), 0.1f);
movecamera(camera1, up, std::clamp(dist - camera1->o.dist(pos), 0.0f, 1.0f), 0.1f);
}
if(thirdpersonside)
{
@ -1489,7 +1491,7 @@ void recomputecamera()
float dist = fabs(thirdpersonside);
if(thirdpersonside < 0) side.neg();
movecamera(camera1, side, dist, 1);
movecamera(camera1, side, clamp(dist - camera1->o.dist(pos), 0.0f, 1.0f), 0.1f);
movecamera(camera1, side, std::clamp(dist - camera1->o.dist(pos), 0.0f, 1.0f), 0.1f);
}
}
else
@ -1904,7 +1906,7 @@ static void blendfog(int fogmat, float below, float blend, float logblend, float
{
const bvec &wcol = getwatercolour(fogmat), &wdeepcol = getwaterdeepcolour(fogmat);
int wfog = getwaterfog(fogmat), wdeep = getwaterdeep(fogmat);
float deepfade = clamp(below/max(wdeep, wfog), 0.0f, 1.0f);
float deepfade = std::clamp(below/max(wdeep, wfog), 0.0f, 1.0f);
vec color;
color.lerp(wcol.tocolor(), wdeepcol.tocolor(), deepfade);
fogc.add(vec(color).mul(blend));
@ -1985,7 +1987,7 @@ static void blendfogoverlay(int fogmat, float below, float blend, vec &overlay)
{
const bvec &wcol = getwatercolour(fogmat), &wdeepcol = getwaterdeepcolour(fogmat);
int wfog = getwaterfog(fogmat), wdeep = getwaterdeep(fogmat);
float deepfade = clamp(below/max(wdeep, wfog), 0.0f, 1.0f);
float deepfade = std::clamp(below/max(wdeep, wfog), 0.0f, 1.0f);
vec color = vec(wcol.r, wcol.g, wcol.b).lerp(vec(wdeepcol.r, wdeepcol.g, wdeepcol.b), deepfade);
overlay.add(color.div(min(32.0f + max(color.r, max(color.g, color.b))*7.0f/8.0f, 255.0f)).max(0.4f).mul(blend));
break;
@ -2262,7 +2264,7 @@ void drawcubemap(int size, const vec &o, float yaw, float pitch, const cubemapsi
renderwaterfog(fogmat, fogbelow);
setfog(fogmat, fogbelow, clamp(fogbelow, 0.0f, 1.0f), abovemat);
setfog(fogmat, fogbelow, std::clamp(fogbelow, 0.0f, 1.0f), abovemat);
}
rendertransparent();
@ -2458,7 +2460,7 @@ void gl_drawview()
renderwaterfog(fogmat, fogbelow);
setfog(fogmat, fogbelow, clamp(fogbelow, 0.0f, 1.0f), abovemat);
setfog(fogmat, fogbelow, std::clamp(fogbelow, 0.0f, 1.0f), abovemat);
}
rendertransparent();
@ -2492,7 +2494,7 @@ void gl_drawview()
glDisable(GL_CULL_FACE);
glDisable(GL_DEPTH_TEST);
if(fogoverlay && fogmat != MAT_AIR) drawfogoverlay(fogmat, fogbelow, clamp(fogbelow, 0.0f, 1.0f), abovemat);
if(fogoverlay && fogmat != MAT_AIR) drawfogoverlay(fogmat, fogbelow, std::clamp(fogbelow, 0.0f, 1.0f), abovemat);
doaa(setuppostfx(vieww, viewh, scalefbo), processhdr);
renderpostfx(scalefbo);

View File

@ -2,6 +2,8 @@
#include <climits>
#include <algorithm>
#include <shared/command.hh>
#include <shared/glemu.hh>
#include <shared/igame.hh>
@ -1604,7 +1606,7 @@ struct lightinfo
if(e.attached && e.attached->type == ET_SPOTLIGHT)
{
dir = vec(e.attached->o).sub(e.o).normalize();
spot = clamp(int(e.attached->attr1), 1, 89);
spot = std::clamp(int(e.attached->attr1), 1, 89);
calcspot();
}
calcscissor();
@ -3555,8 +3557,8 @@ void collectlights()
int w, h;
if(l.spot) { w = 1; h = 1; prec *= tan360(l.spot); lod = smspotprec; }
else { w = 3; h = 2; lod = smcubeprec; }
lod *= clamp(l.radius * prec / sqrtf(max(1.0f, l.dist/l.radius)), float(smminsize), float(smmaxsize));
int size = clamp(int(ceil((lod * shadowatlaspacker.w) / SHADOWATLAS_SIZE)), 1, shadowatlaspacker.w / w);
lod *= std::clamp(l.radius * prec / sqrtf(max(1.0f, l.dist/l.radius)), float(smminsize), float(smmaxsize));
int size = std::clamp(int(ceil((lod * shadowatlaspacker.w) / SHADOWATLAS_SIZE)), 1, shadowatlaspacker.w / w);
w *= size;
h *= size;
@ -3743,8 +3745,8 @@ void packlights()
int w, h;
if(l.spot) { w = 1; h = 1; prec *= tan360(l.spot); lod = smspotprec; }
else { w = 3; h = 2; lod = smcubeprec; }
lod *= clamp(l.radius * prec / sqrtf(max(1.0f, l.dist/l.radius)), float(smminsize), float(smmaxsize));
int size = clamp(int(ceil((lod * shadowatlaspacker.w) / SHADOWATLAS_SIZE)), 1, shadowatlaspacker.w / w);
lod *= std::clamp(l.radius * prec / sqrtf(max(1.0f, l.dist/l.radius)), float(smminsize), float(smmaxsize));
int size = std::clamp(int(ceil((lod * shadowatlaspacker.w) / SHADOWATLAS_SIZE)), 1, shadowatlaspacker.w / w);
w *= size;
h *= size;
ushort x = USHRT_MAX, y = USHRT_MAX;
@ -4330,7 +4332,7 @@ int calcshadowinfo(const extentity &e, vec &origin, float &radius, vec &spotloc,
border = 0;
lod = smspotprec;
spotloc = e.attached->o;
spotangle = clamp(int(e.attached->attr1), 1, 89);
spotangle = std::clamp(int(e.attached->attr1), 1, 89);
}
else
{
@ -4343,7 +4345,7 @@ int calcshadowinfo(const extentity &e, vec &origin, float &radius, vec &spotloc,
}
lod *= smminsize;
int size = clamp(int(ceil((lod * shadowatlaspacker.w) / SHADOWATLAS_SIZE)), 1, shadowatlaspacker.w / w);
int size = std::clamp(int(ceil((lod * shadowatlaspacker.w) / SHADOWATLAS_SIZE)), 1, shadowatlaspacker.w / w);
bias = border / float(size - border);
return type;
@ -4936,8 +4938,8 @@ void shademodelpreview(int x, int y, int w, int h, bool background, bool scissor
if(scissor) glEnable(GL_SCISSOR_TEST);
int sx = clamp(x, 0, hudw), sy = clamp(y, 0, hudh),
sw = clamp(x + w, 0, hudw) - sx, sh = clamp(y + h, 0, hudh) - sy;
int sx = std::clamp(x, 0, hudw), sy = std::clamp(y, 0, hudh),
sw = std::clamp(x + w, 0, hudw) - sx, sh = std::clamp(y + h, 0, hudh) - sy;
float sxk = 2.0f/hudw, syk = 2.0f/hudh, txk = vieww/float(w), tyk = viewh/float(h);
hudquad(sx*sxk - 1, sy*syk - 1, sw*sxk, sh*syk, (sx-x)*txk, (sy-y)*tyk, sw*txk, sh*tyk);

View File

@ -3,6 +3,7 @@
#include <climits>
#include <new>
#include <algorithm>
#include <sauerlib/encoding.hh>
@ -116,14 +117,14 @@ COMMAND(mdlspec, "f");
static void mdlgloss(int *gloss)
{
checkmdl;
loadingmodel->setgloss(clamp(*gloss, 0, 2));
loadingmodel->setgloss(std::clamp(*gloss, 0, 2));
}
COMMAND(mdlgloss, "i");
static void mdlalphatest(float *cutoff)
{
checkmdl;
loadingmodel->setalphatest(max(0.0f, min(1.0f, *cutoff)));
loadingmodel->setalphatest(std::max(0.0f, std::min(1.0f, *cutoff)));
}
COMMAND(mdlalphatest, "f");
@ -305,7 +306,7 @@ static void rdlimitdist(int *v1, int *v2, float *mindist, float *maxdist)
d.vert[0] = *v1;
d.vert[1] = *v2;
d.mindist = *mindist;
d.maxdist = max(*maxdist, *mindist);
d.maxdist = std::max(*maxdist, *mindist);
}
COMMAND(rdlimitdist, "iiff");
@ -345,7 +346,7 @@ static void mapmodel(char *name)
static void mapmodelreset(int *n)
{
if(!(identflags&IDF_OVERRIDDEN) && !game::allowedittoggle()) return;
mapmodels.shrink(clamp(*n, 0, mapmodels.length()));
mapmodels.shrink(std::clamp(*n, 0, mapmodels.length()));
}
const char *mapmodelname(int i) { return mapmodels.inrange(i) ? mapmodels[i].name : nullptr; }
@ -766,10 +767,10 @@ void rendermodelbatches()
ivec bbmin(vec(bm.center).sub(bm.radius)), bbmax(vec(bm.center).add(bm.radius+1));
if(calcbbscissor(bbmin, bbmax, sx1, sy1, sx2, sy2))
{
transmdlsx1 = min(transmdlsx1, sx1);
transmdlsy1 = min(transmdlsy1, sy1);
transmdlsx2 = max(transmdlsx2, sx2);
transmdlsy2 = max(transmdlsy2, sy2);
transmdlsx1 = std::min(transmdlsx1, sx1);
transmdlsy1 = std::min(transmdlsy1, sy1);
transmdlsx2 = std::max(transmdlsx2, sx2);
transmdlsy2 = std::max(transmdlsy2, sy2);
masktiles(transmdltiles, sx1, sy1, sx2, sy2);
}
continue;
@ -976,7 +977,7 @@ void rendermodel(const char *mdl, int anim, const vec &o, float yaw, float pitch
{
if(anim&ANIM_RAGDOLL && d->ragdoll->millis >= basetime)
{
radius = max(radius, d->ragdoll->radius);
radius = std::max(radius, d->ragdoll->radius);
center = d->ragdoll->center;
goto hasboundbox;
}
@ -1144,7 +1145,7 @@ void setbbfrommodel(dynent *d, const char *mdl)
if(m->collide != COLLIDE_ELLIPSE) d->collidetype = COLLIDE_OBB;
d->xradius = radius.x + fabs(center.x);
d->yradius = radius.y + fabs(center.y);
d->radius = d->collidetype==COLLIDE_OBB ? sqrtf(d->xradius*d->xradius + d->yradius*d->yradius) : max(d->xradius, d->yradius);
d->radius = d->collidetype==COLLIDE_OBB ? sqrtf(d->xradius*d->xradius + d->yradius*d->yradius) : std::max(d->xradius, d->yradius);
d->eyeheight = (center.z-radius.z) + radius.z*2*m->eyeheight;
d->aboveeye = radius.z*2*(1.0f-m->eyeheight);
if (d->aboveeye + d->eyeheight <= 0.5f)

View File

@ -1,5 +1,9 @@
// renderparticles.cpp
#include "renderparticles.hh"
#include <algorithm>
#include <shared/command.hh>
#include <shared/glemu.hh>
#include <shared/igame.hh>
@ -1137,7 +1141,7 @@ void particle_trail(int type, int fade, const vec &s, const vec &e, int color, f
if(!canaddparticles()) return;
vec v;
float d = e.dist(s, v);
int steps = clamp(int(d*2), 1, maxtrail);
int steps = std::clamp(int(d*2), 1, maxtrail);
v.div(steps);
vec p = s;
loopi(steps)
@ -1170,7 +1174,7 @@ void particle_meter(const vec &s, float val, int type, int fade, int color, int
p->color2[0] = color2>>16;
p->color2[1] = (color2>>8)&0xFF;
p->color2[2] = color2&0xFF;
p->progress = clamp(int(val*100), 0, 100);
p->progress = std::clamp(int(val*100), 0, 100);
}
void particle_flare(const vec &p, const vec &dest, int fade, int type, int color, float size, physent *owner)
@ -1277,7 +1281,7 @@ static void regularshape(int type, int radius, int color, int dir, int num, int
if(taper)
{
float dist = clamp(from.dist2(camera1->o)/maxparticledistance, 0.0f, 1.0f);
float dist = std::clamp(from.dist2(camera1->o)/maxparticledistance, 0.0f, 1.0f);
if(dist > 0.2f)
{
dist = 1 - (dist - 0.2f)/0.8f;
@ -1341,7 +1345,7 @@ static void makeparticles(entity &e)
if(e.attr3 > 0) color = colorfromattr(e.attr3);
else
{
int mat = MAT_WATER + clamp(-e.attr3, 0, 3);
int mat = MAT_WATER + std::clamp(-e.attr3, 0, 3);
color = getwaterfallcolour(mat).tohexcolor();
if(!color) color = getwatercolour(mat).tohexcolor();
}
@ -1375,7 +1379,7 @@ static void makeparticles(entity &e)
p->color2[0] = color2>>16;
p->color2[1] = (color2>>8)&0xFF;
p->color2[2] = color2&0xFF;
p->progress = clamp(int(e.attr2), 0, 100);
p->progress = std::clamp(int(e.attr2), 0, 100);
break;
}
case 11: // flame <radius> <height> <rgb> - radius=100, height=100 is the classic size

View File

@ -4,6 +4,8 @@
#include <climits>
#include <algorithm>
#include <shared/command.hh>
#include <shared/glemu.hh>
#include <shared/igame.hh>
@ -139,7 +141,7 @@ static inline void addvisibleva(vtxarray *va)
float dist = vadist(va, camera1->o);
va->distance = int(dist); /*cv.dist(camera1->o) - va->size*SQRT3/2*/
int hash = clamp(int(dist*VASORTSIZE/worldsize), 0, VASORTSIZE-1);
int hash = std::clamp(int(dist*VASORTSIZE/worldsize), 0, VASORTSIZE-1);
vtxarray **prev = &vasort[hash], *cur = vasort[hash];
while(cur && va->distance >= cur->distance)
@ -913,7 +915,7 @@ static inline void addshadowva(vtxarray *va, float dist)
{
va->rdistance = int(dist);
int hash = clamp(int(dist*VASORTSIZE/shadowradius), 0, VASORTSIZE-1);
int hash = std::clamp(int(dist*VASORTSIZE/shadowradius), 0, VASORTSIZE-1);
vtxarray **prev = &vasort[hash], *cur = vasort[hash];
while(cur && va->rdistance > cur->rdistance)
@ -2724,7 +2726,7 @@ shadowmesh *findshadowmesh(int idx, extentity &e)
switch(m->type)
{
case SM_SPOT:
if(!e.attached || e.attached->type != ET_SPOTLIGHT || m->spotloc != e.attached->o || m->spotangle < clamp(int(e.attached->attr1), 1, 89))
if(!e.attached || e.attached->type != ET_SPOTLIGHT || m->spotloc != e.attached->o || m->spotangle < std::clamp(int(e.attached->attr1), 1, 89))
return nullptr;
break;
}

View File

@ -4,6 +4,8 @@
#include <climits>
#include <algorithm>
#include <sauerlib/encoding.hh>
#include <shared/command.hh>
@ -586,7 +588,7 @@ static void allocglslactiveuniforms(Shader &s)
name[0] = '\0';
glGetActiveUniform_(s.program, i, sizeof(name)-1, &namelen, &size, &format, name);
if(namelen <= 0 || size <= 0) continue;
name[clamp(int(namelen), 0, (int)sizeof(name)-2)] = '\0';
name[std::clamp(int(namelen), 0, (int)sizeof(name)-2)] = '\0';
char *brak = strchr(name, '[');
if(brak) *brak = '\0';
setglsluniformformat(s, name, format, size);
@ -1521,7 +1523,7 @@ ICOMMAND(addpostfx, "siisffff", (char *name, int *bind, int *scale, char *inputs
}
inputmask &= (1<<NUMPOSTFXBINDS)-1;
freemask &= (1<<NUMPOSTFXBINDS)-1;
addpostfx(name, clamp(*bind, 0, NUMPOSTFXBINDS-1), max(*scale, 0), inputmask, freemask, vec4(*x, *y, *z, *w));
addpostfx(name, std::clamp(*bind, 0, NUMPOSTFXBINDS-1), max(*scale, 0), inputmask, freemask, vec4(*x, *y, *z, *w));
});
ICOMMAND(setpostfx, "sffff", (char *name, float *x, float *y, float *z, float *w),

View File

@ -779,7 +779,7 @@ struct skelmodel : animmodel
vec forward1 = pose1.transformnormal(forward).project(axis).normalize(),
forward2 = pose2.transformnormal(forward).project(axis).normalize(),
daxis = vec().cross(forward1, forward2);
float dx = clamp(forward1.dot(forward2), -1.0f, 1.0f), dy = clamp(daxis.magnitude(), -1.0f, 1.0f);
float dx = std::clamp(forward1.dot(forward2), -1.0f, 1.0f), dy = std::clamp(daxis.magnitude(), -1.0f, 1.0f);
if(daxis.dot(axis) < 0) dy = -dy;
return atan2f(dy, dx)/RAD;
}
@ -802,7 +802,7 @@ struct skelmodel : animmodel
float tpitch = pitch - t.deviated;
for(int parent = t.corrects; parent >= 0; parent = pitchcorrects[parent].parent)
tpitch -= pitchcorrects[parent].pitchangle;
if(t.pitchmin || t.pitchmax) tpitch = clamp(tpitch, t.pitchmin, t.pitchmax);
if(t.pitchmin || t.pitchmax) tpitch = std::clamp(tpitch, t.pitchmin, t.pitchmax);
loopv(pitchcorrects)
{
pitchcorrect &c = pitchcorrects[i];
@ -812,11 +812,11 @@ struct skelmodel : animmodel
used = tpitch*c.pitchscale;
if(c.pitchmin || c.pitchmax)
{
if(used < 0) used = clamp(c.pitchmin, used, 0.0f);
else used = clamp(c.pitchmax, 0.0f, used);
if(used < 0) used = std::clamp(c.pitchmin, used, 0.0f);
else used = std::clamp(c.pitchmax, 0.0f, used);
}
if(used < 0) used = clamp(avail, used, 0.0f);
else used = clamp(avail, 0.0f, used);
if(used < 0) used = std::clamp(avail, used, 0.0f);
else used = std::clamp(avail, 0.0f, used);
c.pitchangle = used;
c.pitchtotal = used + total;
}
@ -871,7 +871,7 @@ struct skelmodel : animmodel
else sc.bdata[b.interpindex].mul(sc.bdata[b.interpparent], d);
float angle;
if(b.pitchscale) { angle = b.pitchscale*pitch + b.pitchoffset; if(b.pitchmin || b.pitchmax) angle = clamp(angle, b.pitchmin, b.pitchmax); }
if(b.pitchscale) { angle = b.pitchscale*pitch + b.pitchoffset; if(b.pitchmin || b.pitchmax) angle = std::clamp(angle, b.pitchmin, b.pitchmax); }
else if(b.correctindex >= 0) angle = pitchcorrects[b.correctindex].pitchangle;
else continue;
if(as->cur.anim&ANIM_NOPITCH || (as->interp < 1 && as->prev.anim&ANIM_NOPITCH))
@ -1660,7 +1660,7 @@ template<class MDL> struct skelcommands : modelcommands<MDL, struct MDL::skelmes
if(!MDL::loading) { conoutf("not loading an %s", MDL::formatname()); return; }
defformatstring(filename, "%s/%s", MDL::dir, meshfile);
part &mdl = MDL::loading->addpart();
mdl.meshes = MDL::loading->sharemeshes(path(filename), skelname[0] ? skelname : nullptr, *smooth > 0 ? cosf(clamp(*smooth, 0.0f, 180.0f)*RAD) : 2);
mdl.meshes = MDL::loading->sharemeshes(path(filename), skelname[0] ? skelname : nullptr, *smooth > 0 ? cosf(std::clamp(*smooth, 0.0f, 180.0f)*RAD) : 2);
if(!mdl.meshes) conoutf("could not load %s", filename);
else
{
@ -1749,7 +1749,7 @@ template<class MDL> struct skelcommands : modelcommands<MDL, struct MDL::skelmes
loopv(skel->pitchtargets) if(skel->pitchtargets[i].bone == bone) return;
pitchtarget &t = skel->pitchtargets.add();
t.bone = bone;
t.frame = sa->frame + clamp(*frameoffset, 0, sa->range-1);
t.frame = sa->frame + std::clamp(*frameoffset, 0, sa->range-1);
t.pitchmin = *pitchmin;
t.pitchmax = *pitchmax;
}

View File

@ -4,6 +4,8 @@
#include <ctime>
#include <algorithm>
#include <zlib.h>
#include <sauerlib/encoding.hh>
@ -79,8 +81,8 @@ template<int BPP> static void scaletexture(uchar * RESTRICT src, uint sw, uint s
int over, under;
for(over = 0; (darea>>over) > sarea; over++);
for(under = 0; (darea<<under) < sarea; under++);
uint cscale = clamp(under, over - 12, 12),
ascale = clamp(12 + under - over, 0, 24),
uint cscale = std::clamp(under, over - 12, 12),
ascale = std::clamp(12 + under - over, 0, 24),
dscale = ascale + 12 - cscale,
area = ((ullong)darea<<ascale)/sarea;
dw *= wfrac;
@ -518,8 +520,8 @@ static void texoffset(ImageData &s, int xoffset, int yoffset)
static void texcrop(ImageData &s, int x, int y, int w, int h)
{
x = clamp(x, 0, s.w);
y = clamp(y, 0, s.h);
x = std::clamp(x, 0, s.w);
y = std::clamp(y, 0, s.h);
w = min(w < 0 ? s.w : w, s.w - x);
h = min(h < 0 ? s.h : h, s.h - y);
if(!w || !h) return;
@ -540,7 +542,7 @@ static void texmad(ImageData &s, const vec &mul, const vec &add)
swizzleimage(s);
int maxk = min(int(s.bpp), 3);
writetex(s,
loopk(maxk) dst[k] = uchar(clamp(dst[k]*mul[k] + 255*add[k], 0.0f, 255.0f));
loopk(maxk) dst[k] = uchar(std::clamp(dst[k]*mul[k] + 255*add[k], 0.0f, 255.0f));
);
}
@ -550,7 +552,7 @@ static void texcolorify(ImageData &s, const vec &color, vec weights)
if(weights.iszero()) weights = vec(0.21f, 0.72f, 0.07f);
writetex(s,
float lum = dst[0]*weights.x + dst[1]*weights.y + dst[2]*weights.z;
loopk(3) dst[k] = uchar(clamp(lum*color[k], 0.0f, 255.0f));
loopk(3) dst[k] = uchar(std::clamp(lum*color[k], 0.0f, 255.0f));
);
}
@ -561,7 +563,7 @@ static void texcolormask(ImageData &s, const vec &color1, const vec &color2)
readwritetex(d, s,
vec color;
color.lerp(color2, color1, src[3]/255.0f);
loopk(3) dst[k] = uchar(clamp(color[k]*src[k], 0.0f, 255.0f));
loopk(3) dst[k] = uchar(std::clamp(color[k]*src[k], 0.0f, 255.0f));
);
s.replace(d);
}
@ -651,7 +653,7 @@ static void texagrad(ImageData &s, float x2, float y2, float x1, float y1)
float curx = minx;
for(uchar *dst = dstrow, *end = &dstrow[s.w*s.bpp]; dst < end; dst += s.bpp)
{
dst[0] = uchar(dst[0]*clamp(curx, 0.0f, 1.0f)*clamp(cury, 0.0f, 1.0f));
dst[0] = uchar(dst[0]*std::clamp(curx, 0.0f, 1.0f)*std::clamp(cury, 0.0f, 1.0f));
curx += dx;
}
cury += dy;
@ -1437,7 +1439,7 @@ static void blurtexture(int w, int h, uchar *dst, const uchar *src, int margin)
static void blurtexture(int n, int bpp, int w, int h, uchar *dst, const uchar *src, int margin = 0)
{
switch((clamp(n, 1, 2)<<4) | bpp)
switch((std::clamp(n, 1, 2)<<4) | bpp)
{
case 0x13: blurtexture<1, 3, false>(w, h, dst, src, margin); break;
case 0x23: blurtexture<2, 3, false>(w, h, dst, src, margin); break;
@ -1449,7 +1451,7 @@ static void blurtexture(int n, int bpp, int w, int h, uchar *dst, const uchar *s
#if 0
static void blurnormals(int n, int w, int h, bvec *dst, const bvec *src, int margin = 0)
{
switch(clamp(n, 1, 2))
switch(std::clamp(n, 1, 2))
{
case 1: blurtexture<1, 3, true>(w, h, dst->v, src->v, margin); break;
case 2: blurtexture<2, 3, true>(w, h, dst->v, src->v, margin); break;
@ -1601,7 +1603,7 @@ static bool texturedata(ImageData &d, const char *tname, bool msg = true, int *c
else if(matchstring(cmd, len, "blur"))
{
int emphasis = atoi(arg[0]), repeat = atoi(arg[1]);
texblur(d, emphasis > 0 ? clamp(emphasis, 1, 2) : 1, repeat > 0 ? repeat : 1);
texblur(d, emphasis > 0 ? std::clamp(emphasis, 1, 2) : 1, repeat > 0 ? repeat : 1);
}
else if(matchstring(cmd, len, "premul")) texpremul(d);
else if(matchstring(cmd, len, "agrad")) texagrad(d, atof(arg[0]), atof(arg[1]), atof(arg[2]), atof(arg[3]));
@ -1715,7 +1717,7 @@ static void texturereset(int *n)
if(!(identflags&IDF_OVERRIDDEN) && !game::allowedittoggle()) return;
defslot = nullptr;
resetslotshader();
int limit = clamp(*n, 0, slots.length());
int limit = std::clamp(*n, 0, slots.length());
for(int i = limit; i < slots.length(); i++)
{
Slot *s = slots[i];
@ -1978,11 +1980,11 @@ static void mergevslot(VSlot &dst, const VSlot &src, int diff, Slot *slot = null
}
if(diff & (1<<VSLOT_SCALE))
{
dst.scale = clamp(dst.scale*src.scale, 1/8.0f, 8.0f);
dst.scale = std::clamp(dst.scale*src.scale, 1/8.0f, 8.0f);
}
if(diff & (1<<VSLOT_ROTATION))
{
dst.rotation = clamp(dst.rotation + src.rotation, 0, 7);
dst.rotation = std::clamp(dst.rotation + src.rotation, 0, 7);
if(!dst.offset.iszero()) clampvslotoffset(dst, slot);
}
if(diff & (1<<VSLOT_OFFSET))
@ -2162,11 +2164,11 @@ bool unpackvslot(ucharbuf &buf, VSlot &dst, bool delta)
case VSLOT_SCALE:
dst.scale = getfloat(buf);
if(dst.scale <= 0) dst.scale = 1;
else if(!delta) dst.scale = clamp(dst.scale, 1/8.0f, 8.0f);
else if(!delta) dst.scale = std::clamp(dst.scale, 1/8.0f, 8.0f);
break;
case VSLOT_ROTATION:
dst.rotation = getint(buf);
if(!delta) dst.rotation = clamp(dst.rotation, 0, 7);
if(!delta) dst.rotation = std::clamp(dst.rotation, 0, 7);
break;
case VSLOT_OFFSET:
dst.offset.x = getint(buf);
@ -2184,19 +2186,19 @@ bool unpackvslot(ucharbuf &buf, VSlot &dst, bool delta)
break;
}
case VSLOT_ALPHA:
dst.alphafront = clamp(getfloat(buf), 0.0f, 1.0f);
dst.alphaback = clamp(getfloat(buf), 0.0f, 1.0f);
dst.alphafront = std::clamp(getfloat(buf), 0.0f, 1.0f);
dst.alphaback = std::clamp(getfloat(buf), 0.0f, 1.0f);
break;
case VSLOT_COLOR:
dst.colorscale.r = clamp(getfloat(buf), 0.0f, 2.0f);
dst.colorscale.g = clamp(getfloat(buf), 0.0f, 2.0f);
dst.colorscale.b = clamp(getfloat(buf), 0.0f, 2.0f);
dst.colorscale.r = std::clamp(getfloat(buf), 0.0f, 2.0f);
dst.colorscale.g = std::clamp(getfloat(buf), 0.0f, 2.0f);
dst.colorscale.b = std::clamp(getfloat(buf), 0.0f, 2.0f);
break;
case VSLOT_REFRACT:
dst.refractscale = clamp(getfloat(buf), 0.0f, 1.0f);
dst.refractcolor.r = clamp(getfloat(buf), 0.0f, 1.0f);
dst.refractcolor.g = clamp(getfloat(buf), 0.0f, 1.0f);
dst.refractcolor.b = clamp(getfloat(buf), 0.0f, 1.0f);
dst.refractscale = std::clamp(getfloat(buf), 0.0f, 1.0f);
dst.refractcolor.r = std::clamp(getfloat(buf), 0.0f, 1.0f);
dst.refractcolor.g = std::clamp(getfloat(buf), 0.0f, 1.0f);
dst.refractcolor.b = std::clamp(getfloat(buf), 0.0f, 1.0f);
break;
case VSLOT_DETAIL:
{
@ -2335,7 +2337,7 @@ static void texture(char *type, char *name, int *rot, int *xoffset, int *yoffset
{
setslotshader(s);
VSlot &vs = s.emptyvslot();
vs.rotation = clamp(*rot, 0, 7);
vs.rotation = std::clamp(*rot, 0, 7);
vs.offset = ivec2(*xoffset, *yoffset).max(0);
vs.scale = *scale <= 0 ? 1 : *scale;
propagatevslot(&vs, (1<<VSLOT_NUM)-1);
@ -2375,7 +2377,7 @@ static void texrotate_(int *rot)
{
if(!defslot) return;
Slot &s = *defslot;
s.variants->rotation = clamp(*rot, 0, 7);
s.variants->rotation = std::clamp(*rot, 0, 7);
propagatevslot(s.variants, 1<<VSLOT_ROTATION);
}
COMMANDN(texrotate, texrotate_, "i");
@ -2411,8 +2413,8 @@ static void texalpha(float *front, float *back)
{
if(!defslot) return;
Slot &s = *defslot;
s.variants->alphafront = clamp(*front, 0.0f, 1.0f);
s.variants->alphaback = clamp(*back, 0.0f, 1.0f);
s.variants->alphafront = std::clamp(*front, 0.0f, 1.0f);
s.variants->alphaback = std::clamp(*back, 0.0f, 1.0f);
propagatevslot(s.variants, 1<<VSLOT_ALPHA);
}
COMMAND(texalpha, "ff");
@ -2421,7 +2423,7 @@ static void texcolor(float *r, float *g, float *b)
{
if(!defslot) return;
Slot &s = *defslot;
s.variants->colorscale = vec(clamp(*r, 0.0f, 2.0f), clamp(*g, 0.0f, 2.0f), clamp(*b, 0.0f, 2.0f));
s.variants->colorscale = vec(std::clamp(*r, 0.0f, 2.0f), std::clamp(*g, 0.0f, 2.0f), std::clamp(*b, 0.0f, 2.0f));
propagatevslot(s.variants, 1<<VSLOT_COLOR);
}
COMMAND(texcolor, "fff");
@ -2430,9 +2432,9 @@ static void texrefract(float *k, float *r, float *g, float *b)
{
if(!defslot) return;
Slot &s = *defslot;
s.variants->refractscale = clamp(*k, 0.0f, 1.0f);
s.variants->refractscale = std::clamp(*k, 0.0f, 1.0f);
if(s.variants->refractscale > 0 && (*r > 0 || *g > 0 || *b > 0))
s.variants->refractcolor = vec(clamp(*r, 0.0f, 1.0f), clamp(*g, 0.0f, 1.0f), clamp(*b, 0.0f, 1.0f));
s.variants->refractcolor = vec(std::clamp(*r, 0.0f, 1.0f), std::clamp(*g, 0.0f, 1.0f), std::clamp(*b, 0.0f, 1.0f));
else
s.variants->refractcolor = vec(1, 1, 1);
propagatevslot(s.variants, 1<<VSLOT_REFRACT);
@ -2451,8 +2453,8 @@ static void decaldepth(float *depth, float *fade)
{
if(!defslot || defslot->type() != Slot::DECAL) return;
DecalSlot &s = *(DecalSlot *)defslot;
s.depth = clamp(*depth, 1e-3f, 1e3f);
s.fade = clamp(*fade, 0.0f, s.depth);
s.depth = std::clamp(*depth, 1e-3f, 1e3f);
s.fade = std::clamp(*fade, 0.0f, s.depth);
}
COMMAND(decaldepth, "ff");
@ -2461,13 +2463,13 @@ static void addglow(ImageData &c, ImageData &g, const vec &glowcolor)
if(g.bpp < 3)
{
readwritergbtex(c, g,
loopk(3) dst[k] = clamp(int(dst[k]) + int(src[0]*glowcolor[k]), 0, 255);
loopk(3) dst[k] = std::clamp(int(dst[k]) + int(src[0]*glowcolor[k]), 0, 255);
);
}
else
{
readwritergbtex(c, g,
loopk(3) dst[k] = clamp(int(dst[k]) + int(src[k]*glowcolor[k]), 0, 255);
loopk(3) dst[k] = std::clamp(int(dst[k]) + int(src[k]*glowcolor[k]), 0, 255);
);
}
}
@ -3089,9 +3091,9 @@ void initenvmaps()
const extentity &ent = *ents[i];
if(ent.type != ET_ENVMAP) continue;
envmap &em = envmaps.add();
em.radius = ent.attr1 ? clamp(int(ent.attr1), 0, 10000) : envmapradius;
em.size = ent.attr2 ? clamp(int(ent.attr2), 4, 9) : 0;
em.blur = ent.attr3 ? clamp(int(ent.attr3), 1, 2) : 0;
em.radius = ent.attr1 ? std::clamp(int(ent.attr1), 0, 10000) : envmapradius;
em.size = ent.attr2 ? std::clamp(int(ent.attr2), 4, 9) : 0;
em.blur = ent.attr3 ? std::clamp(int(ent.attr3), 1, 2) : 0;
em.o = ent.o;
}
}

View File

@ -461,7 +461,7 @@ template<class MDL> struct vertcommands : modelcommands<MDL, struct MDL::vertmes
defformatstring(filename, "%s/%s", MDL::dir, model);
part &mdl = MDL::loading->addpart();
if(mdl.index) mdl.disablepitch();
mdl.meshes = MDL::loading->sharemeshes(path(filename), *smooth > 0 ? cosf(clamp(*smooth, 0.0f, 180.0f)*RAD) : 2);
mdl.meshes = MDL::loading->sharemeshes(path(filename), *smooth > 0 ? cosf(std::clamp(*smooth, 0.0f, 180.0f)*RAD) : 2);
if(!mdl.meshes) conoutf("could not load %s", filename);
else mdl.initskins();
}

View File

@ -2,6 +2,8 @@
#include "world.hh"
#include <algorithm>
#include <shared/command.hh>
#include <shared/glemu.hh>
#include <shared/igame.hh>
@ -764,7 +766,7 @@ static void renderentradius(extentity &e, bool color)
float radius = e.attached->attr1;
if(radius <= 0) break;
vec dir = vec(e.o).sub(e.attached->o).normalize();
float angle = clamp(int(e.attr1), 1, 89);
float angle = std::clamp(int(e.attr1), 1, 89);
renderentattachment(e);
renderentcone(*e.attached, dir, radius, angle);
}
@ -877,7 +879,7 @@ void renderentselection(const vec &o, const vec &ray, bool entmoving)
}
gle::colorub(200,0,0);
boxs(entorient, eo, es);
boxs(entorient, eo, es, clamp(0.015f*camera1->o.dist(eo)*tan(fovy*0.5f*RAD), 0.1f, 1.0f));
boxs(entorient, eo, es, std::clamp(0.015f*camera1->o.dist(eo)*tan(fovy*0.5f*RAD), 0.1f, 1.0f));
}
if(showentradius)

View File

@ -4,6 +4,8 @@
#include <new>
#include <algorithm>
#include <sauerlib/encoding.hh>
#include <shared/command.hh>
@ -475,7 +477,7 @@ static void loadvslot(stream *f, VSlot &vs, int changed)
}
}
if(vs.changed & (1<<VSLOT_SCALE)) vs.scale = f->getlil<float>();
if(vs.changed & (1<<VSLOT_ROTATION)) vs.rotation = clamp(f->getlil<int>(), 0, 7);
if(vs.changed & (1<<VSLOT_ROTATION)) vs.rotation = std::clamp(f->getlil<int>(), 0, 7);
if(vs.changed & (1<<VSLOT_OFFSET))
{
loopk(2) vs.offset[k] = f->getlil<int>();

View File

@ -285,7 +285,8 @@ struct databuf
databuf subbuf(int sz)
{
sz = clamp(sz, 0, maxlen-len);
if (sz < 0) sz = 0;
if (sz > (maxlen - len)) sz = maxlen - len;
len += sz;
return databuf(&buf[len-sz], sz);
}

View File

@ -2,6 +2,8 @@
#include <zlib.h>
#include <algorithm>
#include "command.hh"
#include <engine/console.hh> /* conoutf */
@ -434,7 +436,7 @@ struct zipstream : stream
case SEEK_SET: pos += info->offset; break;
default: return false;
}
pos = clamp(pos, offset(info->offset), offset(info->offset + info->size));
pos = std::clamp(pos, offset(info->offset), offset(info->offset + info->size));
arch->owner = nullptr;
if(fseek(arch->data, int(pos), SEEK_SET) < 0) return false;
arch->owner = this;