I'm new to programming and have a problem with a directx example program I've been trying to complete. The source is from Beginning Game programming by Jonathan Harbour.
Basically when it compiles fine, but when I run the program it just goes to the desktop without warning. I've put in some error checking code, but I can't figure out what's wrong.
Here some variable explainations:
d3ddev is a LPDIRECT3DDEVICE9
sprite_handler is a LPD3DXSPRITE
kitty is a SPRITE which is an user defined type
kitty_image is an array of LPDIRECT3DTEXTURE9
result is a HRESULT
Below is a snippet of where I think the problem is.
//start rendering
if (d3ddev->BeginScene())
{
//erase the entire background
d3ddev->StretchRect(back, NULL, backbuffer, NULL, D3DTEXF_NONE);
//start sprite handler
result = sprite_handler->Begin(D3DXSPRITE_ALPHABLEND);
if(result != S_OK)
MessageBox(hwnd, "sprite_handler begin error", "Error", MB_OK);
//Create vector to update sprite position
D3DXVECTOR3 position((float)kitty.x, (float)kitty.y, 0);
// Draw the sprite
sprite_handler->Draw(
kitty_image[kitty.curframe],
NULL,
NULL,
&position,
D3DCOLOR_XRGB(255,255,255));
//stop drawing
result = sprite_handler->End();
if(result != S_OK)
MessageBox(hwnd, "sprite_handler end error", "Error", MB_OK);
//stop rendering
d3ddev->EndScene();
}
//display the back buffer on the screen
d3ddev->Present(NULL, NULL, NULL, NULL);
Any help is appreciated and if I forgot to add anything just ask for it...
Thanks,
John
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I've tried to use the debugger, but unfortunately the screen modes change and I can't get the code to continue because the program just hangs. I have to ctrl-alt-del and end the dev-C++ process to get back to windows. Any suggestions or ways to properly use the debugger?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
It might sound obvious, but presumably you're running this section in a main game loop, and the program's not exiting normally, right?
An alternative would be to run the program through the debugger, but with no breakpoints. The debugger should trap the program exit, although it won't give you the most helpful error message, it might pinpoint the problem for you.
Final tedious step would be to put a message box after every statement, displaying "Line xxx" etc. and find out which is the last to show before your program exits.
Good luck,
Ian
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I've narrowed the problem to this one function call (thanks for the tip for the message boxes btw :) )
sprite_handler->End();
The program never gets past this point and exits to windows. According to MSDN this function "Calls ID3DXSprite::Flush and restores the device state to how it was before ID3DXSprite::Begin was called"
Any ideas why it would fail here?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
You could try putting a try{} block around that call, and adding a catch( ... ) with a suitable error message, to see if it is throwing an exception.
If you are using a hardware surface, have you tried reverting to using a software one and see if the problem goes away? If it does, then you might need to check the capabilities of your video card driver, perhaps the alpha-blended sprites?
Last resort is to get yourself some debug DirectX DLLs and prepare for some considerable pain......
Good luck,
Ian
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hello,
I'm new to programming and have a problem with a directx example program I've been trying to complete. The source is from Beginning Game programming by Jonathan Harbour.
Basically when it compiles fine, but when I run the program it just goes to the desktop without warning. I've put in some error checking code, but I can't figure out what's wrong.
Here some variable explainations:
d3ddev is a LPDIRECT3DDEVICE9
sprite_handler is a LPD3DXSPRITE
kitty is a SPRITE which is an user defined type
kitty_image is an array of LPDIRECT3DTEXTURE9
result is a HRESULT
Below is a snippet of where I think the problem is.
Any help is appreciated and if I forgot to add anything just ask for it...
Thanks,
John
Your if statement is testing the return value of d3ddev->BeginScene()
However, if the function call is successful, it will return D3D_OK, which enumerates to zero, and all of the code in the if block will be skipped!
Change it to if(d3ddev->BeginScene == D3D_OK) and the problem should go away. However, you ought to put in some decent error handling at some point.
Cheers,
Ian
...or use OpenGL :)
Thank you for your reply. I changed the above if statement to:
result = d3ddev->BeginScene();
if (result!=D3d_OK)
MessageBox(hwnd, "begin scene error", "Error", MB_OK);
if (result==D3D_OK)
{
...
}
The program will still exit to windows...I've put in error code checks at various places, but I can't seem to track down why it's exiting out?
Your help is greatly appreciated!
put a stop every couple of lines and run throu the debger.
I've tried to use the debugger, but unfortunately the screen modes change and I can't get the code to continue because the program just hangs. I have to ctrl-alt-del and end the dev-C++ process to get back to windows. Any suggestions or ways to properly use the debugger?
It might sound obvious, but presumably you're running this section in a main game loop, and the program's not exiting normally, right?
An alternative would be to run the program through the debugger, but with no breakpoints. The debugger should trap the program exit, although it won't give you the most helpful error message, it might pinpoint the problem for you.
Final tedious step would be to put a message box after every statement, displaying "Line xxx" etc. and find out which is the last to show before your program exits.
Good luck,
Ian
I've narrowed the problem to this one function call (thanks for the tip for the message boxes btw :) )
sprite_handler->End();
The program never gets past this point and exits to windows. According to MSDN this function "Calls ID3DXSprite::Flush and restores the device state to how it was before ID3DXSprite::Begin was called"
Any ideas why it would fail here?
You could try putting a try{} block around that call, and adding a catch( ... ) with a suitable error message, to see if it is throwing an exception.
If you are using a hardware surface, have you tried reverting to using a software one and see if the problem goes away? If it does, then you might need to check the capabilities of your video card driver, perhaps the alpha-blended sprites?
Last resort is to get yourself some debug DirectX DLLs and prepare for some considerable pain......
Good luck,
Ian