0

I am trying to create a direct 3D 9 device. I am setting everything up with this piece of code:

LPDIRECT3D9 pD3D = Direct3DCreate9(D3D_SDK_VERSION);
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.hDeviceWindow = getGameHWND();

LPDIRECT3DDEVICE9 device;

HRESULT res;
res = pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, getGameHWND(), D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &device);

if (res != D3D_OK) {
    char buff[100];
    sprintf_s(buff, 100, "%X", res);
    MessageBoxA(0, "ERROR", buff, 0);
    sprintf_s(buff, 100, "%X", getGameHWND());
    MessageBoxA(0, "ERROR", buff, 0);
    return;
}

But this function fails everytime I execute it. As a response I get the error code 0x80070057, which is according to Google E_INVALIDARG. But which argument is wrong here? I followed a tutorial, which has the exact same code as I do.

I also checked if getGameHWND() returns 0, which is not the case.

Here is my updated code:

LPDIRECT3D9 pD3D = Direct3DCreate9(D3D_SDK_VERSION);
if (pD3D == NULL) {
    MessageBoxA(0, "ERROR", "pD3D", 0);
}

D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.hDeviceWindow = getGameHWND();
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
d3dpp.BackBufferCount = 1;
d3dpp.EnableAutoDepthStencil = 0;
d3dpp.BackBufferWidth = 0;
d3dpp.BackBufferHeight = 0;
d3dpp.FullScreen_RefreshRateInHz = 0;
d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;
d3dpp.MultiSampleQuality = 0;

LPDIRECT3DDEVICE9 device;

HRESULT res;
res = pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, getGameHWND(), D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &device);

if (res != D3D_OK) {
    res = pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, getGameHWND(), D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &device);
}

if (res != D3D_OK) {
    res = pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, getGameHWND(), D3DCREATE_MIXED_VERTEXPROCESSING, &d3dpp, &device);
}

if (res != D3D_OK) {
    char buff[100];
    sprintf_s(buff, 100, "%X", res);
    MessageBoxA(0, "ERROR", buff, 0);
    sprintf_s(buff, 100, "%d", getPID());
    MessageBoxA(0, "ERROR", buff, 0);
    return;
}

But I still get the same error message

user143366
  • 11
  • 2
  • I'm deleting my answer, my assumptions were incorrect. Can you verify the dimensions of the game window pointed to by getGameHWND()? Where in your code are you creating the device? – ozeanix Jun 15 '17 at 10:14
  • What do you mean with dimensions? I checked the PID of the game window, that one was correct – user143366 Jun 15 '17 at 10:48
  • As in, what is the width and height of the window described by getGameHWND()? Can you post an example that is really simple - just a single .c or .cpp file of the window creation and D3D initialization? There's a few different reasons why CreateDevice can fail, but most of them seem a bit esoteric. – ozeanix Jun 15 '17 at 11:43
  • 1
    You will have to initialize more members of D3DPRESENT_PARAMETERS. I am sure that's why it's failing – Asesh Jun 16 '17 at 04:23
  • Note that using ``D3DCREATE_SOFTWARE_VERTEXPROCESSING`` is going to be particularly painful in terms of performance. You should avoid using it. Of course, you should probably not be using legacy Direct3D 9 either. – Chuck Walbourn Jun 16 '17 at 18:33
  • Hello everybody, I did some trial and error in the past days. I initialized more members of D3DPRESENT_PARAMETERS (even though all tutorials I found online did only initialize the members I used originally) and replaced SOFTWARE_VERTEXTPROCESS with HARDWARE. After these changes I run my code again and it worked, but only ONCE. Also I started this project some months ago and my computer is making problems (crashing sometimes with bluescreen, no display when booting), so is it possible that my graphic card (or something) is causing this problem? Since the original code I posted worked back then – user143366 Jun 18 '17 at 13:02
  • Try assigning a valid value for back buffer width and height. It's width and height should be supported by your graphics card. That might be causing this issue – Asesh Jul 05 '17 at 03:19

0 Answers0