npcgamer 時常出沒的會員
註冊時間: 2007-11-02 文章: 47
3.00 果凍幣
|
發表於: 2008-2-17, PM 4:08 星期日 文章主題: [教學][範例] Direct3D Sprite (2D) |
|
|
Spirte 是 Direct3D 專門描繪 2D 圖像的物件。
建立Sprite
代碼: | 關鍵字:
D3DXCreateSprite
範例碼:
LPD3DXSPRITE pSprite;
D3DXCreateSprite(pDevice,&pSprite); |
描繪Sprite
代碼: | 關鍵字:
ID3DXSprite::Begin
ID3DXSprite::End
ID3DXSprite::Draw
範例碼:
LPDIRECT3DTEXTURE9 pTexture;
// 載入2D影像
D3DXCreateTextureFromFileEx(pDevice, TEXT("demo.png"), 100, 100, 1, 0,
D3DFMT_UNKNOWN, D3DPOOL_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT,
0, NULL, NULL, &pTexture);
// 描繪
RECT SrcRect = RECT(0, 0, 50, 50); // 材質的矩形區域
D3DXVECTOR3 Center = D3DXVECTOR3(0, 0, 0); // 材質的矩形區域的中心點
D3DXVECTOR3 Position = D3DXVECTOR3(30.0f, 20.0f); // 描繪的位置
D3DCOLOR Color = D3DCOLOR_ARGB(255, 255, 255, 255); // 色彩遮罩
pSprite->Begin(D3DXSPRITE_ALPHABLEND);
pSprite->Draw(pTexture, &SrcRect, &Center, &Position, Color);
pSprite->End(); |
旋轉與縮放
代碼: | 關鍵字:
D3DXMatrixTransformation2D
ID3DXSprite::SetTransform
範例碼:
pSprite->Begin(D3DXSPRITE_ALPHABLEND);
D3DXVECTOR2 Translation = D3DXVECTOR2(200.0f,50.0f); // 新的原點
D3DXVECTOR2 RotationCenter = D3DXVECTOR2(50.0f,50.0f); // 旋轉中心
int Rotation = 45; // 旋轉角度
D3DXVECTOR2 Scaling(1.0f,1.0f); // 縮放比例
D3DXMATRIX Matrix; // 座標轉換矩陣
D3DXMatrixTransformation2D(&Matrix, NULL, 0.0, &Scaling, &RotationCenter, D3DXToRadian(Rotation), &Translation);
pSprite->SetTransform(&Matrix);
RECT SrcRect;
SrcRect.left = 0;
SrcRect.top = 0;
SrcRect.right = SrcRect.left + 100;
SrcRect.bottom = SrcRect.top + 100;
D3DXVECTOR3 Center = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
D3DXVECTOR3 Position = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
pSprite->Draw(pTexture, &SrcRect, &Center, &Position, D3DCOLOR_ARGB(255,255,255,255));
// 復原 座標轉換設定
Translation = D3DXVECTOR2(0.0f, 0.0f);
RotationCenter = D3DXVECTOR2(0.0f, 0.0f);
Rotation = 0;
Scaling = D3DXVECTOR2(1.0f, 1.0f);
D3DXMatrixTransformation2D(&Matrix, NULL, 0.0, &Scaling, &RotationCenter, D3DXToRadian(Rotation), &Translation);
pSprite->SetTransform(&Matrix);RECT SrcRect = RECT(0, 0, 50, 50);
pSprite->End(); |
描繪文字
代碼: | 關鍵字:
D3DXFONT_DESC
D3DXCreateFontIndirect
LPD3DXFONT
LPD3DXFONT::DrawText
範例碼:
LPD3DXFONT pFont; // Direct3D Font
// 建立 Direct3D Font
HDC hdc;
D3DXFONT_DESC FontParam;
ZeroMemory(&FontParam, sizeof(FontParam));
hdc = GetDC(0);
FontParam.Height = -MulDiv(9, GetDeviceCaps(hdc, LOGPIXELSY), 72);;
FontParam.CharSet = DEFAULT_CHARSET;
StrCpy(FontParam.FaceName, TEXT("新細明體"));
ReleaseDC(0, hdc);
D3DXCreateFontIndirect(pD3DD, &FontParam, &pFont);
// 描繪
pSprite->Begin(D3DXSPRITE_ALPHABLEND);
D3DCOLOR Color = D3DCOLOR_ARGB(255,255,255,255);
RECT Rect;
Rect.left = 200;
Rect.top = 100;
Rect.right = Rect.left + 300;
Rect.bottom = Rect.top + 50;
pFont->DrawText(pSprite, TEXT("Hello World! 您好!"), -1, &Rect, 0, Color);
pSprite->End(); |
|
|