summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSchark <jordan@schark.online>2023-05-12 01:31:13 -0700
committerSchark <jordan@schark.online>2023-05-12 01:31:13 -0700
commit4b23f2fcc44fc636d8d4dbe94881cf937c54199b (patch)
treede316f938ce661052de23b089760aba7c1268402 /src
parentdc8ad2d7f67e47b00ed0ecb77fae976275fc7a16 (diff)
downloadgamedev-4b23f2fcc44fc636d8d4dbe94881cf937c54199b.tar.gz
gamedev-4b23f2fcc44fc636d8d4dbe94881cf937c54199b.zip
Mouse inputs working
Diffstat (limited to 'src')
-rw-r--r--src/main.c22
-rw-r--r--src/player.c69
-rw-r--r--src/player.h6
3 files changed, 72 insertions, 25 deletions
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();
diff --git a/src/player.c b/src/player.c
index 91c4520..c06dfa4 100644
--- a/src/player.c
+++ b/src/player.c
@@ -3,7 +3,7 @@
#include "player.h"
-void move_player(Player *player, GLFWwindow* window) {
+void move_player(Player *player, GLFWwindow* window, double* mouse_x, double* mouse_y, double width, double height) {
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
player->pos_x += player->dir_x * player->move_speed;
player->pos_y += player->dir_y * player->move_speed;
@@ -12,23 +12,54 @@ void move_player(Player *player, GLFWwindow* window) {
player->pos_x -= player->dir_x * player->move_speed;
player->pos_y -= player->dir_y * player->move_speed;
}
- if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) {
- // this changes where we're casting our rays
- double old_dir_x = player->dir_x;
- player->dir_x = player->dir_x * cos(-player->rot_speed) - player->dir_y * sin(-player->rot_speed);
- player->dir_y = old_dir_x * sin(-player->rot_speed) + player->dir_y * cos(-player->rot_speed);
- // this updates perspective
- double old_plane_x = player->plane_x;
- player->plane_x = player->plane_x * cos(-player->rot_speed) - player->plane_y * sin(-player->rot_speed);
- player->plane_y = old_plane_x * sin(-player->rot_speed) + player->plane_y * cos(-player->rot_speed);
- }
- if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) {
- double old_dir_x = player->dir_x;
- player->dir_x = player->dir_x * cos(player->rot_speed) - player->dir_y * sin(player->rot_speed);
- player->dir_y = old_dir_x * sin(player->rot_speed) + player->dir_y * cos(player->rot_speed);
- double old_plane_x = player->plane_x;
- player->plane_x = player->plane_x * cos(player->rot_speed) - player->plane_y * sin(player->rot_speed);
- player->plane_y = old_plane_x * sin(player->rot_speed) + player->plane_y * cos(player->rot_speed);
- }
+
+ // old implementation
+ // if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) {
+ // // this changes where we're casting our rays
+ // double old_dir_x = player->dir_x;
+ // player->dir_x = player->dir_x * cos(-player->sensitivity) - player->dir_y * sin(-player->sensitivity);
+ // player->dir_y = old_dir_x * sin(-player->sensitivity) + player->dir_y * cos(-player->sensitivity);
+ // // this updates perspective
+ // double old_plane_x = player->plane_x;
+ // player->plane_x = player->plane_x * cos(-player->sensitivity) - player->plane_y * sin(-player->sensitivity);
+ // player->plane_y = old_plane_x * sin(-player->sensitivity) + player->plane_y * cos(-player->sensitivity);
+ // }
+ // if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) {
+ // double old_dir_x = player->dir_x;
+ // player->dir_x = player->dir_x * cos(player->sensitivity) - player->dir_y * sin(player->sensitivity);
+ // player->dir_y = old_dir_x * sin(player->sensitivity) + player->dir_y * cos(player->sensitivity);
+ // double old_plane_x = player->plane_x;
+ // player->plane_x = player->plane_x * cos(player->sensitivity) - player->plane_y * sin(player->sensitivity);
+ // player->plane_y = old_plane_x * sin(player->sensitivity) + player->plane_y * cos(player->sensitivity);
+ // }
+
+ // issues
+ //double new_mouse_x = 0, new_mouse_y = 0;
+ //glfwGetCursorPos(window, &new_mouse_x, &new_mouse_y);
+ //new_mouse_x -= width / 2;
+ //new_mouse_y -= height / 2;
+ //double dx = new_mouse_x - *mouse_x;
+ //double dy = new_mouse_y - *mouse_y;
+ //*mouse_x = new_mouse_x;
+ //*mouse_y = new_mouse_y;
+ //player->dir += dx * player->sensitivity;
+ //player->dir_x = cos(player->dir);
+ //player->dir_y = sin(player->dir);
+
+
+ double mouse_dx, mouse_dy;
+ double old_mouse_x, old_mouse_y;
+ old_mouse_x = *mouse_x;
+ old_mouse_y = *mouse_y;
+ glfwGetCursorPos(window, mouse_x, mouse_y);
+ mouse_dx = *mouse_x - old_mouse_x;
+ mouse_dy = *mouse_y - old_mouse_y;
+ double rot_speed = mouse_dx * player->sensitivity;
+ double old_dir_x = player->dir_x;
+ player->dir_x = player->dir_x * cos(rot_speed) - player->dir_y * sin(rot_speed);
+ player->dir_y = old_dir_x * sin(rot_speed) + player->dir_y * cos(rot_speed);
+ double old_plane_x = player->plane_x;
+ player->plane_x = player->plane_x * cos(rot_speed) - player->plane_y * sin(rot_speed);
+ player->plane_y = old_plane_x * sin(rot_speed) + player->plane_y * cos(rot_speed);
}
diff --git a/src/player.h b/src/player.h
index 1ee8154..e45dfd9 100644
--- a/src/player.h
+++ b/src/player.h
@@ -3,12 +3,12 @@
typedef struct Player {
float pos_x, pos_y;
- float dir_x, dir_y;
+ float dir, dir_x, dir_y;
float plane_x, plane_y;
float move_speed;
- float rot_speed;
+ float sensitivity;
} Player;
-void move_player(Player *player, GLFWwindow* window);
+void move_player(Player *player, GLFWwindow* window, double* mouse_x, double* mouse_y, double width, double height);
#endif