Jump to content

  • Log In with Google      Sign In   
  • Create Account

#ActualSteveHatcher

Posted 28 November 2013 - 09:21 AM

Hi Again,
 
I come with more perils. I am now trying to add keyboard support, and like before, the program is crashing when it comes to the device I thought I have created. I am keeping my eye out for uninitiated global variables as you taught me before but cannot find the problem. The added code is:
 

//**********************
//DirectInput variables*
//**********************
#define KEYDOWN( name, key ) ( name[key] & 0x80 )
LPDIRECTINPUT8 directInput_ = NULL;
LPDIRECTINPUTDEVICE8 keyboardDevice_ = NULL;
char keyboardKeys_[256];
char prevKeyboardKeys_[256];

For the Initialization routine (which is called before Update() and Render():

bool InitInput(void)
{
    HRESULT result;

    ZeroMemory(keyboardKeys_, sizeof(keyboardKeys_));
    ZeroMemory(prevKeyboardKeys_, sizeof(prevKeyboardKeys_));

    result = DirectInput8Create(hInstance, DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&directInput_, 0); //Initialize DirectInput8

    if (FAILED(result))
    {
        return false;
    }

    result = directInput_->CreateDevice(GUID_SysKeyboard, &keyboardDevice_, 0);

    if (FAILED(result))
    {
        return false;
    }

    result = keyboardDevice_->SetDataFormat(&c_dfDIKeyboard);

    if (FAILED(result))
    {
        return false;
    }

    result = keyboardDevice_->SetCooperativeLevel(ghHwnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE);

    if (FAILED(result))
    {
        return false;
    }

    result = keyboardDevice_->Acquire();

    if (FAILED(result))
    {
        return false;
    }

    return true;
}

And for the Update() routine:

//******************************
//Get current state of Keyboard*
//******************************
void Update(void)
{
	float fElapsed;
	float rotZ = 0;
	float fY = 0.0f;

	keyboardDevice_->GetDeviceState(sizeof(keyboardKeys_), (LPVOID)&keyboardKeys_);

	if (GetAsyncKeyState(VK_ESCAPE))
	{
		PostQuitMessage(0);
	}

	// Button up event.
	if (KEYDOWN(prevKeyboardKeys_, DIK_DOWN) && !KEYDOWN(keyboardKeys_, DIK_DOWN))
	{
		fY -= 0.1f;
	}


	if (KEYDOWN(prevKeyboardKeys_, DIK_UP) && !KEYDOWN(keyboardKeys_, DIK_UP))
	{
		fY += 0.1f;
	}

	memcpy(prevKeyboardKeys_, keyboardKeys_, sizeof(keyboardKeys_));

	gfTimeScale = 0.001f;
	fElapsed = GetElapsedTime();
	rotZ += fElapsed;
}

 
It is crashing at keyboardDevice_->GetDeviceState(sizeof(keyboardKeys_), (LPVOID)&keyboardKeys_);

 

The debugger shows keyboardDevice_ becomes a 0x00000000 (similar to before). Again this is adapting code that works when it was done in the way the book shows.

 

Again any help is greatly appreciated.

 

Thanks


#3SteveHatcher

Posted 28 November 2013 - 09:18 AM

Hi Again,
 
I come with more perils. I am now trying to add keyboard support, and like before, the program is crashing when it comes to the device I thought I have created. I am keeping my eye out for uninitiated global variables as you taught me before but cannot find the problem. The added code is:
 

//**********************
//DirectInput variables*
//**********************
#define KEYDOWN( name, key ) ( name[key] & 0x80 )
LPDIRECTINPUT8 directInput_ = NULL;
LPDIRECTINPUTDEVICE8 keyboardDevice_ = NULL;
char keyboardKeys_[256];
char prevKeyboardKeys_[256];

For the Initialization routine (which is called before Update() and Render():

bool InitInput(void)
{
    HRESULT result;

    ZeroMemory(keyboardKeys_, sizeof(keyboardKeys_));
    ZeroMemory(prevKeyboardKeys_, sizeof(prevKeyboardKeys_));

    result = DirectInput8Create(hInstance, DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&directInput_, 0); //Initialize DirectInput8

    if (FAILED(result))
    {
        return false;
    }

    result = directInput_->CreateDevice(GUID_SysKeyboard, &keyboardDevice_, 0);

    if (FAILED(result))
    {
        return false;
    }

    result = keyboardDevice_->SetDataFormat(&c_dfDIKeyboard);

    if (FAILED(result))
    {
        return false;
    }

    result = keyboardDevice_->SetCooperativeLevel(ghHwnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE);

    if (FAILED(result))
    {
        return false;
    }

    result = keyboardDevice_->Acquire();

    if (FAILED(result))
    {
        return false;
    }

    return true;
}

And for the Update() routine:

//******************************
//Get current state of Keyboard*
//******************************
void Update(void)
{
	float fElapsed;
	float rotZ = 0;
	float fY = 0.0f;

	keyboardDevice_->GetDeviceState(sizeof(keyboardKeys_), (LPVOID)&keyboardKeys_);

	if (GetAsyncKeyState(VK_ESCAPE))
	{
		PostQuitMessage(0);
	}

	// Button up event.
	if (KEYDOWN(prevKeyboardKeys_, DIK_DOWN) && !KEYDOWN(keyboardKeys_, DIK_DOWN))
	{
		fY -= 0.1f;
	}


	if (KEYDOWN(prevKeyboardKeys_, DIK_UP) && !KEYDOWN(keyboardKeys_, DIK_UP))
	{
		fY += 0.1f;
	}

	memcpy(prevKeyboardKeys_, keyboardKeys_, sizeof(keyboardKeys_));

	gfTimeScale = 0.001f;
	fElapsed = GetElapsedTime();
	rotZ += fElapsed;
}

 
It is crashing at keyboardDevice_->GetDeviceState(sizeof(keyboardKeys_), (LPVOID)&keyboardKeys_);

 

The debugger shows keyboardDevice_ becomes a 0x00000000 (similar to before).

 

Again any help is greatly appreciated.

 

Thanks


#2SteveHatcher

Posted 28 November 2013 - 09:18 AM

Hi Again,
 
I come with more perils. I am now trying to add keyboard support, and like before, the program is crashing when it comes to the device I thought I have created. I am keeping my eye out for uninitiated global variables as you taught me before but cannot find the problem. The added code is:
 

//**********************
//DirectInput variables*
//**********************
#define KEYDOWN( name, key ) ( name[key] & 0x80 )
LPDIRECTINPUT8 directInput_ = NULL;
LPDIRECTINPUTDEVICE8 keyboardDevice_ = NULL;
char keyboardKeys_[256];
char prevKeyboardKeys_[256];

For the Initialization routine (which is called before Update() and Render():

//**************************************
//Creates a hardware device in Direct3D*
//**************************************
bool InitD3D(void)
{
	D3D_DRIVER_TYPE driverType_;
	D3D_FEATURE_LEVEL featureLevel_;
	RECT dimensions;
	GetClientRect(ghHwnd, &dimensions);

	unsigned int width = dimensions.right - dimensions.left;
	unsigned int height = dimensions.bottom - dimensions.top;

	D3D_DRIVER_TYPE driverTypes[] =
	{
		D3D_DRIVER_TYPE_HARDWARE, D3D_DRIVER_TYPE_WARP,
		D3D_DRIVER_TYPE_REFERENCE, D3D_DRIVER_TYPE_SOFTWARE
	};

	unsigned int totalDriverTypes = ARRAYSIZE(driverTypes);

	D3D_FEATURE_LEVEL featureLevels[] =
	{
		D3D_FEATURE_LEVEL_11_0,
		D3D_FEATURE_LEVEL_10_1,
		D3D_FEATURE_LEVEL_10_0
	};

	unsigned int totalFeatureLevels = ARRAYSIZE(featureLevels);

	DXGI_SWAP_CHAIN_DESC swapChainDesc;
	ZeroMemory(&swapChainDesc, sizeof(swapChainDesc));
	swapChainDesc.BufferCount = 1;
	swapChainDesc.BufferDesc.Width = width;
	swapChainDesc.BufferDesc.Height = height;
	swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
	swapChainDesc.BufferDesc.RefreshRate.Numerator = 60;
	swapChainDesc.BufferDesc.RefreshRate.Denominator = 1;
	swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
	swapChainDesc.OutputWindow = ghHwnd;
	swapChainDesc.Windowed = true;
	swapChainDesc.SampleDesc.Count = 1;
	swapChainDesc.SampleDesc.Quality = 0;

	if (gbFullScreen)
	{
		swapChainDesc.Windowed = false;
	}

	unsigned int creationFlags = 0;

#ifdef _DEBUG
	creationFlags |= D3D11_CREATE_DEVICE_DEBUG;
#endif

	HRESULT result;
	unsigned int driver = 0;

	for (driver = 0; driver < totalDriverTypes; ++driver)
	{
		result = D3D11CreateDeviceAndSwapChain(0, driverTypes[driver], 0, creationFlags, featureLevels, totalFeatureLevels, D3D11_SDK_VERSION, &swapChainDesc, &swapChain_,
			&d3dDevice_, &featureLevel_, &d3dContext_);

		if (SUCCEEDED(result))
		{
			driverType_ = driverTypes[driver];
			break;
		}
	}

	if (FAILED(result))
	{
		DXTRACE_MSG("Failed to create the Direct3D device!");
		return false;
	}

	return true;
}

And for the Update() routine:

//******************************
//Get current state of Keyboard*
//******************************
void Update(void)
{
	float fElapsed;
	float rotZ = 0;
	float fY = 0.0f;

	keyboardDevice_->GetDeviceState(sizeof(keyboardKeys_), (LPVOID)&keyboardKeys_);

	if (GetAsyncKeyState(VK_ESCAPE))
	{
		PostQuitMessage(0);
	}

	// Button up event.
	if (KEYDOWN(prevKeyboardKeys_, DIK_DOWN) && !KEYDOWN(keyboardKeys_, DIK_DOWN))
	{
		fY -= 0.1f;
	}


	if (KEYDOWN(prevKeyboardKeys_, DIK_UP) && !KEYDOWN(keyboardKeys_, DIK_UP))
	{
		fY += 0.1f;
	}

	memcpy(prevKeyboardKeys_, keyboardKeys_, sizeof(keyboardKeys_));

	gfTimeScale = 0.001f;
	fElapsed = GetElapsedTime();
	rotZ += fElapsed;
}

 
It is crashing at keyboardDevice_->GetDeviceState(sizeof(keyboardKeys_), (LPVOID)&keyboardKeys_);

 

The debugger shows keyboardDevice_ becomes a 0x00000000 (similar to before).

 

Again any help is greatly appreciated.

 

Thanks


#1SteveHatcher

Posted 28 November 2013 - 09:17 AM

Hi Again,
 
I come with more perils. I am now trying to add keyboard support, and like before, the program is crashing when it comes to the device I thought I have created. I am keeping my eye out for uninitiated global variables as you taught me before but cannot find the problem. The added code is:
 

//**********************
//DirectInput variables*
//**********************
#define KEYDOWN( name, key ) ( name[key] & 0x80 )
LPDIRECTINPUT8 directInput_ = NULL;
LPDIRECTINPUTDEVICE8 keyboardDevice_ = NULL;
char keyboardKeys_[256];
char prevKeyboardKeys_[256];

For the Initialization routine (which is called before Update() and Render()"

//**************************************
//Creates a hardware device in Direct3D*
//**************************************
bool InitD3D(void)
{
	D3D_DRIVER_TYPE driverType_;
	D3D_FEATURE_LEVEL featureLevel_;
	RECT dimensions;
	GetClientRect(ghHwnd, &dimensions);

	unsigned int width = dimensions.right - dimensions.left;
	unsigned int height = dimensions.bottom - dimensions.top;

	D3D_DRIVER_TYPE driverTypes[] =
	{
		D3D_DRIVER_TYPE_HARDWARE, D3D_DRIVER_TYPE_WARP,
		D3D_DRIVER_TYPE_REFERENCE, D3D_DRIVER_TYPE_SOFTWARE
	};

	unsigned int totalDriverTypes = ARRAYSIZE(driverTypes);

	D3D_FEATURE_LEVEL featureLevels[] =
	{
		D3D_FEATURE_LEVEL_11_0,
		D3D_FEATURE_LEVEL_10_1,
		D3D_FEATURE_LEVEL_10_0
	};

	unsigned int totalFeatureLevels = ARRAYSIZE(featureLevels);

	DXGI_SWAP_CHAIN_DESC swapChainDesc;
	ZeroMemory(&swapChainDesc, sizeof(swapChainDesc));
	swapChainDesc.BufferCount = 1;
	swapChainDesc.BufferDesc.Width = width;
	swapChainDesc.BufferDesc.Height = height;
	swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
	swapChainDesc.BufferDesc.RefreshRate.Numerator = 60;
	swapChainDesc.BufferDesc.RefreshRate.Denominator = 1;
	swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
	swapChainDesc.OutputWindow = ghHwnd;
	swapChainDesc.Windowed = true;
	swapChainDesc.SampleDesc.Count = 1;
	swapChainDesc.SampleDesc.Quality = 0;

	if (gbFullScreen)
	{
		swapChainDesc.Windowed = false;
	}

	unsigned int creationFlags = 0;

#ifdef _DEBUG
	creationFlags |= D3D11_CREATE_DEVICE_DEBUG;
#endif

	HRESULT result;
	unsigned int driver = 0;

	for (driver = 0; driver < totalDriverTypes; ++driver)
	{
		result = D3D11CreateDeviceAndSwapChain(0, driverTypes[driver], 0, creationFlags, featureLevels, totalFeatureLevels, D3D11_SDK_VERSION, &swapChainDesc, &swapChain_,
			&d3dDevice_, &featureLevel_, &d3dContext_);

		if (SUCCEEDED(result))
		{
			driverType_ = driverTypes[driver];
			break;
		}
	}

	if (FAILED(result))
	{
		DXTRACE_MSG("Failed to create the Direct3D device!");
		return false;
	}

	return true;
}

And for the Update() routine:

//******************************
//Get current state of Keyboard*
//******************************
void Update(void)
{
	float fElapsed;
	float rotZ = 0;
	float fY = 0.0f;

	keyboardDevice_->GetDeviceState(sizeof(keyboardKeys_), (LPVOID)&keyboardKeys_);

	if (GetAsyncKeyState(VK_ESCAPE))
	{
		PostQuitMessage(0);
	}

	// Button up event.
	if (KEYDOWN(prevKeyboardKeys_, DIK_DOWN) && !KEYDOWN(keyboardKeys_, DIK_DOWN))
	{
		fY -= 0.1f;
	}


	if (KEYDOWN(prevKeyboardKeys_, DIK_UP) && !KEYDOWN(keyboardKeys_, DIK_UP))
	{
		fY += 0.1f;
	}

	memcpy(prevKeyboardKeys_, keyboardKeys_, sizeof(keyboardKeys_));

	gfTimeScale = 0.001f;
	fElapsed = GetElapsedTime();
	rotZ += fElapsed;
}

 
It is crashing at keyboardDevice_->GetDeviceState(sizeof(keyboardKeys_), (LPVOID)&keyboardKeys_);

 

The debugger shows keyboardDevice_ becomes a 0x00000000 (similar to before).

 

Again any help is greatly appreciated.

 

Thanks


PARTNERS