您现在的位置:首页 >> 图形媒体 >> 图形媒体 >> 内容

使用D3D8实现2D图形显示技术一(3)

时间:2011/9/3 15:03:10 点击:

例子源码:
D3DDrawHelper 
头文件:

#ifndef D3D_DRAW_HELPER_H_
#define D3D_DRAW_HELPER_H_

#include <d3d8.h>
#include <d3dx8.h>

class D3DDrawHelper
{
public:
D3DDrawHelper();
virtual ~D3DDrawHelper();

HRESULT Init(HWND hWnd);
void Destroy();

HRESULT Blank();
HRESULT Display();

inline IDirect3DDevice8 * GetDevice()
{ return m_pD3DDevice; }

HRESULT Draw(IDirect3DSurface8 * pSurface, const RECT * p_src_rect,
const POINT * p_dest_pt);

protected:
IDirect3D8 * m_pD3D;
IDirect3DDevice8 * m_pD3DDevice;
D3DPRESENT_PARAMETERS m_d3dpp;
};

#endif

源码文件:

#include "StdAfx.h"
#include "d3d_draw_helper.h"

D3DDrawHelper::D3DDrawHelper()
: m_pD3DDevice(NULL)
{
ZeroMemory(&m_d3dpp, sizeof(m_d3dpp));
}

D3DDrawHelper::~D3DDrawHelper()
{
Destroy();
}

HRESULT D3DDrawHelper::Init(HWND hWnd)
{
// acquire IDirect3D8 interface instance.
m_pD3D = Direct3DCreate8(D3D_SDK_VERSION);
if (m_pD3D == NULL)
{
::MessageBox(hWnd,
"ERROR: Failed to acquire IDirect3D8 interface instance.",
"ERROR", MB_OK);
return E_FAIL;
}

// Now get the device.
// Get the current(desktop) display mode. This is really only needed if
// we're running in a window.
// And we are using window mode. We are not using full screen mode.
D3DDISPLAYMODE display_mode;
HRESULT hRet = m_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,
&display_mode);
if(FAILED(hRet))
{
::MessageBox(hWnd,
"ERROR: Failed to acquire current display mode.",
"ERROR", MB_OK);
return hRet;
}

// OK I want 32Bit color depth in the format of X8R8G8B8.
if (display_mode.Format != D3DFMT_X8R8G8B8)
{
// All right, now you tell the user to change their display mode,
::MessageBox(hWnd,
"ERROR: Display Resolution is not up to spec. Pixel resolution should be 32bit.",
"ERROR", MB_OK);
return hRet;
}

ZeroMemory(&m_d3dpp, sizeof(m_d3dpp));
// Throw away previous frames, we don't need them
m_d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
m_d3dpp.hDeviceWindow = hWnd;
//We only need a single back buffer
m_d3dpp.BackBufferCount= 1;

// even though you uses windowed mode, we can still
// set the backbuffer width and height.
m_d3dpp.BackBufferWidth = 640;
m_d3dpp.BackBufferHeight = 480;

m_d3dpp.Windowed = TRUE;
m_d3dpp.BackBufferFormat = display_mode.Format;
m_d3dpp.Flags = D3DPRESENTFLAG_LOCKABLE_BACKBUFFER;

hRet = m_pD3D->CreateDevice(D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL, // we want full hardware support.
hWnd,
// we are not doing vertex processing.
D3DCREATE_MIXED_VERTEXPROCESSING,
&m_d3dpp,
&m_pD3DDevice);
if(FAILED(hRet))
{
::MessageBox(hWnd,
"ERROR: Failed to acquire IDitect3DDevice instance.",
"ERROR", MB_OK);
return hRet;
}

return S_OK;
}

void D3DDrawHelper::Destroy()
{
if (m_pD3DDevice)
{
m_pD3DDevice->Release();
m_pD3DDevice = NULL;
}
}

HRESULT D3DDrawHelper::Blank()
{
return m_pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET,
D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
}

HRESULT D3DDrawHelper::Display()
{
return m_pD3DDevice->Present(NULL, NULL, NULL, NULL);
}

HRESULT D3DDrawHelper::Draw(IDirect3DSurface8 * pSurface,
const RECT * p_src_rect,
const POINT * p_dest_pt)
{
if (pSurface == NULL
|| p_src_rect == NULL
|| p_dest_pt == NULL)
return E_FAIL;

IDirect3DSurface8 * pBackBuffer;
HRESULT hRet = m_pD3DDevice->GetBackBuffer(0,
D3DBACKBUFFER_TYPE_MONO, &pBackBuffer);
if (FAILED (hRet))
{
::MessageBox(NULL, "Failed 1", "ERROR", MB_OK);
return hRet;
}

hRet = m_pD3DDevice->CopyRects(pSurface,
p_src_rect,
1,
pBackBuffer,
p_dest_pt);
if (FAILED (hRet))
{
if (hRet == D3DERR_INVALIDCALL)
::MessageBox(NULL, "Failed 2", "ERROR", MB_OK);
return hRet;
}

return S_OK;
}

 
by Lacutis. 
 

D3DSurfaceHelper 

头文件:
#ifndef D3D_SURFACE_HELPER_H_
#define D3D_SURFACE_HELPER_H_

#include <d3d8.h>
#include <d3dx8.h>

class D3DSurfaceHelper
{
public:
D3DSurfaceHelper();
virtual ~D3DSurfaceHelper();

void Destroy();
// initialize the colorkey to pitch black.
HRESULT CreateSurfaceFromFile(IDirect3DDevice8 * pDevice,
const char * fn,
int img_size_x, int img_size_y,
D3DCOLOR col_key = 0xFF000000);
inline IDirect3DSurface8 * GetSurface()
{ return m_pD3DSurface; }

protected:
IDirect3DSurface8 * m_pD3DSurface;
};


#endif

源码文件:

#include "StdAfx.h"
#include "d3d_surface_helper.h"

D3DSurfaceHelper::D3DSurfaceHelper()
: m_pD3DSurface(NULL)
{
}

D3DSurfaceHelper::~D3DSurfaceHelper()
{
}

void D3DSurfaceHelper::Destroy()
{
if (m_pD3DSurface)
{
m_pD3DSurface->Release();
m_pD3DSurface = NULL;
}
}

HRESULT D3DSurfaceHelper::CreateSurfaceFromFile(IDirect3DDevice8 * pDevice,
const char * fn,
int img_size_x, int img_size_y,
D3DCOLOR col_key)
{
if (!pDevice)
return E_FAIL;

HRESULT hRet = pDevice->CreateImageSurface(img_size_x, img_size_y,
D3DFMT_X8R8G8B8, &m_pD3DSurface);
if (FAILED (hRet))
{
return hRet;
}


/*
D3DXIMAGE_INFO src_info;
ZeroMemory((BYTE *)&src_info, sizeof(D3DXIMAGE_INFO);
src_info.Width = 800;
src_info.Height = 600;
src_info.Depth = 24; // how many bits per pixel.
src_info.MipLevel = 0; // not important.
src_info.ResourceType = D3DRTYPE_TEXTURE; // set a
*/

return D3DXLoadSurfaceFromFile(m_pD3DSurface,
NULL,
NULL,
fn,
NULL,
D3DX_DEFAULT,
col_key,
NULL);
}

上一页123下一页

作者:网络 来源:转载
共有评论 0相关评论
发表我的评论
  • 大名:
  • 内容:
本类推荐
  • 没有
本类固顶
  • 没有
  • 盒子文章(www.2ccc.com) © 2024 版权所有 All Rights Reserved.
  • 沪ICP备05001939号