Game Development Reference
In-Depth Information
that is at least three times the pixel width, plus the extra up to 3 bytes to
ensure divisibility by 4. We set two variables x and y to the starting value.
Then we create two distance variables for the x distance, dx , and the y
distance, dy .
We set xinc and yinc to 1. The value for xinc indicates whether xe is
greater than xs . If so, then we will reach xe by 1 to xs , a dx number of
times. If, however, xs > xe , then we must subtract 1 from xs , dx number
of times. We check dx to indicate which direction we are working in. If dx
is less than 0, then the xinc is made to be -1 and the value for dx is
negated. A similar technique is used for the y values.
The COLORREF parameter passed to the function is simply a 32-bit
integer. The values for red, green and blue are embedded in this value,
and we retrieve them with the code:
red= col&0xFF;
green=(col&0xFF00)>>8;
blue=(col&0xFF0000)>>16;
Now we use a switch statement to select based on bit depth. In our code
we only support 24 bits, but it gives us flexibility for the future to support
alternative bit depths.
void CCanvas::DrawLine(int xs,int ys,int xe,int ye,COLORREF col){
int x, y, d, dx, dy, c,m, xinc, yinc, width;
BYTE red,blue,green;
if (!ClipLine(xs,ys,xe,ye)) return;
//If ClipLine returns false then start and end are out off the
//canvas so there is nothing to draw
//m_swidth is the DWORD aligned storage width
width=m_swidth;
//x and y are set to the starting point
x=xs; y=ys;
//dx and dy are the distances in the x and y directions
//respectively
dx=xe-xs; dy=ye-ys;
//ptr is the memory location of x,y