作者:Kagula
日期:2009-3-31
[1]了解DXUT框架的基本使用
[2]能够读懂C源码
[1]Visual Studio 2005
[2]DirectX SDK (November 2008)
在DXUT框架下,设置当前窗口的背景。
理论方面可以参考《Beginning.DirectX9》这本书,2D部份。
这里仅给出源码,C2DDraw.h、C2DDraw.cpp
#pragma once
#include "DX9Obj.h"
/*
二维绘图类
*/
class C2DDraw :public CDX9Obj //CDX9Obj是个抽象类,它规定了类的接口
{
public:
C2DDraw(LPCWSTR pFilename) //可以是JPG文件,图片尺寸会自动拉缩
{
m_pFilename = pFilename;
m_pSurface=NULL;
}
~C2DDraw(void)
{
SAFE_RELEASE(m_pSurface);
}
public:
//Callback 函数列表
HRESULT OnCreateDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc,
void* pUserContext ) ;
void OnLostDevice( void* pUserContext ) ;
HRESULT OnResetDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc,
void* pUserContext ) ;
void OnDestroyDevice( void* pUserContext ) ;
//Render
void OnFrameRender( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext ) ;
private:
LPDIRECT3DSURFACE9 m_pSurface;
LPCWSTR m_pFilename;
HRESULT createSurfaces( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc);
};
#include "dxut.h"
#include "C2DDraw.h"
//Callback 函数列表
HRESULT C2DDraw::OnCreateDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc,
void* pUserContext )
{
return createSurfaces(pd3dDevice,pBackBufferSurfaceDesc);
}
void C2DDraw::OnLostDevice( void* pUserContext )
{
SAFE_RELEASE(m_pSurface);
}
HRESULT C2DDraw::OnResetDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc,
void* pUserContext )
{
SAFE_RELEASE(m_pSurface);
return createSurfaces(pd3dDevice,pBackBufferSurfaceDesc);
}
void C2DDraw::OnDestroyDevice( void* pUserContext )
{
SAFE_RELEASE(m_pSurface);
}
//Render
void C2DDraw::OnFrameRender( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext )
{
// This will hold the back buffer
IDirect3DSurface9* backbuffer = NULL;
// Check to make sure you have a valid Direct3D device
if( NULL == pd3dDevice )
return;
// Clear the back buffer to a blue color
//pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,255), 1.0f, 0 );
// Get the back buffer
pd3dDevice->GetBackBuffer( 0,0,D3DBACKBUFFER_TYPE_MONO,&backbuffer );
// Copy the offscreen surface to the back buffer
// Note the use of NULL values for the source and destination RECTs
// This ensures a copy of the entire surface to the back buffer
//m_surface -> backbuffer
pd3dDevice->StretchRect( m_pSurface,NULL,backbuffer,NULL,D3DTEXF_NONE );
// Present the back buffer contents to the display
//pd3dDevice->Present ( NULL, NULL, NULL, NULL );
SAFE_RELEASE(backbuffer);
}
HRESULT C2DDraw::createSurfaces( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc)
{
HRESULT hr;
V_RETURN( pd3dDevice->CreateOffscreenPlainSurface(
pBackBufferSurfaceDesc->Width, // the width of the surface to create
pBackBufferSurfaceDesc->Height, // the height of the surface to create
D3DFMT_X8R8G8B8, // the surface format
D3DPOOL_DEFAULT, // the memory pool to use
&m_pSurface, // holds the resulting surface
NULL) ); // reserved; should be NULL
V_RETURN( D3DXLoadSurfaceFromFile( m_pSurface,NULL,NULL,m_pFilename,NULL,D3DX_DEFAULT,0,NULL ) );
return hr;
}