void WritePixel(const int x,const int y,
const float r, const float g, const float b, const float alpha)
{
//3 is for RGB 24bit deepth
unsigned char* pVideo=VideoBuffer+(x+(VideoDimX-(y+1))*VideoDimY)*3;
pVideo[0]=(alpha*r)*255+(1.0f-alpha)*pVideo[0];
pVideo[1]=(alpha*g)*255+(1.0f-alpha)*pVideo[1];
pVideo[2]=(alpha*b)*255+(1.0f-alpha)*pVideo[2];
}
void LineAA(const int x1,const int y1,const int x2,const int const y2,
const float r,const float g, const float b)
{
// useful constants
const float dw=x2-x1;
const float dh=y2-y1;
const float slx=dh/dw;
const float sly=dw/dh;
// determine the slope
if(fabs(slx)<1.0)
{
// x scan
int tx1=x1;
int tx2=x2;
float raster=y1;
if(x1>x2)
{
tx1=x2;
tx2=x1;
raster=y2;
}
for(int x=tx1;x<=tx2;x++)
{
const int ri=int(raster);
const float AlphaY0=1.0-(raster-ri);
const float AlphaY1=1.0-(ri+1-raster);
WritePixel(x,ri ,r,g,b,AlphaY0);
WritePixel(x,ri+1,r,g,b,AlphaY1);
raster+=slx;
}
}
else
{
// y scan
int ty1=y1;
int ty2=y2;
float raster=x1;
if(y1>y2)
{
ty1=y2;
ty2=y1;
raster=x2;
}
for(int y=ty1;y<=ty2;y++)
{
const int ri=int(raster);
const float AlphaX0=1.0-(raster-ri);
const float AlphaX1=1.0-(ri+1-raster);
WritePixel(ri ,y,r,g,b,AlphaX0);
WritePixel(ri+1,y,r,g,b,AlphaX1);
raster+=sly;
}
}
}