从零开始的openGL--cs游戏(6)Input类

Input类主要解决游戏中的输入

#ifndef Input_H
#define Input_H
enum class Key
{
    
    
	W = 0,
	A = 1,
	S = 2,
	D = 3,
	Mouse1 = 4,
	Mouse2 = 5,
	Space = 6,
	Shift = 7,
	R = 8,
};
class Input
{
    
    
private:
	bool _keys[9]{
    
     false };
	void Check(Key key, bool newValue);
public:
	static Input* Instance;
	Input();
	void Update();
	bool KeyIsDown(Key key);

};
#endif
------------------
#include "Input.h"
#include "Window.h"
#include "EventCenter.h"
Input* Input::Instance = nullptr;
Input::Input()
{
    
    
	Input::Instance = this;
}

void Input::Update()
{
    
    
	if (glfwGetKey(Window::Instance->window_ptr, GLFW_KEY_ESCAPE))
	{
    
    
		glfwSetWindowShouldClose(Window::Instance->window_ptr, true);
	}
	Check(Key::A, glfwGetKey(Window::Instance->window_ptr, GLFW_KEY_A));
	Check(Key::W, glfwGetKey(Window::Instance->window_ptr, GLFW_KEY_W));
	Check(Key::S, glfwGetKey(Window::Instance->window_ptr, GLFW_KEY_S));
	Check(Key::D, glfwGetKey(Window::Instance->window_ptr, GLFW_KEY_D));
	Check(Key::Mouse1, glfwGetKey(Window::Instance->window_ptr, GLFW_MOUSE_BUTTON_LEFT));
	Check(Key::Mouse2, glfwGetKey(Window::Instance->window_ptr, GLFW_MOUSE_BUTTON_RIGHT));
	Check(Key::Space, glfwGetKey(Window::Instance->window_ptr, GLFW_KEY_SPACE));
	Check(Key::Shift, glfwGetKey(Window::Instance->window_ptr, GLFW_KEY_RIGHT_SHIFT));
}

bool Input::KeyIsDown(Key key)
{
    
    
	return _keys[(int)key];
}

void Input::Check(Key key, bool newValue)
{
    
    
	if (_keys[(int)key] != newValue)
	{
    
    
		if (newValue)
			EventCenter::Instance->Invoke<Key>("KeyDown", key);
		else
			EventCenter::Instance->Invoke<Key>("KeyUp", key);
		_keys[(int)key] = newValue;
	}}


猜你喜欢

转载自blog.csdn.net/qq_41041725/article/details/121903897
今日推荐