Files
LearningOpenGL/input.txt
2025-08-15 16:12:18 -04:00

85 lines
2.2 KiB
Plaintext

#pragma once
#include <IInputComponent.h>
#include <iostream>
#include <InputEnums.h>
class Player;
class deadPlayerInputComponent : public IInputComponent
{
Player& PlayerCharacter;
float lastX = 1600 / 2.0f;
float lastY = 900 / 2.0f;
bool bFirstMouse = true;
public:
deadPlayerInputComponent(GLFWwindow* Window, Player& PlayerCharacter)
: IInputComponent(Window),
PlayerCharacter(PlayerCharacter)
{
glfwSetWindowUserPointer(Window, this);
glfwSetCursorPosCallback(Window, ProcessMouse);
}
void Update(float DeltaTime) override
{
ProcessKeyboard(DeltaTime);
}
void ProcessKeyboard(float DeltaTime)
{
if (glfwGetKey(Window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
//glfwSetWindowShouldClose(Window, true);
glfwSetInputMode(Window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
}
;
if (glfwGetKey(Window, GLFW_KEY_W) == GLFW_PRESS)
PlayerCharacter.ProcessKeyboard(FORWARD, DeltaTime);
if (glfwGetKey(Window, GLFW_KEY_S) == GLFW_PRESS)
PlayerCharacter.ProcessKeyboard(BACKWARD, DeltaTime);
if (glfwGetKey(Window, GLFW_KEY_A) == GLFW_PRESS)
PlayerCharacter.ProcessKeyboard(LEFT, DeltaTime);
if (glfwGetKey(Window, GLFW_KEY_D) == GLFW_PRESS)
PlayerCharacter.ProcessKeyboard(RIGHT, DeltaTime);
}
static void ProcessMouse(GLFWwindow* Window, double xposIn, double yposIn)
{
deadPlayerInputComponent* instance = static_cast<deadPlayerInputComponent*>(glfwGetWindowUserPointer(Window));
if (!instance)
{
std::cout << "Player Input Component not found" << std::endl;
return;
}
if (glfwGetMouseButton(Window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS)
{
glfwSetInputMode(Window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
instance->bFirstMouse = true;
return;
}
glfwSetInputMode(Window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
//std::cout << "Mouse callback" << std::endl;
float xpos = static_cast<float>(xposIn);
float ypos = static_cast<float>(yposIn);
if (instance->bFirstMouse)
{
instance->lastX = xpos;
instance->lastY = ypos;
instance->bFirstMouse = false;
}
float xoffset = xpos - instance->lastX;
float yoffset = instance->lastY - ypos;
instance->lastX = xpos;
instance->lastY = ypos;
instance->PlayerCharacter.ProcessMouseMovement(xoffset, yoffset);
}
};