Jump to content

  • Log In with Google      Sign In   
  • Create Account


Help with d3d->CreateDevice


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 feariel   Members   -  Reputation: 122

Like
0Likes
Like

Posted 18 April 2012 - 08:32 AM

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.

Sponsor:

#2 gfxgangsta   Members   -  Reputation: 532

Like
1Likes
Like

Posted 18 April 2012 - 08:46 AM

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)

...

#3 feariel   Members   -  Reputation: 122

Like
0Likes
Like

Posted 18 April 2012 - 10:00 AM

Wow,
do i feel silly.

Thank you so much, i dont know how i could miss that.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS