我正在使用 ID3DXSprite 制作一个简单的 2D 瓷砖游戏。当我设法绘制图像时,我注意到它的质量与我编辑它时的质量不同。我已经读过在 Directx 8 和更新版本中所有纹理都必须是 2 的倍数,但这不是我的情况,因为图像是 32x32。
原图:链接
渲染图:链接
初始化代码:
bool D3DEngine::InitD3D(HWND hwnd)
{
D3D = Direct3DCreate9(D3D_SDK_VERSION);
D3DPRESENT_PARAMETERS D3DPP;
ZeroMemory(&D3DPP, sizeof(D3DPP));
D3DPP.Windowed = true;
D3DPP.SwapEffect = D3DSWAPEFFECT_DISCARD;
D3DPP.hDeviceWindow = hwnd;
D3DPP.BackBufferFormat = D3DFMT_A8R8G8B8;
D3DPP.BackBufferWidth = 300;
D3DPP.BackBufferHeight = 300;
D3DPP.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
if (D3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, D3DCREATE_MIXED_VERTEXPROCESSING, &D3DPP, &D3DDevice) != D3D_OK)
{
return false;
}
else
{
D3DXCreateTextureFromFile(D3DDevice, ".bmp", &Texture);
D3DDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
D3DDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
Sprite = new D3DSprite(D3DDevice);
Camera = new Camera2D(0, 0, ScreenPosX, ScreenPosY, 1.0f, 0.0f);
Debug->Output("Started DirectX");
return true;
}
}
渲染代码:
void D3DSprite::DrawTexture(LPDIRECT3DTEXTURE9 Texture, CONST RECT* Rect, ScreenPos Pos, D3DCOLOR Color)
{
D3DXVECTOR3 Center(0.0f, 0.0f, 0.0f);
D3DXVECTOR3 Position(Pos.x - Camera->GetX(), Pos.y - Camera->GetY(), 0.0f);
Handle->Draw(Texture, Rect, &Center, &Position, Color);
}
struct ScreenPos
{
float x;
float y;
};
class D3DSprite
{
public:
LPD3DXSPRITE Handle;
D3DSprite(LPDIRECT3DDEVICE9 Device);
~D3DSprite();
void DrawTexture(LPDIRECT3DTEXTURE9 Texture, CONST RECT* Rect, ScreenPos Pos, D3DCOLOR Color);
void DrawTexture(LPDIRECT3DTEXTURE9 Texture, CONST RECT* Rect, WorldPos Pos, D3DCOLOR Color);
void Begin(DWORD Flags) { Handle->Begin(Flags); };
void End() { Handle->End(); };
};
void D3DEngine::Render()
{
D3DDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_COLORVALUE(0.0f, 0.0f, 0.0, 1.0f), 1.0f, 0);
D3DDevice->BeginScene();
ScreenPos Position = {0, 0};
Sprite->Begin(D3DXSPRITE_OBJECTSPACE | D3DXSPRITE_ALPHABLEND);
Sprite->DrawTexture(Texture, NULL, Position, D3DCOLOR_COLORVALUE(1.0f, 1.0f, 1.0f, 1.0f));
Sprite->End();
D3DDevice->EndScene();
D3DDevice->Present(NULL, NULL, NULL, NULL);
}