gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Coding > MODE 4 line drawing

#7244 - hnager - Fri Jun 13, 2003 2:42 am

this seems to work, but after rotating the polygon I can't get it to draw a completely straight vertical line as part of the shape, even if it's a square. THe lines are stright except for a pixel or 2.

void BLine(int x0, int y0, int x1, int y1, unsigned char color){

int dx, dy, x_inc, y_inc, error=0, index, x, y;

u16* vidMem;
x = x0;
y = y0;

dx = x1-x0;
dy = y1-y0;

if (dx>0){
x_inc = 1;
}else{
x_inc = -1;
dx = -dx;
}

if (dy>0){
y_inc = 1;
}else{

y_inc = -1;
dy = -dy;
}

if(dx>dy){

for(index=0; index<=dx; index++){

vidMem = &VideoBuffer[ ((y*240) + x)>>1 ];

if(x % 2)
*vidMem = (*vidMem & 0x00FF) | (color<<8);
else
*vidMem = (*vidMem & 0xFF00) | color;

error+=dy;

if(error>dx){
error-=dx;
y+=y_inc;
}
x+=x_inc;

}
} else {
for(index=0; index <=dy; index++){

vidMem = &VideoBuffer[ ((y*240) + x)>>1 ];

if(x % 2)
*vidMem = (*vidMem & 0x00FF) | (color<<8);
else
*vidMem = (*vidMem & 0xFF00) | color;

error+=dx;

if(error>0){
error -= dy;
x+=x_inc;
}
y+=y_inc;
}
}

}