Description: id3dxsprite is a simple module in driectx 9.0. The features described in the driectx 9.0 help document are as follows: "It provides users with a simple set of interfaces for implementing sprite rendering on the screen." To put it bluntly, it is to render 2D images. id3dxsprite helps users to use driectx 9.0 to create 2D games (rendering 2D images) with simple operations. The id3dxsprite features include: it helps users implement the "announcement board" Technology in 3D games. The following details how to use id3dxsprite.
Get an id3dxsprite object: Anyone who has played driectx knows how to get an instance object before doing anything. In fact, you only need to simply call a function provided by d3dx for us:
Hresult d3dxcreatesprite (
Lpdirect3ddevice9 pdevice,
Lpd3dxsprite * ppsprite
)
I don't need to explain how to call this function. Anyone who has touched driectx should know what it means.
Rendering preparation: DirectX 9.0 stipulates that before using id3dxsprite to render a 2D image, you should call id3dxsprite: begin to prepare the image, and call id3dxsprite: end to complete the rendering. The format is as follows:
G_psprite-> begin (null );
// Render code section ....
G_psprite-> end ();
Id3dxsprite: Begin receives a parameter, which determines the rendering method of the sprite. This parameter can be one of the following values:
· D3dxsprite_alphablend
· D3dxsprite_billboard
· D3dxsprite_donotmodify_renderstate
· D3dxsprite_donotsavestate
· D3dxsprite_objectspace
· D3dxsprite_sort_depth_backtofront
· D3dxsprite_sort_depth_fronttoback
· D3dxsprite_sort_texture
The above logo can be used in combination, And the logo meaning can be omitted from its name. The specific usage will be introduced later when it is used.
Rendering: it is easy to use id3dxsprite to render 2D images. You only need to call the id3dxsprite: Draw interface. The interface prototype is as follows:
Hresult draw (
Lpdirect3dtexture9 ptexture,
Const rect * psrcrect,
Const d3dxvector3 * pcenter,
Const d3dxvector3 * pposition,
D3dcolor color
);
The parameter is the texture used by the sprite.
Parameter 2 is the area of the texture rectangle to be rendered, which means the original bitmap rectangle as described in DirectDraw (specifies the part of the texture to be rendered to the screen)
Parameter 3 requires that the center point coordinates of the texture be input. If null is input, the default value is used. The default value is to set the center point in the upper left corner of the texture. The setting of the central point will be related to the effect of moving and rotating the genie in the future.
Parameter 4 is the rendering position of the genie on the screen. to render the genie at the (100,100) pixel position on the screen, input & d3dxvector3 (100f, 100f, 0.0f ), in fact, the three-plus parameter 4 can be simply understood as the destination rectangle in DirectDraw.
Parameter 5 requires a 32-bit color value. During rendering, the color value of each pixel on the texture is multiplied to obtain the final rendering color. For example, if 0x00000000 is input, after multiplication, the color value will be 0, so the sprite will be rendered in full black. this parameter is also useful in controlling the transparent value of the sprite: when the user calls id3dxsprite :: when you pass in the d3dxsprite_alphablend flag during in, it indicates that the transparent rendering function is enabled. In this case, the 8-bit height of parameter 5 is used to specify the transparency of the rendering. For example, to render an image completely opaque, 0 xffffffff should be input. to render the image completely transparently, 0x00ffffff should be input.
Genie displacement, scaling, rotation, and other processing: You can call id3dxsprite: settransform to implement this interface. This interface requires passing in a transformation matrix. Note: here, the transformation matrix refers to the transformation of coordinates on the 2D plane, rather than the commonly used 3D transformation matrix. The 2D plane transformation matrix should be obtained by calling the d3dxmatrixtransformation2d function, for how to use this function, see the directx9.0 help file.
The following code is attached to render a 2D sprite at the (100,100) coordinate of the screen. The sprite is reduced to a tenth of the original, with a 0.5-degree rotation, the transparency is about 60%.
// Initialize the sprite object
D3dxcreatesprite (g_pdevice, & g_psprite );
G_pdevice-> beginscene ();
G_pdevice-> clear (0, null, d3dclear_target | d3dclear_zbuffer | d3dclear_stencel,
D3dcolor_xrgb (0, 0), 1.0f, 0l );
G_psprite-> begin (d3dxsprite_alphablend );
// Obtain the 2D coordinate matrix.
D3dxmatrixtransformation2d (& mat, null, 0.0f, & d3dxvector2 (0.1f, 0.1f ),
& D3dxvector2 (500000f, 500000f), 0.5f, & d3dxvector2 (1000000f, 1000000f ));
G_psprite-> settransform (& mat );
// Rendering genie
G_psprite-> draw (g_ptexsprite, null, 0x99ffffff );
G_psprite-> end ();
G_pdevice-> endscene ();
G_pdevice-> present (null, null );
Certificate -------------------------------------------------------------------------------------------------------------------------------------
Define an id3dxsprite * variable:
Id3dxsprite * psprite = NULL;
Then, after creating a d3d device (for example, pd3ddevice), use d3dxcreatesprite to create a Sprite:
D3dxcreatesprite (pd3ddevice, & psprite );
Remember to use safe_release (psprite) when the device is lost/released );
When rendering a scenario, use:
Psprite-> begin (X );
... // Draw code
Psprite-> end ();
X can be a combination of the following values, such as d3dxsprite_alphablend | d3dxsprite_objectspace
D3dxsprite_donotsavestate call begin () or end () to not save/restore the device status. (For example, some statuses set in pd3ddevice-> setrenderstate)
D3dxsprite_donotmodify_renderstate is not very clear...
D3dxsprite_objectspace does not change the world matrix (World), projection matrix (Transform), and viewpoint matrix (view), and uses the matrix set on d3ddevice. If this flag is not specified, the three matrices are automatically changed to the screen space coordinates.
D3dxsprite_billboard billboard
D3dxsprite_alphablend it is very important to allow sprite to support alphablend. This flag must be specified almost every time you call in. In addition, the d3drs_alphatestenable status must be set to true. d3dblend_srcalpha/targets are both source
D3dxsprite_sort_texture sprite will be sorted by rendering order. It is recommended when rendering in the same depth of Sprite.
D3dxsprite_sort_depth_fronttoback sorts sprite Based on the rendering order from the front to the back. It is recommended to use Sprite with transparent information in different depth rendering.
D3dxsprite_sort_depth_backtofront is used to sort the Sprite in the rendering order from the back to the front. It is recommended to use transparent Sprite in different depth rendering.
It is usually d3dxsprite_alphablend, or add d3dxsprite_objectspace as needed. I am not very familiar with other applications .....
Use psprite-> draw to draw code in the middle. The function prototype is:
Hresult draw (
Lpdirect3dtexture9 ptexture,
Const rect * psrcrect,
Const d3dxvector3 * pcenter,
Const d3dxvector3 * pposition,
D3dcolor color );
Ptexture is the texture to be drawn
Psrcrect is a rectangular area on the texture to be drawn. You can specify NULL to draw an integral texture.
Pcenter is the central coordinate of the painting (This point is centered during rotation). If null is specified, the central coordinate is (0, 0, 0)
Pposition is the coordinate of the drawn position. You can also specify NULL to indicate (0, 0, 0)
Color is the color of the image, which is usually 0xffffffff. the 8 bytes of the maximum bits are alpha, red, green, and blue. If 0x80ffffff is specified, it is a translucent texture. if 0xffff0000 is used, only the red component in the texture is retained. of course, the texture itself can contain Alpha information.
For functions such as rotation, you can use psprite-> settransform (). The function prototype is:
Hresult settransform (const d3dxmatrix * ptransform );
I believe this does not need to be explained clearly.
Finally, we should remind you that psprite-> begin and psprite-> end must appear in pairs between idirect3ddevice9: beginscene and idirect3ddevice9: endscene.
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/wind2006/archive/2010/07/14/5734217.aspx