From 4b23f2fcc44fc636d8d4dbe94881cf937c54199b Mon Sep 17 00:00:00 2001 From: Schark Date: Fri, 12 May 2023 01:31:13 -0700 Subject: Mouse inputs working --- src/main.c | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) (limited to 'src/main.c') diff --git a/src/main.c b/src/main.c index 0ea84d4..34a4d0e 100644 --- a/src/main.c +++ b/src/main.c @@ -154,6 +154,8 @@ void raycast(Player *player, int width, int height) { int main(int argc, char *argv[]) { GLFWwindow* window; + double mouse_x; + double mouse_y; // init player Player player; @@ -163,8 +165,8 @@ int main(int argc, char *argv[]) { player.dir_y = 0.0f; // facing right player.plane_x = 0.0f; // FOV related player.plane_y = 0.66f; // FOV related - player.move_speed = 0.05f; - player.rot_speed = 0.1f; + player.move_speed = 0.02f; + player.sensitivity = 0.001f; // initiate glfw library if (!glfwInit()){ @@ -185,6 +187,12 @@ int main(int argc, char *argv[]) { // make window current glfwMakeContextCurrent(window); + glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); + + // update mouse position + // glfwGetCursorPos(window, &mouse_x, &mouse_y); + mouse_x -= mode->width / 2; + mouse_y -= mode->height / 2; // setup orthographic projection camera glMatrixMode(GL_PROJECTION); @@ -200,6 +208,11 @@ int main(int argc, char *argv[]) { while (!glfwWindowShouldClose(window)) { + // Stop loop if window isn't focused + if(!glfwGetWindowAttrib(window, GLFW_FOCUSED)){ + continue; + } + // timing section double current_time = glfwGetTime(); nb_frames++; @@ -208,15 +221,18 @@ int main(int argc, char *argv[]) { glfwSetWindowTitle(window, title); nb_frames = 0; last_time += 1.0; + } else if (current_time - last_time >= 0.7) { + //glfwSetCursorPos(window, mode->width / 2, mode->height / 2); } // clear window glClear(GL_COLOR_BUFFER_BIT); glLoadIdentity(); + // main raycast function + move_player(&player, window, &mouse_x, &mouse_y, mode->width, mode->height); raycast(&player, mode->width, mode->height); - move_player(&player, window); glfwSwapBuffers(window); glfwPollEvents(); -- cgit v1.2.3-18-g5258