Skip to content

Commit 3a36109

Browse files
committed
Replace min/max/clamp with versions from STL
1 parent 92b4414 commit 3a36109

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+377
-317
lines changed

src/Core/ByteStream.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#include <stdlib.h>
77
#include <stdint.h>
88

9+
#include <algorithm>
10+
911
namespace Vortex {
1012

1113
// ================================================================================================
@@ -44,7 +46,7 @@ void WriteStream::write(const void* in, int bytes)
4446
}
4547
else if(!is_external_buffer_)
4648
{
47-
capacity_ = max(capacity_ << 1, newSize);
49+
capacity_ = std::max(capacity_ << 1, newSize);
4850
buffer_ = (uint8_t*)realloc(buffer_, capacity_);
4951
memcpy(buffer_ + current_size_, in, bytes);
5052
current_size_ = newSize;

src/Core/Canvas.cpp

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
#include <math.h>
66

7+
#include <algorithm>
8+
79
namespace Vortex {
810

911
// ================================================================================================
@@ -103,13 +105,13 @@ struct GetRoundRectDist : public DistanceFunc
103105

104106
float Get(float px, float py) const
105107
{
106-
float x = min(max(px, x1 + r), x2 - r);
107-
float y = min(max(py, y1 + r), y2 - r);
108+
float x = std::min(std::max(px, x1 + r), x2 - r);
109+
float y = std::min(std::max(py, y1 + r), y2 - r);
108110
if(x == px && y == py)
109111
{
110-
float dx = min(x - x1, x2 - x);
111-
float dy = min(y - y1, y2 - y);
112-
return -min(dx, dy);
112+
float dx = std::min(x - x1, x2 - x);
113+
float dy = std::min(y - y1, y2 - y);
114+
return -std::min(dx, dy);
113115
}
114116
else
115117
{
@@ -143,7 +145,7 @@ struct GetPolyDist : public DistanceFunc
143145
for(int i = 0, j = count - 1; i < count; j = i, ++i)
144146
{
145147
GetLineDist ld(x[i], y[i], x[j], y[j], 0);
146-
d = min(d, ld.Get(px, py));
148+
d = std::min(d, ld.Get(px, py));
147149
}
148150
return InsidePoly(px, py) ? -d : d;
149151
}
@@ -180,13 +182,13 @@ void Canvas::Data::draw(float* buf, int w, int h, const areaf& area, DistanceFun
180182
if(blendMode == Canvas::BM_ALPHA) blendfunc = BlendAlpha;
181183
if(blendMode == Canvas::BM_ADD) blendfunc = BlendAdd;
182184

183-
int x1 = max(mask.l, (int)(area.l - outerGlow - 1 + 0.5f));
184-
int y1 = max(mask.t, (int)(area.t - outerGlow - 1 + 0.5f));
185-
int x2 = min(mask.r, (int)(area.r + outerGlow + 1 + 0.5f));
186-
int y2 = min(mask.b, (int)(area.b + outerGlow + 1 + 0.5f));
185+
int x1 = std::max(mask.l, (int)(area.l - outerGlow - 1 + 0.5f);
186+
int y1 = std::max(mask.t, (int)(area.t - outerGlow - 1 + 0.5f);
187+
int x2 = std::min(mask.r, (int)(area.r + outerGlow + 1 + 0.5f);
188+
int y2 = std::min(mask.b, (int)(area.b + outerGlow + 1 + 0.5f);
187189

188-
float rh = 1.f / max(1, y2 - y1);
189-
float rw = 1.f / max(1, x2 - x1);
190+
float rh = 1.f / std::max(1, y2 - y1);
191+
float rw = 1.f / std::max(1, x2 - x1);
190192
float ig = 1.f / (innerGlow + 1);
191193
float og = 1.f / (outerGlow + 1);
192194

@@ -206,12 +208,12 @@ void Canvas::Data::draw(float* buf, int w, int h, const areaf& area, DistanceFun
206208
if(outline && dist < -0.5f)
207209
{
208210
float a = 1 - ig * ((-dist - lineWidth) + 0.5f);
209-
if(a > 0) blendfunc(*dst, src, min(a, 1.f));
211+
if(a > 0) blendfunc(*dst, src, std::min(a, 1.f));
210212
}
211213
else
212214
{
213215
float a = 1 - og * (dist + 0.5f);
214-
if(a > 0) blendfunc(*dst, src, min(a, 1.f));
216+
if(a > 0) blendfunc(*dst, src, std::min(a, 1.f));
215217
}
216218
}
217219
}
@@ -248,7 +250,7 @@ Canvas::Canvas(const Canvas& other)
248250

249251
void Canvas::setMask(int l, int t, int r, int b)
250252
{
251-
data_->mask = {max(0, l), max(0, t), min(r, canvas_width_), min(r, canvas_height_)};
253+
data_->mask = {std::max(0, l), std::max(0, t), std::min(r, canvas_width_), std::min(r, canvas_height_)};
252254
}
253255

254256
void Canvas::setOutline(float size)
@@ -312,8 +314,8 @@ void Canvas::clear(float l)
312314
void Canvas::line(float x1, float y1, float x2, float y2, float width)
313315
{
314316
float r = width * 0.5f;
315-
float xmin = min(x1, x2), xmax = max(x1, x2);
316-
float ymin = min(y1, y2), ymax = max(y1, y2);
317+
float xmin = std::min(x1, x2), xmax = std::max(x1, x2);
318+
float ymin = std::min(y1, y2), ymax = std::max(y1, y2);
317319
GetLineDist func(x1, y1, x2, y2, r);
318320
data_->draw(canvas_data_, canvas_width_, canvas_height_, {xmin - r, ymin - r, xmax + r, ymax + r}, &func);
319321
}
@@ -326,9 +328,9 @@ void Canvas::circle(float x, float y, float r)
326328

327329
void Canvas::box(float x1, float y1, float x2, float y2, float radius)
328330
{
329-
float xl = min(x1, x2), xr = max(x1, x2);
330-
float yt = min(y1, y2), yb = max(y1, y2);
331-
float r = min(min(xr - xl, yb - yt)*0.5f, max(0.f, radius));
331+
float xl = std::min(x1, x2), xr = std::max(x1, x2);
332+
float yt = std::min(y1, y2), yb = std::max(y1, y2);
333+
float r = std::min(std::min(xr - xl, yb - yt)*0.5f, std::max(0.f, radius));
332334
GetRoundRectDist func(xl, yt, xr, yb, r);
333335
data_->draw(canvas_data_, canvas_width_, canvas_height_, {xl, yt, xr, yb}, &func);
334336
}
@@ -351,7 +353,7 @@ Texture Canvas::createTexture(bool mipmap) const
351353
for(int i = 0; i < canvas_width_ * canvas_height_ * 4; ++i)
352354
{
353355
int v = (int)(canvas_data_[i] * 255.f + 0.5f);
354-
dst[i] = min(max(v, 0), 255);
356+
dst[i] = std::min(std::max(v, 0), 255);
355357
}
356358
Texture result(canvas_width_, canvas_height_, dst, mipmap);
357359
free(dst);

src/Core/Draw.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#include <Core/Utils.h>
44
#include <Core/Renderer.h>
55

6+
#include <algorithm>
7+
68
namespace Vortex {
79
namespace {
810

@@ -82,7 +84,7 @@ static void TB_setVerts(const TileBar& bar, int* vp, float* vt, recti r, areaf u
8284
}
8385

8486
// Texture coordinates: left-to-right [s,t,u,v] top-to-bottom [w,x].
85-
float rw = 1.0f / max(bar.texture.width(), 1);
87+
float rw = 1.0f / std::max(bar.texture.width(), 1);
8688
float s = uvs.l, t = uvs.l + (b - a) * rw, u = uvs.r - (d - c) * rw, v = uvs.r;
8789
float w = uvs.t, x = uvs.b;
8890

@@ -172,8 +174,8 @@ static void TR_setVerts(const TileRect& rect, int* vp, float* vt, recti r, areaf
172174

173175
// Texture coordinates: left-to-right [s,t,u,v] top-to-bottom [w,x,y,z].
174176
vec2i size = rect.texture.size();
175-
float rw = 1.f / max(size.x, 1);
176-
float rh = 1.f / max(size.y, 1);
177+
float rw = 1.f / std::max(size.x, 1);
178+
float rh = 1.f / std::max(size.y, 1);
177179
float s = uvs.l, t = uvs.l + (b - a) * rw, u = uvs.r - (d - c) * rw, v = uvs.r;
178180
float w = uvs.t, x = uvs.t + (f - e) * rh, y = uvs.b - (h - g) * rh, z = uvs.b;
179181

@@ -242,8 +244,8 @@ static void TR2_setVerts(const TileRect2& rect, int* vp, float* vt, recti r, int
242244

243245
// Texture coordinates: left-to-right [s,t,u,v] top-to-bottom [w,x,y,z].
244246
vec2i size = rect.texture.size();
245-
float rw = 1.f / max(size.x, 1);
246-
float rh = 1.f / max(size.y, 1);
247+
float rw = 1.f / std::max(size.x, 1);
248+
float rh = 1.f / std::max(size.y, 1);
247249
float s = 0, t = (b - a) * rw, u = 0.5f - (d - c) * rw, v = 0.5f;
248250
float w = 0, x = (f - e) * rh, y = 1.0f - (h - g) * rh, z = 1.0f;
249251

src/Core/FontData.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#include FT_FREETYPE_H
88
#include FT_GLYPH_H
99

10+
#include <algorithm>
11+
1012
#include <System/OpenGL.h>
1113

1214
namespace Vortex {
@@ -217,7 +219,7 @@ static Glyph* PutGlyphInCache(GlyphCache* cache, FT_GlyphSlot slot)
217219
// Copy the glyph pixels to the cache texture.
218220
uint8_t* pixels = CopyGlyphBitmap(bitmapW, bitmapH, bitmap);
219221
cache->tex->modify(glyph->box.x, glyph->box.y, bitmapW, bitmapH, pixels);
220-
cache->shelfH = max(cache->shelfH, bitmapH);
222+
cache->shelfH = std::max(cache->shelfH, bitmapH);
221223
free(pixels);
222224

223225
// Set the glyph uvs.

src/Core/FontManager.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#include FT_LCD_FILTER_H
1212
#include FT_GLYPH_H
1313

14+
#include <algorithm>
15+
1416
namespace Vortex {
1517

1618
namespace {
@@ -252,7 +254,7 @@ void FontManager::startFrame(float dt)
252254

253255
const Glyph& FontManager::getPlaceholderGlyph(int size)
254256
{
255-
int i = max(0, min(size / 8, 7));
257+
int i = std::max(0, std::min(size / 8, 7));
256258
return FM->placeholderGlyphs[i];
257259
}
258260

src/Core/Gui.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
#include <System/Debug.h>
1818

19+
#include <algorithm>
20+
1921
namespace Vortex {
2022
namespace {
2123

@@ -91,7 +93,7 @@ static void handleTooltip()
9193
{
9294
TextStyle style;
9395

94-
int alpha = clamp((int)(GUI->tooltipTimer * 1000 - 1000), 0, 255);
96+
int alpha = std::clamp((int)(GUI->tooltipTimer * 1000 - 1000, 0, 255);
9597

9698
style.textColor = Color32(0, alpha);
9799
style.shadowColor = Colors::blank;
@@ -114,8 +116,8 @@ static void handleTooltip()
114116
}
115117
}
116118

117-
pos.x = clamp(pos.x, 4, GUI->viewSize.x - textSize.x - 4);
118-
pos.y = clamp(pos.y, 4, GUI->viewSize.y - textSize.y - 4);
119+
pos.x = std::clamp(pos.x, 4, GUI->viewSize.x - textSize.x - 4);
120+
pos.y = std::clamp(pos.y, 4, GUI->viewSize.y - textSize.y - 4);
119121

120122
recti textBox = {pos.x, pos.y, textSize.x, textSize.y};
121123
recti box = Expand(textBox, 3);

src/Core/GuiContext.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#include <Core/MapUtils.h>
55
#include <Core/VectorUtils.h>
66

7+
#include <algorithm>
8+
79
namespace Vortex {
810

911
GuiContext::~GuiContext()
@@ -45,8 +47,8 @@ void GuiContextImpl::tick(recti view, float deltaTime, InputEvents& events)
4547
{
4648
view_rect_ = view;
4749

48-
view_rect_.w = max(view_rect_.w, 0);
49-
view_rect_.h = max(view_rect_.h, 0);
50+
view_rect_.w = std::max(view_rect_.w, 0);
51+
view_rect_.h = std::max(view_rect_.h, 0);
5052

5153
delta_time_ = deltaTime;
5254
input_events_ = &events;

src/Core/GuiDialog.cpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include <Core/GuiContext.h>
66
#include <Core/GuiWidget.h>
77

8+
#include <algorithm>
9+
810
namespace Vortex {
911

1012
#define MY_GUI ((GuiContextImpl*)gui_)
@@ -213,14 +215,14 @@ void DialogData::ClampRect()
213215
if(current_action_ && current_action_->type >= ACT_RESIZE)
214216
{
215217
auto a = (ResizeAction*)current_action_;
216-
if(a->dirH < 0) rect_.w = min(rect_.w, a->anchor.x - bounds.x);
217-
if(a->dirH > 0) rect_.w = min(rect_.w, bounds.x + bounds.w - a->anchor.x);
218-
if(a->dirV < 0) rect_.h = min(rect_.h, a->anchor.y - bounds.y);
219-
if(a->dirV > 0) rect_.h = min(rect_.h, bounds.y + bounds.h - a->anchor.y);
218+
if(a->dirH < 0) rect_.w = std::min(rect_.w, a->anchor.x - bounds.x);
219+
if(a->dirH > 0) rect_.w = std::min(rect_.w, bounds.x + bounds.w - a->anchor.x);
220+
if(a->dirV < 0) rect_.h = std::min(rect_.h, a->anchor.y - bounds.y);
221+
if(a->dirV > 0) rect_.h = std::min(rect_.h, bounds.y + bounds.h - a->anchor.y);
220222
}
221223

222-
rect_.w = max(min_size_.x, min(max_size_.x, min(bounds.w, rect_.w)));
223-
rect_.h = max(min_size_.y, min(max_size_.y, min(bounds.h, rect_.h)));
224+
rect_.w = std::max(min_size_.x, std::min(max_size_.x, std::min(bounds.w, rect_.w)));
225+
rect_.h = std::max(min_size_.y, std::min(max_size_.y, std::min(bounds.h, rect_.h)));
224226

225227
if(current_action_ && current_action_->type >= ACT_RESIZE)
226228
{
@@ -230,8 +232,8 @@ void DialogData::ClampRect()
230232
}
231233

232234
int marginH = minimized_state_ ? (FRAME_PADDING * -2) : rect_.h;
233-
rect_.x = max(min(rect_.x, bounds.x + bounds.w - rect_.w), bounds.x);
234-
rect_.y = max(min(rect_.y, bounds.y + bounds.h - marginH), bounds.y);
235+
rect_.x = std::max(std::min(rect_.x, bounds.x + bounds.w - rect_.w), bounds.x);
236+
rect_.y = std::max(std::min(rect_.y, bounds.y + bounds.h - marginH), bounds.y);
235237
}
236238

237239
void DialogData::arrange()
@@ -528,22 +530,22 @@ void GuiDialog::setHeight(int h)
528530

529531
void GuiDialog::setMinimumWidth(int w)
530532
{
531-
DATA->min_size_.x = max(0, w);
533+
DATA->min_size_.x = std::max(0, w);
532534
}
533535

534536
void GuiDialog::setMinimumHeight(int h)
535537
{
536-
DATA->min_size_.y = max(0, h);
538+
DATA->min_size_.y = std::max(0, h);
537539
}
538540

539541
void GuiDialog::setMaximumWidth(int w)
540542
{
541-
DATA->max_size_.x = max(0, w);
543+
DATA->max_size_.x = std::max(0, w);
542544
}
543545

544546
void GuiDialog::setMaximumHeight(int h)
545547
{
546-
DATA->max_size_.y = max(0, h);
548+
DATA->max_size_.y = std::max(0, h);
547549
}
548550

549551
void GuiDialog::setCloseable(bool enable)

src/Core/Renderer.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#include <Core/Draw.h>
77
#include <Core/Canvas.h>
88

9+
#include <algorithm>
10+
911
#include <System/Debug.h>
1012
#include <System/OpenGL.h>
1113

@@ -206,7 +208,7 @@ void Renderer::pushScissorRect(const recti& r)
206208
void Renderer::pushScissorRect(int x, int y, int w, int h)
207209
{
208210
auto& stack = RI->scissorStack;
209-
w = max(w, 0), h = max(h, 0);
211+
w = std::max(w, 0), h = std::max(h, 0);
210212
if(stack.empty())
211213
{
212214
// The new scissor region is the first scissor region, use it as-is.
@@ -217,10 +219,10 @@ void Renderer::pushScissorRect(int x, int y, int w, int h)
217219
{
218220
// Calculate the intersection of the current and new scissor region.
219221
recti last = stack.back();
220-
int r = min(last.x + last.w, x + w);
221-
int b = min(last.y + last.h, y + h);
222-
x = max(last.x, x), w = max(0, r - x);
223-
y = max(last.y, y), h = max(0, b - y);
222+
int r = std::min(last.x + last.w, x + w);
223+
int b = std::min(last.y + last.h, y + h);
224+
x = std::max(last.x, x), w = std::max(0, r - x);
225+
y = std::max(last.y, y), h = std::max(0, b - y);
224226
stack.push_back({x, y, w, h});
225227
}
226228

0 commit comments

Comments
 (0)