• No results found

Loading the Texture

In document Programming 2D Games - Charles Kelly (Page 127-130)

5 Sprites and Animation

5.3 Drawing with Transparency

5.3.3 Loading the Texture

The texture that is applied to a sprite will typically be located in a file. We need a function to load the texture image from the file. The DirectX function we will use for this task is D3DXCreateTextureFromFileEx. Here is the formal syntax:

HRESULT D3DXCreateTextureFromFileEx(

LPDIRECT3DDEVICE9 pDevice, LPCTSTR pSrcFile,

D3DXIMAGE_INFO *pSrcInfo, PALETTEENTRY *pPalette, LPDIRECT3DTEXTURE9 *ppTexture );

The parameters:

pDevice. Pointer to an IDirect3DDevice9 interface.

pSrcFile. Pointer to a string that specifies the file name.

Width. Width of the texture in pixels. If 0 or D3DX_DEFAULT is specified, the dimen-sions are taken from the file and rounded up to a power of 2. If the device supports non-power of 2 textures and D3DX_DEFAULT_NONPOW2 is specified, the size is not rounded.

Height. Height of the texture in pixels. If 0 or D3DX_DEFAULT is specified, the di-mensions are taken from the file and rounded up to a power of 2. If the device sup-ports non-power of 2 textures and D3DX_DEFAULT_NONPOW2 is specified, the size is not rounded.

MipLevels. Number of mip levels to make. If 0 or D3DX_DEFAULT is specified, a complete mipmap chain is created. Mip levels are used to smooth the transition of textures when moving the player closer or farther away from the textured surface. In 2D games, the surface does not change in distance from the viewer, so mip levels are not used; therefore, we will specify 1 for this parameter.

Usage. We will always use 0. The other choices are D3DUSAGE_RENDERTARGET, which specifies that the surface is to be used as a render target, and D3DUSAGE_DYNAMIC, which specifies that the surface should be handled dynamically as part of a dynamic texture.

Format. Must be an enumerated type from D3DFORMAT, which describes the re-quested pixel format for the texture. The actual texture returned might have a

dif-115 5.3. Drawing with Transparency

ferent format than requested. The actual format is returned in the pSrcInfo struc-ture. Use D3DFMT_UINKNOWN to use the format from the file.

Pool. Must be an enumerated type from D3DPOOL. Describes the memory class into which the texture is loaded.

Filter. Requires one or more constants from D3DX_FILTER and specifies how the image is filtered. We always use D3DX_DEFAULT.

MipFilter. Requires one or more constants from D3DX_FILTER and specifies how the image is filtered. We always use D3DX_DEFAULT.

ColorKey. The color to treat as transparent, specified as a D3DCOLOR value. All 32 bits of the color are significant. Pixels in the source image that match ColorKey are converted to transparent when the image is loaded. The ColorKey is not required for images that support alpha channel transparency.

pSrcInfo. Pointer to a D3DXIMAGE_INFO structure that is filled with a descrip-tion of the source image.

pPalette. Pointer to a PALETTEENTRY structure that represents a 256-color pal-ette that is either filled in or NULL. We will always use NULL.

ppTexture. Address of a pointer to an IDirect3DTexture9 interface that points to the created texture.

Check the return value using the FAILED or SUCCEEDED macros. The width and height of the texture file is obtained with the D3DXGetImageInfoFromFile function:

HRESULT D3DXGetImageInfoFromFile(

LPCSTR pSrcFile,

D3DXIMAGE_INFO *pSrcInfo );

The parameters:

pSrcFile. Pointer to the file name of the image.

pSrcInfo. Pointer to a D3DXIMAGE_INFO structure to be filled with information about the image file.

The D3DXIMAGE_INFO structure looks like this:

typedef struct D3DXIMAGE_INFO { UINT Width;

UINT Height;

UINT Depth;

UINT MipLevels;

D3DFORMAT Format;

D3DRESOURCETYPE ResourceType;

D3DXIMAGE_FILEFORMAT ImageFileFormat;

} D3DXIMAGE_INFO, *LPD3DXIMAGE_INFO;

116 5. Sprites and Animation

The members are:

Width. Width of the image in pixels.

Height. Height of the image in pixels.

Depth. Depth of the image in pixels.

MipLevels. Number of mip levels in the image.

Format. A value of D3DFORMAT enumerated type that describes the format of the data in the image.

ResourceType. The type of texture in the file defined as either D3DRTYPE_TEX TURE, D3DRTYPE_VOLUMETEXTURE, or D3DRTYPE_CubeTexture.

ImageFileFormat. The format of the image file.

We need to create a function in our Graphics class to load textures. Let’s name the function loadTexture. It receives parameters for the file name and transparent color. The texture width, height, and pointer are returned via the reference parameters. Our complete loadTexture function is in Listing 5.4.

//========================================================================

// Load the texture into default D3D memory (normal texture use)

// For internal engine use only. Use the TextureManager class to load game // textures.

// Pre: filename is name of texture file // transcolor is transparent color // Post: width and height = size of texture // texture points to texture

// Returns HRESULT

//========================================================================

HRESULT Graphics::loadTexture(const char *filename, COLOR_ARGB transcolor, UINT &width, UINT &height, LP_TEXTURE &texture)

{ // The struct for reading file info D3DXIMAGE_INFO info;

result = E_FAIL;

try{

if(filename == NULL) {

texture = NULL;

return D3DERR_INVALIDCALL;

}

// Get width and height from file

result = D3DXGetImageInfoFromFile(filename, &info);

if (result != D3D_OK) return result;

width = info.Width;

117 5.3. Drawing with Transparency

height = info.Height;

// Create the new texture by loading from file result = D3DXCreateTextureFromFileEx(

device3d, // 3D device

throw(GameError(gameErrorNS::FATAL_ERROR,

"Error in Graphics::loadTexture"));

}

return result;

}

Listing 5.4. The loadTexture function.

In document Programming 2D Games - Charles Kelly (Page 127-130)