In the project I’m working on, I need a lot (hundreds) of lines of variable thickness to be drawn on the screen. I noticed that the painting becomes excessively slow when using Graphics::drawLine(). I managed to write some code that is exactly 10x (!) faster than JUCE’s drawLine() function, but which has 3 drawbacks:
- the line thickness is actually always twice than the thickness parameter I am passing
- the ends of the lines are not properly drawn like in JUCE
- it seems that the lines’ visual thickness changes a bit when the angle changes.
Jules, would it be possible to create a faster drawLine() function in JUCE? Right now you’re creating a Path and filling it, but I’m sure it can be done much faster.
Anyway, for reference, here’s my code:
[code]// for lineThickness, pass a value that is HALF of the actual thickness
void MyRenderer::drawLineFast(int x0, int y0, int x1, int y1, int lineThickness)
{
int dx = abs(x1-x0), sx = x0<x1 ? 1 : -1;
int dy = abs(y1-y0), sy = y0<y1 ? 1 : -1;
int j1=1-lineThickness;
int j2=lineThickness;
if (dx>=dy)
{
float y=float(y0);
float yAdd=float(dy)/float(dx)*float(sy);
while (x0!=x1)
{
x0+=sx;
y+=yAdd;
int iY=int(y);
float k=y-float(iY);
blendPixelInternal2(x0, iY+j1-1, alpha*(1.0f-k));
for (int j=j1; j<j2; j++)
blendPixelInternal2(x0, iY+j, alpha);
blendPixelInternal2(x0, iY+j2, alpha*k);
}
}
else
{
float x=float(x0);
float xAdd=float(dx)/float(dy)*float(sx);
while (y0!=y1)
{
y0+=sy;
x+=xAdd;
int iX=int(x);
float k=x-float(iX);
blendPixelInternal2(iX+j1-1, y0, alpha*(1.0f-k));
for (int j=j1; j<j2; j++)
blendPixelInternal2(iX+j, y0, alpha);
blendPixelInternal2(iX+j2, y0, alpha*k);
}
}
}
void MYRenderer::blendPixelInternal2(int x0, int y0, uint8 alpha_)
{
if (x0>=clipLeft && x0<clipRight && y0>=clipTop && y0<clipBottom)
{
uint8 data = buf+ x0pixelStride + y0*lineStride;
uint8 b_=255-alpha_;
data[0]=((uint16(data[0])b_+balpha_)>>8);
data[1]=((uint16(data[1])b_+galpha_)>>8);
data[2]=((uint16(data[2])b_+ralpha_)>>8);
}
}[/code]