From 495ba620af3f3fd376bf3c2928988aaa26d0c3b4 Mon Sep 17 00:00:00 2001 From: djonker Date: Thu, 2 Nov 2023 04:30:19 +0100 Subject: [PATCH] added vertical aiming --- Makefile | 4 ++-- Readme.md | 2 +- cub3d.h | 3 ++- src/init.c | 4 ++-- src/keys.c | 40 ++++++++++++++++++++++------------------ src/main.c | 5 +++-- src/map.c | 5 +++-- 7 files changed, 35 insertions(+), 28 deletions(-) diff --git a/Makefile b/Makefile index 89ec55e..a87f384 100644 --- a/Makefile +++ b/Makefile @@ -6,13 +6,13 @@ # By: houtworm +#+ # # +#+ # # Created: 2023/10/26 10:46:29 by houtworm #+# #+# # -# Updated: 2023/11/01 14:59:05 by houtworm ######## odam.nl # +# Updated: 2023/11/02 04:04:36 by houtworm ######## odam.nl # # # # **************************************************************************** # NAME =cub3d CC =gcc -FC =-Wall -Werror -Wextra -Wunreachable-code -Ofast -g -fsanitize=address +FC =-Wall -Werror -Wextra -Wunreachable-code -Ofast -g #-fsanitize=address HEAD =-I ./include -I $(MLX)/include RM =rm -rf LIB =libft/libft.a getnextline/get_next_line.a mlx/build/libmlx42.a -ldl -lglfw -pthread -lm diff --git a/Readme.md b/Readme.md index abd792c..751b63b 100644 --- a/Readme.md +++ b/Readme.md @@ -25,7 +25,6 @@ Cub3D is a simple raycasting game using the mlx library - A Start Menu? - Jumping? - Crouching? -- Vertical Aiming? --- ## Features @@ -46,6 +45,7 @@ Cub3D is a simple raycasting game using the mlx library - Player can walk in 8 directions - Player can Run - Map sizes up to 1 Megabyte (1000x1000) +- Vertical Aiming --- ## Bugs diff --git a/cub3d.h b/cub3d.h index a17c6fb..4f17603 100644 --- a/cub3d.h +++ b/cub3d.h @@ -6,7 +6,7 @@ /* By: houtworm +#+ */ /* +#+ */ /* Created: 2023/10/26 10:46:35 by houtworm #+# #+# */ -/* Updated: 2023/11/01 15:42:29 by houtworm ######## odam.nl */ +/* Updated: 2023/11/02 04:18:39 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -64,6 +64,7 @@ typedef struct s_varlist int side; double run; double oldmouseposx; + double oldmouseposy; int32_t fcolor; int32_t ccolor; int resize; diff --git a/src/init.c b/src/init.c index 1b3be38..c105160 100644 --- a/src/init.c +++ b/src/init.c @@ -6,7 +6,7 @@ /* By: houtworm +#+ */ /* +#+ */ /* Created: 2023/10/26 16:49:12 by houtworm #+# #+# */ -/* Updated: 2023/11/01 21:41:55 by houtworm ######## odam.nl */ +/* Updated: 2023/11/02 04:28:53 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -18,7 +18,7 @@ t_varlist initvarlist(void) vl.w = 800; vl.h = 600; - vl.hoffset = 50; + vl.hoffset = 0; vl.ccolor = 0; vl.fcolor = 0; vl.northtext = NULL; diff --git a/src/keys.c b/src/keys.c index 1c3f82b..f07a6ac 100644 --- a/src/keys.c +++ b/src/keys.c @@ -6,7 +6,7 @@ /* By: houtworm +#+ */ /* +#+ */ /* Created: 2023/10/26 16:50:23 by houtworm #+# #+# */ -/* Updated: 2023/11/02 02:21:54 by houtworm ######## odam.nl */ +/* Updated: 2023/11/02 04:25:38 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -14,8 +14,7 @@ void ft_movementkeys(t_varlist *vl) { - double olddirx; - double oldplanex; + double temp; double distance; if (vl->side) @@ -27,9 +26,7 @@ void ft_movementkeys(t_varlist *vl) if (mlx_is_key_down(vl->mlx, MLX_KEY_SPACE)) ft_putendl("jump"); if (mlx_is_key_down(vl->mlx, MLX_KEY_LEFT_CONTROL)) - vl->hoffset = 0; - else - vl->hoffset = 50; + ft_putendl("crouch"); if (mlx_is_key_down(vl->mlx, MLX_KEY_W)) { if (vl->map[(int)(vl->posx + vl->dirx * vl->movespeed)][(int)vl->posy] == '0' && distance > 0.4) @@ -84,21 +81,21 @@ void ft_movementkeys(t_varlist *vl) } if (mlx_is_key_down(vl->mlx, MLX_KEY_LEFT)) { - olddirx = vl->dirx; - vl->dirx = vl->dirx * cos(vl->rotspeed) - vl->diry * sin(vl->rotspeed); - vl->diry = olddirx * sin(vl->rotspeed) + vl->diry * cos(vl->rotspeed); - oldplanex = vl->planex; - vl->planex = vl->planex * cos(vl->rotspeed) - vl->planey * sin(vl->rotspeed); - vl->planey = oldplanex * sin(vl->rotspeed) + vl->planey * cos(vl->rotspeed); + temp = vl->dirx; + vl->dirx = temp * cos(vl->rotspeed) - vl->diry * sin(vl->rotspeed); + vl->diry = temp * sin(vl->rotspeed) + vl->diry * cos(vl->rotspeed); + temp = vl->planex; + vl->planex = temp * cos(vl->rotspeed) - vl->planey * sin(vl->rotspeed); + vl->planey = temp * sin(vl->rotspeed) + vl->planey * cos(vl->rotspeed); } if (mlx_is_key_down(vl->mlx, MLX_KEY_RIGHT)) { - olddirx = vl->dirx; - vl->dirx = vl->dirx * cos(-vl->rotspeed) - vl->diry * sin(-vl->rotspeed); - vl->diry = olddirx * sin(-vl->rotspeed) + vl->diry * cos(-vl->rotspeed); - oldplanex = vl->planex; - vl->planex = vl->planex * cos(-vl->rotspeed) - vl->planey * sin(-vl->rotspeed); - vl->planey = oldplanex * sin(-vl->rotspeed) + vl->planey * cos(-vl->rotspeed); + temp = vl->dirx; + vl->dirx = temp * cos(-vl->rotspeed) - vl->diry * sin(-vl->rotspeed); + vl->diry = temp * sin(-vl->rotspeed) + vl->diry * cos(-vl->rotspeed); + temp = vl->planex; + vl->planex = temp * cos(-vl->rotspeed) - vl->planey * sin(-vl->rotspeed); + vl->planey = temp * sin(-vl->rotspeed) + vl->planey * cos(-vl->rotspeed); } if (mlx_is_key_down(vl->mlx, MLX_KEY_LEFT_SHIFT)) vl->run = 2; @@ -165,6 +162,13 @@ void cursorhook(double xpos, double ypos, void *param) vl->planey = oldplanex * sin(-speed) + vl->planey * cos(-speed); } vl->oldmouseposx = xpos; + if (ypos < vl->oldmouseposy) + if (vl->hoffset < 300) + vl->hoffset = vl->hoffset + 3; + if (ypos > vl->oldmouseposy) + if (vl->hoffset > -300) + vl->hoffset = vl->hoffset - 3; + vl->oldmouseposy = ypos; } void resizehook(int x, int y, void *param) diff --git a/src/main.c b/src/main.c index 44f8111..49514e0 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: houtworm +#+ */ /* +#+ */ /* Created: 2023/10/26 14:13:07 by houtworm #+# #+# */ -/* Updated: 2023/11/01 15:42:14 by houtworm ######## odam.nl */ +/* Updated: 2023/11/02 03:57:40 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -20,7 +20,6 @@ void ft_frametime(t_varlist *vl) vl->frametime = vl->mlx->delta_time; vl->movespeed = vl->frametime * 3.0; vl->rotspeed = vl->frametime * 3.0; - mlx_delete_image(vl->mlx, vl->fps); itoa = ft_itoa(1 / vl->frametime); print = ft_strjoin(itoa, " FPS"); vl->fps = mlx_put_string(vl->mlx, print, 10, 10); @@ -34,6 +33,7 @@ void mainloop(void *param) vl = param; mlx_delete_image(vl->mlx, vl->img); + mlx_delete_image(vl->mlx, vl->fps); vl->img = mlx_new_image(vl->mlx, vl->w, vl->h); ft_raycast(vl); ft_frametime(vl); @@ -68,6 +68,7 @@ int main(int argc, char **argv) mlx_delete_texture(vl.southtext); mlx_delete_texture(vl.westtext); mlx_delete_texture(vl.barreltext); + mlx_delete_image(vl.mlx, vl.fps); mlx_delete_image(vl.mlx, vl.img); mlx_terminate(vl.mlx); exit (0); diff --git a/src/map.c b/src/map.c index 0d68bac..41459f6 100644 --- a/src/map.c +++ b/src/map.c @@ -6,7 +6,7 @@ /* By: houtworm +#+ */ /* +#+ */ /* Created: 2023/10/26 17:33:50 by houtworm #+# #+# */ -/* Updated: 2023/10/29 19:30:54 by houtworm ######## odam.nl */ +/* Updated: 2023/11/02 03:56:15 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -69,11 +69,12 @@ char **ft_getmap(t_varlist *vl, int fd) ret = get_next_line(fd, &line); if (line[0]) break ; + free(line); } if (ret == 0) { free(map[y]); - free(line); + /*free(line);*/ return (map); } x = 0;