我打算创建一个窗口并初始化Direct9,但是我发现VS2010指出DX_Init()函数上的访问冲突,我错在哪里弄错了。

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

最佳答案

这看起来错了

#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);

关于c++ - 初始化DX时的c++访问冲突,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18416405/

10-11 18:54