Jump to content
  • Advertisement
Sign in to follow this  
feariel

Help with d3d->CreateDevice

This topic is 2282 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

Im reading the book "Beginning Game Programming" second edition by Jonathan s. Harbour,
and in chapter 5 my program crashes at d3d->CreateDevice. here is my source code:


//Beginning game programming
//Chapter 5
//d3d_windowed program

#include <d3d9.h>
#include <time.h>

#define APPTITLE "Direct3d_Windowed"

LRESULT WINAPI WinProc(HWND, UINT, WPARAM, LPARAM);
ATOM MyRegisterClass(HINSTANCE);
int Game_Init(HWND);
void Game_Run(HWND);
void Game_End(HWND);

LPDIRECT3D9 d3d = NULL;
LPDIRECT3DDEVICE9 d3ddev = NULL;

LRESULT WINAPI WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_DESTROY:
Game_End(hWnd);
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd,msg,wParam,lParam);
}

ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wc;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC)WinProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = NULL;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = APPTITLE;
wc.hIconSm = NULL;

return RegisterClassEx(&wc);
}

int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPreviousInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
MSG msg;

MyRegisterClass(hInstance);

HWND hWnd;

hWnd = CreateWindow(
APPTITLE,
APPTITLE,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
500,
400,
NULL,
NULL,
hInstance,
NULL);

if (!hWnd)
return FALSE;

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

if (!Game_Init(hWnd))
return 0;

int done = 0;

while (!done)
{
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
if (msg.message == WM_QUIT)
{
MessageBox(hWnd,"Recieved WM_QUIT message","WinMain",MB_OK);
done = 1;
}

TranslateMessage(&msg);
DispatchMessage(&msg);
}

else
Game_Run(hWnd);
}
return msg.wParam;
}

int Game_Init(HWND hWnd)
{
MessageBox(hWnd,"Game Is About To Run", "Game_Init",MB_OK);

d3d = Direct3DCreate9(D3D_SDK_VERSION);

if (d3d = NULL)
{
MessageBox(hWnd,"Error Initializing Direct3d","Error",MB_OK);
return 0;
}

D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp,sizeof(d3dpp));
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;

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

if (d3ddev = NULL)
{
MessageBox(hWnd,"Error creating direct3d device","Error",MB_OK);
return 0;
}

srand(time(NULL));

return 1;
}

void Game_Run(HWND hWnd)
{
if (d3ddev == NULL)
return;

d3ddev->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,255,255),1.0f,0);

if (d3ddev->BeginScene())
{
d3ddev->EndScene();
}

d3ddev->Present(NULL,NULL,NULL,NULL);
}

void Game_End(HWND hWnd)
{
MessageBox(hWnd,"Program is about to end","Game_End",MB_OK);

if (d3ddev != NULL)
d3ddev->Release();

if (d3d != NULL)
d3d->Release();
}


it seems that the crush has something to do with the pointers of d3d and d3ddev although i might be mistaken.

All help will be greatly appreciated.

Share this post


Link to post
Share on other sites
Advertisement
Your problem is this line:

if (d3d = NULL)

...

it's assigning your d3d pointer to NULL, instead of comparing... what you want is :

if (d3d == NULL)

...

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!