Code: Select all
/////////////CSurface.h/////////////////
class CSurface
{
private:
DWORD m_Color;
UINT m_uWidth;
UINT m_uHeight;
char* m_pszPathname;
public:
CSurface(void);
~CSurface(void);
void CreateSurface(LPDIRECT3D8 pD3D, LPDIRECT3DDEVICE8 pD3D_Device, char* Pathname, UINT width, UINT height, DWORD color);
protected:
LPDIRECT3DSURFACE8 m_pSurface;
};
////////////CSurface.cpp//////////////
CSurface::CSurface(void) : m_pszPathname(NULL), m_Color(0), m_uWidth(0), m_uHeight(0)
{}
CSurface::~CSurface(void)
{
SAFE_RELEASE(m_pSurface);
SAFE_DELETE_ARRAY(m_pszPathname);
}
void CSurface::CreateSurface(LPDIRECT3D8 pD3D, LPDIRECT3DDEVICE8 pD3D_Device, char *Pathname, UINT width, UINT height, DWORD color)
{
D3DDISPLAYMODE DisplayMode;
D3DXIMAGE_INFO srcInfo;
LPDIRECT3DSURFACE8 pSurface;
HRESULT hr;
if (!pD3D_Device) Except(999, "Invalid Direct3D device", "CSurface::CreateSurface()");
m_Color = color;
m_pszPathname = new char[strlen(Pathname) + 1];
strcpy(m_pszPathname, Pathname);
m_uWidth = width;
m_uHeight = height;
hr = pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &DisplayMode);
if (FAILED(hr)) Except(999, "Failed to get adapter display mode", "CSurface::CreateSusrface()");
if (m_uWidth == 0 || m_uHeight == 0)
{
// Gets image size.
hr = pD3D_Device->CreateImageSurface(1, 1, DisplayMode.Format, &pSurface );
if (FAILED(hr)) Except(999, "Failed to create image surface", "CSurface::CreateSusrface()");
hr = D3DXLoadSurfaceFromFile(pSurface, NULL, NULL, Pathname, NULL, D3DX_FILTER_NONE, 0, &srcInfo);
if (FAILED(hr)) Except(999, "Failed to load surface from file", "CSurface::CreateSusrface()");
hr = pSurface->Release();
if (FAILED(hr)) Except(999, "Failed to release surface", "CSurface::CreateSusrface()");
//Создаём поверхность
hr = pD3D_Device->CreateImageSurface(srcInfo.Width, srcInfo.Height, DisplayMode.Format, &m_pSurface);
if (FAILED(hr)) Except(999, "Failed to create image surface", "CSurface::CreateSusrface()");
}
else
{
//Creates surface
hr = pD3D_Device->CreateImageSurface(m_uWidth, m_uHeight, DisplayMode.Format, &m_pSurface);
if (FAILED(hr)) Except(999, "Failed to create image surface", "CSurface::CreateSusrface()");
}
// Loads surface from file
hr = D3DXLoadSurfaceFromFile(m_pSurface, NULL, NULL, m_pszPathname, NULL, D3DX_FILTER_NONE, m_Color, NULL);
if (FAILED(hr)) Except(999, "Failed to load surface from file", "CSurface::CreateSusrface()");
}
/////////////CBackSurface.h//////////////
class CBackSurface: public CSurface
{
private:
public:
CBackSurface(void);
~CBackSurface(void);
void Update(LPDIRECT3DDEVICE8 pD3D_Device, LPDIRECT3DSURFACE8 pBackBuffer);
};
//////////////CBackSurface.cpp///////////
CBackSurface::CBackSurface(void) : CSurface() {}
CBackSurface::~CBackSurface(void)
{
CSurface::~CSurface();
}
void CBackSurface::Update(LPDIRECT3DDEVICE8 pD3D_Device, LPDIRECT3DSURFACE8 pBackBuffer)
{
HRESULT hr;
if (!pBackBuffer) Except(999, "Invalid back buffer passed in", "CBackSurface::Update()");
hr = pD3D_Device->CopyRects(m_pSurface, NULL, 0, pBackBuffer, NULL);
if (FAILED(hr)) Except(999, "Failed to render back surface", "CBackSurface::Update()");
}
Code: Select all
CBackSurface surface;
surface.CreateSurface(D3D8Object, D3DDevice8Object(), "filename.bmp", 800, 600, D3DCOLOR_XRGB(255, 255, 255);
Then in game loop between beginScene() and endScene() we call:
surface.Update(D3DDevice8Object, BackBuffer);
//BackBuffer var is a surface:
LPDIRECT3DSURFACE8 BackBuffer;
//To init this var we must in video driver initialization code write:
hr = m_pD3D_Device->GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO, &BackBuffer);
What you think about it?