Dinput problem
(Using ''Tricks of the windows game programming gurus'' by LaMothe.)
If I try to initiate either the mouse or the keyboard (using t3dlib2) the game will just quit on me. I hate this, because I can''t debug it.
Anyone know of any known problems using this peace of code from this book?
*arrggh*
//John
-----------------------------------------
Swedish saying:
"The one who joins the game must face the consequences of the game."
YES!!! I know the problem....
That book uses direct input code for Dx5.0 i think.
BUT If you have the 8.0 api installed the code won''t compile.
You need to update the code for directx8.0
Here is some code that I updated for Windows Programming for dumbies" which uses a lot of the same code from "Gurus"
I think this is all hyou will need to change... My function names may be slightly different. ALso you may need to change a little more of the other functions but I can''t remember if you do. This should get you started
//globals
LPDIRECTINPUT8 lpdi; // dinput object
LPDIRECTINPUTDEVICE8 lpdikey; // dinput keyboard
LPDIRECTINPUTDEVICE8 lpdimouse; // dinput mouse
LPDIRECTINPUTDEVICE8 lpdijoy2; // dinput joystick
//functions
int DInput_Init(void)
{
// this function initializes directinput
if (DirectInput8Create(main_instance,DIRECTINPUT_VERSION,IID_IDirectInput8,(void**)&lpdi,NULL)!=DI_OK)
return(0);
// return success
return(1);
} // end DInput_Init
int DI_Init_Joystick(int min_x, int max_x, int min_y, int max_y)
{
// this function initializes the joystick, it allows you to set
// the minimum and maximum x-y ranges
// first find the fucking GUID of your particular joystick
lpdi->EnumDevices(DI8DEVCLASS_GAMECTRL,
DI_Enum_Joysticks,
&joystickGUID,
DIEDFL_ATTACHEDONLY);
if (lpdi->CreateDevice(joystickGUID, &lpdijoy2, NULL)!=DI_OK)
return(0);
// set cooperation level
if (lpdijoy2->SetCooperativeLevel(main_window_handle,
DISCL_NONEXCLUSIVE | DISCL_BACKGROUND)!=DI_OK)
return(0);
// set data format
if (lpdijoy2->SetDataFormat(&c_dfDIJoystick2)!=DI_OK)
return(0);
// set the range of the joystick
DIPROPRANGE joy_axis_range;
// first x axis
joy_axis_range.lMin = min_x;
joy_axis_range.lMax = max_x;
joy_axis_range.diph.dwSize = sizeof(DIPROPRANGE);
joy_axis_range.diph.dwHeaderSize = sizeof(DIPROPHEADER);
joy_axis_range.diph.dwObj = DIJOFS_X;
joy_axis_range.diph.dwHow = DIPH_BYOFFSET;
lpdijoy2->SetProperty(DIPROP_RANGE,&joy_axis_range.diph);
// now y-axis
joy_axis_range.lMin = min_y;
joy_axis_range.lMax = max_y;
joy_axis_range.diph.dwSize = sizeof(DIPROPRANGE);
joy_axis_range.diph.dwHeaderSize = sizeof(DIPROPHEADER);
joy_axis_range.diph.dwObj = DIJOFS_Y;
joy_axis_range.diph.dwHow = DIPH_BYOFFSET;
lpdijoy2->SetProperty(DIPROP_RANGE,&joy_axis_range.diph);
// acquire the joystick
if (lpdijoy2->Acquire()!=DI_OK)
return(0);
// set found flag
joystick_found = 1;
// return success
return(1);
} // end DI_Init_Joystick
BOOL CALLBACK DI_Enum_Joysticks(LPCDIDEVICEINSTANCE lpddi,
LPVOID guid_ptr)
{
// this function enumerates the joysticks, but
// stops at the first one and returns the
// instance guid of it, so we can create it
*(GUID*)guid_ptr = lpddi->guidInstance;
// copy name into global
strcpy(joyname, (char *)lpddi->tszProductName);
// stop enumeration after one iteration
return(DIENUM_STOP);
} // end DI_Enum_Joysticks
int DI_Read_Joystick(void)
{
// this function reads the joystick state
// make sure the joystick was initialized
if (!joystick_found)
return(0);
if (lpdijoy2)
{
// this is needed for joysticks only
if (FAILED(lpdijoy2->Poll()))
{
return(0);
}
if (FAILED(lpdijoy2->GetDeviceState(sizeof(DIJOYSTATE2), (LPVOID)&joy_state)))
return(0);
}
else
{
// joystick isn''t plugged in, zero out state
memset(&joy_state,0,sizeof(joy_state));
// return error
return(0);
} // end else
// return sucess
return(1);
} // end DI_Read_Joystick
big kid with adult powers
That book uses direct input code for Dx5.0 i think.
BUT If you have the 8.0 api installed the code won''t compile.
You need to update the code for directx8.0
Here is some code that I updated for Windows Programming for dumbies" which uses a lot of the same code from "Gurus"
I think this is all hyou will need to change... My function names may be slightly different. ALso you may need to change a little more of the other functions but I can''t remember if you do. This should get you started
//globals
LPDIRECTINPUT8 lpdi; // dinput object
LPDIRECTINPUTDEVICE8 lpdikey; // dinput keyboard
LPDIRECTINPUTDEVICE8 lpdimouse; // dinput mouse
LPDIRECTINPUTDEVICE8 lpdijoy2; // dinput joystick
//functions
int DInput_Init(void)
{
// this function initializes directinput
if (DirectInput8Create(main_instance,DIRECTINPUT_VERSION,IID_IDirectInput8,(void**)&lpdi,NULL)!=DI_OK)
return(0);
// return success
return(1);
} // end DInput_Init
int DI_Init_Joystick(int min_x, int max_x, int min_y, int max_y)
{
// this function initializes the joystick, it allows you to set
// the minimum and maximum x-y ranges
// first find the fucking GUID of your particular joystick
lpdi->EnumDevices(DI8DEVCLASS_GAMECTRL,
DI_Enum_Joysticks,
&joystickGUID,
DIEDFL_ATTACHEDONLY);
if (lpdi->CreateDevice(joystickGUID, &lpdijoy2, NULL)!=DI_OK)
return(0);
// set cooperation level
if (lpdijoy2->SetCooperativeLevel(main_window_handle,
DISCL_NONEXCLUSIVE | DISCL_BACKGROUND)!=DI_OK)
return(0);
// set data format
if (lpdijoy2->SetDataFormat(&c_dfDIJoystick2)!=DI_OK)
return(0);
// set the range of the joystick
DIPROPRANGE joy_axis_range;
// first x axis
joy_axis_range.lMin = min_x;
joy_axis_range.lMax = max_x;
joy_axis_range.diph.dwSize = sizeof(DIPROPRANGE);
joy_axis_range.diph.dwHeaderSize = sizeof(DIPROPHEADER);
joy_axis_range.diph.dwObj = DIJOFS_X;
joy_axis_range.diph.dwHow = DIPH_BYOFFSET;
lpdijoy2->SetProperty(DIPROP_RANGE,&joy_axis_range.diph);
// now y-axis
joy_axis_range.lMin = min_y;
joy_axis_range.lMax = max_y;
joy_axis_range.diph.dwSize = sizeof(DIPROPRANGE);
joy_axis_range.diph.dwHeaderSize = sizeof(DIPROPHEADER);
joy_axis_range.diph.dwObj = DIJOFS_Y;
joy_axis_range.diph.dwHow = DIPH_BYOFFSET;
lpdijoy2->SetProperty(DIPROP_RANGE,&joy_axis_range.diph);
// acquire the joystick
if (lpdijoy2->Acquire()!=DI_OK)
return(0);
// set found flag
joystick_found = 1;
// return success
return(1);
} // end DI_Init_Joystick
BOOL CALLBACK DI_Enum_Joysticks(LPCDIDEVICEINSTANCE lpddi,
LPVOID guid_ptr)
{
// this function enumerates the joysticks, but
// stops at the first one and returns the
// instance guid of it, so we can create it
*(GUID*)guid_ptr = lpddi->guidInstance;
// copy name into global
strcpy(joyname, (char *)lpddi->tszProductName);
// stop enumeration after one iteration
return(DIENUM_STOP);
} // end DI_Enum_Joysticks
int DI_Read_Joystick(void)
{
// this function reads the joystick state
// make sure the joystick was initialized
if (!joystick_found)
return(0);
if (lpdijoy2)
{
// this is needed for joysticks only
if (FAILED(lpdijoy2->Poll()))
{
return(0);
}
if (FAILED(lpdijoy2->GetDeviceState(sizeof(DIJOYSTATE2), (LPVOID)&joy_state)))
return(0);
}
else
{
// joystick isn''t plugged in, zero out state
memset(&joy_state,0,sizeof(joy_state));
// return error
return(0);
} // end else
// return sucess
return(1);
} // end DI_Read_Joystick
big kid with adult powers
lets try this again:
//globalsLPDIRECTINPUT8 lpdi; // dinput objectLPDIRECTINPUTDEVICE8 lpdikey; // dinput keyboardLPDIRECTINPUTDEVICE8 lpdimouse; // dinput mouseLPDIRECTINPUTDEVICE8 lpdijoy2; // dinput joystick//functionsint DInput_Init(void){// this function initializes directinputif (DirectInput8Create(main_instance,DIRECTINPUT_VERSION,IID_IDirectInput8,(void**)&lpdi,NULL)!=DI_OK)return(0);// return successreturn(1);} // end DInput_Initint DI_Init_Joystick(int min_x, int max_x, int min_y, int max_y){// this function initializes the joystick, it allows you to set// the minimum and maximum x-y ranges // first find the fucking GUID of your particular joysticklpdi->EnumDevices(DI8DEVCLASS_GAMECTRL, DI_Enum_Joysticks, &joystickGUID, DIEDFL_ATTACHEDONLY); if (lpdi->CreateDevice(joystickGUID, &lpdijoy2, NULL)!=DI_OK)return(0);// set cooperation levelif (lpdijoy2->SetCooperativeLevel(main_window_handle, DISCL_NONEXCLUSIVE | DISCL_BACKGROUND)!=DI_OK)return(0);// set data formatif (lpdijoy2->SetDataFormat(&c_dfDIJoystick2)!=DI_OK)return(0);// set the range of the joystickDIPROPRANGE joy_axis_range;// first x axisjoy_axis_range.lMin = min_x;joy_axis_range.lMax = max_x;joy_axis_range.diph.dwSize = sizeof(DIPROPRANGE); joy_axis_range.diph.dwHeaderSize = sizeof(DIPROPHEADER); joy_axis_range.diph.dwObj = DIJOFS_X;joy_axis_range.diph.dwHow = DIPH_BYOFFSET;lpdijoy2->SetProperty(DIPROP_RANGE,&joy_axis_range.diph);// now y-axisjoy_axis_range.lMin = min_y;joy_axis_range.lMax = max_y;joy_axis_range.diph.dwSize = sizeof(DIPROPRANGE); joy_axis_range.diph.dwHeaderSize = sizeof(DIPROPHEADER); joy_axis_range.diph.dwObj = DIJOFS_Y;joy_axis_range.diph.dwHow = DIPH_BYOFFSET;lpdijoy2->SetProperty(DIPROP_RANGE,&joy_axis_range.diph);// acquire the joystickif (lpdijoy2->Acquire()!=DI_OK)return(0);// set found flagjoystick_found = 1;// return successreturn(1);} // end DI_Init_JoystickBOOL CALLBACK DI_Enum_Joysticks(LPCDIDEVICEINSTANCE lpddi,LPVOID guid_ptr) {// this function enumerates the joysticks, but// stops at the first one and returns the// instance guid of it, so we can create it*(GUID*)guid_ptr = lpddi->guidInstance; // copy name into globalstrcpy(joyname, (char *)lpddi->tszProductName);// stop enumeration after one iterationreturn(DIENUM_STOP);} // end DI_Enum_Joysticksint DI_Read_Joystick(void){// this function reads the joystick state// make sure the joystick was initializedif (!joystick_found)return(0);if (lpdijoy2){// this is needed for joysticks only if (FAILED(lpdijoy2->Poll())){return(0);}if (FAILED(lpdijoy2->GetDeviceState(sizeof(DIJOYSTATE2), (LPVOID)&joy_state)))return(0);}else{// joystick isn''t plugged in, zero out statememset(&joy_state,0,sizeof(joy_state));// return errorreturn(0);} // end else// return sucessreturn(1);} // end DI_Read_Joystick
I Dig you!
You don''t have code for the other devices?
Hm. I get this error when I try to build. I think it''s because I''m missing some file, but I''m not sure.
"t3dlib2.obj : error LNK2001: unresolved external symbol _DirectInput8Create@20"
please help?
//John
-----------------------------------------
Swedish saying:
"The one who joins the game must face the consequences of the game."
You don''t have code for the other devices?
Hm. I get this error when I try to build. I think it''s because I''m missing some file, but I''m not sure.
"t3dlib2.obj : error LNK2001: unresolved external symbol _DirectInput8Create@20"
please help?
//John
-----------------------------------------
Swedish saying:
"The one who joins the game must face the consequences of the game."
Yes, you are right. I made a search on gamedev and found out. But thanks anyway.
I love you gamedev''ers
//John
-----------------------------------------
Swedish saying:
"The one who joins the game must face the consequences of the game."
I love you gamedev''ers
//John
-----------------------------------------
Swedish saying:
"The one who joins the game must face the consequences of the game."
Yup...
Sorry about that parklife... I forgot about dinput8.dll
D''OH!!!!!!!!!!
Anyway... The code for the other devices (mouse and keyboard)should work just fine. I think they changed DI for DX8 to support the XBOX controllers and other more complex joysticks that may come out in the future (just a guess though)
big kid with adult powers
Sorry about that parklife... I forgot about dinput8.dll
D''OH!!!!!!!!!!
Anyway... The code for the other devices (mouse and keyboard)should work just fine. I think they changed DI for DX8 to support the XBOX controllers and other more complex joysticks that may come out in the future (just a guess though)
big kid with adult powers
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement