📄 input_device_directinput.cpp
字号:
/* $Id: input_device_directinput.cpp,v 1.9 2003/12/10 02:26:52 mbn Exp $
**
** ClanLib Game SDK
** Copyright (C) 2003 The ClanLib Team
** For a total list of contributers see the file CREDITS.
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License as published by the Free Software Foundation; either
** version 2.1 of the License, or (at your option) any later version.
**
** This library is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public
** License along with this library; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "Display/display_precomp.h"
#include "API/Display/input_event.h"
#include "input_device_directinput.h"
#include "API/Core/System/clanstring.h"
#include "API/Core/System/error.h"
#include "API/Core/System/log.h"
/////////////////////////////////////////////////////////////////////////////
// CL_InputDevice_DirectInput construction:
CL_InputDevice_DirectInput::CL_InputDevice_DirectInput(
CL_DisplayWindow_Win32 *window,
LPCDIDEVICEINSTANCE ptr_device_instance)
: window(window), device_instance(*ptr_device_instance), directinput_device(0)
{
HRESULT result = window->get_directinput()->CreateDevice(
device_instance.guidInstance,
&directinput_device,
0);
if (FAILED(result))
{
throw CL_Error(
CL_String::format(
"Unable to open device %1 (%2)",
device_instance.tszInstanceName,
device_instance.tszProductName));
}
result = directinput_device->SetCooperativeLevel(window->get_hwnd(), DISCL_FOREGROUND|DISCL_NONEXCLUSIVE);
if (FAILED(result))
{
directinput_device->Release();
throw CL_Error(
CL_String::format(
"Unable to the set cooperative level for %1 (%2)",
device_instance.tszInstanceName,
device_instance.tszProductName));
}
result = directinput_device->SetDataFormat(&c_dfDIJoystick2);
if (FAILED(result))
{
directinput_device->Release();
throw CL_Error(
CL_String::format(
"Unable to set device dataformat for %1 (%2)",
device_instance.tszInstanceName,
device_instance.tszProductName));
}
// Enable buffering of input events:
DIPROPDWORD value;
memset(&value, 0, sizeof(DIPROPDWORD));
value.diph.dwSize = sizeof(DIPROPDWORD);
value.diph.dwHeaderSize = sizeof(DIPROPHEADER);
value.diph.dwObj = 0;
value.diph.dwHow = DIPH_DEVICE;
value.dwData = 128;
result = directinput_device->SetProperty(DIPROP_BUFFERSIZE, &value.diph);
if (FAILED(result))
{
directinput_device->Release();
throw CL_Error(
CL_String::format(
"Unable to set buffer size attribute on device %1 (%2)",
device_instance.tszInstanceName,
device_instance.tszProductName));
}
result = directinput_device->Acquire();
if (FAILED(result))
{
directinput_device->Release();
throw CL_Error(
CL_String::format(
"Unable to acquire device %1 (%2)",
device_instance.tszInstanceName,
device_instance.tszProductName));
}
}
CL_InputDevice_DirectInput::~CL_InputDevice_DirectInput()
{
if (directinput_device) directinput_device->Release();
}
/////////////////////////////////////////////////////////////////////////////
// CL_InputDevice_DirectInput attributes:
int CL_InputDevice_DirectInput::get_x() const
{
return 0;
}
int CL_InputDevice_DirectInput::get_y() const
{
return 0;
}
bool CL_InputDevice_DirectInput::get_keycode(int keycode) const
{
if (keycode < 0 || keycode >= 128) return false;
DIJOYSTATE2 joystate2;
HRESULT result = directinput_device->GetDeviceState(sizeof(DIJOYSTATE2), &joystate2);
if (FAILED(result)) return false;
return LOBYTE(joystate2.rgbButtons[keycode]) != 0;
}
std::string CL_InputDevice_DirectInput::get_key_name(int id) const
{
return CL_String::format("Joystick button %1", id);
}
float CL_InputDevice_DirectInput::get_axis(int index) const
{
DIJOYSTATE2 joystate2;
HRESULT result = directinput_device->GetDeviceState(sizeof(DIJOYSTATE2), &joystate2);
if (FAILED(result)) return 0;
switch (index)
{
case 0:
return float(joystate2.lX)/(0xffff/2)-1.0f;
case 1:
return float(joystate2.lY)/(0xffff/2)-1.0f;
case 2:
return float(joystate2.lZ)/(0xffff/2)-1.0f;
case 3:
return float(joystate2.lRx)/(0xffff/2)-1.0f;
case 4:
return float(joystate2.lRy)/(0xffff/2)-1.0f;
case 5:
return float(joystate2.lRz)/(0xffff/2)-1.0f;
case 6:
return float(joystate2.rglSlider[0])/(0xffff/2)-1.0f;
case 7:
return float(joystate2.rglSlider[1])/(0xffff/2)-1.0f;
case 8:
return float(joystate2.lVX)/(0xffff/2)-1.0f;
case 9:
return float(joystate2.lVY)/(0xffff/2)-1.0f;
case 10:
return float(joystate2.lVZ)/(0xffff/2)-1.0f;
case 11:
return float(joystate2.lVRx)/(0xffff/2)-1.0f;
case 12:
return float(joystate2.lVRy)/(0xffff/2)-1.0f;
case 13:
return float(joystate2.lVRz)/(0xffff/2)-1.0f;
case 14:
return float(joystate2.rglVSlider[0])/(0xffff/2)-1.0f;
case 15:
return float(joystate2.rglVSlider[1])/(0xffff/2)-1.0f;
case 16:
return float(joystate2.lAX)/(0xffff/2)-1.0f;
case 17:
return float(joystate2.lAY)/(0xffff/2)-1.0f;
case 18:
return float(joystate2.lAZ)/(0xffff/2)-1.0f;
case 19:
return float(joystate2.lARx)/(0xffff/2)-1.0f;
case 20:
return float(joystate2.lARy)/(0xffff/2)-1.0f;
case 21:
return float(joystate2.lARz)/(0xffff/2)-1.0f;
case 22:
return float(joystate2.rglASlider[0])/(0xffff/2)-1.0f;
case 23:
return float(joystate2.rglASlider[1])/(0xffff/2)-1.0f;
case 24:
return float(joystate2.lFX)/(0xffff/2)-1.0f;
case 25:
return float(joystate2.lFY)/(0xffff/2)-1.0f;
case 26:
return float(joystate2.lFZ)/(0xffff/2)-1.0f;
case 27:
return float(joystate2.lFRx)/(0xffff/2)-1.0f;
case 28:
return float(joystate2.lFRy)/(0xffff/2)-1.0f;
case 29:
return float(joystate2.lFRz)/(0xffff/2)-1.0f;
case 30:
return float(joystate2.rglFSlider[0])/(0xffff/2)-1.0f;
case 31:
return float(joystate2.rglFSlider[1])/(0xffff/2)-1.0f;
case 32:
return float(joystate2.rgdwPOV[0]);
case 33:
return float(joystate2.rgdwPOV[1]);
case 34:
return float(joystate2.rgdwPOV[2]);
case 35:
return float(joystate2.rgdwPOV[3]);
}
return 0.0f;
}
std::string CL_InputDevice_DirectInput::get_name() const
{
return device_instance.tszInstanceName;
}
int CL_InputDevice_DirectInput::get_axis_count() const
{
return 36;
}
int CL_InputDevice_DirectInput::get_button_count() const
{
return -1;
}
/////////////////////////////////////////////////////////////////////////////
// CL_InputDevice_DirectInput operations:
void CL_InputDevice_DirectInput::set_position(int x, int y)
{
}
/////////////////////////////////////////////////////////////////////////////
// CL_InputDevice_DirectInput implementation:
void CL_InputDevice_DirectInput::keep_alive()
{
directinput_device->Poll();
// Get events:
while (true)
{
DIDEVICEOBJECTDATA buffer[16];
DWORD num_events = 16;
HRESULT result = directinput_device->GetDeviceData(
sizeof(DIDEVICEOBJECTDATA),
buffer,
&num_events,
0);
// Try to reacquire joystick if we lost it.
if (result == DIERR_INPUTLOST || result == DIERR_NOTACQUIRED) directinput_device->Acquire();
if (FAILED(result) && result != DI_BUFFEROVERFLOW) break;
if (num_events == 0) break;
// Ok here follows the decoding of events.
//
// Imagine that. Microsoft managed to make something even more ugly than the MAPI API and
// the older Direct3D APIs. Didn't think that was possible.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -