Original Post
I've been working at this all day, and nothing's working for me (and for no apparent reason?). Here's a screenshot I've taken of a recent test creation:
As you can see, there are some definate row- and column-overlapping problems across the window. I loaded the picture as normal with D3DXCreateTextureFromFileEx and blitted it to a 640x480 quad (triangle fan). I just can't figure out why the picture isn't showing up perfectly. Can someone help me out here? Just for the record, here's my code: Thanks a lot.
As you can see, there are some definate row- and column-overlapping problems across the window. I loaded the picture as normal with D3DXCreateTextureFromFileEx and blitted it to a 640x480 quad (triangle fan). I just can't figure out why the picture isn't showing up perfectly. Can someone help me out here? Just for the record, here's my code:
#include <windows.h>
#include <d3d9.h>
#include <d3dx9.h>
#define RELEASE(x) { if(x) { (x)->Release(); (x)=NULL; } }
HINSTANCE hInst;
HWND wndHandle;
bool initWindow(HINSTANCE hInstance);
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
struct D3DVERTEX
{
float x, y, z, rhw;
float tu, tv;
};
const DWORD vertex_fvf = D3DFVF_XYZRHW | D3DFVF_TEX1;
LPDIRECT3D9 pD3D;
LPDIRECT3DDEVICE9 pd3dDevice;
LPDIRECT3DTEXTURE9 Background;
D3DVERTEX vBackground[] = {
{0,0,0,1,0,0}, {640,0,0,1,1,0}, {640,480,0,1,1,1}, {0,480,0,1,0,1}
};
bool initD3D(int,int,D3DFORMAT,bool);
void Render();
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
if(!initWindow(hInstance))
return false;
if(!initD3D(640,480,D3DFMT_UNKNOWN,false))
return false;
D3DXCreateTextureFromFileEx(pd3dDevice,
"background.bmp",
640,
480,
1,
0,
D3DFMT_X8R8G8B8,
D3DPOOL_MANAGED,
D3DX_FILTER_NONE,
D3DX_DEFAULT,
NULL,
NULL,
NULL,
&Background);
pd3dDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
MSG msg;
ZeroMemory(&msg, sizeof(msg));
while(msg.message!=WM_QUIT)
{
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
Render();
}
}
RELEASE(Background);
RELEASE(pd3dDevice);
RELEASE(pD3D);
return (int)msg.wParam;
}
bool initWindow(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = 0;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = "DirectXExample";
wcex.hIconSm = 0;
RegisterClassEx(&wcex);
wndHandle = CreateWindow("DirectXExample",
"DirectXExample",
WS_EX_TOPMOST | WS_POPUP | WS_VISIBLE,
CW_USEDEFAULT,
CW_USEDEFAULT,
640,
480,
NULL,
NULL,
hInstance,
NULL);
if(!wndHandle)
return false;
ShowWindow(wndHandle, SW_SHOW);
UpdateWindow(wndHandle);
return true;
}
bool initD3D(int bbWidth, int bbHeight, D3DFORMAT bbFormat, bool Fullscreen)
{
pD3D = Direct3DCreate9( D3D_SDK_VERSION );
D3DPRESENT_PARAMETERS d3dpp;
{
ZeroMemory( &d3dpp, sizeof(d3dpp) );
d3dpp.Windowed = !Fullscreen;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = bbFormat;
d3dpp.BackBufferCount = 1;
d3dpp.BackBufferHeight = bbHeight;
d3dpp.BackBufferWidth = bbWidth;
d3dpp.hDeviceWindow = wndHandle;
}
if( FAILED (pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_REF, wndHandle,
D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pd3dDevice) ) )
return false;
pd3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
pd3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
pd3dDevice->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_INVSRCALPHA);
pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE,TRUE);
return true;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_KEYDOWN:
switch(wParam)
{
case VK_ESCAPE:
PostQuitMessage(0);
break;
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
void Render()
{
pd3dDevice->SetFVF(vertex_fvf);
pd3dDevice->BeginScene();
pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, 0xFF0000FF, 1.0f, 0);
pd3dDevice->SetTexture(0, Background);
pd3dDevice->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, vBackground, sizeof(D3DVERTEX));
pd3dDevice->EndScene();
pd3dDevice->Present(NULL, NULL, NULL, NULL);
}
