2016-02-25 98 views
-1

我创建了一个程序。当我运行它时,一个背景为灰色的窗口,并且必须创建一个带有三角形的黄色背景。问题是什么?为什么不在窗口中渲染?

我一直立足之本编程“Gornakova - 的DirectX编程经验”

#include <windows.h> 
#include <d3d9.h> 

LPDIRECT3D9 pDirect3D = NULL; 
LPDIRECT3DDEVICE9 pDirect3DDevice = NULL; 
LPDIRECT3DVERTEXBUFFER9 pBufferVershin = NULL; 

struct CUSTOMVERTEX 
{ 
    FLOAT x,y,z,rhw; 
    DWORD color; 
}; 
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE) 

HRESULT InitialBufferVershin() 
{ 
    CUSTOMVERTEX Vershin[] = 
    { 
     {300.0f, 300.0f, 0.5f, 1.0f, 0x00000fff, }, 
     {150.0f, 300.0f, 0.5f, 1.0f, 0x00000fff, }, 
     {150.0f, 150.0f, 0.5f, 1.0f, 0x00000fff, }, 
    }; 

    if(FAILED(pDirect3DDevice->CreateVertexBuffer(3*sizeof(CUSTOMVERTEX), 0, D3DFVF_CUSTOMVERTEX, 
               D3DPOOL_DEFAULT, &pBufferVershin, NULL))) 
    return E_FAIL; 

    VOID* pBV; 
    if(FAILED(pBufferVershin->Lock(0, sizeof(Vershin), (void**)&pBV, 0))) 
     return E_FAIL; 
    memcpy(pBV, Vershin, sizeof(Vershin)); 
    pBufferVershin->Unlock(); 
    return S_OK; 
} 

void DeleteDirect3D() 
{ 
    if(pBufferVershin != NULL) 
     pBufferVershin->Release(); 

    if(pDirect3DDevice != NULL) 
     pDirect3DDevice->Release(); 

    if(pDirect3D != NULL) 
     pDirect3D->Release(); 
} 

void RenderingDirect3D() 
{ 
    if(pDirect3DDevice == NULL) 
     return; 
    pDirect3DDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(255,255,0), 1.0f, 0); 

    pDirect3DDevice->BeginScene(); 

     pDirect3DDevice->SetStreamSource(0, pBufferVershin, 0, sizeof(CUSTOMVERTEX)); 

     pDirect3DDevice ->SetFVF(D3DFVF_CUSTOMVERTEX); 
     pDirect3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1); 

     pDirect3DDevice->Present(NULL, NULL, NULL, NULL); 

    pDirect3DDevice->EndScene(); 
} 

LRESULT IntailDirect3D(HWND hwnd) 
{ 
    if(NULL == (pDirect3D = Direct3DCreate9(D3D9b_SDK_VERSION))) 
     return E_FAIL; 

    D3DDISPLAYMODE Display; 
    if(FAILED(pDirect3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &Display))) 
     return E_FAIL; 

    D3DPRESENT_PARAMETERS Direct3DParametr; 
    ZeroMemory(&Direct3DParametr, sizeof(Direct3DParametr)); 
    Direct3DParametr.Windowed = TRUE; 
    Direct3DParametr.SwapEffect = D3DSWAPEFFECT_DISCARD; 
    Direct3DParametr.BackBufferFormat = Direct3DParametr.BackBufferFormat; 
    if(FAILED(pDirect3D->CreateDevice(D3DADAPTER_DEFAULT, 
            D3DDEVTYPE_HAL, 
            hwnd, 
            D3DCREATE_HARDWARE_VERTEXPROCESSING, 
            &Direct3DParametr, &pDirect3DDevice))) 
     return E_FAIL; 
     return S_OK; 
} 

LRESULT CALLBACK MainWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) 
{ 
    switch(msg) 
    { 
    case WM_PAINT: 
     RenderingDirect3D(); 
     ValidateRect(hwnd, NULL); 
     break; 
    case WM_DESTROY: 
     DeleteDirect3D(); 
     PostQuitMessage(0); 
     return 0; 
     break; 
    default: 
     return DefWindowProc(hwnd, msg, wParam, lParam); 
     break; 
    } 
} 

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE pPrevInstance, LPSTR lCmdLine, int nCmdShow) 
{ 
    WNDCLASSEX wEx; 
    MSG msg; 
    wEx.cbSize = sizeof(WNDCLASSEX); 
    wEx.style = CS_VREDRAW || CS_HREDRAW || CS_OWNDC || CS_DBLCLKS; 
    wEx.lpfnWndProc = MainWndProc; 
    wEx.cbClsExtra = 0; 
    wEx.cbWndExtra = 0; 
    wEx.hInstance = hInstance; 
    wEx.hIcon = LoadIcon(NULL, IDI_APPLICATION); 
    wEx.hCursor = LoadCursor(NULL, IDC_ARROW); 
    wEx.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH); 
    wEx.lpszMenuName = NULL; 
    wEx.lpszClassName = "WINDOWCLASS"; 
    wEx.hIconSm = LoadIcon(NULL, IDI_APPLICATION); 

    RegisterClassEx(&wEx); 


    HWND hwnd; 
    if(!(hwnd = CreateWindowEx(NULL, 
           "WINDOWCLASS", 
           "Базовое окно для DirectX", 
           WS_OVERLAPPED||CW_USEDEFAULT, 
           0,0, 
           500,400, 
           NULL, 
           NULL, 
           hInstance, 
           NULL))) 
    { 
     return 0; 
    } 
    if(SUCCEEDED((IntailDirect3D(hwnd)))) 
    { 
     ShowWindow(hwnd, nCmdShow); 
     UpdateWindow(hwnd); 
     ZeroMemory(&msg, sizeof(msg)); 

     while(msg.message != WM_QUIT) 
     { 
      if(PeekMessage(&msg, NULL, 0,0, PM_REMOVE)) 
      { 
       TranslateMessage(&msg); 
       DispatchMessage(&msg); 
      } 
      else{RenderingDirect3D();} 
     } 
    } 
    return 0; 
} 

回答

0

它看起来并不代码是借鉴什么,也许这是一个教程

几个第一部分明显的错误:

变化的逻辑或按位OR:|||

wEx.style = CS_VREDRAW | CS_HREDRAW | CS_OWNDC | CS_DBLCLKS; 

更改WS_OVERLAPPED||CW_USEDEFAULTWS_OVERLAPPED,它应该是逻辑或,并且CW_USEDEFAULT不属于那里。

这是说的风格是所有上述标志(有点像+运营商)的组合

删除此整段:

case WM_PAINT: 
    RenderingDirect3D(); 
    ValidateRect(hwnd, NULL); 
    break; 

WM_PAINT必须正确与BeginPaint/EndPaint处理或不处理所有。


编辑:

#include <windows.h> 
#include <d3d9.h> 

LPDIRECT3D9 pDirect3D = NULL; 
LPDIRECT3DDEVICE9 pDirect3DDevice = NULL; 
LPDIRECT3DVERTEXBUFFER9 pBufferVershin = NULL; 

struct CUSTOMVERTEX 
{ 
    FLOAT x, y, z, rhw; 
    DWORD color; 
}; 
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE) 

HRESULT InitialBufferVershin() 
{ 
    CUSTOMVERTEX Vershin[] = 
    { 
     { 300.0f, 300.0f, 0.5f, 1.0f, 0x00000fff, }, 
     { 150.0f, 300.0f, 0.5f, 1.0f, 0x00000fff, }, 
     { 150.0f, 150.0f, 0.5f, 1.0f, 0x00000fff, }, 
    }; 

    if (FAILED(pDirect3DDevice->CreateVertexBuffer(3 * sizeof(CUSTOMVERTEX), 0, 
     D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &pBufferVershin, NULL))) 
     return E_FAIL; 

    VOID* pBV; 
    if (FAILED(pBufferVershin->Lock(0, sizeof(Vershin), (void**)&pBV, 0))) 
     return E_FAIL; 
    memcpy(pBV, Vershin, sizeof(Vershin)); 
    pBufferVershin->Unlock(); 
    return S_OK; 
} 

void DeleteDirect3D() 
{ 
    if (pBufferVershin) pBufferVershin->Release(); 
    if (pDirect3DDevice) pDirect3DDevice->Release(); 
    if (pDirect3D) pDirect3D->Release(); 
} 

void RenderingDirect3D() 
{ 
    if (pDirect3DDevice == NULL) 
     return; 
    pDirect3DDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(255, 255, 0), 1.0f, 0); 

    pDirect3DDevice->BeginScene(); 

    pDirect3DDevice->SetStreamSource(0, pBufferVershin, 0, sizeof(CUSTOMVERTEX)); 

    pDirect3DDevice->SetFVF(D3DFVF_CUSTOMVERTEX); 
    pDirect3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1); 

    pDirect3DDevice->EndScene(); 

    pDirect3DDevice->Present(NULL, NULL, NULL, NULL);//****changed 
} 

LRESULT IntailDirect3D(HWND hwnd) 
{ 
    if (NULL == (pDirect3D = Direct3DCreate9(D3D9b_SDK_VERSION))) 
     return E_FAIL; 

    D3DDISPLAYMODE Display; 
    if (FAILED(pDirect3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &Display))) 
     return E_FAIL; 

    D3DPRESENT_PARAMETERS Direct3DParametr; 
    ZeroMemory(&Direct3DParametr, sizeof(Direct3DParametr)); 
    Direct3DParametr.Windowed = TRUE; 
    Direct3DParametr.SwapEffect = D3DSWAPEFFECT_DISCARD; 
    Direct3DParametr.BackBufferFormat = Direct3DParametr.BackBufferFormat; 
    if (FAILED(pDirect3D->CreateDevice(D3DADAPTER_DEFAULT, 
     D3DDEVTYPE_HAL, 
     hwnd, 
     D3DCREATE_HARDWARE_VERTEXPROCESSING, 
     &Direct3DParametr, &pDirect3DDevice))) 
     return E_FAIL; 

    return S_OK; 
} 

LRESULT CALLBACK MainWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) 
{ 
    switch (msg) 
    { 
    case WM_DESTROY: 
     DeleteDirect3D(); 
     PostQuitMessage(0); 
     return 0; 
    } 
    return DefWindowProc(hwnd, msg, wParam, lParam); 
} 

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int) 
{ 
    WNDCLASSEX wEx = { sizeof(WNDCLASSEX) }; 
    wEx.style = CS_VREDRAW | CS_HREDRAW | CS_OWNDC | CS_DBLCLKS; 
    wEx.lpfnWndProc = MainWndProc; 
    wEx.hInstance = hInstance; 
    wEx.hCursor = LoadCursor(NULL, IDC_ARROW); 
    wEx.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH); 
    wEx.lpszClassName = "WINDOWCLASS"; 
    RegisterClassEx(&wEx); 

    HWND hwnd = CreateWindowEx(NULL, "WINDOWCLASS", "TEST", 
     WS_VISIBLE | WS_OVERLAPPEDWINDOW, 
     0, 0, 800, 600, NULL, NULL, hInstance, NULL); 
    if (!hwnd) 
     return 0; 

    if (SUCCEEDED((IntailDirect3D(hwnd)))) 
    { 
     InitialBufferVershin();//****changed 
     MSG msg = { 0 }; 
     while (msg.message != WM_QUIT) 
     { 
      if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) 
      { 
       TranslateMessage(&msg); 
       DispatchMessage(&msg); 
      } 
      else 
      { 
       RenderingDirect3D(); 
      } 
     } 
    } 

    return 0; 
} 
+0

但在本书从笔者得出在黄色背景上一个蓝色三角形! – andrew2103

+0

我做了一些改变,现在应该运行。看到我把'****改变'的地方 –