Jump to content
  • Advertisement
Sign in to follow this  
cjdb01

LPDIRECT3DDEVICE9 issue

This topic is 2609 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all,

This is my very first DirectX program, so for all of you whom will go grr, please bare with me :).

I'm following the code of Beginning Game Programming by Jonathan S. Harbour but am slightly warping it to my needs. To begin, I'm modulating the code across various files to simplify the compilation time.

Here is my variation of the code:

main.cpp

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
// Start up the game with a safeguard
HWND hWnd;
// Windows variable declarations
MSG message;
// Register the class
MyRegisterClass(hInstance);

int verifyRunning = Init(hWnd, hInstance, nCmdShow);
bool gameIsRunning = false;

if (verifyRunning == 0)
{
// Game variable declarations
gameIsRunning = true;

// Game loop
while (gameIsRunning)
{
if (PeekMessage(&message, NULL, 0, 0, PM_REMOVE))
{
// Look for the quit message
if (message.message == WM_QUIT)
{
break;
}

// Decode and pass messages on to WndProc
TranslateMessage(&message);
DispatchMessage(&message);
}
else
{
RefreshGraphics(hWnd);
}
}


init.cpp

// Global data
// Variables
extern unsigned int screenWidth, screenHeight;
// Constants
extern const char *APP_TITLE;
// Objects
extern LPDIRECT3D9 d3d;
extern LPDIRECT3DDEVICE9 d3ddev;
...
hWnd = CreateWindow(APP_TITLE, APP_TITLE, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, screenWidth, screenHeight, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return WIN32_CREATE_FAILURE;
}

ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);

d3d = NULL;
d3ddev = NULL;

returnVal = DirectInit(hWnd);
if (returnVal != NO_ERROR_DETECTED)
{
return returnVal;
}

return returnVal;


directinit.cpp

extern LPDIRECT3D9 d3d;
extern LPDIRECT3DDEVICE9 d3ddev;
extern bool windowedMode;

int DirectInit(HWND &hWnd)
{
// Initialise all DirectX objects
// Direct3D
d3d = Direct3DCreate9(D3D_SDK_VERSION);
if (d3d == NULL)
{
return DIRECT3D_FAILURE;
}

// Set Direct3D presentation parameters
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = windowedMode;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;

// Create the D3D Device
d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &d3ddev);

if (d3ddev == NULL)
{
return DIRECT3D_DEV_FAILURE;
}

return NO_ERROR_DETECTED;
}


refreshgraphics.cpp
#include <Windows.h>
#include <d3d9.h>

extern LPDIRECT3D9 d3d;
extern LPDIRECT3DDEVICE9 d3ddev;

void RefreshGraphics(HWND hWnd)
{
// Make sure the device is valid
if (d3ddev == NULL)
{
return;
}

// Clear the screen to a green colour
d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 255, 255), 1.0f, 0);

// Start rendering
if (d3ddev->BeginScene())
{
// Do things here

// Stop rendering
d3ddev->EndScene();
}

// Display the back buffer on the screen
d3ddev->Present(NULL, NULL, NULL, NULL);
}


The problem is, I can't get d3ddev to be anything but NULL. According to the code listings of my book (2nd Ed), I am doing everything spot on and should have a green screen. But instead I get my generic error message (not listed) saying that a DirectX error has occurred and the program must shut down (note: this is MY message, not a system message).

What can I do to fix this?

Cheers,

CJDB

Share this post


Link to post
Share on other sites
Advertisement
The 'CreateDevice' call returns a HRESULT value. Check what value you're getting here and that should give you a clue as to what is wrong.

It's a good idea to get into the habit of checking the return values from all d3d calls... might seem tedious at times but I guarantee it'll save you some grief many times in the future.

Share this post


Link to post
Share on other sites
As above, always make use of the returned HRESULT values and check them for errors. There's macros to help with this.

Also, when you install the DirectX SDK, it comes with a program called the "DirectX Control Panel". You can use this app to switch your PC over to use the "debug runtime". Once you've done that, DirectX runs slower, but it will output a lot more information when errors occur to help you debug your code. It usually gives you pretty specific reasons why a function failed - and if you don't understand the messages it gives you, you can always post them up here. Just make sure to switch back to the "retail runtime" before using your PC to play games ;)

Share this post


Link to post
Share on other sites

As above, always make use of the returned HRESULT values and check them for errors. There's macros to help with this.

Also, when you install the DirectX SDK, it comes with a program called the "DirectX Control Panel". You can use this app to switch your PC over to use the "debug runtime". Once you've done that, DirectX runs slower, but it will output a lot more information when errors occur to help you debug your code. It usually gives you pretty specific reasons why a function failed - and if you don't understand the messages it gives you, you can always post them up here. Just make sure to switch back to the "retail runtime" before using your PC to play games ;)


Ok, so I used the FAILED macro on the CreateDevice call and it failed. I'm now playing around with the 64-bit version of DirectX CP. I've changed it to Debug mode for Direct3D9 and increased the level of Debugging Output to maximum. Where can I find this output so I can find out why my CreateDevice function failed? I'm assuming it would be in the DirectX SDK folder, but I can't seem to find it.

Share this post


Link to post
Share on other sites
Sorry, I should've mentioned - It will go to the debug output window of your IDE.

Share this post


Link to post
Share on other sites

Sorry, I should've mentioned - It will go to the debug output window of your IDE.


No no, my apologies, I've never used the output window at all (besides monitoring my build) and so didn't consider it. My debugging stops at breakpoints, inline stepping and debugging output statements.

I looked at the output, but it makes no reference to the failed call. There are however, quite a number of "missing" DLLs in my WOW folder:

[spoiler]'edusuite.exe': Loaded 'E:\edusuite - GUI\Debug\edusuite.exe', Symbols loaded.
'edusuite.exe': Loaded 'C:\Windows\SysWOW64\ntdll.dll', Cannot find or open the PDB file
'edusuite.exe': Loaded 'C:\Windows\SysWOW64\kernel32.dll', Cannot find or open the PDB file
'edusuite.exe': Loaded 'C:\Windows\SysWOW64\KernelBase.dll', Cannot find or open the PDB file
'edusuite.exe': Loaded 'C:\Windows\SysWOW64\user32.dll', Cannot find or open the PDB file
'edusuite.exe': Loaded 'C:\Windows\SysWOW64\gdi32.dll', Cannot find or open the PDB file
'edusuite.exe': Loaded 'C:\Windows\SysWOW64\lpk.dll', Cannot find or open the PDB file
'edusuite.exe': Loaded 'C:\Windows\SysWOW64\usp10.dll', Cannot find or open the PDB file
'edusuite.exe': Loaded 'C:\Windows\SysWOW64\msvcrt.dll', Cannot find or open the PDB file
'edusuite.exe': Loaded 'C:\Windows\SysWOW64\advapi32.dll', Cannot find or open the PDB file
'edusuite.exe': Loaded 'C:\Windows\SysWOW64\sechost.dll', Cannot find or open the PDB file
'edusuite.exe': Loaded 'C:\Windows\SysWOW64\rpcrt4.dll', Cannot find or open the PDB file
'edusuite.exe': Loaded 'C:\Windows\SysWOW64\sspicli.dll', Cannot find or open the PDB file
'edusuite.exe': Loaded 'C:\Windows\SysWOW64\cryptbase.dll', Cannot find or open the PDB file
'edusuite.exe': Loaded 'C:\Windows\SysWOW64\d3d9.dll', Cannot find or open the PDB file
'edusuite.exe': Loaded 'C:\Windows\SysWOW64\version.dll', Cannot find or open the PDB file
'edusuite.exe': Loaded 'C:\Windows\SysWOW64\d3d8thk.dll', Cannot find or open the PDB file
'edusuite.exe': Loaded 'C:\Windows\SysWOW64\dwmapi.dll', Cannot find or open the PDB file
'edusuite.exe': Loaded 'C:\Windows\SysWOW64\msvcr100d.dll', Symbols loaded.
'edusuite.exe': Loaded 'C:\Windows\SysWOW64\msvcp100d.dll', Symbols loaded.
'edusuite.exe': Loaded 'C:\Windows\SysWOW64\imm32.dll', Cannot find or open the PDB file
'edusuite.exe': Loaded 'C:\Windows\SysWOW64\msctf.dll', Cannot find or open the PDB file
'edusuite.exe': Loaded 'C:\Windows\SysWOW64\uxtheme.dll', Cannot find or open the PDB file
'edusuite.exe': Loaded 'C:\Windows\SysWOW64\ole32.dll', Cannot find or open the PDB file
'edusuite.exe': Loaded 'C:\Windows\SysWOW64\clbcatq.dll', Cannot find or open the PDB file
'edusuite.exe': Loaded 'C:\Windows\SysWOW64\oleaut32.dll', Cannot find or open the PDB file
'edusuite.exe': Loaded 'C:\Windows\SysWOW64\aticfx32.dll', Cannot find or open the PDB file
'edusuite.exe': Loaded 'C:\Windows\SysWOW64\atiu9pag.dll', Cannot find or open the PDB file
'edusuite.exe': Loaded 'C:\Windows\SysWOW64\atiumdag.dll', Cannot find or open the PDB file
'edusuite.exe': Loaded 'C:\Windows\SysWOW64\atiumdva.dll', Cannot find or open the PDB file
The program '[2232] edusuite.exe: Native' has exited with code 0 (0x0).[/spoiler]

These errors popped up after I added the DirectX 9 DLLs to the debugging folder too.

Share this post


Link to post
Share on other sites
No need to worry about those; they're just reporting missing symbols files for your system DLLs and don't signify anything that could cause device creation to fail.

Regarding the failure itself, maybe try filling in the rest of your D3DPRESENT_PARAMETERS struct with valid values.

Share this post


Link to post
Share on other sites
Is it possible that there is a Windows programming error? I consulted Game Programming All In One (a book that works with DirectX 8), which was my first book (I preferred that one's style, especially with the class layout, but as it worked with DirectX 8 some features were redundant in DirectX 9 and when I found out that a lot of what I needed was redundant, I swapped books). Basically, the code provided there matches my current code, and when I was initializing my device through that book, there were no issues (until I got to surfaces). Both scenarios are in DirectX 9, not 8.

Before I post any Windows code, I'd like to know the possibility of Windows code errors in this scenario.

Share this post


Link to post
Share on other sites
If you're not making a 64-bit application (Which you're not, going by all the WOW64 references in your debug output), you should be toggling the debug spew in the 32-bit DirectX Control Panel. And you should see output like the following if it's set up correctly:

DebugOutput.PNG

(Excuse the out of date screenshot :))

Share this post


Link to post
Share on other sites

If you're not making a 64-bit application (Which you're not, going by all the WOW64 references in your debug output), you should be toggling the debug spew in the 32-bit DirectX Control Panel. And you should see output like the following if it's set up correctly:

[image removed from quote to conserve reading space and internet quota]

(Excuse the out of date screenshot :))


Ok so I changed to 32-bit and got a list of new output. Initially it was useful as my BuckBufferFormat was in the wrong format. I changed my directinit.cpp to the following (courtesy of Game Programming All In One):

#include <Windows.h>
#include <d3d9.h>
#include "error.h"

extern LPDIRECT3D9 d3d;
extern LPDIRECT3DDEVICE9 d3ddev;
extern bool windowedMode;
D3DPRESENT_PARAMETERS d3dpp;
int DirectInit(HWND hWnd)
{
// Initialise all DirectX objects
// Direct3D
d3d = Direct3DCreate9(D3D_SDK_VERSION);
if (d3d == NULL)
{
return DIRECT3D_FAILURE;
}

D3DDISPLAYMODE d3dDisplayMode;
if (FAILED(d3d->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3dDisplayMode)))
{
return E_FAIL;
}

// Set Direct3D presentation parameters

ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = false;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = d3dDisplayMode.Format;

// Create the D3D Device
if (FAILED(d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_HARDWARE_VERTEXPROCESSING, // Relies on the GPU over the CPU
&d3dpp, &d3ddev)))

{
MessageBox(hWnd, "Error", "Error", MB_OK);
}

if (d3ddev == NULL)
{
return DIRECT3D_DEV_FAILURE;
}

return NO_ERROR_DETECTED;
}

(Note that bold indicates change, underline indicates breakpoint).
I then recompiled and got the following output:

[spoiler]D3D9 Helper: Enhanced D3DDebugging disabled; Application was not compiled with D3D_DEBUG_INFO
Direct3D9: (INFO) :======================= Hal SWVP device selected

Direct3D9: (INFO) :HalDevice Driver Style b

Direct3D9: :BackBufferCount not specified, considered default 1
Direct3D9: :Subclassing window 000806ca
Direct3D9: :StartExclusiveMode
Direct3D9: (ERROR) :Display mode is unsupported
Direct3D9: (ERROR) :Display mode is unsupported
Direct3D9: (ERROR) :Display mode is unsupported
Direct3D9: (ERROR) :Display mode is unsupported
Direct3D9: (ERROR) :Unable to set the new mode. CreateDevice/Reset Fails
Direct3D9: (ERROR) :Failed to initialize primary swapchain
Direct3D9: (ERROR) :Failed to initialize Framework Device. CreateDeviceEx Failed.

Direct3D9: :DoneExclusiveMode
Direct3D9: :INACTIVE: 00001558: Restoring original mode (1920x1080x22x60) at adapter index 0
Direct3D9: :WM_DISPLAYCHANGE: 1920x1080x32
Direct3D9: :Unsubclassing window 000806ca
The thread 'Win32 Thread' (0xda8) has exited with code 0 (0x0).
The program '[5464] edusuite.exe: Native' has exited with code 0 (0x0).[/spoiler]

One other thing to note is that there was an attempt to re size my resolution. I think that's in the output, but I thought I'd say it anyway just in case.

Unfortunately, I do not understand much of this, except that BackBufferFormat was never actually initialised. Other than that, it is obvious that my display mode isn't compatible, but I don't know why, especially seeing as I got a successful runtime with the Game Programming All In One book on the first go. Again, the two listings aren't all that different.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!