Design game menu background to move cyclically

Source: Internet
Author: User

Demo: http://download.csdn.net/detail/jiangcaiyang123/4060455

The program is as follows:

Design game menu background to move cyclically

DirectX texture is very important. Just like a person's face. Without it, it would be as ugly as a human face without facial features. Therefore, for our beginners, mastering texture processing is equivalent to learning a considerable amount of DirectX knowledge. This time I used some texture knowledge in DirectX to implement the context loop of menus that often appear in the game.

To use textures, you must first learn to load textures. I use the d3dxcreatetexturefromfileex () function. Because it has many controllable parameters, it can achieve good results. For more information about the parameters of this function, see msdn. After loading the image, I use the d3dximage_info struct to obtain its width and height. After obtaining the texture, You Can tile it to the screen. The screen resolution is assumed to be 640x480.

After a texture is loaded, the custom vertex format is used. The format is defined as follows:
Struct stvertex // define your vertex Structure
{
Float x, y, z; // texture coordinate position
Float U, V;
// Position in the texture coordinate
}
The u and v variables are used to control the texture tile. Then I will change these two variables in the rendering process to achieve the effect of moving cyclically. When u and v are in a range of less than 0 or more than 1.0, their rendering behavior is usually not fixed. However, DirectX uses the tile effect by default. You can also change it to a mirrored tile without rendering (see the real time rendering series ). You do not have to change the tile effect. Use the default tile effect.
Finally, set the flexible vertex format (fvf), set the source of the vertex stream, and use drawprimitive to draw the image.

Some code of the program is as follows:

// Backgroundmove. CPP implementation file # include "backgroundmove. H "cbackgroundmove: cbackgroundmove (hinstance hinst, hwnd, lpdirect3ddevice9 pdevice, int width, int height) // constructor {// Member values m_pdevice = pdevice; m_texoffsetx = 0.0f; m_texoffsety = 0.0f; m_movespeedx = 0.005f; m_movespeedy = 0.02f; // initialize the input system watermark (immediate, hinst, hwnd); then (buffered, hinst, hwnd); // set the angle of view range Setviewport (width, height); // initialize the vertex initializevertices (); // set the texture settexture (width, height);} void cbackgroundmove: setviewport (INT width, int height) // set the Angle of View {zeromemory (& m_viewport, sizeof (m_viewport); // clear m_viewport.x = 0; m_viewport.y = 0; m_viewport.width = width; m_viewport.height = height; orientation = 0.0f; m_viewport.maxz = 1.0f; m_pdevice-> setviewport (& m_viewport);} void cbackgroundmov E: Draw (void) // drawing {m_pdevice-> drawprimitive (d3dpt_trianglestrip, 0, 2); // drawing function // start interaction m_immediateinput.updatekeystate (); if (m_immediateinput.keydown (dik_up) {m_pvertices [1]. Y + = 0.05f; m_pvertices [3]. Y + = 0.05f;} If (m_immediateinput.keydown (dik_down) {m_pvertices [1]. y-= 0.05f; m_pvertices [3]. y-= 0.05f;} // If (m_immediateinput.keydown (dik_d) {m_movespeedx + = 0.005f;} If (m_im Mediateinput. keydown (dik_a) {m_movespeedx-= 0.005f;} If (second (dik_w) {m_movespeedy + = 0.005f;} If (second (dik_s) {m_movespeedy-= 0.005f ;} // The texture background is automatically moved to int I; for (I = 0; I <4; I ++) {m_pvertices [I]. U + = m_movespeedx; m_pvertices [I]. V + = m_movespeedy;} unsigned long cbackgroundmove: release (void) // release space {If (m_pbuffer! = 0) {m_pbuffer-> release (); m_pbuffer = 0;} If (m_ptexture! = 0) {m_pbuffer-> release (); m_pbuffer = 0;} m_immediateinput.release (); m_bufferinput.release (); Return 0;} void cbackgroundmove: initializevertices (void) // initialize vertex {// create vertex cache hresult hr; HR = m_pdevice-> createvertexbuffer (4 * sizeof (stvertex), d3dusage_writeonly, texture_fvf, d3dpool_managed, & m_pbuffer, null ); throwiffailed (HR, "no, it cannot create a vertex cache. ⊙ B Khan "); HR = m_pbuffer-> lock (0, 4 * sizeof (stvertex), (void **) & m_pvertices, d3dlock_discard); throwiffailed (HR, "No, how can I lock the vertex ?? O (> unlock <) O "); HR = m_pbuffer-> unlock (); throwiffailed (HR," Impossible. How can I unlock the vertex as well ?? O (> vertex <) O "); // set the vertex cache and flexible vertex format m_pdevice-> setstreamsource (0, m_pbuffer, 0, sizeof (stvertex )); // The cache of vertices of the same resource should be combined as m_pdevice-> setfvf (texture_fvf);} void cbackgroundmove: settexture (INT width, int height) // set texture {// load texture d3dximage_info imageinfo; hresult hr; HR = d3dxcreatetexturefromfileex (// create texture m_pdevice from file, // direct3ddevice9 structure pointer text ("test image .png "), // The uploaded image file name d3dx_default, // d3dx_default in width, // high d3dx_fr Om_file, // MIP level 0, // usage d3dfmt_a8r8g8b8, // format d3dpool_managed, // memory pool format d3dx_default, // filter d3dx_default, // MIP filter 0, // key color (used as mask) & imageinfo, // source file information null, // color palette & m_ptexture); // idirect3dtexture9 pointer throwiffailed (HR, "impossible, failed to load texture! Is the file name incorrect? "); // Set the vertex floatnu, NV; Nu = float (width)/float (imageinfo. width), NV = float (height)/float (imageinfo. height); m_pvertices [0]. set (-1.0f,-1.0f, 1.0f, m_texoffsetx, m_texoffsety + NV); m_pvertices [1]. set (-1.0f, 1.0f, 1.0f, m_texoffsetx, m_texoffsety); m_pvertices [2]. set (1.0f,-1.0f, 1.0f, m_texoffsetx + Nu, m_texoffsety + NV); m_pvertices [3]. set (1.0f, 1.0f, 1.0f, m_texoffsetx + Nu, m_te Xoffsety); // set to turn off the light hR = m_pdevice-> setrenderstate (d3drs_lighting, false); throwiffailed (HR, "Why can't I turn off the light? "); // Set the texture hR = m_pdevice-> settexture (0, m_ptexture); throwiffailed (HR," No way, the texture setting is incorrect? ");}

The program is as follows:


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.