Game Development Reference
In-Depth Information
pointer to the pixel or NULL if it is out of range. The function includes a
simple clip test. If either x is greater than the bitmap's width or y is
greater than the bitmap's height, then it is out of range. Similarily, if x or
y are less than 0 then they are out of range. To indicate this fact, the
function returns a null pointer. The bitmap is stored in memory one line
following another. We know how many bytes are required for a single
line; this is the information that we ensured was divisible by 4, the
storage width. To access the appropriate line, we simply need to
multiply the storage width by the value for y . The distance along the line
is dependent on whether we are using a 24-bit pixel or a 32-bit pixel. If
we are using a 24-bit pixel then we need to multiply the x value by the
number of bytes in a 24-bit pixel, that is 3. A 32-bit pixel needs 4 bytes
for each pixel, hence the x value needs to be multiplied by 4. Having
calculated the offset from the start of the bitmap in memory, all that
remains is to add this to the start of the bitmap 'bits' memory to return
the memory location of the pixel ( x , y ).
{
// Make sure it's in range and if it isn't return zero.
if ((x >= m_width)|| (y >= m_height())||(x<0)||(y<0)) return
NULL;
// Calculate the scan line storage width.
if (m_bitDepth()==24) x*=3;
if (m_bitDepth()==32) x*=4;
return m_pBits +y * m_swidth + x;
}
We want to create a function that will draw an arbitrary line that is defined
by the starting and ending points. Before we go any further with such a
function, we need to ensure that the start and end of the line are actually
within the boundaries of the off-screen buffer we are using as a canvas.
For this we will create a ClipLine function. The aim of the ClipLine function
is to adjust the ( x , y ) values of each end of the line so that they are within
the canvas area. That is 0
x < width, where width is the CCanvas width,
and 0
y < height, where height is the CCanvas height.
The ClipLine function creates a code value for the start point, cs , and
the end point, ce . This code value determines whether the point is within
the canvas area, off to the left, right, above or below, or a combination of
these. This is done using the code:
cs=((xs<0)<<3)|((xs>=m_width)<<2)|((ys<0)<<1)|(ys>=m_height);