From 0148ee755e917ca2d66f1f6290ebcd304ea1be8a Mon Sep 17 00:00:00 2001 From: djonker Date: Sun, 29 Oct 2023 15:59:24 +0100 Subject: [PATCH] player can run now:) --- Makefile | 4 ++-- config.cub | 12 ++++++------ cub3d.h | 3 ++- src/init.c | 4 +--- src/keys.c | 38 +++++++++++++++++++++----------------- src/main.c | 4 ++-- src/map.c | 23 ++++++++--------------- src/parse.c | 5 ++++- 8 files changed, 46 insertions(+), 47 deletions(-) diff --git a/Makefile b/Makefile index 38f504b..50b48e2 100644 --- a/Makefile +++ b/Makefile @@ -6,13 +6,13 @@ # By: houtworm +#+ # # +#+ # # Created: 2023/10/26 10:46:29 by houtworm #+# #+# # -# Updated: 2023/10/29 06:31:28 by houtworm ######## odam.nl # +# Updated: 2023/10/29 15:24:58 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/config.cub b/config.cub index 18eb5e4..de42012 100644 --- a/config.cub +++ b/config.cub @@ -1,10 +1,10 @@ C 0,100,255 F 100,100,100 -NO ./path_to_the_north_texture -EA ./path_to_the_east_texture -SO ./path_to_the_south_texture -WE ./path_to_the_west_texture +NO ./assets/bluestone.png +EA ./assets/colorstone.png +SO ./assets/redbrick.png +WE ./assets/wood.png 111111111111111111111111 100000000000000000000001 @@ -26,7 +26,7 @@ WE ./path_to_the_west_texture 110100001000000000000001 110000101000000000000001 110100001000000000000001 -110111111000000000000001 +11011111100000000000N001 110000000000000000000001 -1111111110000000000000N1 +111111111000000000000001 111111111111111111111111 diff --git a/cub3d.h b/cub3d.h index cf2872f..c1b1f1e 100644 --- a/cub3d.h +++ b/cub3d.h @@ -6,7 +6,7 @@ /* By: houtworm +#+ */ /* +#+ */ /* Created: 2023/10/26 10:46:35 by houtworm #+# #+# */ -/* Updated: 2023/10/29 13:46:54 by houtworm ######## odam.nl */ +/* Updated: 2023/10/29 15:54:07 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -51,6 +51,7 @@ typedef struct s_varlist int stepy; int hit; int side; + double run; double oldmouseposx; double oldmouseposy; // only needed if we do vertical aiming int32_t fcolor; diff --git a/src/init.c b/src/init.c index 4197ada..a758e26 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/10/29 13:52:03 by houtworm ######## odam.nl */ +/* Updated: 2023/10/29 15:23:35 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -24,8 +24,6 @@ t_varlist initvarlist(void) vl.eastwt = NULL; vl.southwt = NULL; vl.westwt = NULL; - vl.planex = 0; - vl.planey = 0.66; vl.mlx = mlx_init(vl.w, vl.h, "Cub3D", true); if (!vl.mlx) ft_errorexit("MLX failed to init", "initvarlist", 1); diff --git a/src/keys.c b/src/keys.c index 9cb12ea..dce23fb 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/10/29 14:46:05 by houtworm ######## odam.nl */ +/* Updated: 2023/10/29 15:55:18 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -23,52 +23,52 @@ void ft_movementkeys(t_varlist *vl) { if (vl->map[(int)(vl->posx + vl->dirx * vl->movespeed)][(int)vl->posy] == '0') { - vl->posx += vl->dirx * vl->movespeed; - vl->posy += vl->diry * vl->movespeed; + vl->posx += vl->dirx * vl->movespeed * vl->run; + vl->posy += vl->diry * vl->movespeed * vl->run; } if (vl->map[(int)vl->posx][(int)(vl->posy + vl->diry * vl->movespeed)] == '0') { - vl->posx += vl->dirx * vl->movespeed; - vl->posy += vl->diry * vl->movespeed; + vl->posx += vl->dirx * vl->movespeed * vl->run; + vl->posy += vl->diry * vl->movespeed * vl->run; } } if (mlx_is_key_down(vl->mlx, MLX_KEY_A)) { if (vl->map[(int)(vl->posx - vl->dirx * vl->movespeed)][(int)vl->posy] == '0') { - vl->posx -= vl->diry * vl->movespeed; - vl->posy += vl->dirx * vl->movespeed; + vl->posx -= vl->diry * vl->movespeed * vl->run; + vl->posy += vl->dirx * vl->movespeed * vl->run; } if (vl->map[(int)vl->posx][(int)(vl->posy - vl->diry * vl->movespeed)] == '0') { - vl->posx -= vl->diry * vl->movespeed; - vl->posy += vl->dirx * vl->movespeed; + vl->posx -= vl->diry * vl->movespeed * vl->run; + vl->posy += vl->dirx * vl->movespeed * vl->run; } } if (mlx_is_key_down(vl->mlx, MLX_KEY_S)) { if (vl->map[(int)(vl->posx - vl->dirx * vl->movespeed)][(int)vl->posy] == '0') { - vl->posx -= vl->dirx * vl->movespeed; - vl->posy -= vl->diry * vl->movespeed; + vl->posx -= vl->dirx * vl->movespeed * vl->run; + vl->posy -= vl->diry * vl->movespeed * vl->run; } if (vl->map[(int)vl->posx][(int)(vl->posy - vl->diry * vl->movespeed)] == '0') { - vl->posx -= vl->dirx * vl->movespeed; - vl->posy -= vl->diry * vl->movespeed; + vl->posx -= vl->dirx * vl->movespeed * vl->run; + vl->posy -= vl->diry * vl->movespeed * vl->run; } } if (mlx_is_key_down(vl->mlx, MLX_KEY_D)) { if (vl->map[(int)(vl->posx + vl->dirx * vl->movespeed)][(int)vl->posy] == '0') { - vl->posx += vl->diry * vl->movespeed; - vl->posy -= vl->dirx * vl->movespeed; + vl->posx += vl->diry * vl->movespeed * vl->run; + vl->posy -= vl->dirx * vl->movespeed * vl->run; } if (vl->map[(int)vl->posx][(int)(vl->posy + vl->diry * vl->movespeed)] == '0') { - vl->posx += vl->diry * vl->movespeed; - vl->posy -= vl->dirx * vl->movespeed; + vl->posx += vl->diry * vl->movespeed * vl->run; + vl->posy -= vl->dirx * vl->movespeed * vl->run; } } if (mlx_is_key_down(vl->mlx, MLX_KEY_LEFT)) @@ -105,6 +105,10 @@ void keyhook(mlx_key_data_t kd, void *param) } if (kd.key == MLX_KEY_H && kd.action == MLX_PRESS) ft_putendl("H is pressed"); + if (kd.modifier == MLX_SHIFT) + vl->run = 2; + else + vl->run = 1; } void scrollhook(double xdelta, double ydelta, void *param) diff --git a/src/main.c b/src/main.c index 289bd46..6d9d594 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/10/29 12:26:20 by houtworm ######## odam.nl */ +/* Updated: 2023/10/29 15:56:15 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -17,7 +17,7 @@ void ft_frametime(t_varlist *vl) vl->frametime = vl->mlx->delta_time; ft_putnbr(1 / vl->frametime); ft_putendl(" FPS"); - vl->movespeed = vl->frametime * 5.0; + vl->movespeed = vl->frametime * 3.0; vl->rotspeed = vl->frametime * 3.0; } diff --git a/src/map.c b/src/map.c index 49279d4..22ab0d2 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 14:59:47 by houtworm ######## odam.nl */ +/* Updated: 2023/10/29 15:23:13 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -61,6 +61,8 @@ char **ft_getmap(t_varlist *vl, int fd) ret = 1; while (y <= 512 && ret > 0) { + if (y > 500) + return (NULL); map[y] = ft_calloc(512, 8); while (ret > 0) { @@ -70,22 +72,15 @@ char **ft_getmap(t_varlist *vl, int fd) } if (ret == 0) { - map[y] = NULL; - int i; - i = 0; - while (i < y) - { - printf("%s\n", map[i]); - i++; - } + free(map[y]); free(line); - printf("xpos: %lf ypos %lf xdir %lf ydir %lf\n\n", vl->posy, vl->posx, vl->dirx, vl->diry); return (map); } - printf("line: %s\n", line); x = 0; while (x <= 512 && line[x]) { + if (x > 500) + return (NULL); if (ft_strchr(" 0", line[x])) map[y][x] = '0'; else if (ft_strchr("1", line[x])) @@ -97,8 +92,6 @@ char **ft_getmap(t_varlist *vl, int fd) free(line); y++; } - free(line); - if (x > 500 || y > 500) - return (NULL); - return (map); + ft_errorexit("Something went wrong", "ft_getmap", 1); + return (0); } diff --git a/src/parse.c b/src/parse.c index 655988b..0f3d1be 100644 --- a/src/parse.c +++ b/src/parse.c @@ -6,7 +6,7 @@ /* By: houtworm +#+ */ /* +#+ */ /* Created: 2023/10/26 16:48:55 by houtworm #+# #+# */ -/* Updated: 2023/10/29 14:33:27 by houtworm ######## odam.nl */ +/* Updated: 2023/10/29 15:14:40 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -129,7 +129,10 @@ t_varlist ft_parseconfigfile(t_varlist vl, char *filename) if (error) ft_errorexit("Double config declaration in .cub file", error, 1); if (vl.ccolor && vl.fcolor && vl.northwt && vl.eastwt && vl.southwt && vl.westwt) + { vl.map = ft_getmap(&vl, fd); + break ; + } free(line); } close(fd);