Sign in to follow this  
KnTenshi

Begining Game Programming (2nd Ed.) DirectX Errors

Recommended Posts

I can't get the animated sprite coding to work. After tweaking it to allow UNICODE to be satisfied with the program, it will compile, but breaks in the result = d3ddev->CreateOffscreenPlainSurface(...) part with a call to outside memory. It seems that the info isn't being made. Here is the relevant part of my code. filename comes in corectly.
LPDIRECT3DSURFACE9 LoadSurface(LPCSTR filename, D3DCOLOR transcolor, HWND hWnd)
{
	LPDIRECT3DSURFACE9 image = NULL;
	D3DXIMAGE_INFO info;
	HRESULT result;

	//get width and height from bitmal file
	result = D3DXGetImageInfoFromFileA(filename,&info);
	if(SUCCEEDED(result))
	{
		MessageBox(hWnd, L"fail at info", L"Error!", MB_OK);
		return NULL;
	}

	//create surface
	result = d3ddev->CreateOffscreenPlainSurface(
		info.Width,		//width of the surface
		info.Height,		//height of the surface
		D3DFMT_X8R8G8B8,	//surface format
		D3DPOOL_DEFAULT,	//memory pool to use
		&image,			//pointer to surface
		NULL);			//reserved (always NULL)

	if(result != D3D_OK)
		return NULL;

	//load surface from file
	result = D3DXLoadSurfaceFromFile(
		image,			//destination surface
		NULL,			//destination pallet
		NULL,			//destination rectangle
		(LPCWSTR)(filename),	//source file name
		NULL,			//source rectangle
		D3DX_DEFAULT,		//controls hwo the image is filtered
		transcolor,		//for transparentcy
		NULL);			//source image info(usually NULL)

	//check if the file was loaded ok
	if(result != D3D_OK)
		return NULL;

	return image;
}
Something else I'm missing?

Share this post


Link to post
Share on other sites
result = d3ddev->CreateOffscreenPlainSurface(

info.Width, //width of the surface

info.Height, //height of the surface

D3DFMT_X8R8G8B8, //surface format

D3DPOOL_DEFAULT, //memory pool to use

ℑ, //pointer to surface <--- ???

NULL); //reserved (always NULL)


What u have passed as pointer to surface ? (i saw a weird symbol look like 3)

IDirect3DSurface9 pSurface;
d3ddev->CreateOffscreenPlainSurface(..., ..., ..., ..., &pSurface, ...);

Share this post


Link to post
Share on other sites
Well I dunno what's up with the code you pasted, but you've got some funky character right where your pointer parameter should be in the call to CreateOffscreenPlainSurface.

Another problem I noticed, is this right here:

(LPCWSTR)(filename)


Bad, bad, bad. Casting a pointer to a char string does not convert it to a wide-char string, it just makes that function re-interpret the pointer. It's kinda like your friend saying "Hey give me a bag full quarters", and then instead of giving him a bag a of quarters you take a big of nickles and write "quarters" on it.

If a function expects a Unicode wide-character string, then that's what you have to give it. If all you have is an ANSI character string, you need to convert it. Win32 has a function for doing this. Another alternative would be to directly call the ANSI version of whatever function you're calling, in this case D3DXLoadSurfaceFromFileA (this is exactly what you did with D3DXGetImageInfoFromFile).

Here's some points of advice...

1. Either stick with wide-character (Unicode) strings, or stick with character (ANSI) strings. Don't mix and match...it'll lead to situations like this one. If you don't know which to pick, go with Unicode. For a while now everything in Win32 has been natively Unicode, and it gives you the best support for multiple languages and international settings.

2. If you're not already using it, use std::string (or std::wstring, for wide-character strings). It's much nicer and safer to work with than raw pointers. You're using C++ not C, make use of the benefits.

3. Don't cast pointers unless you really sure you know why you're doing it. If you do it in the wrong place just to make the compiler shut up, you'll run into (harder to debug) errors at runtime. If you do cast, it's generally preferred to use the C++ casting operators as opposed to C-style casts.

4. You seem to be familiar with at least some aspects of Unicode/wide-character strings vs. ANSI/character strings, but if you have some doubts I made journal entry that serves as a sort of basic introduction to that whole issue.

Share this post


Link to post
Share on other sites
I have no idea how "address of image" got converted to an odd cursive I.

I've made the changes that MJP suggested and my problem is still the same: info isn't being set correctly.

filename seems to be passed correctly (it pops up correctly in debuging atleast).

info.Width and Height are being set to some inordinately high number (three billion something or other) and all other of info's parameters are like that. This makes me think it's just using info's address locations for the variables.

Is there any way for me to check to see what is happening inside of D3DXGetImageInfoFromFileA?

Share this post


Link to post
Share on other sites
Quote:

if(SUCCEEDED(result))
{
MessageBox(hWnd, L"fail at info", L"Error!", MB_OK);
return NULL;
}



Also
is this a typo ? should it be if(FAILED(result)) ?
if u dont get an error message box, it means D3DXGetImageInfoFromFileA is failed.

Quote:

Is there any way for me to check to see what is happening inside of D3DXGetImageInfoFromFileA?


just add a breakpoint and Step into (F11) it ?

Share this post


Link to post
Share on other sites
Uh oh, yeah SUCCEEDED would be an error on my part. I'll give that a shot (I'm not too good at debugging past obvious errors or errors in my own code).

Share this post


Link to post
Share on other sites
Well, info works now. But image is not.

I get an access violation from reading location 0x00000000.
I initialize LPDIRECT3DSURFACE9 image to NULL. It then gets called by and should be set by d3ddev->CreateOffscreenPlainSurface(...). That weird 'I' thing is &-image (remove the minus, something in the board is translating that into &image).

image is the surface for the sprite I'm making.

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