1
0
mirror of https://github.com/TerryCavanagh/VVVVVV.git synced 2024-06-01 18:43:33 +02:00

Remove VVV_min/max in favor of SDL_min/max

VVV_min/max are functions that only operate on ints, and SDL_min/max are
macros that operate on any type but double-evaluate everything.

I know I more-or-less said earlier that SDL_min/max were dumb but I've
changed my mind and think it's better to use them, taking care to make
sure you don't double-evaluate, rather than trying to generate your own
litany of functions with either your own hand-rolled generation macros,
C++ templates, C11 generics, or GCC extensions (that last one you'd
technically use in a macro but it doesn't really matter), all of which
have more downsides than just not double-evaluating.

And the upside of not double-evaluating is that you're disencouraged
from having really complicated single-line min/max expressions and
encouraged to precompute the values beforehand anyway so the final
min/max is more readable. And furthermore you'll notice when you
yourself end up doing double-evaluations anyway. I removed a couple
instances of Graphics::len() being double-evaluated in this commit (as
well as cleaned up some other min/max-using code). Although the only
downside to those double-evaluations was unnecessary computation,
rather than checking the wrong result or having multiple side effects,
thankfully, it's still good to minimize double-evaluations where
possible.
This commit is contained in:
Misa 2021-12-22 16:43:31 -08:00
parent f7454baffa
commit aa7b63fa5f
7 changed files with 38 additions and 54 deletions

View File

@ -2291,7 +2291,7 @@ void editorinput(void)
SDL_strlcpy(
coord_x,
key.keybuffer.c_str(),
VVV_min(comma - key.keybuffer.c_str() + 1, sizeof(coord_x))
SDL_min((size_t) (comma - key.keybuffer.c_str() + 1), sizeof(coord_x))
);
SDL_strlcpy(coord_y, &comma[1], sizeof(coord_y));

View File

@ -567,7 +567,7 @@ void Game::loadcustomlevelstats(void)
}
// If the two arrays happen to differ in length, just go with the smallest one
for (int i = 0; i < VVV_min(customlevelnames.size(), customlevelscores.size()); i++)
for (size_t i = 0; i < SDL_min(customlevelnames.size(), customlevelscores.size()); i++)
{
CustomLevelStat stat = {customlevelnames[i], customlevelscores[i]};
customlevelstats.push_back(stat);

View File

@ -629,7 +629,7 @@ bool Graphics::next_wrap_s(
if (retval)
{
/* Like next_split_s(), don't use SDL_strlcpy() here. */
const size_t length = VVV_min(buffer_size - 1, len);
const size_t length = SDL_min(buffer_size - 1, len);
SDL_memcpy(buffer, &str[prev_start], length);
buffer[length] = '\0';
}
@ -685,7 +685,8 @@ void Graphics::bigprint( int _x, int _y, const std::string& _s, int r, int g, i
{
if (cen)
{
_x = VVV_max(160 - (int((len(_s)/ 2.0)*sc)), 0 );
const int len_ = len(_s);
_x = SDL_max(160 - (int((len_/ 2.0)*sc)), 0 );
}
return do_print(_x, _y, _s, r, g, b, 255, sc);
@ -698,7 +699,8 @@ void Graphics::bigbprint(int x, int y, const std::string& s, int r, int g, int b
bigprint(x, y - sc, s, 0, 0, 0, cen, sc);
if (cen)
{
int x_cen = VVV_max(160 - (len(s) / 2) * sc, 0);
const int len_ = len(s);
int x_cen = SDL_max(160 - (len_ / 2) * sc, 0);
bigprint(x_cen - sc, y, s, 0, 0, 0, false, sc);
bigprint(x_cen + sc, y, s, 0, 0, 0, false, sc);
}
@ -1205,13 +1207,13 @@ void Graphics::cutscenebarstimer(void)
if (showcutscenebars)
{
cutscenebarspos += 25;
cutscenebarspos = VVV_min(cutscenebarspos, 361);
cutscenebarspos = SDL_min(cutscenebarspos, 361);
}
else if (cutscenebarspos > 0)
{
//disappearing
cutscenebarspos -= 25;
cutscenebarspos = VVV_max(cutscenebarspos, 0);
cutscenebarspos = SDL_max(cutscenebarspos, 0);
}
}
@ -1645,10 +1647,10 @@ bool Graphics::Hitest(SDL_Surface* surface1, point p1, SDL_Surface* surface2, po
if(intersection)
{
int r3_left = VVV_max(r1_left, r2_left);
int r3_top = VVV_min(r1_top, r2_top);
int r3_right = VVV_min(r1_right, r2_right);
int r3_bottom= VVV_max(r1_bottom, r2_bottom);
int r3_left = SDL_max(r1_left, r2_left);
int r3_top = SDL_min(r1_top, r2_top);
int r3_right = SDL_min(r1_right, r2_right);
int r3_bottom= SDL_max(r1_bottom, r2_bottom);
//for every pixel inside rectangle
for(int x = r3_left; x < r3_right; x++)
@ -3199,13 +3201,15 @@ void Graphics::renderfixedpost(void)
void Graphics::bigrprint(int x, int y, const std::string& t, int r, int g, int b, bool cen, float sc)
{
const int len_ = len(t);
x = x / (sc);
x -= (len(t));
x -= len_;
if (cen)
{
x = VVV_max(160 - (int((len(t)/ 2.0)*sc)), 0 );
x = SDL_max(160 - (int((len_/ 2.0)*sc)), 0 );
}
else
{
@ -3219,11 +3223,12 @@ void Graphics::bigbrprint(int x, int y, const std::string& s, int r, int g, int
{
if (!notextoutline)
{
int x_o = x / sc - len(s);
const int len_ = len(s);
int x_o = x / sc - len_;
bigrprint(x, y - sc, s, 0, 0, 0, cen, sc);
if (cen)
{
x_o = VVV_max(160 - (len(s) / 2) * sc, 0);
x_o = SDL_max(160 - (len_ / 2) * sc, 0);
bigprint(x_o - sc, y, s, 0, 0, 0, false, sc);
bigprint(x_o + sc, y, s, 0, 0, 0, false, sc);
}

View File

@ -332,22 +332,25 @@ SDL_Surface* ApplyFilter( SDL_Surface* _src )
Uint8 green = (pixel & _src->format->Gmask) >> 8;
Uint8 blue = (pixel & _src->format->Bmask) >> 0;
Uint32 pixelOffset = ReadPixel(_src, VVV_min(x+redOffset, 319), sampley) ;
Uint32 pixelOffset = ReadPixel(_src, SDL_min(x+redOffset, 319), sampley) ;
Uint8 red = (pixelOffset & _src->format->Rmask) >> 16 ;
double mult;
if(isscrolling && sampley > 220 && ((rand() %10) < 4))
{
red = VVV_min(int(red+(fRandom() * 0.6) * 254) , 255);
green = VVV_min(int(green+(fRandom() * 0.6) * 254) , 255);
blue = VVV_min(int(blue+(fRandom() * 0.6) * 254) , 255);
mult = 0.6;
}
else
{
red = VVV_min(int(red+(fRandom() * 0.2) * 254) , 255);
green = VVV_min(int(green+(fRandom() * 0.2) * 254) , 255);
blue = VVV_min(int(blue+(fRandom() * 0.2) * 254) , 255);
mult = 0.2;
}
red += fRandom() * mult * 254;
red = SDL_min(red, 255);
green += fRandom() * mult * 254;
green = SDL_min(green, 255);
blue += fRandom() * mult * 254;
blue = SDL_min(blue, 255);
if(y % 2 == 0)
{
@ -359,9 +362,9 @@ SDL_Surface* ApplyFilter( SDL_Surface* _src )
int distX = static_cast<int>((SDL_abs (160.0f -x ) / 160.0f) *16);
int distY = static_cast<int>((SDL_abs (120.0f -y ) / 120.0f)*32);
red = VVV_max(red - ( distX +distY), 0);
green = VVV_max(green - ( distX +distY), 0);
blue = VVV_max(blue - ( distX +distY), 0);
red = SDL_max(red - ( distX +distY), 0);
green = SDL_max(green - ( distX +distY), 0);
blue = SDL_max(blue - ( distX +distY), 0);
Uint32 finalPixel = ((red<<16) + (green<<8) + (blue<<0)) | (pixel &_src->format->Amask);
DrawPixel(_ret,x,y, finalPixel);

View File

@ -24,28 +24,4 @@ struct point
int y;
};
inline int VVV_min(const int a, const int b)
{
if (a < b)
{
return a;
}
else
{
return b;
}
}
inline int VVV_max(const int a, const int b)
{
if (a > b)
{
return a;
}
else
{
return b;
}
}
#endif /* MATHGAME_H */

View File

@ -451,7 +451,7 @@ static void menurender(void)
graphics.Print( 40, 30, "the following patrons", tr, tg, tb, true);
int startidx = game.current_credits_list_index;
int endidx = VVV_min(startidx + 9, (int)SDL_arraysize(Credits::superpatrons));
int endidx = SDL_min(startidx + 9, (int)SDL_arraysize(Credits::superpatrons));
int xofs = 80 - 16;
int yofs = 40 + 20;
@ -469,7 +469,7 @@ static void menurender(void)
graphics.Print( -1, 20, "and also by", tr, tg, tb, true);
int startidx = game.current_credits_list_index;
int endidx = VVV_min(startidx + 14, (int)SDL_arraysize(Credits::patrons));
int endidx = SDL_min(startidx + 14, (int)SDL_arraysize(Credits::patrons));
int maxheight = 10 * 14;
int totalheight = (endidx - startidx) * 10;
@ -490,7 +490,7 @@ static void menurender(void)
graphics.Print( 40, 30, "GitHub from", tr, tg, tb, true);
int startidx = game.current_credits_list_index;
int endidx = VVV_min(startidx + 9, (int)SDL_arraysize(Credits::githubfriends));
int endidx = SDL_min(startidx + 9, (int)SDL_arraysize(Credits::githubfriends));
int maxheight = 14 * 9;
int totalheight = (endidx - startidx) * 14;

View File

@ -122,7 +122,7 @@ bool next_split_s(
/* Using SDL_strlcpy() here results in calling SDL_strlen() */
/* on the whole string, which results in a visible freeze */
/* if it's a very large string */
const size_t length = VVV_min(buffer_size - 1, len);
const size_t length = SDL_min(buffer_size - 1, len);
SDL_memcpy(buffer, &str[prev_start], length);
buffer[length] = '\0';
}
@ -344,6 +344,6 @@ void _VVV_between(
SDL_strlcpy(
middle,
&original[left_length],
VVV_min(middle_length + 1, middle_size)
SDL_min(middle_length + 1, middle_size)
);
}