Game Development Reference
In-Depth Information
1.3.1 Surfaces
A surface is a matrix of pixels that Direct3D uses primarily to store 2D
image data. Figure 1.2 identifies some components of a surface. Note
that while we visualize the surface data as a matrix, the pixel data is
actually stored in a linear array.
Figure 1.2: A surface
The width and height of a surface are measured in pixels. The pitch is
measured in bytes. Furthermore, the pitch may be wider than the
width, depending on the underlying hardware implementation, so you
cannot assume that pitch = width · sizeof(pixelFormat) .
In code, we describe surfaces with the IDirect3DSurface9
interface. This interface provides several methods for reading and writ-
ing data directly to a surface as well as a method to retrieve information
about the surface. The most important methods of IDirect3DSur-
face9 are:
LockRect —This method allows us to obtain a pointer to the sur-
face memory. Then, with some pointer arithmetic, we can read and
write to each pixel in the surface.
UnlockRect —After you have called LockRect and are done
accessing the surface's memory, you must unlock the surface by
calling this method.
GetDesc —This method retrieves a description of the surface by
filling out a D3DSURFACE_DESC structure.
Locking a surface and writing to each pixel can be somewhat confusing
at first, considering the surface pitch, so we have provided the follow-
ing code block that locks a surface and colors each pixel red:
Search WWH ::




Custom Search