feat(close #16): Implement Input class

This commit is contained in:
Huseyn Ismayilov 2024-08-28 16:38:19 +04:00
parent 08c63425a3
commit 0a1e015800
7 changed files with 220 additions and 25 deletions

17
engine/include/Input.h Normal file
View File

@ -0,0 +1,17 @@
#pragma once
#include <glm/glm.hpp>
#include "KeyCodes.h"
#include "MouseCodes.h"
class Input
{
public:
static bool IsKeyPressed(KeyCode key);
static bool IsMouseButtonPressed(MouseCode button);
static glm::vec2 GetMousePosition();
static float GetMouseX();
static float GetMouseY();
};

135
engine/include/KeyCodes.h Normal file
View File

@ -0,0 +1,135 @@
#pragma once
enum KeyCode{
// From glfw3.h
Space = 32,
Apostrophe = 39, /* ' */
Comma = 44, /* , */
Minus = 45, /* - */
Period = 46, /* . */
Slash = 47, /* / */
D0 = 48, /* 0 */
D1 = 49, /* 1 */
D2 = 50, /* 2 */
D3 = 51, /* 3 */
D4 = 52, /* 4 */
D5 = 53, /* 5 */
D6 = 54, /* 6 */
D7 = 55, /* 7 */
D8 = 56, /* 8 */
D9 = 57, /* 9 */
Semicolon = 59, /* ; */
Equal = 61, /* = */
A = 65,
B = 66,
C = 67,
D = 68,
E = 69,
F = 70,
G = 71,
H = 72,
I = 73,
J = 74,
K = 75,
L = 76,
M = 77,
N = 78,
O = 79,
P = 80,
Q = 81,
R = 82,
S = 83,
T = 84,
U = 85,
V = 86,
W = 87,
X = 88,
Y = 89,
Z = 90,
LeftBracket = 91, /* [ */
Backslash = 92, /* \ */
RightBracket = 93, /* ] */
GraveAccent = 96, /* ` */
World1 = 161, /* non-US #1 */
World2 = 162, /* non-US #2 */
/* Function keys */
Escape = 256,
Enter = 257,
Tab = 258,
Backspace = 259,
Insert = 260,
Delete = 261,
Right = 262,
Left = 263,
Down = 264,
Up = 265,
PageUp = 266,
PageDown = 267,
Home = 268,
End = 269,
CapsLock = 280,
ScrollLock = 281,
NumLock = 282,
PrintScreen = 283,
Pause = 284,
F1 = 290,
F2 = 291,
F3 = 292,
F4 = 293,
F5 = 294,
F6 = 295,
F7 = 296,
F8 = 297,
F9 = 298,
F10 = 299,
F11 = 300,
F12 = 301,
F13 = 302,
F14 = 303,
F15 = 304,
F16 = 305,
F17 = 306,
F18 = 307,
F19 = 308,
F20 = 309,
F21 = 310,
F22 = 311,
F23 = 312,
F24 = 313,
F25 = 314,
/* Keypad */
KP0 = 320,
KP1 = 321,
KP2 = 322,
KP3 = 323,
KP4 = 324,
KP5 = 325,
KP6 = 326,
KP7 = 327,
KP8 = 328,
KP9 = 329,
KPDecimal = 330,
KPDivide = 331,
KPMultiply = 332,
KPSubtract = 333,
KPAdd = 334,
KPEnter = 335,
KPEqual = 336,
LeftShift = 340,
LeftControl = 341,
LeftAlt = 342,
LeftSuper = 343,
RightShift = 344,
RightControl = 345,
RightAlt = 346,
RightSuper = 347,
Menu = 348
};

View File

@ -0,0 +1,18 @@
#pragma once
enum MouseCode{
// From glfw3.h
Button0 = 0,
Button1 = 1,
Button2 = 2,
Button3 = 3,
Button4 = 4,
Button5 = 5,
Button6 = 6,
Button7 = 7,
ButtonLast = Button7,
ButtonLeft = Button0,
ButtonRight = Button1,
ButtonMiddle = Button2
};

View File

@ -8,6 +8,7 @@
#include "IndexBuffer.h"
#include "FrameBuffer.h"
#include "Window.h"
#include "Input.h"
#include "Shader.h"
#include "Texture.h"
#include "UI.h"

View File

@ -26,6 +26,6 @@ public:
int GetUniformLocation(const std::string& name);
private:
unsigned int m_ID;
unsigned int m_ID{};
std::unordered_map<std::string, int> m_Uniforms{};
};

30
engine/src/Input.cpp Normal file
View File

@ -0,0 +1,30 @@
#include "Engine.h"
#include "Input.h"
bool Input::IsKeyPressed(const KeyCode key)
{
return glfwGetKey(Engine::Get().GetWindow().GetWindow(), key) == GLFW_PRESS;
}
bool Input::IsMouseButtonPressed(const MouseCode button)
{
return glfwGetMouseButton(Engine::Get().GetWindow().GetWindow(), button) == GLFW_PRESS;
}
glm::vec2 Input::GetMousePosition()
{
double xPos, yPos;
glfwGetCursorPos(Engine::Get().GetWindow().GetWindow(), &xPos, &yPos);
return { static_cast<float>(xPos), static_cast<float>(yPos) };
}
float Input::GetMouseX()
{
return GetMousePosition().x;
}
float Input::GetMouseY()
{
return GetMousePosition().y;
}

View File

@ -1,4 +1,3 @@
#include <glad/glad.h>
#include "Renderer.h"
#include "Engine.h"
@ -126,10 +125,10 @@ void Renderer::Render() {
s_Data.m_Shader->Use();
auto model = glm::mat4(1.0f);
model = glm::translate(model, UI::GetData().m_Position);
if(glm::length(UI::GetData().m_Rotation) != 0)
model = glm::rotate(model, glm::radians(length(UI::GetData().m_Rotation)), normalize(UI::GetData().m_Rotation));
model = glm::scale(model, UI::GetData().m_Scale);
model = translate(model, UI::GetData().m_Position);
if(length(UI::GetData().m_Rotation) != 0)
model = rotate(model, glm::radians(length(UI::GetData().m_Rotation)), normalize(UI::GetData().m_Rotation));
model = scale(model, UI::GetData().m_Scale);
glm::mat4 view = s_Data.m_Camera->GetViewMatrix();
WindowSize size = Engine::Get().GetWindow().GetSize();
@ -153,26 +152,22 @@ void Renderer::Render() {
void Renderer::ProcessInput(GLFWwindow *window)
{
if(glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_2) == GLFW_PRESS)
if(Input::IsMouseButtonPressed(ButtonRight))
{
double xPosIn, yPosIn;
glfwGetCursorPos(window, &xPosIn, &yPosIn);
auto xPos = static_cast<float>(xPosIn);
auto yPos = static_cast<float>(yPosIn);
auto mousePos = Input::GetMousePosition();
if(firstMouse)
{
lastX = xPos;
lastY = yPos;
lastX = mousePos.x;
lastY = mousePos.y;
firstMouse = false;
}
float xOffset = xPos - lastX;
float yOffset = lastY - yPos;
float xOffset = mousePos.x - lastX;
float yOffset = lastY - mousePos.y;
lastX = xPos;
lastY = yPos;
lastX = mousePos.x;
lastY = mousePos.y;
s_Data.m_Camera->ProcessMouseMovement(xOffset, yOffset);
@ -183,19 +178,18 @@ void Renderer::ProcessInput(GLFWwindow *window)
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
}
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
if (Input::IsKeyPressed(W))
s_Data.m_Camera->ProcessKeyboard(FORWARD, deltaTime);
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
if (Input::IsKeyPressed(S))
s_Data.m_Camera->ProcessKeyboard(BACKWARD, deltaTime);
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
if (Input::IsKeyPressed(A))
s_Data.m_Camera->ProcessKeyboard(LEFT, deltaTime);
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
if (Input::IsKeyPressed(D))
s_Data.m_Camera->ProcessKeyboard(RIGHT, deltaTime);
if (glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS)
if (Input::IsKeyPressed(Q))
s_Data.m_Camera->ProcessKeyboard(DOWN, deltaTime);
if (glfwGetKey(window, GLFW_KEY_E) == GLFW_PRESS)
if (Input::IsKeyPressed(E))
s_Data.m_Camera->ProcessKeyboard(UP, deltaTime);
}
void Renderer::Shutdown()