"Reprint" ID3DXSprite interface simple to use

Source: Internet
Author: User
Tags transparent color

Original:ID3DXSprite interface simple to use

The previous days have been studying DDraw, after all, is the DirectX7 of things, so the hands with the directd3d9, with the surface of the drawing, but how to do transparent color is not loadfromfile transparent part of the opaque black, I faint ... Ah ~ There are several brothers on the net from the DDraw turned around, as if there is this problem, there is no way can only switch to ID3DXSprite interface and texture, on-line to see an article is clear, I would like to borrow, do not blame ^_^

Source Address http://blog.csdn.net/tinya0913/archive/2011/01/08/6124024.aspx

ID3DXSprite interface

    1. HRESULT Draw (Lpdirect3dtexture9 ptexture,
    2. CONST RECT * Psrcrect,
    3. CONST D3dxvector3 * Pcenter,
    4. CONST D3dxvector3 * pposition,
    5. D3dcolor Color);

In 2D games, because the depth of this "D" is removed, the size of the object will not change, no matter how close the viewpoint is to the object.

So how do you make 2D games with D3D, and how can you achieve this effect that is not related to depth? The key is not to set up the perspective projection transformation matrix, but to use the function D3DXMatrixOrthoLH or D3DXMATRIXORTHOOFFCENTERLH to establish the orthogonal projection matrix. The usage of these two functions will be discussed in detail in a later section.

Render 2D sprites using the ID3DXSprite interface

The ID3DXSprite interface provides a series of functions to help programmers render 2D sprites using D3D. The typical steps for using the ID3DXSprite interface are:

Get a pointer to the ID3DXSprite interface via the D3dxcreatesprite function

Call the Id3dxsprite::begin function to prepare the render state

Call the Id3dxsprite::settransform function to set the wizard's World coordinate system transformation (including zoom, rotate, and pan)

Call ID3DXSprite::D raw function to display sprites on screen

Create a D3dxsprite object

To use Id3dxspirte, you first need to create D3dxsprite objects using the D3dxcreatesprite function. The D3dxcreatesprite function uses the current 3D device, creates a Sprite object, and returns a pointer to the Sprite object. Its function prototype is as follows:

    1. HRESULT D3dxcreatesprite (
    2. Lpdirect3ddevice9 pdevice,
    3. Lpd3dxsprite * Ppsprite
    4. );

The following code snippet shows how to create a D3dxsprite object and get a pointer to that object. In this code, it is assumed that Pd3ddevice is a well-established D3D device pointer.

    1. ......
    2. Lpd3dxsprite Psprite;
    3. if (FAILED (D3dxcreatesprite (
    4. Pd3ddevice,&psprite)))
    5. {
    6. Return
    7. }
    8. ......

Preparing the Render state

With the ID3DXSprite interface, you can set the rendering state by calling the Beign function. The BEGIN function is prototyped as follows:

HRESULT Begin (

DWORD Flags

);

In 2D games, the most commonly used parameter is D3dxsprite_alphablend, which indicates that the Alpha blend switch will be turned on to enable transparent rendering of the sprite. Common to D3dxsprite_alphablend in 3D games | D3dxsprite_billboard The combination of these two values to implement the particle system. In this chapter, the focus is on 2D game production, so it will only be used in d3dxsprite_alphablend.

By invoking the BEGIN function, the D3D device is ready to render, and then it can perform the rendering work. However, the BEGIN function should appear in pairs with the End function, and the rendered operation should be placed between the being function and the end function. The End function is prototyped as follows:

HRESULT End ();

With the End Function call, the D3D device submits the rendered content to the device. It is worth noting that the Begin/end function provided by the ID3DXSprite interface does not replace the Beginsecne/endscene function, but should be placed between them, as follows;

    1. ......
    2. if (SUCCEEDED (Pd3ddevice->beginscene ()))
    3. {
    4. ......
    5. Psprite->begin (
    6. D3dxsprite_alphablend);
    7. ......
    8. Psprite->end ();
    9. .....
    10. Pd3ddevice->endscene ();
    11. }
    12. ......

Set the world coordinate system transformation for sprites

The world transforms include zooming, rotating, panning, and so on. For these operations, you can use the D3dxmatrixscaling function, the D3DXMatrixTranslation function, and the D3DXMatrixRotationZ to calculate the corresponding transformation matrix. Then use the D3DXMatrixMultiply function to accumulate them in a certain transformation order. Then use the SetTransform function of the D3dxsprite object to set the wizard's world transformation. As in the code snippet below, scale the sprite to 0.5 times times the original in x, y direction, and then rotate 1 radians around the z axis, and then pan to (x,y,0):

  1. ......
  2. D3dxmatrix Matscale,matrotation,mattrans,matworld;
  3. D3dxmatrixscaling (&matscale, 0.5f, 0.5f, 1.0f);
  4. D3DXMatrixRotationZ (&matrotation,1.0);
  5. D3DXMatrixTranslation (&mattrans,x,y,0);
  6. D3DXMatrixMultiply (&matworld,&matscale,
  7. &matrotation);
  8. D3DXMatrixMultiply (&matworld,&matworld,
  9. &mattrans);
  10. Psprite->settransform (&matworld);
  11. ......

Render at a specified location on the screen

When you render with a D3dxsprite object, its coordinate system is in the upper-left corner of the screen as the coordinate origin, along the origin to the left is the x-coordinate direction, and the y-coordinate increases along the origin. The D3dxsprite drawing operation must be placed between the D3dxsprite object's Begin ()/end () function pair. When calling its begin function, you can specify the behavior of the sprite rendering at the same time (such as opening the Alpha blend switch), and the specific operation is given to the draw function. Its function prototype is as follows:

The following code snippet renders the sprite in X, Y, z locations:

......

Turn on the render switch while opening the Alpha blend switch

    1. Psprite->begin (D3dxsprite_alphablend);
    2. HRESULT hr = Psprite->draw (ptexture,
    3. Null
    4. NULL, &d3dxvector3 (x, Y, z),
    5. 0xFFFFFFFF
    6. );
    7. Psprite->end ();
    8. ......

Note: The ID3DXSprite interface is designed to help programmers easily implement 2D games, so when using the ID3DXSprite interface, there is no need for a programmer to build a projected coordinate system, observe a coordinate system, or create vertex buffers and define vertex data structures and flexible vertex formats.

"Reprint" ID3DXSprite interface simple to use

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.