Use std::clamp in various core functions

This commit is contained in:
AJR 2021-02-03 17:45:21 -05:00
parent 0c57408ed8
commit bcf647342a
4 changed files with 18 additions and 27 deletions

View File

@ -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 // 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) xscale *= src_aspect / target_aspect * (maxyscale / yscale);
if (m_keepaspect && target_aspect < src_aspect) yscale *= target_aspect / src_aspect * (maxxscale / xscale); 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 (x_is_integer) xscale = std::clamp(render_round_nearest(xscale), 1.0f, maxxscale);
if (y_is_integer) yscale = std::min(maxyscale, std::max(1.0f, render_round_nearest(yscale))); 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 // 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; int user_scale_x = target_is_portrait? m_int_scale_y : m_int_scale_x;

View File

@ -15,6 +15,7 @@
#include "rendertypes.h" #include "rendertypes.h"
#include <algorithm>
#include <cmath> #include <cmath>
@ -126,11 +127,7 @@ static inline float apply_brightness_contrast_gamma_fp(float srcval, float brigh
srcval = (srcval * contrast) + brightness - 1.0f; srcval = (srcval * contrast) + brightness - 1.0f;
/* clamp and return */ /* clamp and return */
if (srcval < 0.0f) return std::clamp(srcval, 0.0f, 1.0f);
srcval = 0.0f;
if (srcval > 1.0f)
srcval = 1.0f;
return srcval;
} }

View File

@ -383,11 +383,8 @@ public:
// write a sample to the buffer, clamping to +/- the clamp value // write a sample to the buffer, clamping to +/- the clamp value
void put_clamp(s32 index, sample_t sample, sample_t clamp = 1.0) void put_clamp(s32 index, sample_t sample, sample_t clamp = 1.0)
{ {
if (sample > clamp) assert(clamp >= sample_t(0));
sample = clamp; put(index, std::clamp(sample, -clamp, clamp));
if (sample < -clamp)
sample = -clamp;
put(index, sample);
} }
// write a sample to the buffer, converting from an integer with the given maximum // 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 // write a sample to the buffer, converting from an integer with the given maximum
void put_int_clamp(s32 index, s32 sample, s32 maxclamp) void put_int_clamp(s32 index, s32 sample, s32 maxclamp)
{ {
if (sample > maxclamp) assert(maxclamp >= 0);
sample = maxclamp; put_int(index, std::clamp(sample, -maxclamp, maxclamp), maxclamp);
else if (sample < -maxclamp)
sample = -maxclamp;
put_int(index, sample, maxclamp);
} }
// safely add a sample to the buffer // safely add a sample to the buffer

View File

@ -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_width(sdev.visible_area().width());
float const sc_height(sdev.visible_area().height()); float const sc_height(sdev.visible_area().height());
x1 = std::min(std::max(0.0f, x1), sc_width) / sc_width; x1 = std::clamp(x1, 0.0f, sc_width) / sc_width;
y1 = std::min(std::max(0.0f, y1), sc_height) / sc_height; y1 = std::clamp(y1, 0.0f, sc_height) / sc_height;
x2 = std::min(std::max(0.0f, x2), sc_width) / sc_width; x2 = std::clamp(x2, 0.0f, sc_width) / sc_width;
y2 = std::min(std::max(0.0f, y2), sc_height) / sc_height; 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); 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_width(sdev.visible_area().width());
float const sc_height(sdev.visible_area().height()); float const sc_height(sdev.visible_area().height());
x1 = std::min(std::max(0.0f, x1), sc_width) / sc_width; x1 = std::clamp(x1, 0.0f, sc_width) / sc_width;
y1 = std::min(std::max(0.0f, y1), sc_height) / sc_height; y1 = std::clamp(y1, 0.0f, sc_height) / sc_height;
x2 = std::min(std::max(0.0f, x2), sc_width) / sc_width; x2 = std::clamp(x2, 0.0f, sc_width) / sc_width;
y2 = std::min(std::max(0.0f, y2), sc_height) / sc_height; 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)); 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; float x = 0;
if (xobj.is<float>()) 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 *>()) 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"); luaL_error(L, "Error in param 1 to draw_text");
return; 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( mame_machine_manager::instance()->ui().draw_text_full(
sdev.container(), sdev.container(),
msg, msg,