📄 directinputmgr.cpp
字号:
/****************************************************************************************/
/* DirectInputMgr.cpp */
/* */
/* Author: Tom Morris */
/* Description: Helper class that receives interprets mouse movement for QuickViewer */
/* Used by CGenEditPreviewDlg */
/* */
/* The contents of this file are subject to the Genesis3D Public License */
/* Version 1.01 (the "License"); you may not use this file except in */
/* compliance with the License. You may obtain a copy of the License at */
/* http://www.genesis3d.com */
/* */
/* Software distributed under the License is distributed on an "AS IS" */
/* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See */
/* the License for the specific language governing rights and limitations */
/* under the License. */
/* */
/* The Original Code is Genesis3D, released March 25, 1999. */
/* Genesis3D Version 1.1 released November 15, 1999 */
/* Copyright (C) 1999 WildTangent, Inc. All Rights Reserved */
/* */
/* Prepared for GenEdit-Classic ver. 0.5, Dec. 15, 2000 */
/****************************************************************************************/
#ifndef DIRECTINPUT_VERSION
#define DIRECTINPUT_VERSION 0x0700
#endif
#include "stdafx.h"
#include "Globals.h"
#include "DirectInputMgr.h"
#include <dinputd.h>
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
///////////////////////////////////////////////////// TrilobiteShell
CDirectInputMgr::CDirectInputMgr()
{
}
CDirectInputMgr::~CDirectInputMgr()
{
}
LPDIRECTINPUTDEVICE g_pdevJoystick; // our joystick device
LPDIRECTINPUTDEVICE g_pdevKeyboard; // our keyboard device
LPDIRECTINPUTDEVICE g_pdevMouse; // our mouse device
DIDEVCAPS g_diDevCaps; // our joystick's capabilities
int ACTION_FORWARD;
int ACTION_BACKWARD;
int ACTION_SLIDE_LEFT;
int ACTION_SLIDE_RIGHT;
int ACTION_MENU;
int ACTION_FIRE1; // BY TOM
int ACTION_FIRE2; // by tom
int ACTION_SHOWHUD; // BY TOM
int ACTION_HIDEHUD; // BY TOM
// SETDIDWORDPROPERTY ========================================================
// This function set a DWORD property on a DirectInputDevice.
// ============================================================================
HRESULT
SetDIDwordProperty(LPDIRECTINPUTDEVICE pdev, REFGUID guidProperty,
DWORD dwObject, DWORD dwHow, DWORD dwValue)
{
DIPROPDWORD dipdw;
dipdw.diph.dwSize = sizeof(dipdw);
dipdw.diph.dwHeaderSize = sizeof(dipdw.diph);
dipdw.diph.dwObj = dwObject;
dipdw.diph.dwHow = dwHow;
dipdw.dwData = dwValue;
return pdev->SetProperty( guidProperty, &dipdw.diph);
}
// INITJOYSTICKINPUT ================================================================
// This initializes our joystick
// ============================================================================
BOOL CALLBACK InitJoystickInput(const DIDEVICEINSTANCE *pdinst, VOID *pvRef)
{
LPDIRECTINPUT pdi = (LPDIRECTINPUT)pvRef;
DIPROPRANGE diprg;
/*
BOOL CALLBACK EnumJoysticksCallback( const DIDEVICEINSTANCE* pdidInstance,
VOID* pContext )
{
HRESULT hr;
// Obtain an interface to the enumerated joystick.
if (pdi->CreateDevice( pdinst->guidInstance, &g_pdevJoystick, NULL ) != DI_OK)
*/
// Create the DirectInput joystick device.
// if (pdi->CreateDeviceEx( pdinst->guidInstance,
// IID_IDirectInputDevice, (void**) &g_pdevJoystick, NULL) != DI_OK)
{
// MessageBox(0,"Could not create joystick device","Joystick Problem!", MB_OK);
OutputDebugString("IDirectInput::CreateDevice FAILED\n");
return DIENUM_STOP;
}
// set joystick data format
if(g_pdevJoystick->SetDataFormat( &c_dfDIJoystick) != DI_OK)
{
// MessageBox(NULL,"Could not set data format for joystick","Joystick Problem!", MB_OK);
OutputDebugString("IDirectInputDevice::SetDataFormat FAILED\n");
g_pdevJoystick->Release();
return DIENUM_STOP;
}
// set the cooperative level
if(g_pdevJoystick->SetCooperativeLevel( (HWND)AfxGetApp()->m_pMainWnd,
DISCL_NONEXCLUSIVE | DISCL_FOREGROUND) != DI_OK)
{
// MessageBox(NULL,"Could not set cooperative level for joystick","Joystick Problem!", MB_OK);
OutputDebugString("IDirectInputDevice::SetCooperativeLevel FAILED\n");
g_pdevJoystick->Release();
return DIENUM_STOP;
}
// Determine how many axis the joystick has (so we don't error out setting
// properties for unavailable axis)
g_diDevCaps.dwSize = sizeof(DIDEVCAPS);
if(g_pdevJoystick->GetCapabilities(&g_diDevCaps) != DI_OK)
{
// MessageBox(NULL, "Unable to get joystick's capabilities", "Joystick Problem!", MB_OK);
return DIENUM_STOP;
}
// set X-axis range to (-1000 ... +1000) - this will control pitch
// This lets us test against 0 to see which way the stick is pointed.
diprg.diph.dwSize = sizeof(diprg);
diprg.diph.dwHeaderSize = sizeof(diprg.diph);
diprg.diph.dwObj = DIJOFS_X;
diprg.diph.dwHow = DIPH_BYOFFSET;
diprg.lMin = -1000;
diprg.lMax = +1000;
if(g_pdevJoystick->SetProperty( DIPROP_RANGE, &diprg.diph) != DI_OK)
{
// MessageBox(NULL,"Could not set property for joystick x-axis","Joystick Problem!", MB_OK);
g_pdevJoystick->Release();
return DIENUM_CONTINUE;
}
// And again for Y-axis range - this will control yaw
diprg.diph.dwObj = DIJOFS_Y;
if(g_pdevJoystick->SetProperty( DIPROP_RANGE, &diprg.diph) != DI_OK)
{
// MessageBox(NULL,"Could not set property for joystick y-axis","Joystick Problem!", MB_OK);
g_pdevJoystick->Release();
return DIENUM_CONTINUE;
}
// And again for Z-axis - this will be the throttle
diprg.diph.dwObj = DIJOFS_Z;
if(g_pdevJoystick->SetProperty( DIPROP_RANGE, &diprg.diph) != DI_OK)
{
// MessageBox(NULL,"Could not set property for joystick z-axis","Joystick Problem!", MB_OK);
g_pdevJoystick->Release();
return DIENUM_CONTINUE;
}
// And again for Z-axis rotation - this will control roll
// Z-axis rotation happens when you twist the joystick handle
diprg.diph.dwObj = DIJOFS_RZ;
if(g_pdevJoystick->SetProperty( DIPROP_RANGE, &diprg.diph) != DI_OK)
{
// MessageBox(NULL,"Could not set property for z-axis rotation","Joystick Problem!", MB_OK);
g_pdevJoystick->Release();
return DIENUM_CONTINUE;
}
// set X axis dead zone to 15% (to avoid accidental pitch)
if FAILED(SetDIDwordProperty(g_pdevJoystick, DIPROP_DEADZONE, DIJOFS_X,
DIPH_BYOFFSET, 1500))
{
// MessageBox(NULL,"Could not set property for xiaxis deadzone","Joystick Problem!", MB_OK);
g_pdevJoystick->Release();
return DIENUM_CONTINUE;
}
// set Y axis dead zone to 15% (to avoid accidental yaw)
// Units are ten thousandths, so 15% = 1500/10000.
if FAILED(SetDIDwordProperty(g_pdevJoystick, DIPROP_DEADZONE, DIJOFS_Y,
DIPH_BYOFFSET, 1500))
{
// MessageBox(NULL,"Could not set property for y-axis deadzone","Joystick Problem!", MB_OK);
g_pdevJoystick->Release();
return DIENUM_CONTINUE;
}
// set z axis dead zone to 15% (to avoid accidental thrust)
if FAILED(SetDIDwordProperty(g_pdevJoystick, DIPROP_DEADZONE, DIJOFS_Z,
DIPH_BYOFFSET, 1500))
{
// MessageBox(NULL,"Could not set property for joystick z-axis deadzone","Joystick Problem!", MB_OK);
g_pdevJoystick->Release();
return DIENUM_CONTINUE;
}
// set z axis rotation dead zone to 15% (to avoid accidental roll)
if FAILED(SetDIDwordProperty(g_pdevJoystick, DIPROP_DEADZONE, DIJOFS_RZ,
DIPH_BYOFFSET, 1500))
{
// MessageBox(NULL,"Could not set property for joystick z-axis rotation deadzone","Joystick Problem!", MB_OK);
g_pdevJoystick->Release();
return DIENUM_CONTINUE;
}
// set so we can poll the joystick later
g_pdevJoystick->QueryInterface(IID_IDirectInputDevice7, (LPVOID *)&g_pdevJoystick);
if FAILED (IDirectInputDevice_Acquire(g_pdevJoystick))
{
g_pdevJoystick->Release();
return DIENUM_CONTINUE;;
}
g_pdevJoystick->Release();
return DIENUM_CONTINUE;
}
// INITINPUT ================================================================
// This function initializes DirectInput for the keyboard and all joysticks.
// ============================================================================
BOOL InitInput(HINSTANCE hInstance, HWND hWnd, int controller)
{
LPDIRECTINPUT pdi;
HRESULT hr;
hr = DirectInputCreate( GetModuleHandle(NULL), DIRECTINPUT_VERSION,
&pdi, NULL);
if FAILED(hr)
{
MessageBox(NULL,"Failed to DirectInputCreateEx","Error", MB_OK);
return FALSE;
}
/* hr = DirectInputCreateEx(hInstance, DIRECTINPUT_VERSION, IID_IDirectInput7,
(void**)&pdi, NULL);
if FAILED(hr)
{
MessageBox(NULL,"Failed to DirectInputCreateEx","Error", MB_OK);
return FALSE;
}
*/ else if (!FAILED(hr))
{
// KEYBOARD ****************************************************************************
// Obtain an interface to the system keyboard device.
hr = pdi->CreateDevice( GUID_SysKeyboard, &g_pdevKeyboard, NULL );
if FAILED(hr) {return FALSE;}
/* hr = pdi->CreateDeviceEx(GUID_SysKeyboard,
IID_IDirectInputDevice8, (void**)&g_pdevKeyboard, NULL);
if FAILED(hr) {return FALSE;}
*/ // Set the data format using the predefined keyboard data
// format provided by the DirectInput object for keyboards.
hr = g_pdevKeyboard->SetDataFormat(&c_dfDIKeyboard);
if FAILED(hr) {return FALSE;}
// Set the cooperative level
hr = g_pdevKeyboard->SetCooperativeLevel(hWnd,
DISCL_FOREGROUND | DISCL_NONEXCLUSIVE);
if FAILED(hr) {return FALSE;}
hr = g_pdevKeyboard->Acquire();
if FAILED(hr) {return FALSE;}
// END KEYBOARD INTIALIZATION ********************************************************
// MOUSE ///////////////////////////////////////////////////////////////////////////////
hr =pdi->CreateDevice( GUID_SysMouse, &g_pdevMouse, NULL );
// hr = pdi->CreateDeviceEx(GUID_SysMouse, IID_IDirectInputDevice8,
// (void**)&g_pdevMouse, NULL);
if (FAILED(hr)) {return FALSE;}
hr = g_pdevMouse->SetDataFormat(&c_dfDIMouse);
if (FAILED(hr)) {return FALSE;}
hr = g_pdevMouse->SetCooperativeLevel(hWnd,DISCL_FOREGROUND | DISCL_EXCLUSIVE);
if (FAILED(hr)) {return FALSE;}
hr = g_pdevMouse->Acquire();
if FAILED(hr) {return FALSE;}
// END MOUSE INTIALIZATION ////////////////////////////////////////////////////////////
if (controller == 1) // Joystick
{
// Look for a simple joystick we can use for this sample program.
hr = pdi->EnumDevices( DIDEVTYPE_JOYSTICK,
InitJoystickInput,
NULL, DIEDFL_ATTACHEDONLY );
// Enumerate the JOYSTICK devices ====================================================
// hr = IDirectInput8_EnumDevices(pdi, DIDEVTYPE_JOYSTICK,
// InitJoystickInput, pdi, DIEDFL_ATTACHEDONLY); // DI_OK if all is well
// hr = one of these, if above statement had probs.
if (hr != DI_OK )
{
// MessageBox(NULL, "Could not enumerate joystick", "Joystick Problem!", MB_OK);
return FALSE;
}
if (g_pdevJoystick == NULL) // if no joystick...
{
hr = pdi->Release(); // Finished with DX 7.0.
// MessageBox( Game.MainWindow, "Joystick not found. Shifting control to Mouse", "Joystick Problem!", MB_ICONERROR | MB_OK );
return FALSE;
}
// End Joystick Initialization ============================================================
} // if (Game.Controller == 1)
hr = pdi->Release(); // Finished with DX 7.0.
}
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -