#pragma once
#include
#include
class MGraphic
{
private :
//doi tuong direct x
LPDIRECT3D9 d3d;
//thiet bi ve
LPDIRECT3DDEVICE9 d3ddev;
//Ve texture
LPD3DXSPRITE d3dxSprite;
//hwnd cua cua so ve
HWND hWnd;
//Kich thuoc cua bộ đệm
int Width;
int Height;
public:
//Cài đặt thiết bị vẽ
bool InitD3D[];
//Constructor
MGraphic[HWND,int,int];
//Destructor
~MGraphic[void];
//Bắt đầu vẽ
void Begin[];
//Kết thúc vẽ
-
void End[];
//Vẽ các hình cơ bản
void DrawTexture[LPDIRECT3DTEXTURE9 texture,D3DXVECTOR2 postion,D3DCOLOR color];
void DrawTexture[LPDIRECT3DTEXTURE9 texture,RECT source,D3DXVECTOR2 postion,D3DCOLOR color];
void DrawTexture[LPDIRECT3DTEXTURE9 texture,D3DXVECTOR2 postion,D3DCOLOR color,float angle];
//Vẽ các sufface
void DrawSurface[];
//Lấy thiết bị vẽ
LPDIRECT3DDEVICE9 GetDevice[];
};
MGraphic::MGraphic[HWND hwnd,int W,int H]
{
this->hWnd = hwnd;
this->Width = W;
this->Height = H;
this->d3d = NULL;
this->d3ddev = NULL;
this->d3dxSprite = NULL;
}
bool MGraphic::InitD3D[]
{
//tao mot doi tuong Direct3D
this->d3d = Direct3DCreate9[D3D_SDK_VERSION];
if[!this->d3d]
return false;
//tao thiet bi ve
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory[&d3dpp,sizeof[d3dpp]];
d3dpp.BackBufferCount = 1;// Có một back buffer
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;//Tự tạo format phù hợp
d3dpp.BackBufferWidth = this->Width;//Chiều rộng back buffer
d3dpp.BackBufferHeight = this->Height;//Chiều cao back buffer
d3dpp.hDeviceWindow = this->hWnd;// Handle của cửa sổ mình sẽ vẽ lên
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;// Một tham số hay dùng
d3dpp.Windowed = true;// Chế độ có cửa sổ
HRESULT hr = this->d3d->CreateDevice[D3DADAPTER_DEFAULT,//chon thiet bi ve mac dinh
D3DDEVTYPE_HAL,//su dung ho tro cua phan cung. Có rất nhiều sự lựa chọn. Tham khảo MSDN để biết thêm
this->hWnd,// Cửa sổ vẽ
D3DCREATE_SOFTWARE_VERTEXPROCESSING,//MSDN không nói rõ. :D
&d3dpp,//Các thông số cho back buffer
&this->d3ddev//Con trỏ nhận dữ liẹu sau khi tạo devicce
];
// Ở đây vì ngắn gọn nên mình chỉ tạo device cho thiết bị có hỗ trợ phần cứng.
if[FAILED[hr]]
{
return false;
}
Tạo bút vẽ các texture
hr = D3DXCreateSprite[this->d3ddev,&this->d3dxSprite];
if[FAILED[hr]]
return false;
return true;
}
MGraphic::~MGraphic[void]
{
}
void MGraphic::Begin[]
{
// xóa cửa sổ vẽ bằng màu trắng D3DCOLOR_XRGB[255,255,255]
this->d3ddev->Clear[1,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB[255,255,255],1,NULL];
// Bắt đầu vẽ
this->d3ddev->BeginScene[];
this->d3dxSprite->Begin[D3DXSPRITE_ALPHABLEND];
}
void MGraphic::End[]
{
// Kết thúc vẽ
this->d3dxSprite->End[];
this->d3ddev->EndScene[];
// Thể hiện các đối tượng từ back buffer ra ngoài màn hình
this->d3ddev->Present[NULL,NULL,NULL,NULL];
}
void MGraphic::DrawTexture[LPDIRECT3DTEXTURE9 texture,D3DXVECTOR2 postion,D3DCOLOR color]
{
this->d3dxSprite->Draw[
texture,//hình cần vẽ
NULL,//khung cắt từ ảnh. Xem thêm các bài viết trên cviet về sprite. Chọn NULL là lấy hết cả texture
NULL,//Tâm của texture. Chọn NULL là tọa đọ 0,0
&D3DXVECTOR3[postion.x,postion.y,0],//Tọa đọ của texture trên màn hình đồ họa
color//màu của texture
];
}
void MGraphic::DrawTexture[LPDIRECT3DTEXTURE9 texture,RECT source,D3DXVECTOR2 postion,D3DCOLOR color]
{
// Tương tự
this->d3dxSprite->Draw[texture,&source,NULL,&D3DXVECTOR3[postion.x,postion.y,0],color];
}
void MGraphic::DrawSurface[]
{
}
LPDIRECT3DDEVICE9 MGraphic::GetDevice[]
{
return this->d3ddev;
}
void MGraphic::DrawTexture[LPDIRECT3DTEXTURE9 texture,D3DXVECTOR2 postion,D3DCOLOR color,float angle]
{
// Vừa vẽ vừa xoay, Xem thêm trên msdn. Mình chưa fix bug đoạn code này :D
D3DXMATRIX m1,m2,m3;
this->d3dxSprite->GetTransform[&m1];
D3DXMatrixTransformation2D[&m2,NULL,NULL,NULL,NULL,1,&postion];
m3 = m1 * m2;
this->d3dxSprite->SetTransform[&m3];
this->d3dxSprite->Draw[texture,NULL,NULL,&D3DXVECTOR3[postion.x,postion.y,0],D3DCOLOR_XRGB[255,255,255]];
this->d3dxSprite->SetTransform[&m1];
}