Skip to content

Commit e6b323a

Browse files
feat: lock mouse as soon as game starts (#15)
Co-authored-by: AhmedAmrNabil <ahmedamr24680@gmail.com>
1 parent 8e0b4a6 commit e6b323a

2 files changed

Lines changed: 9 additions & 13 deletions

File tree

src/common/input/mouse.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ namespace our {
9999
// Locks the mouse position and hides it (Usually used for FPS games)
100100
static void lockMouse(GLFWwindow* window) {
101101
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
102+
glfwSetCursor(
103+
window,
104+
nullptr); // This is needed to prevent a GLFW bug where the cursor is visible again after locking it.
102105
}
103106
// If the mouse was locked, unlock it (make it visible and allow it to move)
104107
static void unlockMouse(GLFWwindow* window) {

src/common/systems/free-camera-controller.hpp

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ namespace our {
2323
// When a state enters, it should call this function and give it the pointer to the application
2424
void enter(Application* app) {
2525
this->app = app;
26+
app->getMouse().lockMouse(app->getWindow());
27+
app->getMouse().enable(app->getWindow());
28+
mouse_locked = true;
2629
}
2730

2831
// This should be called every frame to update all entities containing a FreeCameraControllerComponent
@@ -42,23 +45,12 @@ namespace our {
4245
// Get the entity that we found via getOwner of camera (we could use controller->getOwner())
4346
Entity* entity = camera->getOwner();
4447

45-
// If the left mouse button is pressed, we lock and hide the mouse. This common in First Person Games.
46-
if (app->getMouse().isPressed(GLFW_MOUSE_BUTTON_1) && !mouse_locked) {
47-
app->getMouse().lockMouse(app->getWindow());
48-
mouse_locked = true;
49-
// If the left mouse button is released, we unlock and unhide the mouse.
50-
} else if (!app->getMouse().isPressed(GLFW_MOUSE_BUTTON_1) && mouse_locked) {
51-
app->getMouse().unlockMouse(app->getWindow());
52-
mouse_locked = false;
53-
}
54-
5548
// We get a reference to the entity's position and rotation
5649
glm::vec3& position = entity->localTransform.position;
5750
glm::vec3& rotation = entity->localTransform.rotation;
5851

59-
// If the left mouse button is pressed, we get the change in the mouse location
60-
// and use it to update the camera rotation
61-
if (app->getMouse().isPressed(GLFW_MOUSE_BUTTON_1)) {
52+
// While the play state is active, the mouse stays locked
53+
if (mouse_locked) {
6254
glm::vec2 delta = app->getMouse().getMouseDelta();
6355
rotation.x -= delta.y * controller->rotationSensitivity; // The y-axis controls the pitch
6456
rotation.y -= delta.x * controller->rotationSensitivity; // The x-axis controls the yaw
@@ -105,6 +97,7 @@ namespace our {
10597
if (mouse_locked) {
10698
mouse_locked = false;
10799
app->getMouse().unlockMouse(app->getWindow());
100+
app->getMouse().enable(app->getWindow());
108101
}
109102
}
110103
};

0 commit comments

Comments
 (0)