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

📄 cmouse.cpp

📁 VC游戏编程基础
💻 CPP
字号:
// CMAIN LIB - APPLICATION AND DIRECT WRAPPER
//
// Written by Mauricio Teichmann Ritter
//
// Copyright (C) 2002, Brazil. All rights reserved.
// 
//

// cMouse.cpp: implementation of the cMouse class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "cMouse.h"
#include "cKeyboard.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

cMouse::cMouse()
{
	m_lXPos = 0;
	m_lYPos = 0;

	m_bButton0 = TRUE;
	m_bButton1 = TRUE;
}

cMouse::~cMouse()
{

}

BOOL cMouse::Create()
{

	cInputDevice::Create();

	HRESULT         hRet; 
	
	hRet = m_lpDI->CreateDevice(GUID_SysMouse, &m_lpDIMouse, NULL); 
	if FAILED(hRet) { 
		Destroy();
		return FALSE; 
	} 

	hRet = m_lpDIMouse->SetDataFormat(&c_dfDIMouse); 
	if FAILED(hRet) { 
		Destroy();
		return FALSE; 
	} 

	
    hRet = m_lpDIMouse->SetCooperativeLevel(GetMainApp()->GetMainWnd(), 
                             DISCL_FOREGROUND | DISCL_EXCLUSIVE); 
    if FAILED(hRet) 
    { 
        Destroy();
        return FALSE; 
    } 


	m_hMouseEvent = CreateEvent(NULL, FALSE, FALSE, NULL);

	if (m_hMouseEvent == NULL) {
		return FALSE;
	}

	hRet = m_lpDIMouse->SetEventNotification(m_hMouseEvent);
 
	if (FAILED(hRet)) {
		return FALSE;
	}


	#define SAMPLE_BUFFER_SIZE  16
 
	DIPROPDWORD dipdw;

	// the header
	dipdw.diph.dwSize       = sizeof(DIPROPDWORD);
	dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER);
	dipdw.diph.dwObj        = 0;
	dipdw.diph.dwHow        = DIPH_DEVICE;
	// the data
	dipdw.dwData            = SAMPLE_BUFFER_SIZE;
 

	hRet = m_lpDIMouse->SetProperty(DIPROP_BUFFERSIZE, &dipdw.diph);

	if (FAILED(hRet))
	{
		return FALSE;
	}

    // Get access to the input device. 
    hRet = m_lpDIMouse->Acquire(); 
    if FAILED(hRet) 
    { 
        Destroy(); 
        return FALSE; 
    }
 
	return TRUE;

}

void cMouse::Destroy()
{
	if(m_lpDIMouse != NULL){
		m_lpDIMouse->Unacquire();
		m_lpDIMouse->Release();
		m_lpDIMouse = NULL;
	}

	cInputDevice::Destroy();

	//free(m_KbdBuffer);
}

void cMouse::Process()
{
    DIDEVICEOBJECTDATA didod[ SAMPLE_BUFFER_SIZE ];  // Receives buffered data 
    DWORD              dwElements;
    DWORD              i;
    HRESULT            hr;

    dwElements = SAMPLE_BUFFER_SIZE;
    hr = m_lpDIMouse->GetDeviceData( sizeof(DIDEVICEOBJECTDATA),
                                     didod, &dwElements, 0 );
    if( hr != DI_OK ) 
    {
        hr = m_lpDIMouse->Acquire();
        while( hr == DIERR_INPUTLOST ) 
            hr = m_lpDIMouse->Acquire();

   }

    // Study each of the buffer elements and process them.
    //
    // Since we really don't do anything, our "processing"
    // consists merely of squirting the name into our
    // local buffer.
    for( i = 0; i < dwElements; i++ ) 
    {
        switch( didod[ i ].dwOfs )
        {
            case DIMOFS_BUTTON0:
                if( didod[ i ].dwData & 0x80 )
                    m_bButton0 = FALSE;
                else
                    m_bButton0 = TRUE;
                break;
            case DIMOFS_BUTTON1:
                if( didod[ i ].dwData & 0x80 )
                    m_bButton1 = FALSE;
                else
                    m_bButton1 = TRUE;
                break;

            case DIMOFS_X:
				m_lXPos += didod[ i ].dwData;
				if(m_lXPos > 640)
					m_lXPos = 640;

				if(m_lXPos < 0)
					m_lXPos = 0;
				break;
            case DIMOFS_Y:
				m_lYPos += didod[ i ].dwData;
				if(m_lYPos > 480)
					m_lYPos = 480;

				if(m_lYPos < 0)
					m_lYPos = 0;
				break;
            /*case DIMOFS_Z:
            {
                TCHAR strCoordValue[20];
                wsprintf( strCoordValue, TEXT("%d "), didod[ i ].dwData );
                _tcscat( strNewText, strCoordValue );
                break;
            }*/
        }
    }

    return;
}


LPDIRECTINPUTDEVICE8 cMouse::m_lpDIMouse = NULL;

⌨️ 快捷键说明

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