Refactor drawing rectangles too

This commit is contained in:
mniip 2023-02-20 21:19:10 +01:00
parent ebf21c0e0e
commit 913ba12f29
3 changed files with 49 additions and 62 deletions

View File

@ -119,3 +119,46 @@ void RasterizeEllipseRows(Vec2<float> radiusSquared, F f)
if (y) f(xLim, -y);
});
}
// Call f for every point on the boundary of the indicated rectangle (so that
// TopLeft and BottomRight are both corners).
template<typename F>
void RasterizeRect(Rect<int> rect, F f)
{
for (int x = rect.TopLeft.X; x <= rect.BottomRight.X; x++)
f(Vec2<int>(x, rect.TopLeft.Y));
if (rect.TopLeft.Y != rect.BottomRight.Y)
for (int x = rect.TopLeft.X; x <= rect.BottomRight.X; x++)
f(Vec2<int>(x, rect.BottomRight.Y));
// corners already drawn
for (int y = rect.TopLeft.Y + 1; y <= rect.BottomRight.Y - 1; y++)
f(Vec2<int>(rect.TopLeft.X, y));
if (rect.TopLeft.X != rect.BottomRight.X)
for (int y = rect.TopLeft.Y + 1; y <= rect.BottomRight.Y - 1; y++)
f(Vec2<int>(rect.BottomRight.X, y));
}
// Call f for every point on the dotted boundary of the indicated rectangle.
template<typename F>
void RasterizeDottedRect(Rect<int> rect, F f)
{
for (int x = rect.TopLeft.X; x <= rect.BottomRight.X; x += 2)
f(Vec2<int>(x, rect.TopLeft.Y));
int bottomOff = (rect.BottomRight.Y - rect.TopLeft.Y) % 2;
if (rect.TopLeft.Y != rect.BottomRight.Y)
for (int x = rect.TopLeft.X + bottomOff; x <= rect.BottomRight.X; x += 2)
f(Vec2<int>(x, rect.BottomRight.Y));
// corners already drawn
for (int y = rect.TopLeft.Y + 1 + 1; y <= rect.BottomRight.Y - 1; y += 2)
f(Vec2<int>(rect.TopLeft.X, y));
int leftOff = (rect.BottomRight.X - rect.TopLeft.X + 1) % 2;
if (rect.TopLeft.X != rect.BottomRight.X)
for (int y = rect.TopLeft.Y + 1 + leftOff; y <= rect.BottomRight.Y - 1; y += 2)
f(Vec2<int>(rect.BottomRight.X, y));
}

View File

@ -180,34 +180,8 @@ void PIXELMETHODS_CLASS::xor_line(int x1, int y1, int x2, int y2)
void PIXELMETHODS_CLASS::xor_rect(int x, int y, int w, int h)
{
int i;
for (i=0; i<w; i+=2)
{
xor_pixel(x+i, y);
}
if (h != 1)
{
if (h%2 == 1) i = 2;
else i = 1;
for (; i<w; i+=2)
{
xor_pixel(x+i, y+h-1);
}
}
for (i=2; i<h; i+=2)
{
xor_pixel(x, y+i);
}
if (w != 1)
{
if (w%2 == 1) i = 2;
else i = 1;
for (; i<h-1; i+=2)
{
xor_pixel(x+w-1, y+i);
}
}
RasterizeDottedRect(RectSized(Vec2<int>(x, y), Vec2<int>(w, h)),
[this](Vec2<int> p) { xor_pixel(p.X, p.Y); });
}
void PIXELMETHODS_CLASS::xor_bitmap(unsigned char * bitmap, int x, int y, int w, int h)
@ -230,19 +204,8 @@ void PIXELMETHODS_CLASS::draw_line(int x1, int y1, int x2, int y2, int r, int g,
void PIXELMETHODS_CLASS::drawrect(int x, int y, int w, int h, int r, int g, int b, int a)
{
int i;
w--;
h--;
for (i=0; i<=w; i++)
{
blendpixel(x+i, y, r, g, b, a);
blendpixel(x+i, y+h, r, g, b, a);
}
for (i=1; i<h; i++)
{
blendpixel(x, y+i, r, g, b, a);
blendpixel(x+w, y+i, r, g, b, a);
}
RasterizeRect(RectSized(Vec2<int>(x, y), Vec2<int>(w, h)),
[this, r, g, b, a](Vec2<int> p) { blendpixel(p.X, p.Y, r, g, b, a); });
}
void PIXELMETHODS_CLASS::fillrect(int x, int y, int w, int h, int r, int g, int b, int a)

View File

@ -805,29 +805,10 @@ void Simulation::SetEdgeMode(int newEdgeMode)
{
case 0:
case 2:
for(int i = 0; i<XCELLS; i++)
{
bmap[0][i] = 0;
bmap[YCELLS-1][i] = 0;
}
for(int i = 1; i<(YCELLS-1); i++)
{
bmap[i][0] = 0;
bmap[i][XCELLS-1] = 0;
}
RasterizeRect(CELLS.OriginRect(), [this](Vec2<int> p) { bmap[p.Y][p.X] = 0; });
break;
case 1:
int i;
for(i=0; i<XCELLS; i++)
{
bmap[0][i] = WL_WALL;
bmap[YCELLS-1][i] = WL_WALL;
}
for(i=1; i<(YCELLS-1); i++)
{
bmap[i][0] = WL_WALL;
bmap[i][XCELLS-1] = WL_WALL;
}
RasterizeRect(CELLS.OriginRect(), [this](Vec2<int> p) { bmap[p.Y][p.X] = WL_WALL; });
break;
default:
SetEdgeMode(0);