Game Development Reference
In-Depth Information
The first option is to write a custom loader that can read in the binary data and
extract the image information. After the image information (for instance, the
number of channels and the pixel data) has been extracted, the information would
be used to build an Image object. This method can be somewhat problematic, espe-
cially if the image format comes in different variations, such as different compres-
sion options and 3D-specific values such as the Microsoft DirectDraw Surface
(DDS) format for example. The custom loader can become quite large in size, and
debugging could prove to be difficult.
The alternate solution, and the one covered in this chapter, is to wrap the built-in
texture-loading capabilities of Microsoft Direct3D into a loader. There are a num-
ber of advantages to building this wrapper over building a custom loader from
scratch. The biggest advantage is the time and money saved by not having to rein-
vent the wheel. Additionally, unless you have a custom image format that neither
GDI+ nor Direct3D support, one that requires a custom loader, the formats sup-
ported by the TextureLoader utility of Managed Direct3D will almost always suffice
for your project.
Now, before you jump into the code, it is important to address the main issue
behind wrapping Managed Direct3D into a loader. The TextureLoader loads image
files into Texture objects, not Image or Bitmap objects, meaning that a valid device
must first be created before any loading can occur. This may sound like a daunt-
ing or cumbersome process, but it really isn't all that bad. The main requirement
for a device is a window handle, and because Windows defines a window as any
control element, we can create a device using the window handle of our texture
browsing control!
The loader will create a Managed Direct3D and bind it to the texture browser con-
trol, at which point the image files are loaded into texture resources. The image
data is then extracted from these resources and saved into Bitmap objects. The
device is released after the images are generated; at no point does any actual ren-
dering occur.
The following code implements the Managed Direct3D device and image loader.
public class Direct3DLoader : AbstractLoader, IDisposable
private Device _device;
public override Bitmap Load(string fileName)
Search Nedrilad ::

Custom Search