From 407d6b025aaa81065a26c2f6d4ef4900a51fd54e Mon Sep 17 00:00:00 2001 From: narnaud Date: Tue, 31 May 2022 09:16:38 +0200 Subject: [PATCH] add: can move player with arrows and see it on minimap --- Makefile | 2 +- includes/cub3d.h | 25 +++++++++------ sources/{utils => }/getline.c | 2 +- sources/main.c | 56 +++++++++++++++++++++++--------- sources/parsing.c | 60 ++++++++++++----------------------- sources/utils.c | 51 +++++++++++++++++++++++++++++ 6 files changed, 131 insertions(+), 65 deletions(-) rename sources/{utils => }/getline.c (98%) create mode 100644 sources/utils.c diff --git a/Makefile b/Makefile index b52147d..d74e266 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ NAME = cub3d LIBFT = libft.a -SRCS = sources/main.c sources/parsing.c sources/utils/getline.c +SRCS = sources/main.c sources/parsing.c sources/getline.c sources/utils.c OBJS = ${SRCS:.c=.o} CC = gcc diff --git a/includes/cub3d.h b/includes/cub3d.h index 77300e7..94e82de 100644 --- a/includes/cub3d.h +++ b/includes/cub3d.h @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/21 19:24:12 by narnaud #+# #+# */ -/* Updated: 2022/05/30 18:02:08 by narnaud ### ########.fr */ +/* Updated: 2022/05/31 09:01:49 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -46,14 +46,14 @@ typedef struct s_vector int y; } t_vec; +typedef struct s_vector_d +{ + double x; + double y; +} t_vec_d; + typedef struct s_env { - int wide; - int deep; - char *wallTexture[4]; - int floorColor; - int ceilColor; - char **map; void *mlx; void *win; void *img; @@ -61,11 +61,18 @@ typedef struct s_env int line_bytes; int endian; int *buffer; - t_vec playerPos; - int yaw; + char *wallTexture[4]; + int floorColor; + int ceilColor; + char **map; + int wide; + int deep; + t_vec_d playerPos; + t_vec_d playerDir; } t_env; int rgb_to_int(char **rgb); +void set_vec(t_vec_d *vec, int x, int y); t_env *parse_envFile(char *filename); char *get_next_line(int fd); diff --git a/sources/utils/getline.c b/sources/getline.c similarity index 98% rename from sources/utils/getline.c rename to sources/getline.c index c3f926e..e1ac626 100644 --- a/sources/utils/getline.c +++ b/sources/getline.c @@ -9,7 +9,7 @@ /* */ /* ************************************************************************** */ -#include "../../includes/cub3d.h" +#include "../includes/cub3d.h" char *save_buffer(char *buffer, int *i) { diff --git a/sources/main.c b/sources/main.c index 783bcb3..7855bc1 100644 --- a/sources/main.c +++ b/sources/main.c @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/21 19:22:50 by narnaud #+# #+# */ -/* Updated: 2022/05/31 07:54:29 by narnaud ### ########.fr */ +/* Updated: 2022/05/31 09:13:28 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,11 +26,6 @@ void init_window(t_env *env) //exit(error_msg("Mlx fail to create window", 1)); env->win = window; } -/* -void launch_game(){ - -} -*/ void draw_square(t_env *env, t_vec vec, int size, int color) { @@ -70,7 +65,45 @@ void render_minimap(t_env *env) } vec.y++; } - draw_square(env, env->playerPos, 6, 255); + vec.x = round(env->playerPos.x); + vec.y = round(env->playerPos.y); + draw_square(env, vec, 6, 255); +} + +void render_view(t_env *env) +{ + (void)env; +} + +int key_hook_primary(int keycode, void *param) +{ + t_env *env; + + env = (t_env *)param; + env->img = mlx_new_image(env->mlx, WIN_X_SZ, WIN_Y_SZ); + env->buffer = (int *)mlx_get_data_addr(env->img, \ + &env->pixel_bits, &env->line_bytes, &env->endian); + env->line_bytes /= 4; + if (keycode == KEY_ARROW_UP) + set_vec(&env->playerPos, env->playerPos.x + env->playerDir.x, env->playerPos.y + env->playerDir.y); + else if (keycode == KEY_ARROW_DOWN) + set_vec(&env->playerPos, env->playerPos.x - env->playerDir.x, env->playerPos.y - env->playerDir.y); + else if (keycode == KEY_ARROW_LEFT) + { + set_vec(&env->playerDir, + cos(-M_PI / 2) * env->playerDir.x - sin(-M_PI / 2) * env->playerDir.y, + sin(-M_PI / 2) * env->playerDir.x + cos(-M_PI / 2) * env->playerDir.y); + } + else if (keycode == KEY_ARROW_RIGHT) + { + set_vec(&env->playerDir, + cos(M_PI / 2) * env->playerDir.x - sin(M_PI / 2) * env->playerDir.y, + sin(M_PI / 2) * env->playerDir.x + cos(M_PI / 2) * env->playerDir.y); + } + render_minimap(env); + render_view(env); + mlx_put_image_to_window(env->mlx, env->win, env->img, 0, 0); + return (1); } int main(int argc, char **argv) @@ -96,14 +129,7 @@ int main(int argc, char **argv) printf("%s\n", env->map[y++]); } init_window(env); - env->img = mlx_new_image(env->mlx, WIN_X_SZ, WIN_Y_SZ); - env->buffer = (int *)mlx_get_data_addr(env->img, \ - &env->pixel_bits, &env->line_bytes, &env->endian); - env->line_bytes /= 4; - //launch_game(env); - render_minimap(env); - mlx_put_image_to_window(env->mlx, env->win, env->img, 0, 0); - //mlx_key_hook(env->win, key_hook_primary, env); + mlx_key_hook(env->win, key_hook_primary, env); mlx_loop(env->mlx); return (EXIT_SUCCESS); } diff --git a/sources/parsing.c b/sources/parsing.c index 0456fdf..b188a8a 100644 --- a/sources/parsing.c +++ b/sources/parsing.c @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/23 17:20:29 by narnaud #+# #+# */ -/* Updated: 2022/05/31 07:56:35 by narnaud ### ########.fr */ +/* Updated: 2022/05/31 09:00:17 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,37 +25,6 @@ int cleanup_datas(t_env *env) free(env->wallTexture[3]); return (1); } -int rgb_to_int(char **rgb) -{ - int ret; - int i; - int c; - char **colors; - - ret = 0; - rgb++; - colors = ft_split(*rgb, ','); - c = 0; - i = 0; - while (c < 3) - { - if (colors[i]) - { - ret |= ft_atoi(colors[i]) << (8 * (2 - c)); - c++; - i++; - } - else - { - ft_free_split(colors); - rgb++; - i = 0; - colors = ft_split(*rgb, ','); - } - } - ft_free_split(colors); - return (ret); -} void register_settings(int *progress, t_env *env, char *line) { @@ -139,6 +108,25 @@ char **create_map_array(t_slist *e_lst, int wide, int deep) return (ret); } +int init_player(t_env *env, int x, int y) +{ + char orientation; + + orientation = env->map[y][x]; + env->map[y][x] = '0'; + set_vec(&env->playerPos, x, y); + if (orientation == 'N') + set_vec(&env->playerDir, 0, -1); + else if (orientation == 'S') + set_vec(&env->playerDir, 0, 1); + else if (orientation == 'E') + set_vec(&env->playerDir, 1, 0); + else if (orientation == 'W') + set_vec(&env->playerDir, -1, -1); + return (1); + +} + int find_player(t_env *env) { char **map; @@ -156,13 +144,7 @@ int find_player(t_env *env) { cell = map[y][x]; if (cell == 'N' || cell == 'S' || cell == 'E' || cell == 'W') - { - env->playerPos.x = x; - env->playerPos.y = y; - env->yaw = cell; - map[y][x] = '0'; - return (1); - } + return (init_player(env, x, y)); x++; } y++; diff --git a/sources/utils.c b/sources/utils.c new file mode 100644 index 0000000..582cef9 --- /dev/null +++ b/sources/utils.c @@ -0,0 +1,51 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/05/31 08:59:01 by narnaud #+# #+# */ +/* Updated: 2022/05/31 09:00:10 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/cub3d.h" + +void set_vec(t_vec_d *vec, int x, int y) +{ + vec->x = x; + vec->y = y; +} + +int rgb_to_int(char **rgb) +{ + int ret; + int i; + int c; + char **colors; + + ret = 0; + rgb++; + colors = ft_split(*rgb, ','); + c = 0; + i = 0; + while (c < 3) + { + if (colors[i]) + { + ret |= ft_atoi(colors[i]) << (8 * (2 - c)); + c++; + i++; + } + else + { + ft_free_split(colors); + rgb++; + i = 0; + colors = ft_split(*rgb, ','); + } + } + ft_free_split(colors); + return (ret); +}