나는 IDirectInputDevice8 인터페이스를 사용하여 직접 입력 장치에 폴링전화을 차단하는 차단 및 비 할 필요가있다.Microsoft의 IDirectInputDevice8을 사용하여 C++에서 조이스틱 코드를 구현하는 방법은 무엇입니까?
차단 할 수있는 리눅스에서, 우리는 사용 선택 예 : 내가 설정하더라도 https://msdn.microsoft.com/en-us/library/windows/desktop/ee417816(v=vs.85).aspx
: 나는 idirectinputdevice8인터페이스를 사용하여 같은 일을 할 수있는 방법
while(::select(_jdev+1, &set, NULL, NULL, &tv) > 0)
{
if(::read(_jdev, &js, sizeof(js_event)) != sizeof(js_event))
{
perror("Joystick : read error");
return;
}
_handleEvent(js);
}
IDirectInputDevice8 :: SetEventNotification(), 새로운 데이터를 가져 오기 위해 매번 poll()을 호출해야하는데 이것은 w가 아닙니다. orkable 솔루션은 cpu가 회전 할 것이기 때문입니다.
어떻게하면됩니까?
*** 지금은 조이스틱 장치에서 데이터를 찾고, 반복하고, 연결하고 가져올 수 있습니다. 차단 호출이 구현되지 않았습니다.
가 ... 여기 내 실험/테스트 코드입니다 ... 구문 오류
#include <windows.h>
#include <dinput.h>
#include <stdio.h>
#include <iostream>
#include <sstream>
#include "joystick.h"
LPDIRECTINPUT8 di;
HRESULT hr;
LPDIRECTINPUTDEVICE8 joystick;
DIDEVCAPS capabilities;
BOOL CALLBACK
enumCallback(const DIDEVICEINSTANCE* instance, VOID* context)
{
HRESULT hr;
hr = di->CreateDevice(instance->guidInstance, &joystick, NULL);
if (FAILED(hr)) {
return DIENUM_CONTINUE;
}
return DIENUM_STOP;
}
int JoyStickProp()
{
if (FAILED(hr = joystick->SetDataFormat(&c_dfDIJoystick2))) {
return hr;
}
if (FAILED(hr = joystick->SetCooperativeLevel(NULL, DISCL_EXCLUSIVE |
DISCL_FOREGROUND))) {
return hr;
}
capabilities.dwSize = sizeof(DIDEVCAPS);
if (FAILED(hr = joystick->GetCapabilities(&capabilities))) {
return hr;
}
}
HRESULT JoyStickPoll(DIJOYSTATE2 *js)
{
HRESULT hr;
if (joystick == NULL) {
return S_OK;
}
// Poll the device to read the current state
hr = joystick->Poll();
if (FAILED(hr)) {
hr = joystick->Acquire();
while (hr == DIERR_INPUTLOST) {
hr = joystick->Acquire();
}
if ((hr == DIERR_INVALIDPARAM) || (hr == DIERR_NOTINITIALIZED)) {
return E_FAIL;
}
// If another application has control of this device, return successfully.
// We'll just have to wait our turn to use the joystick.
if (hr == DIERR_OTHERAPPHASPRIO) {
return S_OK;
}
}
// Get the input's device state
if (FAILED(hr = joystick->GetDeviceState(sizeof(DIJOYSTATE2), js))) {
return hr; // The device should have been acquired during the Poll()
}
return S_OK;
}
int main()
{
// Create a DirectInput device
if (FAILED(hr = DirectInput8Create(GetModuleHandle(NULL), DIRECTINPUT_VERSION,
IID_IDirectInput8, (VOID**)&di, NULL))) {
return hr;
}
// Look for the first simple joystick we can find.
if (FAILED(hr = di->EnumDevices(DI8DEVCLASS_GAMECTRL, enumCallback,
NULL, DIEDFL_ATTACHEDONLY))) {
return hr;
}
// Make sure we got a joystick
if (joystick == NULL) {
printf("Joystick not found.\n");
return E_FAIL;
}
JoyStickProp();
DIJOYSTATE2 diState;
HANDLE ghWriteEvent;
// Create joystick event stuff here
ghWriteEvent = CreateEvent(
NULL, // default security attributes
FALSE, // manual-reset event
FALSE, // initial state is nonsignaled
TEXT("WriteEvent") // object name
);
//makesure we can read fromt the joystick before waiting for an event
JoyStickPoll(&diState);
printf("x axis %d", diState.lX);
printf("y axis %d", diState.lY);
JoyStickPoll(&diState);
printf("x axis %d", diState.lX);
printf("y axis %d", diState.lY);
JoyStickPoll(&diState);
printf("x axis %d", diState.lX);
printf("y axis %d", diState.lY);
JoyStickPoll(&diState);
printf("x axis %d", diState.lX);
printf("y axis %d", diState.lY);
JoyStickPoll(&diState);
printf("x axis %d", diState.lX);
printf("y axis %d", diState.lY);
JoyStickPoll(&diState);
printf("x axis %d", diState.lX);
printf("y axis %d", diState.lY);
joystick->SetEventNotification(ghWriteEvent);
while (TRUE) {
DWORD dwResult = WaitForSingleObject(
ghWriteEvent, // event handle
INFINITE);
switch (dwResult) {
case WAIT_OBJECT_0:
// Event 1 has been set. If the event was created as
// autoreset, it has also been reset.
int x = 0;
//ProcessInputEvent1();
break;
}
}
}
[밑줄을보세요.] (http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-ac-identifier) 있다면 정말 짜증이납니다. 컴파일러의 표준 라이브러리 구현에 정의 된'_handleEvent' 또는'_jdev'가 코드와 교차됩니다. – user4581301
그건 내가 리눅스에서 코드를 내가 Windows에서 성취하고자하는 것에 대한 예제로 ... 그것은 Windows 코드 또는 현재 문제와 관련이 없습니다. –
부적절합니다. 밑줄은 C++에서 C++ 표준에 의해 정의 된 특별한 의미를 지니고 있습니다. 규칙이 Linux 또는 Windows 용인지 여부에 관계없이 규칙은 동일합니다. 결과 만 다릅니다.어떻게 사용하는지주의하십시오. – user4581301