在D3D环境下进行2D图形绘制。
D3DImage类:这个类是是用来载入图片,绘制图片。
- //-----------------------------------------------------------------------------
- // 文件: D3DImage.h
- // 描述: D3DImage类的头文件,定义变量,函数。
- //-----------------------------------------------------------------------------
- #pragma once
- #include <d3dx9.h>
- #pragma comment (lib, "d3d9.lib")
- #pragma comment (lib, "d3dx9.lib")
- class D3DImage
- {
- public:
- D3DImage(void);
- D3DImage(LPDIRECT3DDEVICE9 _pd3dDevice);
- public:
- ~D3DImage(void);
- public:
- HRESULT setVertex();
- VOID drawImage(INT _mode, FLOAT _parameter, FLOAT _parameter2=0);
- VOID setPosition(FLOAT _x, FLOAT _y);
- VOID setCenter(FLOAT _x, FLOAT _y);
- VOID setZoom(FLOAT _zoomX, FLOAT _zoomY);
- VOID setRotation(FLOAT _rotation);
- VOID move(FLOAT _x, FLOAT _y);
- VOID loadImage(LPCTSTR _pSrcFile);
- private:
- VOID drawImageIndex(INT _index);
- VOID drawImageAnime(INT _delay);
- VOID drawImageRotation( FLOAT _rotation, INT _index);
- private:
- LPDIRECT3DDEVICE9 m_pd3dDevice; // D3D设备,用于渲染
- LPDIRECT3DVERTEXBUFFER9 m_pVB; // 顶点缓冲
- LPDIRECT3DTEXTURE9 * m_pTexture; // 纹理指针数组
- FLOAT m_fX, m_fY; // 图片坐标
- FLOAT m_fWidth; // 图片宽度
- FLOAT m_fHeight; // 图片高度
- FLOAT m_fZoomX; // 图片X轴方向缩放比率
- FLOAT m_fZoomY; // 图片Y轴方向缩放比率
- FLOAT * m_fRotation; // 图片旋转角度
- INT m_iMaxIndex; // 最大帧数
- INT m_iCurrentIndex; // 当前帧数
- INT m_iDelay; // 延迟变量
- FLOAT * m_fRotationTemp; // 图片旋转角度临时指针
- LPDIRECT3DTEXTURE9 * m_pTextureTemp; // 纹理指针数组临时指针
- //自定义定点结构体
- struct CUSTOMVERTEX
- {
- FLOAT x, y, z; // 3D坐标
- DWORD color; // 顶点颜色
- FLOAT u, v; // 顶点纹理
- };
- // 自定义FVF
- #define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1)
- };
- //-----------------------------------------------------------------------------
- // 文件: D3DImage.cpp
- // 描述: 实现2D图片的绘制。可以缩放,移动,旋转图片。可实现基本动画。
- //-----------------------------------------------------------------------------
- #include "D3DImage.h"
- D3DImage::D3DImage(void)
- {
- }
- //-----------------------------------------------------------------------------
- // 名称: D3DImage()
- // 描述: 自定义构造函数
- //-----------------------------------------------------------------------------
- D3DImage::D3DImage(LPDIRECT3DDEVICE9 _pd3dDevice)
- {
- // 变量赋值、初始化
- m_pd3dDevice = _pd3dDevice;
- m_iMaxIndex = 0;
- m_iCurrentIndex = 0;
- m_iDelay = 0;
- m_fRotation = new FLOAT[m_iMaxIndex+1];
- m_pTexture = new LPDIRECT3DTEXTURE9[m_iMaxIndex+1];
- ZeroMemory(m_fRotation,(m_iMaxIndex+1)*sizeof(FLOAT));
- // 设置顶点
- setVertex();
- }
- D3DImage::~D3DImage(void)
- {
- }
- //-----------------------------------------------------------------------------
- // 名称: loadImgae()
- // 描述: 载入图片
- //-----------------------------------------------------------------------------
- VOID D3DImage::loadImage(LPCTSTR _pSrcFile)
- {
- // 自动扩展纹理数组
- m_pTextureTemp = new LPDIRECT3DTEXTURE9[m_iMaxIndex+2];
- m_fRotationTemp = new FLOAT[m_iMaxIndex+2];
- ZeroMemory(m_fRotationTemp,(m_iMaxIndex+2)*sizeof(FLOAT));
- memcpy(m_pTextureTemp,m_pTexture, (m_iMaxIndex+1)*sizeof(LPDIRECT3DTEXTURE9));
- memcpy(m_fRotationTemp,m_fRotation, (m_iMaxIndex+1)*sizeof(FLOAT));
- delete m_pTexture;
- delete m_fRotation;
- m_pTexture = m_pTextureTemp;
- m_fRotation = m_fRotationTemp;
- // 获得图片基本信息
- D3DXIMAGE_INFO imageInfo;
- D3DXGetImageInfoFromFile(_pSrcFile, &imageInfo);
- // 载入图片
- D3DXCreateTextureFromFileEx(
- m_pd3dDevice,
- _pSrcFile,
- imageInfo.Width,
- imageInfo.Height,
- imageInfo.MipLevels,
- D3DUSAGE_RENDERTARGET,
- D3DFMT_A8R8G8B8,
- D3DPOOL_DEFAULT,
- D3DX_FILTER_NONE,
- D3DX_DEFAULT,
- 0,
- NULL,
- NULL,
- &m_pTexture[m_iMaxIndex]);
- // 设置图片信息
- m_fWidth = imageInfo.Width;
- m_fHeight = imageInfo.Height;
- m_fX = m_fWidth/2; // 默认X坐标为宽度的一半
- m_fY = m_fHeight/2; // 默认Y坐标为高度的一半
- m_fZoomX = 1.0f; // 默认X1倍缩放
- m_fZoomY = 1.0f; // 默认Y1倍缩放
- // 索引上限加一
- m_iMaxIndex++;
- }
- //-----------------------------------------------------------------------------
- // 名称: setPosition()
- // 描述: 设置图片位置
- //-----------------------------------------------------------------------------
- VOID D3DImage::setPosition(FLOAT _x, FLOAT _y)
- {
- m_fX = _x+m_fWidth/2*m_fZoomX;
- m_fY = _y+m_fHeight/2*m_fZoomY;
- setVertex();
- }
- //-----------------------------------------------------------------------------
- // 名称: setCenter()
- // 描述: 设置图片位置
- //-----------------------------------------------------------------------------
- VOID D3DImage::setCenter(FLOAT _x, FLOAT _y)
- {
- m_fX = _x;
- m_fY = _y;
- setVertex();
- }
- //-----------------------------------------------------------------------------
- // 名称: move()
- // 描述: 移动图片位置
- //-----------------------------------------------------------------------------
- VOID D3DImage::move(FLOAT _x, FLOAT _y)
- {
- m_fX += _x;
- m_fY += _y;
- setVertex();
- }
- //-----------------------------------------------------------------------------
- // 名称: setZoom()
- // 描述: 设置图片缩放
- //-----------------------------------------------------------------------------
- VOID D3DImage::setZoom(FLOAT _zoomX,FLOAT _zoomY)
- {
- m_fZoomX = _zoomX;
- m_fZoomY = _zoomY;
- setVertex();
- }
- //-----------------------------------------------------------------------------
- // 名称: setRotation()
- // 描述: 设置图片旋转
- //-----------------------------------------------------------------------------
- VOID D3DImage::setRotation(FLOAT _rotation)
- {
- _rotation = _rotation * D3DX_PI /180;
- D3DXMATRIX matRotate;
- D3DXMatrixIdentity(&matRotate);
- FLOAT _fCosR = cosf(_rotation);
- FLOAT _fSinR = sinf(_rotation);
- FLOAT _fCenterX = 0.5f;
- FLOAT _fCenterY = 0.5f;
- matRotate._11 = _fCosR;
- matRotate._12 = _fSinR;
- matRotate._21 = -_fSinR;
- matRotate._22 = _fCosR;
- matRotate._31 = _fCenterX + (-_fCenterY * _fCosR)-(-_fCenterX * _fSinR);
- matRotate._32 = _fCenterY + (-_fCenterY * _fSinR)+(-_fCenterX * _fCosR);
- m_pd3dDevice->SetTransform(D3DTS_TEXTURE0, &matRotate);
- }
- //-----------------------------------------------------------------------------
- // 名称: setVertex()
- // 描述: 设置图片位置
- //-----------------------------------------------------------------------------
- HRESULT D3DImage::setVertex()
- {
- // 自定义顶点缓冲数组
- CUSTOMVERTEX Vertices[] =
- {
- //{m_fX-m_fWidth/2*m_fZoomX, m_fY-m_fHeight/2*m_fZoomY, 0.0f, 0xffffffff, (0 -0.5f)/m_fWidth, (0 -0.5f)/m_fHeight}, //0
- //{m_fX+m_fWidth/2*m_fZoomX, m_fY-m_fHeight/2*m_fZoomY, 0.0f, 0xffffffff, (m_fWidth-0.5f)/m_fWidth, (0 -0.5f)/m_fHeight}, //1
- //{m_fX+m_fWidth/2*m_fZoomX, m_fY+m_fHeight/2*m_fZoomY, 0.0f, 0xffffffff, (m_fWidth-0.5f)/m_fWidth, (m_fHeight-0.5f)/m_fHeight}, //2
- //{m_fX-m_fWidth/2*m_fZoomX, m_fY-m_fHeight/2*m_fZoomY, 0.0f, 0xffffffff, (0 -0.5f)/m_fWidth, (0 -0.5f)/m_fHeight}, //0
- //{m_fX+m_fWidth/2*m_fZoomX, m_fY+m_fHeight/2*m_fZoomY, 0.0f, 0xffffffff, (m_fWidth-0.5f)/m_fWidth, (m_fHeight-0.5f)/m_fHeight}, //2
- //{m_fX-m_fWidth/2*m_fZoomX, m_fY+m_fHeight/2*m_fZoomY, 0.0f, 0xffffffff, (0 -0.5f)/m_fWidth, (m_fHeight-0.5f)/m_fHeight}, //3
- {m_fX-m_fWidth/2*m_fZoomX, m_fY-m_fHeight/2*m_fZoomY, 0.0f, 0xffffffff, 0.0f, 0.0f}, //0
- {m_fX+m_fWidth/2*m_fZoomX, m_fY-m_fHeight/2*m_fZoomY, 0.0f, 0xffffffff, 1.0f, 0.0f}, //1
- {m_fX+m_fWidth/2*m_fZoomX, m_fY+m_fHeight/2*m_fZoomY, 0.0f, 0xffffffff, 1.0f, 1.0f}, //2
- {m_fX-m_fWidth/2*m_fZoomX, m_fY-m_fHeight/2*m_fZoomY, 0.0f, 0xffffffff, 0.0f, 0.0f}, //0
- {m_fX+m_fWidth/2*m_fZoomX, m_fY+m_fHeight/2*m_fZoomY, 0.0f, 0xffffffff, 1.0f, 1.0f}, //2
- {m_fX-m_fWidth/2*m_fZoomX, m_fY+m_fHeight/2*m_fZoomY, 0.0f, 0xffffffff, 0.0f, 1.0f}, //3
- };
- // 创建定点缓冲
- if( FAILED( m_pd3dDevice->CreateVertexBuffer( sizeof( Vertices ),
- 0, D3DFVF_CUSTOMVERTEX,
- D3DPOOL_DEFAULT, &m_pVB, NULL ) ) )
- {
- return E_FAIL;
- }
- // 填充定点缓冲
- VOID* pVertices;
- if( FAILED( m_pVB->Lock( 0, sizeof( Vertices ), ( void** )&pVertices, 0 ) ) )
- return E_FAIL;
- memcpy( pVertices, Vertices, sizeof( Vertices ) );
- m_pVB->Unlock();
- return S_OK;
- }
- //-----------------------------------------------------------------------------
- // 名称: drawImage()
- // 描述: 绘制图片
- //-----------------------------------------------------------------------------
- VOID D3DImage::drawImage(INT _mode, FLOAT _parameter, FLOAT _parameter2)
- {
- switch(_mode)
- {
- case 0:
- drawImageIndex(_parameter);
- break;
- case 1:
- drawImageAnime(_parameter);
- break;
- case 2:
- drawImageRotation(_parameter, _parameter2);
- break;
- }
- }
- //-----------------------------------------------------------------------------
- // 名称: drawImageIndex()
- // 描述: 绘制单一帧图片
- //-----------------------------------------------------------------------------
- VOID D3DImage::drawImageIndex(INT _index)
- {
- if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )
- {
- // 绘制
- m_pd3dDevice->SetStreamSource( 0, m_pVB, 0, sizeof( CUSTOMVERTEX ) );
- m_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
- // 设置贴图
- m_pd3dDevice->SetTexture(0,m_pTexture[_index]);
- // 绘制矩形
- m_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 2);
- // 结束
- m_pd3dDevice->EndScene();
- }
- }
- //-----------------------------------------------------------------------------
- // 名称: drawImageAnime()
- // 描述: 绘制切帧动画
- //-----------------------------------------------------------------------------
- VOID D3DImage::drawImageAnime(INT _delay)
- {
- if (m_iDelay < _delay)
- m_iDelay++;
- else
- {
- if (m_iCurrentIndex < m_iMaxIndex-1)
- {
- m_iCurrentIndex++;
- }
- else
- {
- m_iCurrentIndex = 0;
- }
- m_iDelay = 0;
- }
- if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )
- {
- // 绘制
- m_pd3dDevice->SetStreamSource( 0, m_pVB, 0, sizeof( CUSTOMVERTEX ) );
- m_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
- // 设置贴图
- m_pd3dDevice->SetTexture(0,m_pTexture[m_iCurrentIndex]);
- // 绘制矩形
- m_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 2);
- // 结束
- m_pd3dDevice->EndScene();
- }
- }
- //-----------------------------------------------------------------------------
- // 名称: drawImageRotation()
- // 描述: 绘制旋转动画
- //-----------------------------------------------------------------------------
- VOID D3DImage::drawImageRotation(FLOAT _rotation, INT _index)
- {
- if (m_fRotation[_index] < 360)
- {
- m_fRotation[_index] += _rotation;
- }
- else
- {
- m_fRotation[_index] = m_fRotation[_index] - 360;
- }
- if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )
- {
- // 绘制
- m_pd3dDevice->SetStreamSource( 0, m_pVB, 0, sizeof( CUSTOMVERTEX ) );
- m_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
- // 设置贴图
- m_pd3dDevice->SetTexture(0,m_pTexture[_index]);
- // 旋转贴图
- setRotation(m_fRotation[_index]);
- // 绘制矩形
- m_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 2);
- // 恢复贴图旋转
- setRotation(0);
- // 结束
- m_pd3dDevice->EndScene();
- }
- }