Sign in to follow this  
by

CreateOffscreenPlainSurface crash?

Recommended Posts

Hi all, I started learning directx9 yesterday. I made a function to load bmp files to surfaces:
IDirect3DSurface9* getSurfaceFromBitmap(string filename)
{
	HRESULT hResult;
	IDirect3DSurface9* surface = NULL;
	D3DXIMAGE_INFO imageInfo;
	hResult = D3DXGetImageInfoFromFile(filename.c_str(), &imageInfo);
	if(FAILED(hResult))
		return NULL;
	hResult = pd3dDevice->CreateOffscreenPlainSurface(imageInfo.Width, imageInfo.Height, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &surface, NULL);
	if(FAILED(hResult))
		return NULL;
	hResult = D3DXLoadSurfaceFromFile(surface, NULL, NULL, filename.c_str(), NULL, D3DX_DEFAULT, 0, NULL);
	if(FAILED(hResult))
		return NULL;
	return surface;
}



In my InitDirect3d() function:
bool InitDirect3d()
{
	pD3D=NULL;
	pd3dDevice=NULL;
	if( NULL == (pD3D=Direct3DCreate9(D3D_SDK_VERSION)))
	{
		
		return false;
	}
	D3DPRESENT_PARAMETERS d3dpp;
	ZeroMemory(&d3dpp, sizeof(d3dpp));
	d3dpp.Windowed = TRUE;
	d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
	d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
	d3dpp.BackBufferCount = 1;
	d3dpp.BackBufferWidth = 640;
	d3dpp.BackBufferHeight = 480;
	d3dpp.hDeviceWindow = hWndMain;

	if( FALSE( pD3D->CreateDevice( D3DADAPTER_DEFAULT,
									 D3DDEVTYPE_REF,
									 hWndMain,
									 D3DCREATE_SOFTWARE_VERTEXPROCESSING,
									 &d3dpp,
									 &pd3dDevice) ) )
	{
		return false;
	}
	surf = getSurfaceFromBitmap("star.bmp");
	if(surf==NULL)
             return false;

return true;
}


When i compile program, there isn't any problems. But program just crashes without an error. I tried to comment other lines in getSurfaceFromBitmap function, it only crashes while hResult = pd3dDevice->CreateOffscreenPlainSurface(imageInfo.Width, imageInfo.Height, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &surface, NULL); line uncommented. Any suggestions?

Share this post


Link to post
Share on other sites
Have you tried using the debugger to check the values of the parameters and pointers before calling the function? What sort of "crash" is it? An access violation reading/writing 0x00000000 is a NULL pointer.

It looks like your device isn't being created. You're using FALSE instead of FAILED, which will probably eveluate to nothing.

Share this post


Link to post
Share on other sites
Quote:
Original post by Evil SteveIt looks like your device isn't being created. You're using FALSE instead of FAILED, which will probably eveluate to nothing.


I believe the reason is that this code tries to create a Ref device.

Share this post


Link to post
Share on other sites
Thanks!

if( FAILED( pD3D->CreateDevice( D3DADAPTER_DEFAULT,
D3DDEVTYPE_REF,
hWndMain,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp,
&pd3dDevice) ) )




It works fine now. :)

By the way, i read code from a book named Beginning Directx 9 by Wendy Jones, i think this book has lots of code bugs.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this