⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mouse.cpp

📁 这是一个屠宰场游戏,此游戏很休闲的请大家下载来看看.
💻 CPP
字号:
/*******************************************************************
 *         Advanced 3D Game Programming using DirectX 7.0
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *   Title: Mouse.cpp
 *    Desc: Wrapper of a DirectInput mouse object
 *          
 * copyright (c) 1999 by Adrian Perez
 * See license.txt for modification and distribution information
 ******************************************************************/

#include <Windows.h>
#include "GameErrors.h"
#include "InputLayer.h"

#include "Mouse.h"

cMouse::cMouse(  HWND hWnd, bool bExclusive )
{
	m_pTarget = NULL;

	HRESULT hr;

	/**
	 * Create the device
	 */
	hr = Input()->GetDInput()->CreateDevice(
		GUID_SysMouse, 
        &m_pDevice, 
		NULL);
	if( FAILED( hr ))
	{
		throw cGameError("[cMouse::Init]: Couldn't create the device!\n");
	}

	/**
	 * Set the data format
	 */
	hr = m_pDevice->SetDataFormat(&c_dfDIMouse);
 	if( FAILED( hr ))
	{
		SafeRelease( m_pDevice );
		throw cGameError("[cMouse::Init]: SetDataFormat failed\n");
	}

	/**
	 * Set the cooperative level
	 */
	if( bExclusive )
	{
		hr = m_pDevice->SetCooperativeLevel( hWnd, DISCL_EXCLUSIVE | DISCL_NOWINKEY | DISCL_FOREGROUND );
	}
	else
	{
		hr = m_pDevice->SetCooperativeLevel( hWnd, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND);
	}

	if( FAILED( hr ))
	{
		SafeRelease( m_pDevice );
		throw cGameError("[cMouse::Init]: SetCooperativeLevel failed\n");
	}

	// CHANGE: we used to grab the mouse here, but this would cause
	// a crash if the app didn't have focus when it started up.  This is,
	// of course, an undesirable trait :)
	
	// Instead we just set the mouse initially to be stationary, with no
	// buttons pressed.  We can't do a GetDeviceState here since we don't
	// have the device acquired.

	m_lastState.lX = 0;
	m_lastState.lY = 0;
	m_lastState.lZ = 0;
	m_lastState.rgbButtons[0] = 0;
	m_lastState.rgbButtons[1] = 0;
	m_lastState.rgbButtons[2] = 0;
	m_lastState.rgbButtons[3] = 0;
}

cMouse::~cMouse()
{
	if( m_pDevice )
	{
		m_pDevice->Unacquire();
		SafeRelease( m_pDevice );
	}
}


void cMouse::SetReceiver( iMouseReceiver* pTarget )
{
	m_pTarget = pTarget;
}


eResult cMouse::Update()
{
	DIMOUSESTATE currState;
    HRESULT  hr; 
 
	// CHANGE: Polling can't hurt, but it can help.
    hr = m_pDevice->Poll(); 
	hr = m_pDevice->GetDeviceState( sizeof(DIMOUSESTATE), (void*)&currState );

    if( FAILED(hr) )
    { 
		hr = m_pDevice->Acquire();
		if( FAILED( hr ) )
		{
			return resFailed;
		}

	    hr = m_pDevice->Poll(); 
	    hr = m_pDevice->GetDeviceState( sizeof(DIMOUSESTATE),(void*)&currState ); 
		if( FAILED( hr ) )
		{
			return resFailed;
		}
    } 
	if( m_pTarget )
	{
		int dx = currState.lX;
		int dy = currState.lY;
		if( dx || dy )
		{
			m_pTarget->MouseMoved( dx, dy );
		}
		if( currState.rgbButtons[0] & 0x80 )
		{
			// the button got pressed.
			m_pTarget->MouseButtonDown( 0 );
		}
		if( currState.rgbButtons[1] & 0x80 )
		{
			// the button got pressed.
			m_pTarget->MouseButtonDown( 1 );
		}
		if( currState.rgbButtons[2] & 0x80 )
		{
			// the button got pressed.
			m_pTarget->MouseButtonDown( 2 );
		}
		if( !(currState.rgbButtons[0] & 0x80) && (m_lastState.rgbButtons[0] & 0x80) )
		{
			// the button got released.
			m_pTarget->MouseButtonUp( 0 );
		}
		if( !(currState.rgbButtons[1] & 0x80) && (m_lastState.rgbButtons[1] & 0x80) )
		{
			// the button got released.
			m_pTarget->MouseButtonUp( 1 );
		}
		if( !(currState.rgbButtons[2] & 0x80) && (m_lastState.rgbButtons[2] & 0x80) )
		{
			// the button got released.
			m_pTarget->MouseButtonUp( 2 );
		}
	}
	m_lastState = currState;
	return resAllGood;
}


eResult cMouse::Acquire()
{
	HRESULT hr = m_pDevice->Acquire();
	if( FAILED(hr) )
	{
		return resFailed;
	}
	return resAllGood;
}


void cMouse::UnAcquire()
{
	m_pDevice->Unacquire();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -