2013-08-24 82 views
0

我打算創建一個窗口並初始化Direct9,但是我發現VS2010表明在DX_Init()函數上存在訪問衝突,我很困惑我在哪弄錯了。在初始化DX時出現C++訪問衝突

#include <Windows.h> 
#include <d3dx9.h> 
#include <sstream> 
#include <string> 
#include "D3D9.h" 

#define memclear(var) \ memset(&wndClassData, 0x00, sizeof(var)); 

WNDCLASSEX wndClassData; 

LRESULT CALLBACK MainWindowProc(HWND hWnd,UINT uMsg,WPARAM wParam, LPARAM lParam) 
{ 
    switch(uMsg){ 
     case WM_DESTROY: 
      PostQuitMessage(0); 
     default: 
      return DefWindowProc(hWnd,uMsg,wParam,lParam); 
    } 

    return 0; 
} 

struct DXGlobals{ 
    IDirect3D9 *pD3D; 
    IDirect3DDevice9 *pDevice; 
}; 

DXGlobals gDX; 

//我對這個部分的一些問題

void DX_Init(HWND hWnd) 
{ 
    gDX.pD3D = Direct3DCreate9(D3D_SDK_VERSION); 

    D3DPRESENT_PARAMETERS d3dpp; 
    memclear(d3dpp); 
    d3dpp.Windowed = TRUE; 
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; 

    HRESULT hr = gDX.pD3D->CreateDevice(
     D3DADAPTER_DEFAULT, 
     D3DDEVTYPE_HAL, 
     hWnd, 
     D3DCREATE_HARDWARE_VERTEXPROCESSING, 
     &d3dpp, 
     &gDX.pDevice 
     ); 
} 

//問題結束這裏

void DX_Update(){ 
} 

void DX_Dispose(){ 
    gDX.pD3D->Release(); 
    gDX.pDevice->Release(); 
} 

int CALLBACK WinMain(
    HINSTANCE hInstance, 
    HINSTANCE hPrevInstance, 
    LPSTR lpCmdLine, 
    int nCmdShow) 
{ 
    //WNDCLASSEX wndClassData; 

    memclear(wndClassData); 

    wndClassData.cbSize = sizeof(WNDCLASSEX); 
    wndClassData.lpfnWndProc = MainWindowProc; 
    wndClassData.hInstance = hInstance; 
    wndClassData.hCursor = LoadCursor(NULL, MAKEINTRESOURCE(IDC_ARROW)); 
    wndClassData.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); 
    wndClassData.lpszClassName="ColourMatchingWindow"; 
    //memset(&wndClassData, 0x00, sizeof(wndClassData)); 


    ATOM wndClass = RegisterClassEx(&wndClassData); 

    HWND mainWnd = CreateWindow(
     (LPCSTR)wndClass, 
     "ColourMatching", 
     WS_OVERLAPPEDWINDOW|WS_VISIBLE, 
     CW_USEDEFAULT, 
     CW_USEDEFAULT, 
     CW_USEDEFAULT, 
     CW_USEDEFAULT, 
     NULL, 
     NULL, 
     hInstance, 
     NULL); 

    DX_Init(mainWnd); 
    MSG msg; 

    for(;;){ 
     while(PeekMessage(&msg, NULL,0,0,PM_REMOVE)) 
     { 
      TranslateMessage(&msg); 
      DispatchMessage(&msg); 
     } 

     if(msg.message==WM_QUIT){ 
      break; 
     } 

     DX_Update(); 

    } 

    DX_Dispose(); 

return 0; 
} 
} 
+0

對於初學者來說,嘗試通過一個有效的窗口句柄檢查您的返回結果後['CreateWindow的()'](http://msdn.microsoft.com/en-us/library/windows/桌面/ ms632679(v = vs.85).aspx)調用。建議您查看API。您在該調用中指定的類名稱與您正在註冊的類名稱不匹配。 '「ColourMatchingWindow」!=(LPCSTR)wndClass'。除此之外,*編譯*代碼將同樣有幫助。 – WhozCraig

+0

thx回覆 – VinceFIT

回答

1

這看起來錯

#define memclear(var) \ 
    memset(&wndClassData, 0x00, sizeof(var)); 

D3DPRESENT_PARAMETERS d3dpp; 
memclear(d3dpp); 

也許你的意思宏看起來像這樣

#define memclear(var) \ 
    memset(&var, 0x00, sizeof(var)); 

但真的,你不應該使用毫無意義的宏。只需編寫簡單明瞭的代碼,而不是代碼更復雜和不明確的代碼。像這樣

D3DPRESENT_PARAMETERS d3dpp; 
memset(&d3dpp, 0x00, sizeof d3dpp); 
+0

宏的是邪惡的 - > http://stackoverflow.com/questions/14041453/why-preprocessor-macros-are-evil-and-what-is-the-real-alternative-c11 –

+0

它現在的作品,謝謝 – VinceFIT