Very Basic Question that will make all of us facepalm

Started by
4 comments, last by frob 7 years, 2 months ago

I am just getting back into d3d9 and i cannot get my program to work please help


void D3D9CLASS::initD3D(HWND hwnd)
{
 
d3d = Direct3DCreate9(D3D_SDK_VERSION);
 
D3DPRESENT_PARAMETERS d3dpp;
 
ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = true;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.hDeviceWindow = hwnd;
 
d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &d3ddev);
 
}
d3d is in the class public
Advertisement
For starters, you should check the status code returned by every Direct3D API call you make. That should give you enough insight to determine where your problem is, and how to fix it.

make sure you enable debug output, too.

I suggest you start by telling us what the actual problem is. Is it not compiling? Is it crashing when you run it? Is it doing something (or not doing something) unexpectedly? What errors if any are you getting?

Sean Middleditch – Game Systems Engineer – Join my team!

d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &d3ddev);

hal and software vertex processing doesn't sound right... might give you something like E_INVALIDCALL instead of D3D_OK as a return result. make sure those params are not mutually exclusive.

here's the startup code from Caveman 3.0 (a dx9 app)...

do adapter stuff determines the screen resolution to use.

code blocks are being a pain to edit as usual, so the code is shown in blue text.

void do_adapter_stuff(int *w2,int *h2)
{
int num_adaptor_modes,i,w,h;
D3DDISPLAYMODE d[50];
HRESULT result;
FILE *f;
char s[100],s2[100];
if (filefound("startup_rez.dat"))
{
f=infile("startup_rez.dat");
w=readfileint(f);
h=readfileint(f);
fclose(f);
*w2=w;
*h2=h;
if (Zdebug)
{
i2s(w,s);
strcat_s(s,100," x ");
i2s(h,s2);
strcat_s(s,100,s2);
strcat_s(s,100," resolution specified in startup_rez.dat.");
Zmsg(s,Zprogram_name);
}
return;
}
if (Zdebug)
{
Zmsg("Getting adapter mode count",Zprogram_name);
}
num_adaptor_modes=(int)d3d_obj_ptr->GetAdapterModeCount(D3DADAPTER_DEFAULT,D3DFMT_X8R8G8B8);
w=0;
h=0;
if (Zdebug)
{
Zmsg("Enumerating adapter modes",Zprogram_name);
}
for (i=0; i<num_adaptor_modes; i++)
{
result=d3d_obj_ptr->EnumAdapterModes(D3DADAPTER_DEFAULT,D3DFMT_X8R8G8B8,(UINT)i,&d);
if (result!= D3D_OK) d.Width=0;
// TODO: d3d_obj_ptr->CheckDeviceType d3d_obj_ptr->CheckDeviceFormat d3d_obj_ptr->CheckDepthStencilMatch
// check for HAL support
result=d3d_obj_ptr->CheckDeviceType(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,D3DFMT_X8R8G8B8,D3DFMT_A8R8G8B8,FALSE);
if (result != D3D_OK)
{
d.Width=0;
}
// check for 24 bit depth stencil buffer support
result=d3d_obj_ptr->CheckDeviceFormat(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,D3DFMT_X8R8G8B8,D3DUSAGE_DEPTHSTENCIL,D3DRTYPE_SURFACE,D3DFMT_D24X8);
if (result != D3D_OK)
{
d.Width=0;
}
// check for depth stencil buffer compatability with front and back buffers
result=d3d_obj_ptr->CheckDepthStencilMatch(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,D3DFMT_X8R8G8B8,D3DFMT_A8R8G8B8,D3DFMT_D24X8);
if (result != D3D_OK)
{
d.Width=0;
}
if ((int)d.Width>w)
{
w=(int)d.Width;
h=(int)d.Height;
}
else if ((int)d.Width==w)
{
if ((int)d.Height>h) h=(int)d.Height;
}
}
if (Zdebug)
{
i2s(w,s);
strcat_s(s,100," x ");
i2s(h,s2);
strcat_s(s,100,s2);
strcat_s(s,100," resolution auto-selected.");
Zmsg(s,Zprogram_name);
}
*w2=w;
*h2=h;
}

Zsetparams sets the present parameters...

void Zsetparams(int w,int h)

{
ZeroMemory(&Zparams,sizeof(D3DPRESENT_PARAMETERS));
Zparams.AutoDepthStencilFormat = D3DFMT_D24X8;
Zparams.BackBufferCount=1;
Zparams.BackBufferFormat = D3DFMT_A8R8G8B8; // set the back buffer format to 32-bit // turn these on for fullscreen
Zparams.BackBufferWidth = (unsigned)w; //width; // set the width of the buffer
Zparams.BackBufferHeight = (unsigned)h; //height; // set the height of the buffer
Zparams.EnableAutoDepthStencil = TRUE; // automatically run the z-buffer for us
Zparams.Flags=0;
Zparams.FullScreen_RefreshRateInHz=D3DPRESENT_INTERVAL_DEFAULT;
Zparams.hDeviceWindow=Zprogram_window_handle;
Zparams.MultiSampleQuality=0;
Zparams.MultiSampleType=D3DMULTISAMPLE_NONE;
Zparams.PresentationInterval=D3DPRESENT_INTERVAL_DEFAULT;
Zparams.SwapEffect = D3DSWAPEFFECT_COPY; //D3DSWAPEFFECT_DISCARD;
Zparams.Windowed = FALSE; // TRUE for windowed, FALSE for fullscreen
}
and Zinit3D creates the device...
// init 3d stuff. returns 1 on success, 0 on failure.
int Zinit3D(float nearplane,int farplane)
{
HRESULT result;
D3DXMATRIX m;
D3DMATERIAL9 material;
int width,height;
if (Zdebug)
{
Zmsg("Creating D3D object",Zprogram_name);
}
d3d_obj_ptr=Direct3DCreate9(D3D_SDK_VERSION);
if (d3d_obj_ptr==NULL)
{
Zmsg("Error creating D3D object",Zprogram_name);
return(0);
}
do_adapter_stuff(&width,&height);
Zsetparams(width,height);
if (Zdebug)
{
Zmsg("Creating device",Zprogram_name);
}
result=d3d_obj_ptr->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,Zprogram_window_handle,
D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, // D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&Zparams,&Zd3d_device_ptr);
if (result != D3D_OK) { Zmsg("Error creating D3D device.",Zprogram_name); d3d_obj_ptr->Release(); return(0); }
// gone 3d, turn off windows mouse cursor.
ShowCursor(FALSE);
etc
etc
etc

Norm Barrows

Rockland Software Productions

"Building PC games since 1989"

rocklandsoftware.net

PLAY CAVEMAN NOW!

http://rocklandsoftware.net/beta.php

I am just getting back into d3d9

In addition to checking the return codes (which is the best answer), I have to ask: Why are you targeting D3D9?

DirectX was integrated into the OS core components. D3D9 is Windows XP. Is your game targeting Windows XP?

If your game targets Windows 7, prefer to use D3D11 unless there is a compelling reason not to. That is where you will get best performance.

Same thing for Windows 8 using Direct3D 11.1, Windows 8.1 using 11.2, and Windows 10 using 12.

Generally you should use the version that most closely matches your minimum operating system, then detect the card's feature set for the functionality it supports.

This topic is closed to new replies.

Advertisement