mirror of https://github.com/OpenTTD/OpenTTD
(svn r25118) -Codechange: Deduplicate basic line clipping code. (fonsinchen)
parent
5046922f61
commit
166513bbd4
55
src/gfx.cpp
55
src/gfx.cpp
|
@ -191,6 +191,32 @@ void GfxFillRect(int left, int top, int right, int bottom, int colour, FillRectM
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Align parameters of a line to the given DPI and check simple clipping.
|
||||||
|
* @param dpi Screen parameters to align with.
|
||||||
|
* @param x X coordinate of first point.
|
||||||
|
* @param y Y coordinate of first point.
|
||||||
|
* @param x2 X coordinate of second point.
|
||||||
|
* @param y2 Y coordinate of second point.
|
||||||
|
* @param width Width of the line.
|
||||||
|
* @return True if the line is likely to be visible, false if it's certainly
|
||||||
|
* invisible.
|
||||||
|
*/
|
||||||
|
static inline bool GfxPreprocessLine(DrawPixelInfo *dpi, int &x, int &y, int &x2, int &y2, int width)
|
||||||
|
{
|
||||||
|
x -= dpi->left;
|
||||||
|
x2 -= dpi->left;
|
||||||
|
y -= dpi->top;
|
||||||
|
y2 -= dpi->top;
|
||||||
|
|
||||||
|
/* Check simple clipping */
|
||||||
|
if (x + width / 2 < 0 && x2 + width / 2 < 0 ) return false;
|
||||||
|
if (y + width / 2 < 0 && y2 + width / 2 < 0 ) return false;
|
||||||
|
if (x - width / 2 > dpi->width && x2 - width / 2 > dpi->width ) return false;
|
||||||
|
if (y - width / 2 > dpi->height && y2 - width / 2 > dpi->height) return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
void GfxDrawLine(int x, int y, int x2, int y2, int colour, int width)
|
void GfxDrawLine(int x, int y, int x2, int y2, int colour, int width)
|
||||||
{
|
{
|
||||||
Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
|
Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
|
||||||
|
@ -198,40 +224,23 @@ void GfxDrawLine(int x, int y, int x2, int y2, int colour, int width)
|
||||||
|
|
||||||
assert(width > 0);
|
assert(width > 0);
|
||||||
|
|
||||||
x -= dpi->left;
|
if (GfxPreprocessLine(dpi, x, y, x2, y2, width)) {
|
||||||
x2 -= dpi->left;
|
|
||||||
y -= dpi->top;
|
|
||||||
y2 -= dpi->top;
|
|
||||||
|
|
||||||
/* Check clipping */
|
|
||||||
if (x + width / 2 < 0 && x2 + width / 2 < 0 ) return;
|
|
||||||
if (y + width / 2 < 0 && y2 + width / 2 < 0 ) return;
|
|
||||||
if (x - width / 2 > dpi->width && x2 - width / 2 > dpi->width ) return;
|
|
||||||
if (y - width / 2 > dpi->height && y2 - width / 2 > dpi->height) return;
|
|
||||||
|
|
||||||
blitter->DrawLine(dpi->dst_ptr, x, y, x2, y2, dpi->width, dpi->height, colour, width);
|
blitter->DrawLine(dpi->dst_ptr, x, y, x2, y2, dpi->width, dpi->height, colour, width);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void GfxDrawLineUnscaled(int x, int y, int x2, int y2, int colour)
|
void GfxDrawLineUnscaled(int x, int y, int x2, int y2, int colour)
|
||||||
{
|
{
|
||||||
Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
|
Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
|
||||||
DrawPixelInfo *dpi = _cur_dpi;
|
DrawPixelInfo *dpi = _cur_dpi;
|
||||||
|
|
||||||
x -= dpi->left;
|
if (GfxPreprocessLine(dpi, x, y, x2, y2, 1)) {
|
||||||
x2 -= dpi->left;
|
blitter->DrawLine(dpi->dst_ptr,
|
||||||
y -= dpi->top;
|
UnScaleByZoom(x, dpi->zoom), UnScaleByZoom(y, dpi->zoom),
|
||||||
y2 -= dpi->top;
|
|
||||||
|
|
||||||
/* Check clipping */
|
|
||||||
if (x < 0 && x2 < 0) return;
|
|
||||||
if (y < 0 && y2 < 0) return;
|
|
||||||
if (x > dpi->width && x2 > dpi->width) return;
|
|
||||||
if (y > dpi->height && y2 > dpi->height) return;
|
|
||||||
|
|
||||||
blitter->DrawLine(dpi->dst_ptr, UnScaleByZoom(x, dpi->zoom), UnScaleByZoom(y, dpi->zoom),
|
|
||||||
UnScaleByZoom(x2, dpi->zoom), UnScaleByZoom(y2, dpi->zoom),
|
UnScaleByZoom(x2, dpi->zoom), UnScaleByZoom(y2, dpi->zoom),
|
||||||
UnScaleByZoom(dpi->width, dpi->zoom), UnScaleByZoom(dpi->height, dpi->zoom), colour, 1);
|
UnScaleByZoom(dpi->width, dpi->zoom), UnScaleByZoom(dpi->height, dpi->zoom), colour, 1);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Draws the projection of a parallelepiped.
|
* Draws the projection of a parallelepiped.
|
||||||
|
|
Loading…
Reference in New Issue