2016-02-25 95 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

我做了一些改變,現在應該運行。看到我把'****改變'的地方 –