Input类细节调整
This commit is contained in:
		
							parent
							
								
									fb95734a88
								
							
						
					
					
						commit
						284db32f6d
					
				|  | @ -3,27 +3,19 @@ | ||||||
| #include "..\e2dmanager.h" | #include "..\e2dmanager.h" | ||||||
| #pragma comment(lib, "dinput8.lib") | #pragma comment(lib, "dinput8.lib") | ||||||
| 
 | 
 | ||||||
| #define BUFFER_SIZE	256 |  | ||||||
| 
 |  | ||||||
| static char s_KeyBuffer[BUFFER_SIZE] = { 0 };			// 用于保存键盘按键信息缓冲区
 |  | ||||||
| static char s_KeyRecordBuffer[BUFFER_SIZE] = { 0 };		// 键盘消息二级缓冲区
 |  | ||||||
| 
 |  | ||||||
| e2d::Input * e2d::Input::_instance = nullptr; | e2d::Input * e2d::Input::_instance = nullptr; | ||||||
| 
 | 
 | ||||||
| e2d::Input::Input() | e2d::Input::Input() | ||||||
| 	: _directInput(false) | 	: _directInput(false) | ||||||
| 	, _keyboardDevice(false) | 	, _keyboardDevice(false) | ||||||
| 	, _mouseDevice(false) | 	, _mouseDevice(false) | ||||||
| 	, _mouseState() |  | ||||||
| 	, _mouseStateRecord() |  | ||||||
| 	, _mousePos() |  | ||||||
| { | { | ||||||
| 	CoInitialize(nullptr); | 	CoInitialize(nullptr); | ||||||
| 
 | 
 | ||||||
| 	ZeroMemory(s_KeyBuffer, sizeof(s_KeyBuffer)); | 	ZeroMemory(_keyBuffer, sizeof(_keyBuffer)); | ||||||
| 	ZeroMemory(s_KeyRecordBuffer, sizeof(s_KeyRecordBuffer)); | 	ZeroMemory(_keyRecordBuffer, sizeof(_keyRecordBuffer)); | ||||||
| 	ZeroMemory(&_mouseState, sizeof(_mouseState)); | 	ZeroMemory(&_mouseState, sizeof(_mouseState)); | ||||||
| 	ZeroMemory(&_mouseStateRecord, sizeof(_mouseStateRecord)); | 	ZeroMemory(&_mouseRecordState, sizeof(_mouseRecordState)); | ||||||
| 
 | 
 | ||||||
| 	// ³õʼ»¯½Ó¿Ú¶ÔÏó
 | 	// ³õʼ»¯½Ó¿Ú¶ÔÏó
 | ||||||
| 	HRESULT hr = DirectInput8Create( | 	HRESULT hr = DirectInput8Create( | ||||||
|  | @ -124,10 +116,8 @@ void e2d::Input::update() | ||||||
| 		} | 		} | ||||||
| 		else | 		else | ||||||
| 		{ | 		{ | ||||||
| 			for (int i = 0; i < BUFFER_SIZE; ++i) | 			strcpy_s(_keyRecordBuffer, 256, _keyBuffer); | ||||||
| 				s_KeyRecordBuffer[i] = s_KeyBuffer[i]; | 			_keyboardDevice->GetDeviceState(sizeof(_keyBuffer), (void**)&_keyBuffer); | ||||||
| 
 |  | ||||||
| 			_keyboardDevice->GetDeviceState(sizeof(s_KeyBuffer), (void**)&s_KeyBuffer); |  | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | @ -142,34 +132,31 @@ void e2d::Input::update() | ||||||
| 		} | 		} | ||||||
| 		else | 		else | ||||||
| 		{ | 		{ | ||||||
| 			_mouseStateRecord = _mouseState; | 			_mouseRecordState = _mouseState; | ||||||
| 			_mouseDevice->GetDeviceState(sizeof(_mouseState), (void**)&_mouseState); | 			_mouseDevice->GetDeviceState(sizeof(_mouseState), (void**)&_mouseState); | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 
 |  | ||||||
| 	GetCursorPos(&_mousePos); |  | ||||||
| 	ScreenToClient(Window::getInstance()->getHWnd(), &_mousePos); |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| bool e2d::Input::isDown(KeyCode key) | bool e2d::Input::isDown(KeyCode key) | ||||||
| { | { | ||||||
| 	if (s_KeyBuffer[static_cast<int>(key)] & 0x80) | 	if (_keyBuffer[static_cast<int>(key)] & 0x80) | ||||||
| 		return true; | 		return true; | ||||||
| 	return false; | 	return false; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| bool e2d::Input::isPress(KeyCode key) | bool e2d::Input::isPress(KeyCode key) | ||||||
| { | { | ||||||
| 	if ((s_KeyBuffer[static_cast<int>(key)] & 0x80) &&  | 	if ((_keyBuffer[static_cast<int>(key)] & 0x80) &&  | ||||||
| 		!(s_KeyRecordBuffer[static_cast<int>(key)] & 0x80)) | 		!(_keyRecordBuffer[static_cast<int>(key)] & 0x80)) | ||||||
| 		return true; | 		return true; | ||||||
| 	return false; | 	return false; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| bool e2d::Input::isRelease(KeyCode key) | bool e2d::Input::isRelease(KeyCode key) | ||||||
| { | { | ||||||
| 	if (!(s_KeyBuffer[static_cast<int>(key)] & 0x80) &&  | 	if (!(_keyBuffer[static_cast<int>(key)] & 0x80) &&  | ||||||
| 		(s_KeyRecordBuffer[static_cast<int>(key)] & 0x80)) | 		(_keyRecordBuffer[static_cast<int>(key)] & 0x80)) | ||||||
| 		return true; | 		return true; | ||||||
| 	return false; | 	return false; | ||||||
| } | } | ||||||
|  | @ -184,7 +171,7 @@ bool e2d::Input::isDown(MouseCode code) | ||||||
| bool e2d::Input::isPress(MouseCode code) | bool e2d::Input::isPress(MouseCode code) | ||||||
| { | { | ||||||
| 	if ((_mouseState.rgbButtons[static_cast<int>(code)] & 0x80) &&  | 	if ((_mouseState.rgbButtons[static_cast<int>(code)] & 0x80) &&  | ||||||
| 		!(_mouseStateRecord.rgbButtons[static_cast<int>(code)] & 0x80)) | 		!(_mouseRecordState.rgbButtons[static_cast<int>(code)] & 0x80)) | ||||||
| 		return true; | 		return true; | ||||||
| 	return false; | 	return false; | ||||||
| } | } | ||||||
|  | @ -192,24 +179,27 @@ bool e2d::Input::isPress(MouseCode code) | ||||||
| bool e2d::Input::isRelease(MouseCode code) | bool e2d::Input::isRelease(MouseCode code) | ||||||
| { | { | ||||||
| 	if (!(_mouseState.rgbButtons[static_cast<int>(code)] & 0x80) &&  | 	if (!(_mouseState.rgbButtons[static_cast<int>(code)] & 0x80) &&  | ||||||
| 		(_mouseStateRecord.rgbButtons[static_cast<int>(code)] & 0x80)) | 		(_mouseRecordState.rgbButtons[static_cast<int>(code)] & 0x80)) | ||||||
| 		return true; | 		return true; | ||||||
| 	return false; | 	return false; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| double e2d::Input::getMouseX() | double e2d::Input::getMouseX() | ||||||
| { | { | ||||||
| 	return (double)_mousePos.x; | 	return getMousePos().x; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| double e2d::Input::getMouseY() | double e2d::Input::getMouseY() | ||||||
| { | { | ||||||
| 	return (double)_mousePos.y; | 	return getMousePos().y; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| e2d::Point e2d::Input::getMousePos() | e2d::Point e2d::Input::getMousePos() | ||||||
| { | { | ||||||
| 	return Point((double)_mousePos.x, (double)_mousePos.y); | 	POINT mousePos; | ||||||
|  | 	GetCursorPos(&mousePos); | ||||||
|  | 	ScreenToClient(Window::getInstance()->getHWnd(), &mousePos); | ||||||
|  | 	return Point((double)mousePos.x, (double)mousePos.y); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| double e2d::Input::getMouseDeltaX() | double e2d::Input::getMouseDeltaX() | ||||||
|  |  | ||||||
|  | @ -294,12 +294,13 @@ private: | ||||||
| 	E2D_DISABLE_COPY(Input); | 	E2D_DISABLE_COPY(Input); | ||||||
| 
 | 
 | ||||||
| private: | private: | ||||||
| 	IDirectInput8* _directInput; | 	IDirectInput8W* _directInput; | ||||||
| 	IDirectInputDevice8* _keyboardDevice; | 	IDirectInputDevice8W* _keyboardDevice; | ||||||
| 	IDirectInputDevice8* _mouseDevice; | 	IDirectInputDevice8W* _mouseDevice; | ||||||
| 	DIMOUSESTATE _mouseState; | 	DIMOUSESTATE _mouseState; | ||||||
| 	DIMOUSESTATE _mouseStateRecord; | 	DIMOUSESTATE _mouseRecordState; | ||||||
| 	POINT _mousePos; | 	char _keyBuffer[256]; | ||||||
|  | 	char _keyRecordBuffer[256]; | ||||||
| 
 | 
 | ||||||
| 	static Input * _instance; | 	static Input * _instance; | ||||||
| }; | }; | ||||||
|  | @ -395,8 +396,6 @@ private: | ||||||
| // 垃圾回收器
 | // 垃圾回收器
 | ||||||
| class GC | class GC | ||||||
| { | { | ||||||
| 	friend class Game; |  | ||||||
| 
 |  | ||||||
| public: | public: | ||||||
| 	// 自动释放
 | 	// 自动释放
 | ||||||
| 	static void autorelease( | 	static void autorelease( | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue