Beginner Direct X (5)

Beginner Direct X (5)


I learned to use the surface to draw the screen earlier, but compared to the other method, this method has a very slow drawing speed and lack of support for any type of transparency, which is why the front basket and the bomb have a black background, this kind of The method is texture. It can draw objects with transparent effects, that is, only the pixels of the object itself are displayed without the background.

1. Load a texture with a bitmap

The texture should have the same functions as the surface, such as loading a bitmap onto the texture. To do this, first create a texture object, which is of type LPDIRECT3DTEXTURE9:

LPDIRECT3DTEXTURE9 texture = NULL;

Then load the bitmap into the texture from the bitmap, and its interface is as follows:

D3DXCreateTextureFromFileExA(
    LPDIRECT3DDEVICE9         pDevice,
    LPCSTR                    pSrcFile,
    UINT                      Width,
    UINT                      Height,
    UINT                      MipLevels,
    DWORD                     Usage,
    D3DFORMAT                 Format,
    D3DPOOL                   Pool,
    DWORD                     Filter,
    DWORD                     MipFilter,
    D3DCOLOR                  ColorKey,
    D3DXIMAGE_INFO*           pSrcInfo,
    PALETTEENTRY*             pPalette,
    LPDIRECT3DTEXTURE9*       ppTexture);

It can be seen that you need to know the size of the bitmap and D3DXIMAGE_INFO , which needs to be obtained in advance using D3DXGetImageInfoFromFile :

D3DXIMAGE_INFO image_info;
HRESULT result = D3DXGetImageInfoFromFile(filename.c_str(),&image_info);

Here is an example of calling D3DXCreateTextureFromFileExA:

D3DXCreateTextureFromFileEx(
    d3ddev,
    filename.c_str(),
    image_info.Width,
    image_info.Height,
    1,
    D3DPOOL_DEFAULT,
    D3DFMT_UNKNOWN,
    D3DPOOL_DEFAULT,
    D3DX_DEFAULT,
    D3DX_DEFAULT,
    transcolor,
    &image_info,
    NULL,
    &texture
    );

2. Draw the texture to the screen using the renderer

2.1 Initialization

First you need to define a renderer, which is defined as:

LPD3DXSPRITE spriteobj;

The next step is to initialize it. The function is to attach the texture to the Direct device so that the Direct device knows how to draw the texture in the back buffer. The following is the definition of the interface:

HRESULT D3DXCreateSprite( 
        LPDIRECT3DDEVICE9   pDevice, 
        LPD3DXSPRITE*       ppSprite);

2.2 Operations before and after drawing

After the main Direct device calls BeginScene, the texture can be drawn, but the surface needs to be locked, which is only possible through the renderer using the interface:

HRESULT Begin (DWORD Flags)

flags are required, usually D3DXSPRITE_ALPHABLEND; of course, the surface needs to be unlocked after drawing so that other processes can use it:

HRESULT End (VOID)

2.3 Drawing

Very simple, the renderer, LPD3DXSPRITE, only needs to use a single function Draw to handle all transformations, through which it can perform transparency, scaling, and rotation. The following is the definition of this function:

HRESULT Draw(
  [in]       LPDIRECT3DTEXTURE9 pTexture,
  [in] const RECT               *pSrcRect,
  [in] const D3DXVECTOR3        *pCenter, // 旋转发生的中间点
  [in] const D3DXVECTOR3        *pPosition, // 指定纹理的位置
  [in]       D3DCOLOR           Color 
  );

There is also a D3DXVECTOR3 type, which is:

typedef struct D3DXVECTOR3 {
  FLOAT x;
  FLOAT y;
  FLOAT z;
} D3DXVECTOR3, *LPD3DXVECTOR3;

color: can be [0xFFFFFFFF - 0x00FFFFFF]: from opaque to transparent, can be generated by D3DCOLOR_RGBA , generally set to D3DCOLOR_XRGB(255, 255, 255) , that is, opaque

3. Run an example to see

3.1 Draw a bitmap without alpha channel, no ColorKey selection

Before generating Figure 1, there are a few things to know:

1) When loading the bitmap into the texture, set the ColorKey to D3DCOLOR_XRGB(0, 0, 0) , which is black

2) The background color is cleared to green

3) The original bitmap is a 24-bit image and does not contain an alpha channel

figure 1
figure 1

It can be seen from the results that when it is drawn, the background color of the color marked by its ColorKey will be filled, and it seems to achieve the effect of transparency:)

3.2 Draw a bitmap without alpha channel and select ColorKey

Before generating Figure 2, there are also a few things to know:

1) When loading the bitmap to the texture, set the ColorKey to D3DCOLOR_XRGB(255,0,255) , which is pink

2) The background color is cleared to green

3) The original bitmap is a 24-bit image and does not contain an alpha channel

figure 2
figure 2

It can be seen from the results that this image seems to be the transparent image we want (the background color of the original image is pink, which is the same as the original image processed in Figure 1). Here we can see the meaning of the variable ColorKey , which can make The area where it is located displays the background color to achieve a transparent effect.

3.3 Draw alpha channel bitmap, with/without ColorKey

Before generating Figure 3, there are also a few things to know:

1) When loading the bitmap to the texture, set the ColorKey to D3DCOLOR_XRGB(0,0,0) , the same processing as Figure 1

2) The background color is cleared to green

3) The original bitmap is a 32-bit image and contains an alpha channel

image 3
image 3

As can be seen from the results, it seems that the drawing effect is not very good, but it reflects the following information. First: if a valid ColorKey is set when loading the texture , Draw will mercilessly make it transparent, which leads to It is clear that the aircraft head and edge in the display result are the background colors; second: since the aircraft background is set to be transparent in the alpha channel , it is reasonable to make it transparent under the drawing of Draw . From the above analysis, it can be seen that when faced with a bitmap with an alpha channel, Draw will not forget to process the ColorKey area when drawing pixels that should be transparent.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324738281&siteId=291194637