Reputation: 283
I'm working on simple graphic library in DirectX. I use Dx 9 because I'm pretty new in it and I have found a good programming book written for 9th version. Anyway, I can't get anything on the screen because Device's function Present() returns E_FAIL code 0x80004005 (which what I've read mean 'Unspecified failure'). I've also checked all Dx functions used in program and none of them returns fail (except Present() obviously).
The program works fine if I comment line kAnimation.Render() from main.cpp
Here's the causing problems part of code:
main.cpp:
//...
if(FAILED(g_pkD3DDevice->BeginScene())) return ErrorBeginScene;
kAnimation.Render();
if(FAILED(g_pkD3DDevice->EndScene())) return ErrorEndScene;
HRESULT hr;
hr = g_pkD3DDevice->Present(NULL, NULL, NULL, NULL); //Returns E_FAIL
if(FAILED(hr)) return ErrorPresent;
Animation.cpp:
#define D3DFVF_MYVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE|D3DFVF_TEX1)
Error Animation::Render()
{
//...
//This is what contain fX[4] and fY[4] and other variables while debugging
//float fX[4]
//float fY[4]
//fX[0] = -16; fX[1] = 16; fX[2] = -16; fX[3] = 16;
//fY[0] = 16; fY[1] = 16; fY[2] = -16; fY[3] = -16;
//m_iCellHeight = 32
//m_iCellWidth = 32
//m_iTextureWidth = 128
//m_iTextureHeight = 128
//kTextCoord.Left = 1/128; .Right = 33/128; .Top = 1/128; .Bottom = 33/128;
Rect kTextCoord = GetUV(CellID(0,0));
Vertex kVertices[] =
{ //x, y, z, w, color, texture coord (u, v)
{ fX[2], fY[2], 0, 1.0f, iColor, kTextCoord.Left, kTextCoord.Top},
{ fX[3], fY[3], 0, 1.0f, iColor, kTextCoord.Right, kTextCoord.Top},
{ fX[1], fY[1], 0, 1.0f, iColor, kTextCoord.Right, kTextCoord.Bottom},
{ fX[0], fY[0], 0, 1.0f, iColor, kTextCoord.Left, kTextCoord.Boottom},
};
g_D3DDevice->SetTexture(0, m_pkD3DTexture);
g_D3DDevice->SetFVF(D3DFVF_MYVERTEX);
if(FAILED(g_D3DDevice->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, kVertices, sizeof(Vertex)))
return ErrorDrawPrimitive;
return NoError;
}
Rect Animation::GetUV(CellID kPosition)
{
Rect kUVRect;
kUVRect.Left = (1 + ((1 + m_iCellWidth) * kPosition.x)) / m_iTextureWidth;
kUVRect.Right = (1 + ((1 + m_iCellWidth) * kPosition.x) + m_iCellWidth) / m_iTextureWidth;
kUVRect.Top = (1 + ((1 + m_iCellHeight) * kPosition.y)) / m_iTextureHeight;
kUVRect.Bottom =(1 + ((1 + m_iCellHeight) * kPosition.y) + m_iCellHeight) / m_iTextureHeight;
return kUVRect;
}
Rest you need:
class Rect
{
public:
float Left;
float Right;
float Top;
float Bottom;
};
//Position of single cell in animation texture
class CellID
{
public:
unsigned long x;
unsigned long y;
};
My operating system is Windows 7 Ultimate. I'm using VS c++ 2010 If you would like to see entire solution there's link: http://speedy.sh/CmBRb/ConWinLib.rar (It's a bit different than that because I wanted to make code as short as I could)
Thank you for any help!
EDIT
Answering to your questions:
@tbridge The Device should be good because I created few small programs before and they were working prefectly. But anyway there's the code:
//...
g_pkD3D = Direct3DCreate9(D3D_SDK_VERSION);
D3DPRESENT_PARAMETERS kPresentParams;
unsigned long iDeviceType = D3DDEVTYPE_REF; //I have already checked D3DDEVTYPE_HAL and it doesn't work either
ZeroMemory(&kPresentParams, sizeof(D3DPRESENT_PARAMETERS));
kPresentParams.SwapEffect = D3DSWAPEFFECT_DISCARD;
D3DDISPLAYMODE kCurrentMode;
if(FAILED(g_pkD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &kCurrentMode)))
return ErrorGetAdapterDisplayMode;
kPresentParams.Windowed = true;
kPresentParams.BackBufferCount = 1;
kPresentParams.BackBufferFormat = kCurrentMode.Format;
if(FAILED(g_pkD3D->CreateDevice(D3DADAPTER_DEFAULT, (D3DDEVTYPE)iDeviceType, hWindow,
D3DCREATE_SOFTWARE_VERTEXPROCESSING, &kPresentParams, &g_pkD3DDevice)))
return ErrorCreateDevice;
g_pkD3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
g_pkD3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
g_pkD3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
g_pkD3DDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
g_pkD3DDevice->SetRenderState(D3DRSDESTBLEND, D3DBLEND_INVSRCALPHA);
g_pkD3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
g_pkD3DDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
c
Upvotes: 1
Views: 766
Reputation: 1
Stumbling across this 11 years later... (not sure why these come up in search, but in case anyone else hits this problem?):
You must call SetTexture(0,NULL) after you're finished drawing and before endscene/present.
Upvotes: -1