c ++ DirectX函数Present()不起作用

时间:2012-11-08 21:04:53

标签: c++ directx directx-9

我正在使用DirectX中的简单图形库。我使用Dx 9因为我很新,我找到了一本为第9版编写的好编程书。无论如何,我无法在屏幕上显示任何内容,因为设备的功能存在()会返回 E_FAIL 代码0x80004005(我所读到的意思是“未指定的失败”)。 我还检查了程序中使用的所有Dx函数,但没有一个函数返回失败(显然除了Present())。

如果我从main.cpp评论 kAnimation.Render()行,程序运行正常

这是导致问题的一部分代码:

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;
}

你需要休息:

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;
};

我的操作系统是Windows 7旗舰版。我正在使用VS c ++ 2010 如果您希望看到整个解决方案的链接:http://speedy.sh/CmBRb/ConWinLib.rar (它有点不同,因为我想尽可能缩短代码)

感谢您的帮助!

修改

回答你的问题:

@tbridge设备应该是好的,因为我之前创建了一些小程序,并且它们完美地工作。但无论如何都有代码:

//...
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

0 个答案:

没有答案