void DInput::Init(HWND windowHandle)
{
//first we create the dinpute device using the app instance, the dinput version, our interface, a pointer to our dinput device
DirectInput8Create(GetModuleHandle(NULL), DIRECTINPUT_VERSION,
IID_IDirectInput8, reinterpret_cast<void **> (&dInput), NULL);
h_hwndMain=windowHandle;
loadJoystick();
}
HRESULT
DInput::loadJoystick()
{
this->id = id;
device_counter = 0;
this->joyDeadZone=.1f;
HRESULT hr;
// Look for the joystick
if (FAILED(hr = dInput->EnumDevices(DI8DEVCLASS_GAMECTRL, ::enumCallbackB,
(LPVOID)this, DIEDFL_ATTACHEDONLY))) {
return hr;
}
// Fail if no joystick
if (joystick == NULL) {
return E_FAIL;
}
DIDEVCAPS capabilities;
// Set the data format to "simple joystick" - a predefined data format
//
// A data format specifies which controls on a device we are interested in,
// and how they should be reported. This tells DInput that we will be
// passing a DIJOYSTATE2 structure to IDirectInputDevice::GetDeviceState().
//THIS IS WHERE IT BREAKS IN RELEASE BUILD
if (FAILED(hr = joystick->SetDataFormat(&c_dfDIJoystick2))) {
return hr;
}
// Set the cooperative level to let DInput know how this device should
// interact with the system and with other DInput applications.
if (FAILED(hr = joystick->SetCooperativeLevel(h_hwndMain, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE))) {
return hr;
}
// Determine how many axis the joystick has (so we don't error out setting
// properties for unavailable axis)
capabilities.dwSize = sizeof(DIDEVCAPS);
hr = joystick->GetCapabilities(&capabilities);
// Enumerate the axes of the joyctick and set the range of each axis. Note:
// we could just use the defaults, but we're just trying to show an example
// of enumerating device objects (axes, buttons, etc.).
hr = joystick->EnumObjects(::enumAxesCallbackB, (LPVOID)this, DIDFT_AXIS);
return S_OK;
}
BOOL CALLBACK
DInput::enumCallback(const DIDEVICEINSTANCE* instance, VOID* context)
{
// If this is the requested device ID ...
if (device_counter == this->id) {
// Obtain an interface to the enumerated joystick. Stop the enumeration
// if the requested device was created successfully.
if (SUCCEEDED(dInput->CreateDevice(instance->guidInstance, &joystick, NULL))) {
return DIENUM_STOP;
}
}
// Otherwise, increment the device counter and continue with
// the device enumeration.
device_counter++;
return DIENUM_CONTINUE;
}
BOOL CALLBACK
enumCallbackB(const DIDEVICEINSTANCE* instance, VOID* context)
{
if (context != NULL) {
return ((DInput *)context)->enumCallback(instance, context);
} else {
return DIENUM_STOP;
}
}
BOOL CALLBACK
countCallbackB(const DIDEVICEINSTANCE* instance, VOID* counter)
{
if (counter != NULL) {
unsigned int *tmpCounter = (unsigned int *)counter;
(*tmpCounter)++;
counter = tmpCounter;
}
return DIENUM_CONTINUE;
}
BOOL CALLBACK DInput::enumAxesCallback(const DIDEVICEOBJECTINSTANCE* instance, VOID* context)
{
HWND hDlg = (HWND)context;
DIPROPRANGE propRange;
propRange.diph.dwSize = sizeof(DIPROPRANGE);
propRange.diph.dwHeaderSize = sizeof(DIPROPHEADER);
propRange.diph.dwHow = DIPH_BYID;
propRange.diph.dwObj = instance->dwType;
propRange.lMin = AXIS_MIN;
propRange.lMax = AXIS_MAX;
// Set the range for the axis
if (FAILED(joystick->SetProperty(DIPROP_RANGE, &propRange.diph))) {
return DIENUM_STOP;
}
return DIENUM_CONTINUE;
}
BOOL CALLBACK
enumAxesCallbackB(const DIDEVICEOBJECTINSTANCE* instance, VOID* context)
{
if (context != NULL) {
return ((DInput *)context)->enumAxesCallback(instance, context);
} else {
return DIENUM_STOP;
}
}
Problems with DirectInput Joystick in Release Build.
I am having a problem with running my program in Release Build, though everything works fine in Debug. The problem occurs whenever I try to access to Joystick I seemingly just created. The joystick is a 360 USB controller, but it fails on other controllers too such as PS2 and Xbox. It also fails when there is no controller plugged in. Based on previous problems I have had it seems like this is a non-controller dependent problem and just a joystick init problem. It first breaks at the SetDataFormat call. If you need any more info then please let me know.
EDIT: I forgot to mention the error specifically is a System.NullReferenceException and the only other time I had this error was when I was trying to Poll the joystick when there wasn't anything plugged in.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement