The following is the program code: //----------------------------------------------------------------------------- // Name: invalidatedeviceobjects () // Desc: If the lost device can be restored, the application prepares // Device by destroying all video-memory resources and any // Swap chains. This is typically accomplished by using the safe_release // Macro. //----------------------------------------------------------------------------- Hresult invalidatedeviceobjects (void) { // // To see how mismanagement of an object's reference counter can cause us // Problems when calling reset on the device, uncomment the line below. // The line below will call addref () on the vertex buffer object, which W // Ill add one to the vertex buffer's reference count. This will cause it // To hang around after we call release () on it, which is not what we // Wanted to happen here. // // G_pvertexbuffer-> addref (); // // Note: You cocould use the safe_release macro to invalidate your device // Objects like so: // Safe_release (g_ptexture) Safe_release (g_pvertexbuffer) Safe_release (g_pteapotmesh)
Return s_ OK; } //----------------------------------------------------------------------------- // Name: restoredeviceobjects () // Desc: You are encouraged to develop applications with a single code path // Respond to device loss. This code path is likely to be similar, if not // Identical, to the code path taken to initialize the device at startup. //----------------------------------------------------------------------------- Hresult restoredeviceobjects (void) { // // Set some important State settings... // D3dxmatrix matproj; D3dxmatrixperspectivefovlh (& matproj, d3dxtoradian (45.0f ), 640366f/480366f, // (Float) (g_d3dpp.backbufferwidth/g_d3dpp.backbufferheight ), 0.1f, 100366f ); G_pd3ddevice-> settransform (d3dts_projection, & matproj ); G_pd3ddevice-> setrenderstate (d3drs_zenable, true ); G_pd3ddevice-> setrenderstate (d3drs_lighting, true ); G_pd3ddevice-> setrenderstate (d3drs_specularenable, true ); G_pd3ddevice-> setlight (0, & g_plight0 ); G_pd3ddevice-> lightenable (0, true ); G_pd3ddevice-> setrenderstate (d3drs_ambient, d3dcolor_colorvalue (0.2f, 0.2f, 0.2f, 1.0f )); // // Create a texture object... // D3dxcreatetexturefromfile (g_pd3ddevice, "test.bmp", & g_ptexture ); G_pd3ddevice-> setsamplerstate (0, d3dsamp_minfilter, d3dtexf_linear ); G_pd3ddevice-> setsamplerstate (0, d3dsamp_magfilter, d3dtexf_linear ); // // Create a vertex buffer... // // Note: When a device is lost, vertex buffers created using // D3dpool_default must be released properly before calling // Idirect3ddevice9: reset. // G_pd3ddevice-> createvertexbuffer (4 * sizeof (quadvertex ), D3dusage_writeonly, Quadvertex: fvf_flags, // D3dpool_managed, // does not have to be properly released before calling idirect3ddevice9: reset D3dpool_default, // must be released properly before calling idirect3ddevice9: reset & G_pvertexbuffer, null ); Void * pvertices = NULL; G_pvertexbuffer-> lock (0, sizeof (g_quadvertices), (void **) & pvertices, 0 ); Memcpy (pvertices, g_quadvertices, sizeof (g_quadvertices )); G_pvertexbuffer-> unlock (); // // Create a mesh object... // // Note: When a device is lost, meshes created using d3dxmesh_dynamic // Must be released properly before calling idirect3ddevice9: reset. // D3dxloadmeshfromx ("teapot. X ", // D3dxmesh_systemmem, // does not have to be properly released before calling idirect3ddevice9: reset // D3dxmesh_managed, // does not have to be properly released before calling idirect3ddevice9: reset // D3dxmesh_writeonly, // does not have to be properly released before calling idirect3ddevice9: reset D3dxmesh_dynamic, // must be released properly before calling idirect3ddevice9: reset G_pd3ddevice, Null, null, & g_pteapotmesh ); Return s_ OK; } //----------------------------------------------------------------------------- // Name: render () // Desc: //----------------------------------------------------------------------------- Void render (void) { // // Before we render, we need to make sure we haven'tlost our device. // If we have, we'll need to restore it before we can continue. // Hresult hr; If (g_bdevicelost = true) { // Yield some CPU time to other processes Sleep (100); // 100 milliseconds // // Test the cooperative level to see if it's okay to render. // The application can determine what to do on encountering a lost // Device by querying the return value of the testcooperativelevel // Method. // If (failed (hR = g_pd3ddevice-> testcooperativelevel ())) { // The device has been lost but cannot be reset at this time. // Therefore, rendering is not possible and we'll have to return // And try again at a later time. If (hR = d3derr_devicelost) Return; // The device has been lost but it can be reset at this time. If (hR = d3derr_devicenotreset) { // // If the device can be restored, the application prepares // Device by destroying all video-memory resources and any // Swap chains. // Invalidatedeviceobjects (); // // Then, the application callthe reset method. // // Reset is the only method that has an effect when a device // Is lost, and is the only method by which an application can // Change the device from a lost to an operational state. // Reset will fail unless the application releases all // Resources that are allocated in d3dpool_default, including // Those created by the idirect3ddevice9: createrendertarget // And idirect3ddevice9: createdepthstencilsurface methods. // HR = g_pd3ddevice-> Reset (& g_d3dpp ); If (failed (HR )) Return; // // Finally, a lost device must re-create resources (including // Video memory resources) after it has been reset. // Restoredeviceobjects (); } Return; } G_bdevicelost = false; } // // Render a teapot and textured quad... // G_pd3ddevice-> clear (0, null, d3dclear_target | d3dclear_zbuffer, D3dcolor_colorvalue (0.35f, 0.53f, 0.7f, 1.0f), 1.0f, 0 ); G_pd3ddevice-> beginscene (); { D3dxmatrix matview; D3dxmatrix matworld; D3dxmatrix matrotation; D3dxmatrix mattranslation; D3dxmatrixidentity (& matview ); G_pd3ddevice-> settransform (d3dts_view, & matview ); // Place and render first teapot... D3dxmatrixrotationyawpitchroll (& matrotation, d3dxtoradian (g_fspian), d3dxtoradian (g_fspiny), 0.0f ); D3dxmatrixtranslation (& mattranslation, 1.5f, 0.0f, 6.0f ); Matworld = matrotation * mattranslation; G_pd3ddevice-> settransform (d3dts_world, & matworld ); G_pd3ddevice-> setmaterial (& g_teapotmtrl ); G_pteapotmesh-> drawsubset (0 ); // Place and render textured quad... D3dxmatrixtranslation (& mattranslation,-1.5f, 0.0f, 6.0f ); Matworld = matrotation * mattranslation; G_pd3ddevice-> settransform (d3dts_world, & matworld ); G_pd3ddevice-> setmaterial (& g_quadmtrl ); G_pd3ddevice-> settexture (0, g_ptexture ); G_pd3ddevice-> setstreamsource (0, g_pvertexbuffer, 0, sizeof (quadvertex )); G_pd3ddevice-> setfvf (quadvertex: fvf_flags ); G_pd3ddevice-> drawprimitive (d3dpt_trianglestrip, 0, 2 ); } G_pd3ddevice-> endscene (); // // If present fails with d3derr_devicelost the application needs to be // Notified so it cleanup resources and reset the device. // HR = g_pd3ddevice-> present (null, null ); If (g_bhandlelostdevice = true) { If (hR = d3derr_devicelost) G_bdevicelost = true; } } |