summaryrefslogtreecommitdiffstatshomepage
path: root/src
diff options
context:
space:
mode:
author AJR <ajrhacker@users.noreply.github.com>2021-02-03 17:45:21 -0500
committer AJR <ajrhacker@users.noreply.github.com>2021-02-03 17:45:21 -0500
commitbcf647342a9643fedfba6d9ebc3be56bcc4c1cf1 (patch)
treefb2c32cc05cc43803f19ba8b0a2fc661ef0d5cd0 /src
parent0c57408ed81194046b56318dd6fd8583f8de44b3 (diff)
Use std::clamp in various core functions
Diffstat (limited to 'src')
-rw-r--r--src/emu/render.cpp4
-rw-r--r--src/emu/rendutil.h7
-rw-r--r--src/emu/sound.h14
-rw-r--r--src/frontend/mame/luaengine.cpp20
4 files changed, 18 insertions, 27 deletions
diff --git a/src/emu/render.cpp b/src/emu/render.cpp
index 3ef1fb32fec..5e2c023bf4e 100644
--- a/src/emu/render.cpp
+++ b/src/emu/render.cpp
@@ -1216,8 +1216,8 @@ void render_target::compute_visible_area(s32 target_width, s32 target_height, fl
// now apply desired scale mode and aspect correction
if (m_keepaspect && target_aspect > src_aspect) xscale *= src_aspect / target_aspect * (maxyscale / yscale);
if (m_keepaspect && target_aspect < src_aspect) yscale *= target_aspect / src_aspect * (maxxscale / xscale);
- if (x_is_integer) xscale = std::min(maxxscale, std::max(1.0f, render_round_nearest(xscale)));
- if (y_is_integer) yscale = std::min(maxyscale, std::max(1.0f, render_round_nearest(yscale)));
+ if (x_is_integer) xscale = std::clamp(render_round_nearest(xscale), 1.0f, maxxscale);
+ if (y_is_integer) yscale = std::clamp(render_round_nearest(yscale), 1.0f, maxyscale);
// check if we have user defined scale factors, if so use them instead
int user_scale_x = target_is_portrait? m_int_scale_y : m_int_scale_x;
diff --git a/src/emu/rendutil.h b/src/emu/rendutil.h
index c2d18d43f35..5f1a401b592 100644
--- a/src/emu/rendutil.h
+++ b/src/emu/rendutil.h
@@ -15,6 +15,7 @@
#include "rendertypes.h"
+#include <algorithm>
#include <cmath>
@@ -126,11 +127,7 @@ static inline float apply_brightness_contrast_gamma_fp(float srcval, float brigh
srcval = (srcval * contrast) + brightness - 1.0f;
/* clamp and return */
- if (srcval < 0.0f)
- srcval = 0.0f;
- if (srcval > 1.0f)
- srcval = 1.0f;
- return srcval;
+ return std::clamp(srcval, 0.0f, 1.0f);
}
diff --git a/src/emu/sound.h b/src/emu/sound.h
index de4d2f896a0..3aefc4bef3f 100644
--- a/src/emu/sound.h
+++ b/src/emu/sound.h
@@ -383,11 +383,8 @@ public:
// write a sample to the buffer, clamping to +/- the clamp value
void put_clamp(s32 index, sample_t sample, sample_t clamp = 1.0)
{
- if (sample > clamp)
- sample = clamp;
- if (sample < -clamp)
- sample = -clamp;
- put(index, sample);
+ assert(clamp >= sample_t(0));
+ put(index, std::clamp(sample, -clamp, clamp));
}
// write a sample to the buffer, converting from an integer with the given maximum
@@ -399,11 +396,8 @@ public:
// write a sample to the buffer, converting from an integer with the given maximum
void put_int_clamp(s32 index, s32 sample, s32 maxclamp)
{
- if (sample > maxclamp)
- sample = maxclamp;
- else if (sample < -maxclamp)
- sample = -maxclamp;
- put_int(index, sample, maxclamp);
+ assert(maxclamp >= 0);
+ put_int(index, std::clamp(sample, -maxclamp, maxclamp), maxclamp);
}
// safely add a sample to the buffer
diff --git a/src/frontend/mame/luaengine.cpp b/src/frontend/mame/luaengine.cpp
index d8ec8c746db..c7800f70829 100644
--- a/src/frontend/mame/luaengine.cpp
+++ b/src/frontend/mame/luaengine.cpp
@@ -63,10 +63,10 @@ void do_draw_box(screen_device &sdev, float x1, float y1, float x2, float y2, ui
{
float const sc_width(sdev.visible_area().width());
float const sc_height(sdev.visible_area().height());
- x1 = std::min(std::max(0.0f, x1), sc_width) / sc_width;
- y1 = std::min(std::max(0.0f, y1), sc_height) / sc_height;
- x2 = std::min(std::max(0.0f, x2), sc_width) / sc_width;
- y2 = std::min(std::max(0.0f, y2), sc_height) / sc_height;
+ x1 = std::clamp(x1, 0.0f, sc_width) / sc_width;
+ y1 = std::clamp(y1, 0.0f, sc_height) / sc_height;
+ x2 = std::clamp(x2, 0.0f, sc_width) / sc_width;
+ y2 = std::clamp(y2, 0.0f, sc_height) / sc_height;
mame_machine_manager::instance()->ui().draw_outlined_box(sdev.container(), x1, y1, x2, y2, fgcolor, bgcolor);
}
@@ -74,10 +74,10 @@ void do_draw_line(screen_device &sdev, float x1, float y1, float x2, float y2, u
{
float const sc_width(sdev.visible_area().width());
float const sc_height(sdev.visible_area().height());
- x1 = std::min(std::max(0.0f, x1), sc_width) / sc_width;
- y1 = std::min(std::max(0.0f, y1), sc_height) / sc_height;
- x2 = std::min(std::max(0.0f, x2), sc_width) / sc_width;
- y2 = std::min(std::max(0.0f, y2), sc_height) / sc_height;
+ x1 = std::clamp(x1, 0.0f, sc_width) / sc_width;
+ y1 = std::clamp(y1, 0.0f, sc_height) / sc_height;
+ x2 = std::clamp(x2, 0.0f, sc_width) / sc_width;
+ y2 = std::clamp(y2, 0.0f, sc_height) / sc_height;
sdev.container().add_line(x1, y1, x2, y2, UI_LINE_WIDTH, rgb_t(color), PRIMFLAG_BLENDMODE(BLENDMODE_ALPHA));
}
@@ -89,7 +89,7 @@ void do_draw_text(lua_State *L, screen_device &sdev, sol::object &xobj, float y,
float x = 0;
if (xobj.is<float>())
{
- x = std::min(std::max(0.0f, xobj.as<float>()), sc_width) / sc_width;
+ x = std::clamp(xobj.as<float>(), 0.0f, sc_width) / sc_width;
}
else if (xobj.is<char const *>())
{
@@ -106,7 +106,7 @@ void do_draw_text(lua_State *L, screen_device &sdev, sol::object &xobj, float y,
luaL_error(L, "Error in param 1 to draw_text");
return;
}
- y = std::min(std::max(0.0f, y), sc_height) / sc_height;
+ y = std::clamp(y, 0.0f, sc_height) / sc_height;
mame_machine_manager::instance()->ui().draw_text_full(
sdev.container(),
msg,