CreateOffscreenPlainSurface crash?

Started by
2 comments, last by by 17 years, 10 months ago
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?
Advertisement
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.
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.

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.

This topic is closed to new replies.

Advertisement