/*
========================
IN_InitDIMouse
========================
*/
bool IN_InitDIMouse( void ) {
	HRESULT		hr;
	if( win32.g_pdi == NULL ) {
		return false;
	}
	// obtain an interface to the system mouse device.
	hr = win32.g_pdi->CreateDevice( GUID_SysMouse, &win32.g_pMouse, NULL );
	if( FAILED( hr ) ) {
		common->Printf( "mouse: Couldn't open DI mouse device\n" );
		return false;
	}
	// Set the data format to "mouse format" - a predefined data format
	//
	// A data format specifies which controls on a device we
	// are interested in, and how they should be reported.
	//
	// This tells DirectInput that we will be passing a
	// DIMOUSESTATE2 structure to IDirectInputDevice::GetDeviceState.
	if( FAILED( hr = win32.g_pMouse->SetDataFormat( &c_dfDIMouse2 ) ) ) {
		common->Printf( "mouse: Couldn't set DI mouse format\n" );
		return false;
	}
	// set the cooperativity level.
	hr = win32.g_pMouse->SetCooperativeLevel( win32.hWnd, DISCL_EXCLUSIVE | DISCL_FOREGROUND );
	if( FAILED( hr ) ) {
		common->Printf( "mouse: Couldn't set DI coop level\n" );
		return false;
	}
	// IMPORTANT STEP TO USE BUFFERED DEVICE DATA!
	//
	// DirectInput uses unbuffered I/O (buffer size = 0) by default.
	// If you want to read buffered data, you need to set a nonzero
	// buffer size.
	//
	// Set the buffer size to SAMPLE_BUFFER_SIZE (defined above) elements.
	//
	// The buffer size is a DWORD property associated with the device.
	DIPROPDWORD dipdw;
	dipdw.diph.dwSize       = sizeof( DIPROPDWORD );
	dipdw.diph.dwHeaderSize = sizeof( DIPROPHEADER );
	dipdw.diph.dwObj        = 0;
	dipdw.diph.dwHow        = DIPH_DEVICE;
	dipdw.dwData            = DINPUT_BUFFERSIZE; // Arbitary buffer size
	if( FAILED( hr = win32.g_pMouse->SetProperty( DIPROP_BUFFERSIZE, &dipdw.diph ) ) ) {
		common->Printf( "mouse: Couldn't set DI buffersize\n" );
		return false;
	}
	IN_ActivateMouse();
	// clear any pending samples
	Sys_PollMouseInputEvents();
	common->Printf( "mouse: DirectInput initialized.\n" );
	return true;
}
예제 #2
0
/*
===================
idUsercmdGenLocal::Mouse
===================
*/
void idUsercmdGenLocal::Mouse(void)
{
	int i, numEvents;

	numEvents = Sys_PollMouseInputEvents();

	if (numEvents) {
		//
		// Study each of the buffer elements and process them.
		//
		for (i = 0; i < numEvents; i++) {
			int action, value;

			if (Sys_ReturnMouseInputEvent(i, action, value)) {
				if (action >= M_ACTION1 && action <= M_ACTION8) {
					mouseButton = K_MOUSE1 + (action - M_ACTION1);
					mouseDown = (value != 0);
					Key(mouseButton, mouseDown);
				} else {
					switch (action) {
						case M_DELTAX:
							mouseDx += value;
							continuousMouseX += value;
							break;
						case M_DELTAY:
							mouseDy += value;
							continuousMouseY += value;
							break;
						case M_DELTAZ:
							int key = value < 0 ? K_MWHEELDOWN : K_MWHEELUP;
							value = abs(value);

							while (value-- > 0) {
								Key(key, true);
								Key(key, false);
								mouseButton = key;
								mouseDown = true;
							}

							break;
					}
				}
			}
		}
	}

	Sys_EndMouseInputEvents();
}