1

我正在研究 DirectX 中的简单图形库。我使用 Dx 9 是因为我对它很陌生,而且我找到了一本为第 9 版编写的很好的编程书。无论如何,我无法在屏幕上看到任何内容,因为设备的函数Present()返回E_FAIL代码 0x80004005(我读到的意思是“未指定的失败”)。我还检查了程序中使用的所有 Dx 函数,它们都没有返回失败(显然 Present() 除外)。

如果我从 main.cpp注释行kAnimation.Render()程序工作正常

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

主.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;

动画.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 Ultimate。我正在使用 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
4

0 回答 0