All my calls to d3d9->CreateDevice fail and return the error code D3DERR_INVALIDCALL. I used the de bugger and it think I have traced to problem to the D3DPRESENT_PARAMETERS structure. whenever I instanciate it, it's variable hDeviceWindow is set to 0xcccccccc, the value that the compiler assigns to variables that are not explicitly instanciated. I am fairly certain this is the cause of the problem, but I have no idea how to fix it. Here is my source code:
/*
still need to do:
1. initilize sound system
2. initilize input system
3. add particle structure and .x file functions
*/
#define
DIRECTINPUT_VERSION 0x0800#include
"d3d9.h"#include
"d3dx9.h"#include
"dinput.h"#include
<windows.h>#include
<stdio.h>LPDIRECT3D9 d3d9 = NULL;
LPDIRECT3DDEVICE9 d3ddeviceptr = NULL;
D3DXMATRIX defaultworldmat, defaultviewmat, defaultprojmat;
ID3DXFont *defaultfont = NULL;
IDirectInput8 *dinputobj;
IDirectInputDevice8 *keyboard;
IDirectInputDevice8 *mouse;
HINSTANCE globinsthandle;
HWND globwinhandle;
int
PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int nCmdShow);LRESULT FAR PASCAL WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
void
frame();HINSTANCE initilization(HINSTANCE insthandle, HWND winhandle);
void
shutdown();int
PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int nCmdShow){
MSG message;
globinsthandle = hInst;
WNDCLASSEX windowclass;
windowclass.cbSize =
sizeof(windowclass);windowclass.style = CS_CLASSDC;
windowclass.lpfnWndProc = WindowProc;
windowclass.cbClsExtra = 0;
windowclass.cbWndExtra = 0;
windowclass.hInstance = hInst;
windowclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
windowclass.hCursor = LoadCursor(NULL, IDC_ARROW);
windowclass.hbrBackground = NULL;
windowclass.lpszMenuName = NULL;
windowclass.lpszClassName = (LPCWSTR)
"game";windowclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
RegisterClassEx(&windowclass);
HWND handletomainwindow;
handletomainwindow = CreateWindow((LPCWSTR)
"game", (LPCWSTR)"(name of game goes here)", WS_OVERLAPPEDWINDOW, 0, 0, 1024, 768, NULL, NULL, hInst, NULL);ShowWindow(handletomainwindow, SW_NORMAL);
UpdateWindow(handletomainwindow);
initilization(hInst, handletomainwindow);
ZeroMemory(&message,
sizeof(message)); while(message.message != WM_QUIT){
if(PeekMessage(&message, NULL, 0, 0, PM_REMOVE)){
TranslateMessage(&message);
DispatchMessage(&message);
}
elseframe();
}
shutdown();
UnregisterClass((LPCWSTR)
"game", hInst); return 0;}
void
frame(){
}
HINSTANCE initilization(HINSTANCE insthandle, HWND winhandle)
{
// initilize d3d // there is something wrong with this. d3dpresparam.hDeviceWindow equals 0xcccccccc, and I am almost 100% sure it shouldn't.D3DDISPLAYMODE d3ddisplaymode;
d3d9 = Direct3DCreate9(D3D_SDK_VERSION);
if(FAILED(d3d9->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddisplaymode))){
d3d9->Release();
return insthandle;}
D3DPRESENT_PARAMETERS d3dpresparam;
ZeroMemory(&d3dpresparam,
sizeof(&d3dpresparam));d3dpresparam.Windowed = TRUE;
// apparently 0xcccccccc means that the variable was not explicitly initilized. except that it was.d3dpresparam.SwapEffect = D3DSWAPEFFECT_DISCARD;
// just a thought: maybe I should change when I initilize d3dpresparam.d3dpresparam.BackBufferFormat = d3ddisplaymode.Format;
d3dpresparam.EnableAutoDepthStencil = TRUE;
d3dpresparam.AutoDepthStencilFormat = D3DFMT_D16;
HRESULT resulthandle = d3d9->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, globwinhandle, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpresparam, &d3ddeviceptr);
if(FAILED(resulthandle)){
int testvar; if(resulthandle == D3DERR_INVALIDCALL){
testvar = 1;
}
}
if(FAILED(d3ddeviceptr->SetRenderState(D3DRS_ZENABLE, TRUE))){
d3d9->Release();
d3ddeviceptr->Release();
return insthandle;}
// set up default matricesD3DXMatrixPerspectiveFovLH(&defaultprojmat, D3DX_PI/4.0f, 1.33333f, 1.0f, 1000.0f);
if(FAILED(d3ddeviceptr->SetTransform(D3DTS_PROJECTION, &defaultprojmat))){
d3d9->Release();
d3ddeviceptr->Release();
return insthandle;}
D3DXMatrixLookAtLH(&defaultviewmat, &D3DXVECTOR3(0.0f, 0.0f, -500.0f), &D3DXVECTOR3(0.0f, 0.0f, 0.0f), &D3DXVECTOR3(0.0f, 1.0f, 0.0f));
if(FAILED(d3ddeviceptr->SetTransform(D3DTS_VIEW, &defaultviewmat))){
d3d9->Release();
d3ddeviceptr->Release();
return insthandle;}
if(FAILED(d3ddeviceptr->SetTransform(D3DTS_WORLD, &defaultworldmat))){
d3d9->Release();
d3ddeviceptr->Release();
return insthandle;}
//set up default font (just a plain font, need to create more fonts when we need them)LOGFONT dlogfont;
ZeroMemory(&dlogfont,
sizeof(dlogfont));strcpy((
char*)dlogfont.lfFaceName, "Arial");dlogfont.lfHeight = -12;
if(FAILED(D3DXCreateFontIndirect(d3ddeviceptr, (D3DXFONT_DESCW*)&dlogfont, &defaultfont))){
d3d9->Release();
d3ddeviceptr->Release();
return insthandle;}
// note: calls to defaultfont->DrawText must be between calls to defaultfont->Begin() and defaultfont->End() // done with d3d, on to dinput if(FAILED(DirectInput8Create(insthandle, DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&dinputobj, NULL))){
d3d9->Release();
d3ddeviceptr->Release();
defaultfont->Release();
return insthandle;}
if(FAILED(dinputobj->CreateDevice(GUID_SysKeyboard, &keyboard, NULL))){
// no system keyboard.d3d9->Release();
d3ddeviceptr->Release();
defaultfont->Release();
dinputobj->Release();
return insthandle;}
if(FAILED(keyboard->SetDataFormat(&c_dfDIKeyboard))){
d3d9->Release();
d3ddeviceptr->Release();
defaultfont->Release();
dinputobj->Release();
keyboard->Release();
return insthandle;}
if(FAILED(keyboard->SetCooperativeLevel(globwinhandle, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE))){
d3d9->Release();
d3ddeviceptr->Release();
defaultfont->Release();
dinputobj->Release();
keyboard->Release();
return insthandle;}
// might want to change NONEXCLUSIVE to EXLUSIVE. // there are positives and negatives to both. On the one hand, maybe we don't want anyone else to have the use of a keyboard if it's // a fullscreen app. On the other, what if they minimize it to say, check their email. However, for most fullscreen apps the best option // is EXCLUSIVE if(FAILED(keyboard->Acquire())){
d3d9->Release();
d3ddeviceptr->Release();
defaultfont->Release();
dinputobj->Release();
keyboard->Release();
return insthandle;}
// next up, the mouse if(FAILED(dinputobj->CreateDevice(GUID_SysMouse, &mouse, NULL))){
d3d9->Release();
d3ddeviceptr->Release();
defaultfont->Release();
dinputobj->Release();
keyboard->Release();
return insthandle; // no system mouse}
if(FAILED(mouse->SetDataFormat(&c_dfDIMouse))){
d3d9->Release();
d3ddeviceptr->Release();
defaultfont->Release();
dinputobj->Release();
keyboard->Release();
mouse->Release();
return insthandle;}
if(FAILED(mouse->SetCooperativeLevel(globwinhandle, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE))){
d3d9->Release();
d3ddeviceptr->Release();
defaultfont->Release();
dinputobj->Release();
keyboard->Release();
mouse->Release();
return insthandle; // again, might want to make EXCLUSIVE}
if(FAILED(mouse->Acquire())){
d3d9->Release();
d3ddeviceptr->Release();
defaultfont->Release();
dinputobj->Release();
keyboard->Release();
mouse->Release();
return insthandle;}
return insthandle;}
void
shutdown(){
if(d3ddeviceptr != NULL)d3ddeviceptr->Release();
if(defaultfont != NULL)defaultfont->Release();
if(dinputobj != NULL)dinputobj->Release();
if(keyboard != NULL)keyboard->Release();
if(mouse != NULL)mouse->Release();
return;}
LRESULT FAR PASCAL WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg){
case WM_DESTROY:PostQuitMessage(0);
return 0;}
return DefWindowProc(hWnd, uMsg, wParam, lParam);}
note: I am sorry for all the unneccessary error checking. it was part of my trying to fix the bug (at that point i didn't know that my call to CreateDevice was failing).

hDeviceWindow = 0xcccccccc
Jamie Briant
Your ZeroMemory statement to initialize d3dpresparam is wrong.
Try ZeroMemory(&d3dpresparam, sizeof(d3dpresparam));
or ZeroMemory(&d3dpresparam, sizeof(D3DPRESENT_PARAMETERS));