Mouse.cpp
上传用户:sycq158
上传日期:2008-10-22
资源大小:15361k
文件大小:4k
- /*******************************************************************
- * 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 failedn");
- }
- /**
- * 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 failedn");
- }
- // 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();
- }