7 Replies - 2272 Views - Last Post: 26 May 2011 - 08:42 AM Rate Topic: -----

#1 Strikerx22   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 10-April 09

Need help with unhandled exception error

Posted 24 May 2011 - 11:17 PM

Hi, I'm currently on Ch. 5 of "Beginning Game Programming, Third Edition" and am getting an unhandled exception error when I run my code:

Unhandled exception at 0x01281f90 in Bomb_Catcher.exe: 0xC0000005: Access violation reading location 0x00000000.

Tried googling it to see if I can find something that could me solve this but haven't found anything yet.

Here is my code:

/**
	Beginning Game Programming, Third edition
	MyDirectX.cpp
**/

#include "MyDirectX.h"
#include <iostream>
using namespace std;

//Direct3D variables
LPDIRECT3D9 d3d = NULL;
LPDIRECT3DDEVICE9 d3ddev = NULL;
LPDIRECT3DSURFACE9 backbuffer = NULL;

//DirectInput variables
LPDIRECTINPUT8 dinput = NULL;
LPDIRECTINPUTDEVICE8 dimouse = NULL;
LPDIRECTINPUTDEVICE8 dikeyboard = NULL;
DIMOUSESTATE mouse_state;
char keys[256];
XINPUT_GAMEPAD controllers[4];

/**
 ** Direct3D initialization
 **/
bool Direct3D_Init(HWND window, int width, int height, bool fullscreen)
{
	//initialize Direct3D
	d3d = Direct3DCreate9(D3D_SDK_VERSION);
	if (!d3d) return false;

	//set Direct3D presentation parameters
	D3DPRESENT_PARAMETERS d3dpp;
	ZeroMemory(&d3dpp, sizeof(&d3dpp));
	d3dpp.Windowed = (!fullscreen);
	d3dpp.SwapEffect = D3DSWAPEFFECT_COPY;
	d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
	d3dpp.BackBufferCount = 1;
	d3dpp.BackBufferWidth = width;
	d3dpp.BackBufferHeight = height;
	d3dpp.hDeviceWindow = window;

	//create Direct3D device
	d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, window,
		D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &d3ddev);
	if (!d3ddev) return false;

	//get a pointer to the back buffer surface
	d3ddev->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &backbuffer);

	return true;
}

/**
 ** Direct3D shutdown
 **/
void Direct3D_Shutdown()
{
	if (d3ddev) d3ddev->Release();
	if (d3d) d3d->Release();
}

/**
 ** Draws a surface to the screen using StretchRect
 **/
void DrawSurface(LPDIRECT3DSURFACE9 dest,
	float x, float y,
	LPDIRECT3DSURFACE9 source)
{
	//get width/height from source surface
	D3DSURFACE_DESC desc;
	source->GetDesc(&desc);

	//create rects for drawing
	RECT source_rect = {0, 0,
		(long)desc.Width, (long)desc.Height};
	RECT dest_rect = {(long)x, (long)y,
		(long)x+desc.Width, (long)y+desc.Height};

	//draw the source surface onto the dest
	d3ddev->StretchRect(source, &source_rect, dest,
		&dest_rect, D3DTEXF_NONE);
}

/**
 ** Loads a bitmap file into a surface
 **/
LPDIRECT3DSURFACE9 LoadSurface(string filename)
{
    LPDIRECT3DSURFACE9 image = NULL;
    
    //get width and height from bitmap file
    D3DXIMAGE_INFO info;
    HRESULT result = D3DXGetImageInfoFromFile(filename.c_str(), &info);
    if (result != D3D_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 the surface
        NULL);              //reserved (always NULL)

    if (result != D3D_OK) return NULL;

    //load surface from file into newly created surface
    result = D3DXLoadSurfaceFromFile(
        image,                  //destination surface
        NULL,                   //destination palette
        NULL,                   //destination rectangle
        filename.c_str(),       //source filename
        NULL,                   //source rectangle
        D3DX_DEFAULT,           //controls how image is filtered
        D3DCOLOR_XRGB(0,0,0),   //for transparency (0 for none)
        NULL);                  //source image info (usually NULL)

    //make sure file was loaded okay
    if (result != D3D_OK) return NULL;

    return image;
}

/**
 ** DirectInput initialization
 **/
bool DirectInput_Init(HWND hwnd)
{
    //initialize DirectInput object
    HRESULT result = DirectInput8Create(
        GetModuleHandle(NULL), 
        DIRECTINPUT_VERSION, 
        IID_IDirectInput8,
        (void**)&dinput,
        NULL);

    //initialize the keyboard
    dinput->CreateDevice(GUID_SysKeyboard, &dikeyboard, NULL);
    dikeyboard->SetDataFormat(&c_dfDIKeyboard);
    dikeyboard->SetCooperativeLevel(hwnd, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND);
    dikeyboard->Acquire();

    //initialize the mouse
    dinput->CreateDevice(GUID_SysMouse, &dimouse, NULL);
    dimouse->SetDataFormat(&c_dfDIMouse);
    dimouse->SetCooperativeLevel(hwnd, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND);
    dimouse->Acquire();
    d3ddev->ShowCursor(false);

    return true;
}

/**
 ** DirectInput update
 **/
void DirectInput_Update()
{
    //update mouse
    dimouse->GetDeviceState(sizeof(mouse_state), (LPVOID)&mouse_state);

    //update keyboard
    dikeyboard->GetDeviceState(sizeof(keys), (LPVOID)&keys);

    //update controllers
    for (int i=0; i< 4; i++ )
    {
        ZeroMemory( &controllers[i], sizeof(XINPUT_STATE) );

        //get the state of the controller
        XINPUT_STATE state;
        DWORD result = XInputGetState( i, &state );

        //store state in global controllers array
        if (result == 0) controllers[i] = state.Gamepad;
    }

}

/**
 ** Return mouse x movement
 **/
int Mouse_X()
{
    return mouse_state.lX;
}

/**
 ** Return mouse y movement
 **/
int Mouse_Y()
{
    return mouse_state.lY;
}

/**
 ** Return mouse button state
 **/
int Mouse_Button(int button)
{
    return mouse_state.rgbButtons[button] & 0x80;
}

/**
 ** Return key press state
 **/
int Key_Down(int key)
{
    return (keys[key] & 0x80);
}

/**
 ** DirectInput shutdown
 **/
void DirectInput_Shutdown()
{
    if (dikeyboard) 
    {
        dikeyboard->Unacquire();
        dikeyboard->Release();
        dikeyboard = NULL;
    }
    if (dimouse) 
    {
        dimouse->Unacquire();
        dimouse->Release();
        dimouse = NULL;
    }
}

/**
 ** Returns true if controller is plugged in
 **/
bool XInput_Controller_Found()
{
    XINPUT_CAPABILITIES caps;
    ZeroMemory(&caps, sizeof(XINPUT_CAPABILITIES));
    XInputGetCapabilities(0, XINPUT_FLAG_GAMEPAD, &caps);
    if (caps.Type != 0) return false;
    
    return true;
}

/**
 ** Vibrates the controller
 **/
void XInput_Vibrate(int contNum, int amount)
{
    XINPUT_VIBRATION vibration;
    ZeroMemory( &vibration, sizeof(XINPUT_VIBRATION) );
    vibration.wLeftMotorSpeed = amount;
    vibration.wRightMotorSpeed = amount; 
    XInputSetState( contNum, &vibration );
}


The error happens at line 150 which is: d3ddev->ShowCursor(false);

If anyone can help me find a way to resolve this it would be greatly appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Need help with unhandled exception error

#2 stayscrisp   User is offline

  • フカユ
  • member icon

Reputation: 1040
  • View blog
  • Posts: 4,325
  • Joined: 14-February 08

Re: Need help with unhandled exception error

Posted 25 May 2011 - 02:33 AM

So you have stepped through your code and it crashes on the line you mentioned?
Was This Post Helpful? 0
  • +
  • -

#3 anonymous26   User is offline

  • D.I.C Lover

Reputation: 2
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Need help with unhandled exception error

Posted 25 May 2011 - 06:17 AM

If you look at your code you will see that you are setting your LPDIRECT* objects to NULL which is great practice. Now, in Visual Studio NULL is 0, which is also 0x00000000. this tells me that one of your 'nulled' objects has not been initialized correctly, and looking at your code you have no code that checks for successful initialization adequately.

You need to fix this by checking the actual return values when creating your objects.
Was This Post Helpful? 0
  • +
  • -

#4 Strikerx22   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 10-April 09

Re: Need help with unhandled exception error

Posted 25 May 2011 - 08:30 PM

Thanks. I'm stepping through it again to see if I can see if I can figure out the problem. As for checking my objects what would be the best way to do this so I can make sure I do it correctly. Again thanks for the help.
Was This Post Helpful? 0
  • +
  • -

#5 Strikerx22   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 10-April 09

Re: Need help with unhandled exception error

Posted 25 May 2011 - 09:04 PM

Looking closer at where the program is stopping I'm finding this error when I mouse over d3ddev: __vfptr = CXX0030: Error: expression cannot be evaluated. That means that there is a value being passed to d3ddev that isn't being evaluated correctly is that right?
Was This Post Helpful? 0
  • +
  • -

#6 Oler1s   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1397
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Need help with unhandled exception error

Posted 25 May 2011 - 09:36 PM

> That means that there is a value being passed to d3ddev that isn't being evaluated correctly is that right?

d3ddev is not a function. It is a variable in your code.
Was This Post Helpful? 0
  • +
  • -

#7 Strikerx22   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 10-April 09

Re: Need help with unhandled exception error

Posted 25 May 2011 - 10:26 PM

Didn't know that error meant a function error as know d3ddev is a variable but thanks for pointing it out as will continue to look. I also get the same error with the window variable as well and I find that in my WinMain function that the variable hInstance has a value of 0x00a30000 {unused=9460301 }. Not sure if that means anything or not. I will continue to look to see if I can figure out the problem as want to try to solve it before going to the next chapter of the book.
Was This Post Helpful? 0
  • +
  • -

#8 anonymous26   User is offline

  • D.I.C Lover

Reputation: 2
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Need help with unhandled exception error

Posted 26 May 2011 - 08:42 AM

View PostStrikerx22, on 26 May 2011 - 04:30 AM, said:

Thanks. I'm stepping through it again to see if I can see if I can figure out the problem. As for checking my objects what would be the best way to do this so I can make sure I do it correctly. Again thanks for the help.

Well, you look at the microsoft docs, like this example for the possible return values of Direct3DCreate9(). :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1