bool CInputSDL::Create (void) { if (!m_Ready) { // Reset the keyboard state memset (m_KeyState, 0, MAX_KEYS); //SDL_EnableKeyRepeat(100, SDL_DEFAULT_REPEAT_INTERVAL); // Prepare the friendly name for each key MakeKeyFriendlyNames (); // Create all joysticks that are installed on the system for(int i=0; i < SDL_NumJoysticks(); i++ ) { SJoystick *pJoystick = new SJoystick; if (pJoystick == NULL) { // Log failure theLog.WriteLine ("SDLInput => !!! Could not allocate memory for a joystick."); // Get out return false; } // Reset the joystick state memset (&pJoystick->State, 0, sizeof(pJoystick->State)); // The joystick is not opened yet pJoystick->Opened = false; // Set the joystick's device to NULL (will be created later) pJoystick->pDevice = NULL; m_pJoysticks.push_back (pJoystick); // the joystick is not opened theLog.WriteLine ("SDLInput => A joystick was added."); } m_Ready = true; } SDL_JoystickEventState(SDL_ENABLE); // Everything went right return true; }
bool CDirectInput::Create (void) { if (!m_Ready) { // Create the directinput object HRESULT hRet = DirectInputCreateEx (m_hInstance, DIRECTINPUT_VERSION, IID_IDirectInput7, (void**)&m_pDI, NULL); // If it failed if (hRet != DI_OK) { // Log failure theLog.WriteLine ("DirectInput => !!! Could not create DirectInput object."); theLog.WriteLine ("DirectInput => !!! DirectInput error is : %s.", GetDirectInputError(hRet)); // Get out return false; } // If it was successful else { // Log success theLog.WriteLine ("DirectInput => DirectInput object was created successfully."); } hRet = m_pDI->CreateDeviceEx (GUID_SysKeyboard, IID_IDirectInputDevice7, (void**)&m_pKeyboard, NULL); if (hRet != DI_OK) { // Log failure theLog.WriteLine ("DirectInput => !!! Could not create keyboard."); theLog.WriteLine ("DirectInput => !!! DirectInput error is : %s.", GetDirectInputError(hRet)); // Get out return false; } else { // Log success theLog.WriteLine ("DirectInput => Keyboard was created."); } // Set the keyboard data format hRet = m_pKeyboard->SetDataFormat (&c_dfDIKeyboard); // If it failed if (hRet != DI_OK) { // Log failure theLog.WriteLine ("DirectInput => !!! Could not set data format (keyboard)."); theLog.WriteLine ("DirectInput => !!! DirectInput error is : %s.", GetDirectInputError(hRet)); // Get out return false; } // Set the keyboard's cooperative level hRet = m_pKeyboard->SetCooperativeLevel (m_hWnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE); // If it failed if (hRet != DI_OK) { // Log failure theLog.WriteLine ("DirectInput => !!! Could not set cooperative level (keyboard)."); theLog.WriteLine ("DirectInput => !!! DirectInput error is : %s.", GetDirectInputError(hRet)); // Get out return false; } // Reset the keyboard state ZeroMemory (m_KeyState, MAX_KEYS); // The keyboard is not opened yet m_KeyboardOpened = false; // We need this variable to get information about each key of the keyboard DIDEVICEOBJECTINSTANCE DeviceObjectInstance; // Scan the keys the keyboard could have for (int Key = 0 ; Key < MAX_KEYS ; Key++) { // Initialize the device object instance and get the key's information ZeroMemory (&DeviceObjectInstance, sizeof (DIDEVICEOBJECTINSTANCE)); DeviceObjectInstance.dwSize = sizeof (DIDEVICEOBJECTINSTANCE); HRESULT hRet = m_pKeyboard->GetObjectInfo (&DeviceObjectInstance, Key, DIPH_BYOFFSET); ASSERT (hRet == DI_OK || hRet == DIERR_OBJECTNOTFOUND); // Store the key's real name, that is to say the name given by Windows strcpy (m_KeyRealName[Key], (hRet != DIERR_OBJECTNOTFOUND ? DeviceObjectInstance.tszName : "Key does not exist")); } // Prepare the friendly name for each key MakeKeyFriendlyNames (); // Prepare the structure to pass when enumerating the devices SEnumParam EnumParam; EnumParam.pDI = m_pDI; // Create all joysticks that are installed in Windows hRet = m_pDI->EnumDevices (DIDEVTYPE_JOYSTICK, CreateInputDevice, &EnumParam, DIEDFL_ALLDEVICES); // Assert it's not an unexpected return value ASSERT (hRet == DI_OK); // The EnumParam contains the input devices, browse them for (unsigned int Index = 0 ; Index < EnumParam.pDevices.size() ; Index++) { LPDIRECTINPUTDEVICE7 pDevice = EnumParam.pDevices[Index]; // Set the joystick data format hRet = pDevice->SetDataFormat (&c_dfDIJoystick); // If it failed if (hRet != DI_OK) { // Log failure theLog.WriteLine ("DirectInput => !!! Could not set data format (joystick)."); theLog.WriteLine ("DirectInput => !!! DirectInput error is : %s.", GetDirectInputError(hRet)); // Get out return false; } // Set the joystick cooperative level hRet = pDevice->SetCooperativeLevel (m_hWnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE); // If it failed if (hRet != DI_OK) { // Log failure theLog.WriteLine ("DirectInput => !!! Could not set cooperative level (joystick)."); theLog.WriteLine ("DirectInput => !!! DirectInput error is : %s.", GetDirectInputError(hRet)); // Get out return false; } // We need to prepare this in order to set the joystick dead zone DIPROPDWORD PropertyDword; PropertyDword.diph.dwSize = sizeof(PropertyDword); PropertyDword.diph.dwHeaderSize = sizeof(PropertyDword.diph); PropertyDword.diph.dwObj = 0; // Apply to the PropertyDword.diph.dwHow = DIPH_DEVICE; // entire device PropertyDword.dwData = 10000 * JOYSTICK_DEAD_ZONE / 100; // Set the property hRet = pDevice->SetProperty (DIPROP_DEADZONE, &PropertyDword.diph); // Assert it's not an unexpected error ASSERT (hRet == DI_OK || hRet == DI_PROPNOEFFECT); // We need to prepare this in order to set the joystick range DIPROPRANGE PropertyRange; PropertyRange.diph.dwSize = sizeof(PropertyRange); PropertyRange.diph.dwHeaderSize = sizeof(PropertyRange.diph); PropertyRange.diph.dwObj = 0; // Apply to the PropertyRange.diph.dwHow = DIPH_DEVICE; // entire device PropertyRange.lMin = JOYSTICK_MINIMUM_AXIS; PropertyRange.lMax = JOYSTICK_MAXIMUM_AXIS; // Set the property hRet = pDevice->SetProperty (DIPROP_RANGE, &PropertyRange.diph); // Assert it's not an unexpected error ASSERT (hRet == DI_OK || hRet == DI_PROPNOEFFECT); // Allocate memory for a joystick SJoystick *pJoystick = new SJoystick; // If allocation failed if (pJoystick == NULL) { // Log failure theLog.WriteLine ("DirectInput => !!! Could not allocate memory for a joystick."); // Get out return false; } // Reset the joystick state ZeroMemory (&pJoystick->State, sizeof(pJoystick->State)); // The joystick is not opened yet pJoystick->Opened = false; // Set the joystick's directinput device pJoystick->pDevice = pDevice; // Add the joystick to the container m_pJoysticks.push_back (pJoystick); // Log we added one joystick theLog.WriteLine ("DirectInput => One joystick was created."); } m_Ready = true; } // Everything went right return true; }